1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/basictypes.h"
6 #include "base/files/file_path.h"
7 #include "chrome/browser/chrome_notification_types.h"
8 #include "chrome/browser/ui/browser.h"
9 #include "chrome/browser/ui/browser_commands.h"
10 #include "chrome/browser/ui/browser_tabstrip.h"
11 #include "chrome/browser/ui/tabs/tab_strip_model.h"
12 #include "chrome/test/base/in_process_browser_test.h"
13 #include "chrome/test/base/ui_test_utils.h"
14 #include "content/public/browser/notification_service.h"
15 #include "content/public/browser/notification_source.h"
16 #include "content/public/browser/render_view_host.h"
17 #include "content/public/browser/web_contents.h"
18 #include "content/public/test/browser_test_utils.h"
19 #include "net/base/net_util.h"
22 class ChromeRenderWidgetHostViewMacDelegateTest : public InProcessBrowserTest {
24 ChromeRenderWidgetHostViewMacDelegateTest() {
25 const base::FilePath base_path(FILE_PATH_LITERAL("scroll"));
26 url1_ = ui_test_utils::GetTestUrl(
27 base_path, base::FilePath(FILE_PATH_LITERAL("text.html")));
28 url2_ = ui_test_utils::GetTestUrl(
29 base_path, base::FilePath(FILE_PATH_LITERAL("blank.html")));
35 content::WindowedNotificationObserver observer(
36 content::NOTIFICATION_LOAD_STOP,
37 content::NotificationService::AllSources());
38 chrome::GoBack(browser(), CURRENT_TAB);
42 // Returns the active web contents.
43 content::WebContents* GetWebContents() {
44 return browser()->tab_strip_model()->GetActiveWebContents();
47 // Returns the value of |query| from Javascript as an int.
48 int GetScriptIntValue(const std::string& query) {
50 EXPECT_TRUE(content::ExecuteScriptAndExtractInt(
52 "domAutomationController.send(" + query + ")",
57 // Returns the vertical scroll offset of the current page.
59 return GetScriptIntValue("document.body.scrollTop");
62 // Returns the horizontal scroll offset of the current page.
64 return GetScriptIntValue("document.body.scrollLeft");
67 // Simulates a mouse wheel event, forwarding it to the renderer.
68 void SendWheelEvent(int dx, int dy, WebKit::WebMouseWheelEvent::Phase phase) {
69 WebKit::WebMouseWheelEvent event;
70 event.type = WebKit::WebInputEvent::MouseWheel;
74 event.wheelTicksY = -2;
75 event.hasPreciseScrollingDeltas = 1;
76 GetWebContents()->GetRenderViewHost()->ForwardWheelEvent(event);
83 DISALLOW_COPY_AND_ASSIGN(ChromeRenderWidgetHostViewMacDelegateTest);
86 IN_PROC_BROWSER_TEST_F(ChromeRenderWidgetHostViewMacDelegateTest,
88 ui_test_utils::NavigateToURL(browser(), url1_);
89 ASSERT_EQ(url1_, GetWebContents()->GetURL());
91 SendWheelEvent(0, -200, WebKit::WebMouseWheelEvent::PhaseNone);
92 const int scroll_offset = GetScrollTop();
93 ASSERT_NE(0, scroll_offset);
95 ui_test_utils::NavigateToURL(browser(), url2_);
96 ASSERT_EQ(url2_, GetWebContents()->GetURL());
97 ASSERT_EQ(0, GetScrollTop());
100 ASSERT_EQ(url1_, GetWebContents()->GetURL());
101 ASSERT_EQ(scroll_offset, GetScrollTop());
104 IN_PROC_BROWSER_TEST_F(ChromeRenderWidgetHostViewMacDelegateTest,
105 GoBackUsingGestureScrollOffset) {
106 ui_test_utils::NavigateToURL(browser(), url1_);
107 ASSERT_EQ(url1_, GetWebContents()->GetURL());
109 SendWheelEvent(0, -200, WebKit::WebMouseWheelEvent::PhaseNone);
110 const int scroll_offset = GetScrollTop();
111 ASSERT_NE(0, scroll_offset);
113 ui_test_utils::NavigateToURL(browser(), url2_);
114 ASSERT_EQ(url2_, GetWebContents()->GetURL());
115 ASSERT_EQ(0, GetScrollTop());
117 // Send wheel events that shouldn't be handled by the web content since it's
118 // not scrollable in the horizontal direction.
119 SendWheelEvent(500, 0, WebKit::WebMouseWheelEvent::PhaseBegan);
120 SendWheelEvent(500, 0, WebKit::WebMouseWheelEvent::PhaseEnded);
121 ASSERT_EQ(0, GetScrollLeft());
123 // Simulate a back being triggered as a result of the unhandled wheel events.
124 // This doesn't invoke the code in ChromeRenderWidgetHostViewMacDelegate
125 // because that expects NSEvents which are much harder to fake.
127 ASSERT_EQ(url1_, GetWebContents()->GetURL());
128 ASSERT_EQ(scroll_offset, GetScrollTop());