1 /* 2 * Copyright (C) 2012 Google Inc. All rights reserved. 3 * 4 * Redistribution and use in source and binary forms, with or without 5 * modification, are permitted provided that the following conditions are 6 * met: 7 * 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. 17 * 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. 29 */ 30 31 #include "config.h" 32 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> 42 43 using namespace blink; 44 45 namespace blink { 46 47 void setCurrentInputEventForTest(const WebInputEvent* event) 48 { 49 WebViewImpl::m_currentInputEvent = event; 50 } 51 52 } 53 54 namespace { 55 56 class TestWebViewClient : public FrameTestHelpers::TestWebViewClient { 57 public: 58 explicit TestWebViewClient(WebNavigationPolicy* target) : m_target(target) { } 59 ~TestWebViewClient() { } 60 61 virtual void show(WebNavigationPolicy policy) 62 { 63 *m_target = policy; 64 } 65 66 private: 67 WebNavigationPolicy* m_target; 68 }; 69 70 class TestWebFrameClient : public WebFrameClient { 71 public: 72 ~TestWebFrameClient() { } 73 }; 74 75 class GetNavigationPolicyTest : public testing::Test { 76 public: 77 GetNavigationPolicyTest() 78 : m_result(WebNavigationPolicyIgnore) 79 , m_webViewClient(&m_result) 80 { 81 } 82 83 protected: 84 virtual void SetUp() 85 { 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; 91 } 92 93 virtual void TearDown() 94 { 95 m_webView->close(); 96 m_mainFrame->close(); 97 } 98 99 WebNavigationPolicy getNavigationPolicyWithMouseEvent(int modifiers, WebMouseEvent::Button button, bool asPopup) 100 { 101 WebMouseEvent event; 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); 109 return m_result; 110 } 111 112 bool isNavigationPolicyPopup() 113 { 114 m_chromeClientImpl->show(WebCore::NavigationPolicyIgnore); 115 return m_result == WebNavigationPolicyNewPopup; 116 } 117 118 protected: 119 WebNavigationPolicy m_result; 120 TestWebViewClient m_webViewClient; 121 WebViewImpl* m_webView; 122 WebFrame* m_mainFrame; 123 TestWebFrameClient m_webFrameClient; 124 ChromeClientImpl* m_chromeClientImpl; 125 }; 126 127 TEST_F(GetNavigationPolicyTest, LeftClick) 128 { 129 int modifiers = 0; 130 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft; 131 bool asPopup = false; 132 EXPECT_EQ(WebNavigationPolicyNewForegroundTab, 133 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup)); 134 } 135 136 TEST_F(GetNavigationPolicyTest, LeftClickPopup) 137 { 138 int modifiers = 0; 139 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft; 140 bool asPopup = true; 141 EXPECT_EQ(WebNavigationPolicyNewPopup, 142 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup)); 143 } 144 145 TEST_F(GetNavigationPolicyTest, ShiftLeftClick) 146 { 147 int modifiers = WebInputEvent::ShiftKey; 148 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft; 149 bool asPopup = false; 150 EXPECT_EQ(WebNavigationPolicyNewWindow, 151 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup)); 152 } 153 154 TEST_F(GetNavigationPolicyTest, ShiftLeftClickPopup) 155 { 156 int modifiers = WebInputEvent::ShiftKey; 157 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft; 158 bool asPopup = true; 159 EXPECT_EQ(WebNavigationPolicyNewPopup, 160 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup)); 161 } 162 163 TEST_F(GetNavigationPolicyTest, ControlOrMetaLeftClick) 164 { 165 #if OS(MACOSX) 166 int modifiers = WebInputEvent::MetaKey; 167 #else 168 int modifiers = WebInputEvent::ControlKey; 169 #endif 170 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft; 171 bool asPopup = false; 172 EXPECT_EQ(WebNavigationPolicyNewBackgroundTab, 173 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup)); 174 } 175 176 TEST_F(GetNavigationPolicyTest, ControlOrMetaLeftClickPopup) 177 { 178 #if OS(MACOSX) 179 int modifiers = WebInputEvent::MetaKey; 180 #else 181 int modifiers = WebInputEvent::ControlKey; 182 #endif 183 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft; 184 bool asPopup = true; 185 EXPECT_EQ(WebNavigationPolicyNewBackgroundTab, 186 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup)); 187 } 188 189 TEST_F(GetNavigationPolicyTest, ControlOrMetaAndShiftLeftClick) 190 { 191 #if OS(MACOSX) 192 int modifiers = WebInputEvent::MetaKey; 193 #else 194 int modifiers = WebInputEvent::ControlKey; 195 #endif 196 modifiers |= WebInputEvent::ShiftKey; 197 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft; 198 bool asPopup = false; 199 EXPECT_EQ(WebNavigationPolicyNewForegroundTab, 200 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup)); 201 } 202 203 TEST_F(GetNavigationPolicyTest, ControlOrMetaAndShiftLeftClickPopup) 204 { 205 #if OS(MACOSX) 206 int modifiers = WebInputEvent::MetaKey; 207 #else 208 int modifiers = WebInputEvent::ControlKey; 209 #endif 210 modifiers |= WebInputEvent::ShiftKey; 211 WebMouseEvent::Button button = WebMouseEvent::ButtonLeft; 212 bool asPopup = true; 213 EXPECT_EQ(WebNavigationPolicyNewForegroundTab, 214 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup)); 215 } 216 217 TEST_F(GetNavigationPolicyTest, MiddleClick) 218 { 219 int modifiers = 0; 220 bool asPopup = false; 221 WebMouseEvent::Button button = WebMouseEvent::ButtonMiddle; 222 EXPECT_EQ(WebNavigationPolicyNewBackgroundTab, 223 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup)); 224 } 225 226 TEST_F(GetNavigationPolicyTest, MiddleClickPopup) 227 { 228 int modifiers = 0; 229 bool asPopup = true; 230 WebMouseEvent::Button button = WebMouseEvent::ButtonMiddle; 231 EXPECT_EQ(WebNavigationPolicyNewBackgroundTab, 232 getNavigationPolicyWithMouseEvent(modifiers, button, asPopup)); 233 } 234 235 TEST_F(GetNavigationPolicyTest, NoToolbarsForcesPopup) 236 { 237 m_chromeClientImpl->setToolbarsVisible(false); 238 EXPECT_TRUE(isNavigationPolicyPopup()); 239 m_chromeClientImpl->setToolbarsVisible(true); 240 EXPECT_FALSE(isNavigationPolicyPopup()); 241 } 242 243 TEST_F(GetNavigationPolicyTest, NoStatusbarForcesPopup) 244 { 245 m_chromeClientImpl->setStatusbarVisible(false); 246 EXPECT_TRUE(isNavigationPolicyPopup()); 247 m_chromeClientImpl->setStatusbarVisible(true); 248 EXPECT_FALSE(isNavigationPolicyPopup()); 249 } 250 251 TEST_F(GetNavigationPolicyTest, NoMenubarForcesPopup) 252 { 253 m_chromeClientImpl->setMenubarVisible(false); 254 EXPECT_TRUE(isNavigationPolicyPopup()); 255 m_chromeClientImpl->setMenubarVisible(true); 256 EXPECT_FALSE(isNavigationPolicyPopup()); 257 } 258 259 TEST_F(GetNavigationPolicyTest, NotResizableForcesPopup) 260 { 261 m_chromeClientImpl->setResizable(false); 262 EXPECT_TRUE(isNavigationPolicyPopup()); 263 m_chromeClientImpl->setResizable(true); 264 EXPECT_FALSE(isNavigationPolicyPopup()); 265 } 266 267 } // namespace 268