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 "core/page/Page.h"
35 #include "public/web/WebFrameClient.h"
36 #include "public/web/WebInputEvent.h"
37 #include "public/web/WebLocalFrame.h"
38 #include "public/web/WebView.h"
39 #include "public/web/WebViewClient.h"
40 #include "web/WebViewImpl.h"
41 #include "web/tests/FrameTestHelpers.h"
42 #include <gtest/gtest.h>
44 using namespace blink;
48 void setCurrentInputEventForTest(const WebInputEvent* event)
50 WebViewImpl::m_currentInputEvent = event;
57 class TestWebViewClient : public FrameTestHelpers::TestWebViewClient {
59 explicit TestWebViewClient(WebNavigationPolicy* target) : m_target(target) { }
60 ~TestWebViewClient() { }
62 virtual void show(WebNavigationPolicy policy)
68 WebNavigationPolicy* m_target;
71 class TestWebFrameClient : public WebFrameClient {
73 ~TestWebFrameClient() { }
76 class GetNavigationPolicyTest : public testing::Test {
78 GetNavigationPolicyTest()
79 : m_result(WebNavigationPolicyIgnore)
80 , m_webViewClient(&m_result)
87 m_webView = toWebViewImpl(WebView::create(&m_webViewClient));
88 m_mainFrame = WebLocalFrame::create(&m_webFrameClient);
89 m_webView->setMainFrame(m_mainFrame);
90 m_chromeClientImpl = toChromeClientImpl(&m_webView->page()->chrome().client());
91 m_result = WebNavigationPolicyIgnore;
94 virtual void TearDown()
100 WebNavigationPolicy getNavigationPolicyWithMouseEvent(int modifiers, WebMouseEvent::Button button, bool asPopup)
103 event.modifiers = modifiers;
104 event.type = WebInputEvent::MouseUp;
105 event.button = button;
106 setCurrentInputEventForTest(&event);
107 m_chromeClientImpl->setScrollbarsVisible(!asPopup);
108 m_chromeClientImpl->show(blink::NavigationPolicyIgnore);
109 setCurrentInputEventForTest(0);
113 bool isNavigationPolicyPopup()
115 m_chromeClientImpl->show(blink::NavigationPolicyIgnore);
116 return m_result == WebNavigationPolicyNewPopup;
120 WebNavigationPolicy m_result;
121 TestWebViewClient m_webViewClient;
122 WebViewImpl* m_webView;
123 WebFrame* m_mainFrame;
124 TestWebFrameClient m_webFrameClient;
125 ChromeClientImpl* m_chromeClientImpl;
128 TEST_F(GetNavigationPolicyTest, LeftClick)
131 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
132 bool asPopup = false;
133 EXPECT_EQ(WebNavigationPolicyNewForegroundTab,
134 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
137 TEST_F(GetNavigationPolicyTest, LeftClickPopup)
140 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
142 EXPECT_EQ(WebNavigationPolicyNewPopup,
143 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
146 TEST_F(GetNavigationPolicyTest, ShiftLeftClick)
148 int modifiers = WebInputEvent::ShiftKey;
149 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
150 bool asPopup = false;
151 EXPECT_EQ(WebNavigationPolicyNewWindow,
152 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
155 TEST_F(GetNavigationPolicyTest, ShiftLeftClickPopup)
157 int modifiers = WebInputEvent::ShiftKey;
158 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
160 EXPECT_EQ(WebNavigationPolicyNewPopup,
161 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
164 TEST_F(GetNavigationPolicyTest, ControlOrMetaLeftClick)
167 int modifiers = WebInputEvent::MetaKey;
169 int modifiers = WebInputEvent::ControlKey;
171 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
172 bool asPopup = false;
173 EXPECT_EQ(WebNavigationPolicyNewBackgroundTab,
174 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
177 TEST_F(GetNavigationPolicyTest, ControlOrMetaLeftClickPopup)
180 int modifiers = WebInputEvent::MetaKey;
182 int modifiers = WebInputEvent::ControlKey;
184 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
186 EXPECT_EQ(WebNavigationPolicyNewBackgroundTab,
187 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
190 TEST_F(GetNavigationPolicyTest, ControlOrMetaAndShiftLeftClick)
193 int modifiers = WebInputEvent::MetaKey;
195 int modifiers = WebInputEvent::ControlKey;
197 modifiers |= WebInputEvent::ShiftKey;
198 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
199 bool asPopup = false;
200 EXPECT_EQ(WebNavigationPolicyNewForegroundTab,
201 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
204 TEST_F(GetNavigationPolicyTest, ControlOrMetaAndShiftLeftClickPopup)
207 int modifiers = WebInputEvent::MetaKey;
209 int modifiers = WebInputEvent::ControlKey;
211 modifiers |= WebInputEvent::ShiftKey;
212 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
214 EXPECT_EQ(WebNavigationPolicyNewForegroundTab,
215 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
218 TEST_F(GetNavigationPolicyTest, MiddleClick)
221 bool asPopup = false;
222 WebMouseEvent::Button button = WebMouseEvent::ButtonMiddle;
223 EXPECT_EQ(WebNavigationPolicyNewBackgroundTab,
224 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
227 TEST_F(GetNavigationPolicyTest, MiddleClickPopup)
231 WebMouseEvent::Button button = WebMouseEvent::ButtonMiddle;
232 EXPECT_EQ(WebNavigationPolicyNewBackgroundTab,
233 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
236 TEST_F(GetNavigationPolicyTest, NoToolbarsForcesPopup)
238 m_chromeClientImpl->setToolbarsVisible(false);
239 EXPECT_TRUE(isNavigationPolicyPopup());
240 m_chromeClientImpl->setToolbarsVisible(true);
241 EXPECT_FALSE(isNavigationPolicyPopup());
244 TEST_F(GetNavigationPolicyTest, NoStatusbarForcesPopup)
246 m_chromeClientImpl->setStatusbarVisible(false);
247 EXPECT_TRUE(isNavigationPolicyPopup());
248 m_chromeClientImpl->setStatusbarVisible(true);
249 EXPECT_FALSE(isNavigationPolicyPopup());
252 TEST_F(GetNavigationPolicyTest, NoMenubarForcesPopup)
254 m_chromeClientImpl->setMenubarVisible(false);
255 EXPECT_TRUE(isNavigationPolicyPopup());
256 m_chromeClientImpl->setMenubarVisible(true);
257 EXPECT_FALSE(isNavigationPolicyPopup());
260 TEST_F(GetNavigationPolicyTest, NotResizableForcesPopup)
262 m_chromeClientImpl->setResizable(false);
263 EXPECT_TRUE(isNavigationPolicyPopup());
264 m_chromeClientImpl->setResizable(true);
265 EXPECT_FALSE(isNavigationPolicyPopup());