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