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/Page.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>
45 void setCurrentInputEventForTest(const WebInputEvent
* event
)
47 WebViewImpl::m_currentInputEvent
= event
;
52 class TestWebViewClient
: public FrameTestHelpers::TestWebViewClient
{
54 explicit TestWebViewClient(WebNavigationPolicy
* target
) : m_target(target
) { }
55 ~TestWebViewClient() override
{ }
57 void show(WebNavigationPolicy policy
) override
63 WebNavigationPolicy
* m_target
;
66 class TestWebFrameClient
: public WebFrameClient
{
68 ~TestWebFrameClient() override
{ }
71 } // anonymous namespace
73 class GetNavigationPolicyTest
: public testing::Test
{
75 GetNavigationPolicyTest()
76 : m_result(WebNavigationPolicyIgnore
)
77 , m_webViewClient(&m_result
)
84 m_webView
= toWebViewImpl(WebView::create(&m_webViewClient
));
85 m_mainFrame
= WebLocalFrame::create(WebTreeScopeType::Document
, &m_webFrameClient
);
86 m_webView
->setMainFrame(m_mainFrame
);
87 m_chromeClientImpl
= toChromeClientImpl(&m_webView
->page()->chromeClient());
88 m_result
= WebNavigationPolicyIgnore
;
91 void TearDown() override
97 WebNavigationPolicy
getNavigationPolicyWithMouseEvent(int modifiers
, WebMouseEvent::Button button
, bool asPopup
)
100 event
.modifiers
= modifiers
;
101 event
.type
= WebInputEvent::MouseUp
;
102 event
.button
= button
;
103 setCurrentInputEventForTest(&event
);
104 m_chromeClientImpl
->setScrollbarsVisible(!asPopup
);
105 m_chromeClientImpl
->show(NavigationPolicyIgnore
);
106 setCurrentInputEventForTest(0);
110 bool isNavigationPolicyPopup()
112 m_chromeClientImpl
->show(NavigationPolicyIgnore
);
113 return m_result
== WebNavigationPolicyNewPopup
;
117 WebNavigationPolicy m_result
;
118 TestWebViewClient m_webViewClient
;
119 WebViewImpl
* m_webView
;
120 WebFrame
* m_mainFrame
;
121 TestWebFrameClient m_webFrameClient
;
122 ChromeClientImpl
* m_chromeClientImpl
;
125 TEST_F(GetNavigationPolicyTest
, LeftClick
)
128 WebMouseEvent::Button button
= WebMouseEvent::ButtonLeft
;
129 bool asPopup
= false;
130 EXPECT_EQ(WebNavigationPolicyNewForegroundTab
,
131 getNavigationPolicyWithMouseEvent(modifiers
, button
, asPopup
));
134 TEST_F(GetNavigationPolicyTest
, LeftClickPopup
)
137 WebMouseEvent::Button button
= WebMouseEvent::ButtonLeft
;
139 EXPECT_EQ(WebNavigationPolicyNewPopup
,
140 getNavigationPolicyWithMouseEvent(modifiers
, button
, asPopup
));
143 TEST_F(GetNavigationPolicyTest
, ShiftLeftClick
)
145 int modifiers
= WebInputEvent::ShiftKey
;
146 WebMouseEvent::Button button
= WebMouseEvent::ButtonLeft
;
147 bool asPopup
= false;
148 EXPECT_EQ(WebNavigationPolicyNewWindow
,
149 getNavigationPolicyWithMouseEvent(modifiers
, button
, asPopup
));
152 TEST_F(GetNavigationPolicyTest
, ShiftLeftClickPopup
)
154 int modifiers
= WebInputEvent::ShiftKey
;
155 WebMouseEvent::Button button
= WebMouseEvent::ButtonLeft
;
157 EXPECT_EQ(WebNavigationPolicyNewPopup
,
158 getNavigationPolicyWithMouseEvent(modifiers
, button
, asPopup
));
161 TEST_F(GetNavigationPolicyTest
, ControlOrMetaLeftClick
)
164 int modifiers
= WebInputEvent::MetaKey
;
166 int modifiers
= WebInputEvent::ControlKey
;
168 WebMouseEvent::Button button
= WebMouseEvent::ButtonLeft
;
169 bool asPopup
= false;
170 EXPECT_EQ(WebNavigationPolicyNewBackgroundTab
,
171 getNavigationPolicyWithMouseEvent(modifiers
, button
, asPopup
));
174 TEST_F(GetNavigationPolicyTest
, ControlOrMetaLeftClickPopup
)
177 int modifiers
= WebInputEvent::MetaKey
;
179 int modifiers
= WebInputEvent::ControlKey
;
181 WebMouseEvent::Button button
= WebMouseEvent::ButtonLeft
;
183 EXPECT_EQ(WebNavigationPolicyNewBackgroundTab
,
184 getNavigationPolicyWithMouseEvent(modifiers
, button
, asPopup
));
187 TEST_F(GetNavigationPolicyTest
, ControlOrMetaAndShiftLeftClick
)
190 int modifiers
= WebInputEvent::MetaKey
;
192 int modifiers
= WebInputEvent::ControlKey
;
194 modifiers
|= WebInputEvent::ShiftKey
;
195 WebMouseEvent::Button button
= WebMouseEvent::ButtonLeft
;
196 bool asPopup
= false;
197 EXPECT_EQ(WebNavigationPolicyNewForegroundTab
,
198 getNavigationPolicyWithMouseEvent(modifiers
, button
, asPopup
));
201 TEST_F(GetNavigationPolicyTest
, ControlOrMetaAndShiftLeftClickPopup
)
204 int modifiers
= WebInputEvent::MetaKey
;
206 int modifiers
= WebInputEvent::ControlKey
;
208 modifiers
|= WebInputEvent::ShiftKey
;
209 WebMouseEvent::Button button
= WebMouseEvent::ButtonLeft
;
211 EXPECT_EQ(WebNavigationPolicyNewForegroundTab
,
212 getNavigationPolicyWithMouseEvent(modifiers
, button
, asPopup
));
215 TEST_F(GetNavigationPolicyTest
, MiddleClick
)
218 bool asPopup
= false;
219 WebMouseEvent::Button button
= WebMouseEvent::ButtonMiddle
;
220 EXPECT_EQ(WebNavigationPolicyNewBackgroundTab
,
221 getNavigationPolicyWithMouseEvent(modifiers
, button
, asPopup
));
224 TEST_F(GetNavigationPolicyTest
, MiddleClickPopup
)
228 WebMouseEvent::Button button
= WebMouseEvent::ButtonMiddle
;
229 EXPECT_EQ(WebNavigationPolicyNewBackgroundTab
,
230 getNavigationPolicyWithMouseEvent(modifiers
, button
, asPopup
));
233 TEST_F(GetNavigationPolicyTest
, NoToolbarsForcesPopup
)
235 m_chromeClientImpl
->setToolbarsVisible(false);
236 EXPECT_TRUE(isNavigationPolicyPopup());
237 m_chromeClientImpl
->setToolbarsVisible(true);
238 EXPECT_FALSE(isNavigationPolicyPopup());
241 TEST_F(GetNavigationPolicyTest
, NoStatusbarForcesPopup
)
243 m_chromeClientImpl
->setStatusbarVisible(false);
244 EXPECT_TRUE(isNavigationPolicyPopup());
245 m_chromeClientImpl
->setStatusbarVisible(true);
246 EXPECT_FALSE(isNavigationPolicyPopup());
249 TEST_F(GetNavigationPolicyTest
, NoMenubarForcesPopup
)
251 m_chromeClientImpl
->setMenubarVisible(false);
252 EXPECT_TRUE(isNavigationPolicyPopup());
253 m_chromeClientImpl
->setMenubarVisible(true);
254 EXPECT_FALSE(isNavigationPolicyPopup());
257 TEST_F(GetNavigationPolicyTest
, NotResizableForcesPopup
)
259 m_chromeClientImpl
->setResizable(false);
260 EXPECT_TRUE(isNavigationPolicyPopup());
261 m_chromeClientImpl
->setResizable(true);
262 EXPECT_FALSE(isNavigationPolicyPopup());