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