Home | History | Annotate | Download | only in tests
      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 "WebFrameClient.h"
     34 #include "WebInputEvent.h"
     35 #include "WebView.h"
     36 #include "WebViewClient.h"
     37 #include "WebViewImpl.h"
     38 #include "core/page/Chrome.h"
     39 #include <gtest/gtest.h>
     40 
     41 using namespace WebKit;
     42 
     43 namespace WebKit {
     44 
     45 void setCurrentInputEventForTest(const WebInputEvent* event)
     46 {
     47     WebViewImpl::m_currentInputEvent = event;
     48 }
     49 
     50 }
     51 
     52 namespace {
     53 
     54 class TestWebWidgetClient : public WebWidgetClient {
     55 public:
     56     ~TestWebWidgetClient() { }
     57 };
     58 
     59 class TestWebViewClient : public WebViewClient {
     60 public:
     61     explicit TestWebViewClient(WebNavigationPolicy* target) : m_target(target) { }
     62     ~TestWebViewClient() { }
     63 
     64     virtual void show(WebNavigationPolicy policy)
     65     {
     66         *m_target = policy;
     67     }
     68 
     69 private:
     70     WebNavigationPolicy* m_target;
     71 };
     72 
     73 class TestWebFrameClient : public WebFrameClient {
     74 public:
     75     ~TestWebFrameClient() { }
     76 };
     77 
     78 class GetNavigationPolicyTest : public testing::Test {
     79 public:
     80     GetNavigationPolicyTest()
     81         : m_result(WebNavigationPolicyIgnore)
     82         , m_webViewClient(&m_result)
     83     {
     84     }
     85 
     86 protected:
     87     virtual void SetUp()
     88     {
     89         m_webView = static_cast<WebViewImpl*>(WebView::create(&m_webViewClient));
     90         m_webView->initializeMainFrame(&m_webFrameClient);
     91         m_chromeClientImpl = static_cast<ChromeClientImpl*>(m_webView->page()->chrome().client());
     92         m_result = WebNavigationPolicyIgnore;
     93     }
     94 
     95     virtual void TearDown()
     96     {
     97         m_webView->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(WebCore::NavigationPolicyIgnore);
    109         setCurrentInputEventForTest(0);
    110         return m_result;
    111     }
    112 
    113     bool isNavigationPolicyPopup()
    114     {
    115         m_chromeClientImpl->show(WebCore::NavigationPolicyIgnore);
    116         return m_result == WebNavigationPolicyNewPopup;
    117     }
    118 
    119 protected:
    120     WebNavigationPolicy m_result;
    121     TestWebViewClient m_webViewClient;
    122     WebViewImpl* m_webView;
    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(DARWIN)
    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(DARWIN)
    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(DARWIN)
    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(DARWIN)
    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