Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(621)

Side by Side Diff: content/browser/renderer_host/input/wheel_scroll_latching_browsertest.cc

Issue 2928793003: Re-target wheel events only when a new scroll sequence has started. (Closed)
Patch Set: Landed migration of wheel handling to MouseWheelEventManager separately. Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2017 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.
4
5 #include "base/test/scoped_feature_list.h"
6 #include "content/browser/renderer_host/render_widget_host_impl.h"
7 #include "content/browser/renderer_host/render_widget_host_input_event_router.h"
8 #include "content/browser/web_contents/web_contents_impl.h"
9 #include "content/common/input/synthetic_web_input_event_builders.h"
10 #include "content/public/common/content_features.h"
11 #include "content/public/test/browser_test_utils.h"
12 #include "content/public/test/content_browser_test.h"
13 #include "content/public/test/content_browser_test_utils.h"
14 #include "content/shell/browser/shell.h"
15 #include "ui/events/gesture_detection/gesture_configuration.h"
16
17 #if defined(OS_ANDROID)
18 #include "content/browser/renderer_host/render_widget_host_view_android.h"
19 #endif
20
21 using blink::WebMouseWheelEvent;
22
23 namespace {
24 void GiveItSomeTime() {
25 base::RunLoop run_loop;
26 base::ThreadTaskRunnerHandle::Get()->PostDelayedTask(
27 FROM_HERE, run_loop.QuitClosure(), base::TimeDelta::FromMilliseconds(10));
28 run_loop.Run();
29 }
30
31 const char kWheelEventLatchingDataURL[] =
32 "data:text/html;charset=utf-8,"
33 "<!DOCTYPE html>"
34 "<style>"
35 "body {"
36 " height: 10000px;"
37 "}"
38 "#scrollableDiv {"
39 " position: absolute;"
40 " left: 100px;"
41 " top: 200px;"
42 " width: 400px;"
43 " height: 800px;"
44 " overflow: scroll;"
45 " background: red;"
46 "}"
47 "#nestedDiv {"
48 " width: 400px;"
49 " height: 8000px;"
50 " opacity: 0;"
51 "}"
52 "</style>"
53 "<div id='scrollableDiv'>"
54 " <div id='nestedDiv'></div>"
55 "</div>"
56 "<script>"
57 " var scrollableDiv = document.getElementById('scrollableDiv');"
58 " var scrollableDivWheelEventCounter = 0;"
59 " var documentWheelEventCounter = 0;"
60 " scrollableDiv.addEventListener('wheel',"
61 " function(e) { scrollableDivWheelEventCounter++;"
62 " e.stopPropagation(); });"
63 " document.scrollingElement.addEventListener('wheel',"
64 " function(e) { documentWheelEventCounter++; });"
65 "</script>";
66 } // namespace
67
68 namespace content {
69 class WheelScrollLatchingBrowserTest : public ContentBrowserTest {
70 public:
71 WheelScrollLatchingBrowserTest(bool wheel_scroll_latching_enabled = true)
72 : wheel_scroll_latching_enabled_(wheel_scroll_latching_enabled) {
73 ui::GestureConfiguration::GetInstance()->set_scroll_debounce_interval_in_ms(
74 0);
75
76 if (wheel_scroll_latching_enabled_)
77 EnableWheelScrollLatching();
78 else
79 DisableWheelScrollLatching();
80 }
81 ~WheelScrollLatchingBrowserTest() override {}
82
83 protected:
84 RenderWidgetHostImpl* GetWidgetHost() {
85 return RenderWidgetHostImpl::From(
86 web_contents()->GetRenderViewHost()->GetWidget());
87 }
88
89 WebContentsImpl* web_contents() const {
90 return static_cast<WebContentsImpl*>(shell()->web_contents());
91 }
92
93 RenderWidgetHostInputEventRouter* GetRouter() {
94 return web_contents()->GetInputEventRouter();
95 }
96
97 RenderWidgetHostViewBase* GetRootView() {
98 return static_cast<RenderWidgetHostViewBase*>(web_contents()
99 ->GetFrameTree()
100 ->root()
101 ->current_frame_host()
102 ->GetView());
103 }
104
105 float GetPageScaleFactor() {
106 return GetWidgetHost()->last_frame_metadata().page_scale_factor;
107 }
108
109 void LoadURL() {
110 const GURL data_url(kWheelEventLatchingDataURL);
111 NavigateToURL(shell(), data_url);
112
113 RenderWidgetHostImpl* host = GetWidgetHost();
114 host->GetView()->SetSize(gfx::Size(600, 600));
115
116 // The page is loaded in the renderer, wait for a new frame to arrive.
117 while (!host->ScheduleComposite())
118 GiveItSomeTime();
119 }
120 int ExecuteScriptAndExtractInt(const std::string& script) {
121 int value = 0;
122 EXPECT_TRUE(content::ExecuteScriptAndExtractInt(
123 shell(), "domAutomationController.send(" + script + ")", &value));
124 return value;
125 }
126 void EnableWheelScrollLatching() {
127 feature_list_.InitFromCommandLine(
128 features::kTouchpadAndWheelScrollLatching.name, "");
129 }
130 void DisableWheelScrollLatching() {
131 feature_list_.InitFromCommandLine(
132 "", features::kTouchpadAndWheelScrollLatching.name);
133 }
134
135 void WheelEventTargetTest() {
136 LoadURL();
137 EXPECT_EQ(0, ExecuteScriptAndExtractInt("documentWheelEventCounter"));
138 EXPECT_EQ(0, ExecuteScriptAndExtractInt("scrollableDivWheelEventCounter"));
139
140 FrameWatcher frame_watcher(shell()->web_contents());
141 scoped_refptr<InputMsgWatcher> input_msg_watcher(new InputMsgWatcher(
142 GetWidgetHost(), blink::WebInputEvent::kMouseWheel));
143 float scale_factor = GetPageScaleFactor();
144
145 float scrollable_div_top =
146 ExecuteScriptAndExtractInt("scrollableDiv.getBoundingClientRect().top");
147 float x = scale_factor *
148 (ExecuteScriptAndExtractInt(
149 "scrollableDiv.getBoundingClientRect().left") +
150 ExecuteScriptAndExtractInt(
151 "scrollableDiv.getBoundingClientRect().right")) /
152 2;
153 float y = scale_factor * 0.5 * scrollable_div_top;
154 float delta_x = 0;
155 float delta_y = -0.5 * scrollable_div_top;
156 blink::WebMouseWheelEvent wheel_event =
157 SyntheticWebMouseWheelEventBuilder::Build(x, y, x, y, delta_x, delta_y,
158 0, true);
159 if (wheel_scroll_latching_enabled_)
160 wheel_event.phase = blink::WebMouseWheelEvent::kPhaseBegan;
tdresser 2017/06/15 13:40:01 Couldn't you set this phase information in both ca
sahel 2017/06/16 14:56:20 Done.
161 GetRouter()->RouteMouseWheelEvent(GetRootView(), &wheel_event,
162 ui::LatencyInfo());
163
164 // Runs until we get the InputMsgAck callback.
165 EXPECT_EQ(INPUT_EVENT_ACK_STATE_NOT_CONSUMED,
166 input_msg_watcher->WaitForAck());
167
168 while (ExecuteScriptAndExtractInt("document.scrollingElement.scrollTop") <
169 -delta_y)
tdresser 2017/06/15 13:40:01 I prefer using {} when the condition is multi-line
sahel 2017/06/16 14:56:20 Done.
170 frame_watcher.WaitFrames(1);
171
172 EXPECT_EQ(0, ExecuteScriptAndExtractInt("scrollableDiv.scrollTop"));
173 EXPECT_EQ(1, ExecuteScriptAndExtractInt("documentWheelEventCounter"));
174 EXPECT_EQ(0, ExecuteScriptAndExtractInt("scrollableDivWheelEventCounter"));
175
176 if (wheel_scroll_latching_enabled_)
177 wheel_event.phase = blink::WebMouseWheelEvent::kPhaseChanged;
178 GetRouter()->RouteMouseWheelEvent(GetRootView(), &wheel_event,
179 ui::LatencyInfo());
180
181 // Runs until we get the InputMsgAck callback.
182 EXPECT_EQ(INPUT_EVENT_ACK_STATE_NOT_CONSUMED,
183 input_msg_watcher->WaitForAck());
184
185 if (wheel_scroll_latching_enabled_) {
186 while (ExecuteScriptAndExtractInt("document.scrollingElement.scrollTop") <
187 -2 * delta_y)
tdresser 2017/06/15 13:40:01 {}
sahel 2017/06/16 14:56:20 Done.
188 frame_watcher.WaitFrames(1);
189
190 EXPECT_EQ(0, ExecuteScriptAndExtractInt("scrollableDiv.scrollTop"));
191 EXPECT_EQ(2, ExecuteScriptAndExtractInt("documentWheelEventCounter"));
192 EXPECT_EQ(0,
193 ExecuteScriptAndExtractInt("scrollableDivWheelEventCounter"));
194 } else {
195 while (ExecuteScriptAndExtractInt("scrollableDiv.scrollTop") < -delta_y)
196 frame_watcher.WaitFrames(1);
197
198 EXPECT_EQ(1, ExecuteScriptAndExtractInt("documentWheelEventCounter"));
199 EXPECT_EQ(1,
200 ExecuteScriptAndExtractInt("scrollableDivWheelEventCounter"));
201 }
202 }
203
204 private:
205 base::test::ScopedFeatureList feature_list_;
206 bool wheel_scroll_latching_enabled_;
207 };
208
209 class WheelScrollLatchingDisabledBrowserTest
210 : public WheelScrollLatchingBrowserTest {
211 public:
212 WheelScrollLatchingDisabledBrowserTest()
213 : WheelScrollLatchingBrowserTest(false) {}
214 ~WheelScrollLatchingDisabledBrowserTest() override {}
215 };
216
217 // Start scrolling by mouse wheel on the document: the wheel event will be sent
218 // to the document's scrolling element, the scrollable div will be under the
219 // cursor after applying the scrolling. Continue scrolling by mouse wheel, since
220 // wheel scroll latching is enabled the wheel event will be still sent to the
221 // document's scrolling element and the document's scrolling element will
222 // continue scrolling.
223 IN_PROC_BROWSER_TEST_F(WheelScrollLatchingBrowserTest, WheelEventTarget) {
224 WheelEventTargetTest();
225 }
226
227 // Start scrolling by mouse wheel on the document: the wheel event will be sent
228 // to the document's scrolling element, the scrollable div will be under the
229 // cursor after applying the scrolloffsets. Continue scrolling by mouse wheel,
230 // since wheel scroll latching is disabled the wheel event will be still sent to
231 // the scrollable div which is currently under the cursor. The div will start
232 // scrolling.
233 IN_PROC_BROWSER_TEST_F(WheelScrollLatchingDisabledBrowserTest,
234 WheelEventTarget) {
235 WheelEventTargetTest();
236 }
237
238 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698