Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / Source / web / tests / ChromeClientImplTest.cpp
blob6dbafb4a5706bbc007ef0f87a3627ad50bf30599
1 /*
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
6 * met:
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
13 * distribution.
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.
31 #include "config.h"
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>
43 namespace blink {
45 void setCurrentInputEventForTest(const WebInputEvent* event)
47 WebViewImpl::m_currentInputEvent = event;
50 namespace {
52 class TestWebViewClient : public FrameTestHelpers::TestWebViewClient {
53 public:
54 explicit TestWebViewClient(WebNavigationPolicy* target) : m_target(target) { }
55 ~TestWebViewClient() override { }
57 void show(WebNavigationPolicy policy) override
59 *m_target = policy;
62 private:
63 WebNavigationPolicy* m_target;
66 class TestWebFrameClient : public WebFrameClient {
67 public:
68 ~TestWebFrameClient() override { }
71 } // anonymous namespace
73 class GetNavigationPolicyTest : public testing::Test {
74 public:
75 GetNavigationPolicyTest()
76 : m_result(WebNavigationPolicyIgnore)
77 , m_webViewClient(&m_result)
81 protected:
82 void SetUp() override
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
93 m_webView->close();
94 m_mainFrame->close();
97 WebNavigationPolicy getNavigationPolicyWithMouseEvent(int modifiers, WebMouseEvent::Button button, bool asPopup)
99 WebMouseEvent event;
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);
107 return m_result;
110 bool isNavigationPolicyPopup()
112 m_chromeClientImpl->show(NavigationPolicyIgnore);
113 return m_result == WebNavigationPolicyNewPopup;
116 protected:
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)
127 int modifiers = 0;
128 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
129 bool asPopup = false;
130 EXPECT_EQ(WebNavigationPolicyNewForegroundTab,
131 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
134 TEST_F(GetNavigationPolicyTest, LeftClickPopup)
136 int modifiers = 0;
137 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
138 bool asPopup = true;
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;
156 bool asPopup = true;
157 EXPECT_EQ(WebNavigationPolicyNewPopup,
158 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
161 TEST_F(GetNavigationPolicyTest, ControlOrMetaLeftClick)
163 #if OS(MACOSX)
164 int modifiers = WebInputEvent::MetaKey;
165 #else
166 int modifiers = WebInputEvent::ControlKey;
167 #endif
168 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
169 bool asPopup = false;
170 EXPECT_EQ(WebNavigationPolicyNewBackgroundTab,
171 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
174 TEST_F(GetNavigationPolicyTest, ControlOrMetaLeftClickPopup)
176 #if OS(MACOSX)
177 int modifiers = WebInputEvent::MetaKey;
178 #else
179 int modifiers = WebInputEvent::ControlKey;
180 #endif
181 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
182 bool asPopup = true;
183 EXPECT_EQ(WebNavigationPolicyNewBackgroundTab,
184 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
187 TEST_F(GetNavigationPolicyTest, ControlOrMetaAndShiftLeftClick)
189 #if OS(MACOSX)
190 int modifiers = WebInputEvent::MetaKey;
191 #else
192 int modifiers = WebInputEvent::ControlKey;
193 #endif
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)
203 #if OS(MACOSX)
204 int modifiers = WebInputEvent::MetaKey;
205 #else
206 int modifiers = WebInputEvent::ControlKey;
207 #endif
208 modifiers |= WebInputEvent::ShiftKey;
209 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft;
210 bool asPopup = true;
211 EXPECT_EQ(WebNavigationPolicyNewForegroundTab,
212 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
215 TEST_F(GetNavigationPolicyTest, MiddleClick)
217 int modifiers = 0;
218 bool asPopup = false;
219 WebMouseEvent::Button button = WebMouseEvent::ButtonMiddle;
220 EXPECT_EQ(WebNavigationPolicyNewBackgroundTab,
221 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup));
224 TEST_F(GetNavigationPolicyTest, MiddleClickPopup)
226 int modifiers = 0;
227 bool asPopup = true;
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());
265 } // namespace blink