2 * Copyright (C) 2012 Google Inc. All rights reserved.
4 * Redistribution and use in source and binary forms, with or without
5 * modification, are permitted provided that the following conditions are
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above
11 * copyright notice, this list of conditions and the following disclaimer
12 * in the documentation and/or other materials provided with the
14 * * Neither the name of Google Inc. nor the names of its
15 * contributors may be used to endorse or promote products derived from
16 * this software without specific prior written permission.
18 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
33 #include "core/page/Chrome.h"
34 #include "public/web/WebFrameClient.h"
35 #include "public/web/WebInputEvent.h"
36 #include "public/web/WebLocalFrame.h"
37 #include "public/web/WebView.h"
38 #include "public/web/WebViewClient.h"
39 #include "web/WebViewImpl.h"
40 #include "web/tests/FrameTestHelpers.h"
41 #include <gtest/gtest.h>
43 using namespace blink;
47 void setCurrentInputEventForTest(const WebInputEvent* event)
49 WebViewImpl::m_currentInputEvent = event;
56 class TestWebViewClient : public FrameTestHelpers::TestWebViewClient {
58 explicit TestWebViewClient(WebNavigationPolicy* target) : m_target(target) { }
59 ~TestWebViewClient() { }
61 virtual void show(WebNavigationPolicy policy)
67 WebNavigationPolicy* m_target;
70 class TestWebFrameClient : public WebFrameClient {
72 ~TestWebFrameClient() { }
75 class GetNavigationPolicyTest : public testing::Test {
77 GetNavigationPolicyTest()
78 : m_result(WebNavigationPolicyIgnore)
79 , m_webViewClient(&m_result)
86 m_webView = toWebViewImpl(WebView::create(&m_webViewClient));
87 m_mainFrame = WebLocalFrame::create(&m_webFrameClient);
88 m_webView->setMainFrame(m_mainFrame);
89 m_chromeClientImpl = toChromeClientImpl(&m_webView->page()->chrome().client());
90 m_result = WebNavigationPolicyIgnore;
93 virtual void TearDown()
99 WebNavigationPolicy getNavigationPolicyWithMouseEvent(int modifiers, WebMouseEvent::Button button, bool asPopup)
102 event.modifiers = modifiers;
103 event.type = WebInputEvent::MouseUp;
104 event.button = button;
105 setCurrentInputEventForTest(&event);
106 m_chromeClientImpl->setScrollbarsVisible(!asPopup);
107 m_chromeClientImpl->show(WebCore::NavigationPolicyIgnore);
108 setCurrentInputEventForTest(0);
112 bool isNavigationPolicyPopup()
114 m_chromeClientImpl->show(WebCore::NavigationPolicyIgnore);
115 return m_result == WebNavigationPolicyNewPopup;
119 WebNavigationPolicy m_result;
120 TestWebViewClient m_webViewClient;
121 WebViewImpl* m_webView;
122 WebFrame* m_mainFrame;
123 TestWebFrameClient m_webFrameClient;
124 ChromeClientImpl* m_chromeClientImpl;
127 TEST_F(GetNavigationPolicyTest, LeftClick)
130 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
131 bool asPopup = false;
132 EXPECT_EQ(WebNavigationPolicyNewForegroundTab,
133 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
136 TEST_F(GetNavigationPolicyTest, LeftClickPopup)
139 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
141 EXPECT_EQ(WebNavigationPolicyNewPopup,
142 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
145 TEST_F(GetNavigationPolicyTest, ShiftLeftClick)
147 int modifiers = WebInputEvent::ShiftKey;
148 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
149 bool asPopup = false;
150 EXPECT_EQ(WebNavigationPolicyNewWindow,
151 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
154 TEST_F(GetNavigationPolicyTest, ShiftLeftClickPopup)
156 int modifiers = WebInputEvent::ShiftKey;
157 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
159 EXPECT_EQ(WebNavigationPolicyNewPopup,
160 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
163 TEST_F(GetNavigationPolicyTest, ControlOrMetaLeftClick)
166 int modifiers = WebInputEvent::MetaKey;
168 int modifiers = WebInputEvent::ControlKey;
170 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
171 bool asPopup = false;
172 EXPECT_EQ(WebNavigationPolicyNewBackgroundTab,
173 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
176 TEST_F(GetNavigationPolicyTest, ControlOrMetaLeftClickPopup)
179 int modifiers = WebInputEvent::MetaKey;
181 int modifiers = WebInputEvent::ControlKey;
183 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
185 EXPECT_EQ(WebNavigationPolicyNewBackgroundTab,
186 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
189 TEST_F(GetNavigationPolicyTest, ControlOrMetaAndShiftLeftClick)
192 int modifiers = WebInputEvent::MetaKey;
194 int modifiers = WebInputEvent::ControlKey;
196 modifiers |= WebInputEvent::ShiftKey;
197 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
198 bool asPopup = false;
199 EXPECT_EQ(WebNavigationPolicyNewForegroundTab,
200 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
203 TEST_F(GetNavigationPolicyTest, ControlOrMetaAndShiftLeftClickPopup)
206 int modifiers = WebInputEvent::MetaKey;
208 int modifiers = WebInputEvent::ControlKey;
210 modifiers |= WebInputEvent::ShiftKey;
211 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
213 EXPECT_EQ(WebNavigationPolicyNewForegroundTab,
214 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
217 TEST_F(GetNavigationPolicyTest, MiddleClick)
220 bool asPopup = false;
221 WebMouseEvent::Button button = WebMouseEvent::ButtonMiddle;
222 EXPECT_EQ(WebNavigationPolicyNewBackgroundTab,
223 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
226 TEST_F(GetNavigationPolicyTest, MiddleClickPopup)
230 WebMouseEvent::Button button = WebMouseEvent::ButtonMiddle;
231 EXPECT_EQ(WebNavigationPolicyNewBackgroundTab,
232 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
235 TEST_F(GetNavigationPolicyTest, NoToolbarsForcesPopup)
237 m_chromeClientImpl->setToolbarsVisible(false);
238 EXPECT_TRUE(isNavigationPolicyPopup());
239 m_chromeClientImpl->setToolbarsVisible(true);
240 EXPECT_FALSE(isNavigationPolicyPopup());
243 TEST_F(GetNavigationPolicyTest, NoStatusbarForcesPopup)
245 m_chromeClientImpl->setStatusbarVisible(false);
246 EXPECT_TRUE(isNavigationPolicyPopup());
247 m_chromeClientImpl->setStatusbarVisible(true);
248 EXPECT_FALSE(isNavigationPolicyPopup());
251 TEST_F(GetNavigationPolicyTest, NoMenubarForcesPopup)
253 m_chromeClientImpl->setMenubarVisible(false);
254 EXPECT_TRUE(isNavigationPolicyPopup());
255 m_chromeClientImpl->setMenubarVisible(true);
256 EXPECT_FALSE(isNavigationPolicyPopup());
259 TEST_F(GetNavigationPolicyTest, NotResizableForcesPopup)
261 m_chromeClientImpl->setResizable(false);
262 EXPECT_TRUE(isNavigationPolicyPopup());
263 m_chromeClientImpl->setResizable(true);
264 EXPECT_FALSE(isNavigationPolicyPopup());