OLD | NEW |
(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 "<meta name='viewport' content='width=device-width, minimum-scale=1'>" |
| 35 "<style>" |
| 36 "body {" |
| 37 " height: 10000px;" |
| 38 "}" |
| 39 "#scrollableDiv {" |
| 40 " position: absolute;" |
| 41 " left: 50px;" |
| 42 " top: 100px;" |
| 43 " width: 200px;" |
| 44 " height: 200px;" |
| 45 " overflow: scroll;" |
| 46 " background: red;" |
| 47 "}" |
| 48 "#nestedDiv {" |
| 49 " width: 200px;" |
| 50 " height: 8000px;" |
| 51 " opacity: 0;" |
| 52 "}" |
| 53 "</style>" |
| 54 "<div id='scrollableDiv'>" |
| 55 " <div id='nestedDiv'></div>" |
| 56 "</div>" |
| 57 "<script>" |
| 58 " var scrollableDiv = document.getElementById('scrollableDiv');" |
| 59 " var scrollableDivWheelEventCounter = 0;" |
| 60 " var documentWheelEventCounter = 0;" |
| 61 " scrollableDiv.addEventListener('wheel'," |
| 62 " function(e) { scrollableDivWheelEventCounter++;" |
| 63 " e.stopPropagation(); });" |
| 64 " document.scrollingElement.addEventListener('wheel'," |
| 65 " function(e) { documentWheelEventCounter++; });" |
| 66 "</script>"; |
| 67 } // namespace |
| 68 |
| 69 namespace content { |
| 70 class WheelScrollLatchingBrowserTest : public ContentBrowserTest { |
| 71 public: |
| 72 WheelScrollLatchingBrowserTest(bool wheel_scroll_latching_enabled = true) |
| 73 : wheel_scroll_latching_enabled_(wheel_scroll_latching_enabled) { |
| 74 ui::GestureConfiguration::GetInstance()->set_scroll_debounce_interval_in_ms( |
| 75 0); |
| 76 |
| 77 if (wheel_scroll_latching_enabled_) |
| 78 EnableWheelScrollLatching(); |
| 79 else |
| 80 DisableWheelScrollLatching(); |
| 81 } |
| 82 ~WheelScrollLatchingBrowserTest() override {} |
| 83 |
| 84 protected: |
| 85 RenderWidgetHostImpl* GetWidgetHost() { |
| 86 return RenderWidgetHostImpl::From( |
| 87 web_contents()->GetRenderViewHost()->GetWidget()); |
| 88 } |
| 89 |
| 90 WebContentsImpl* web_contents() const { |
| 91 return static_cast<WebContentsImpl*>(shell()->web_contents()); |
| 92 } |
| 93 |
| 94 RenderWidgetHostInputEventRouter* GetRouter() { |
| 95 return web_contents()->GetInputEventRouter(); |
| 96 } |
| 97 |
| 98 RenderWidgetHostViewBase* GetRootView() { |
| 99 return static_cast<RenderWidgetHostViewBase*>(web_contents() |
| 100 ->GetFrameTree() |
| 101 ->root() |
| 102 ->current_frame_host() |
| 103 ->GetView()); |
| 104 } |
| 105 |
| 106 void LoadURL() { |
| 107 const GURL data_url(kWheelEventLatchingDataURL); |
| 108 NavigateToURL(shell(), data_url); |
| 109 |
| 110 RenderWidgetHostImpl* host = GetWidgetHost(); |
| 111 host->GetView()->SetSize(gfx::Size(600, 600)); |
| 112 |
| 113 // The page is loaded in the renderer, wait for a new frame to arrive. |
| 114 while (!host->ScheduleComposite()) |
| 115 GiveItSomeTime(); |
| 116 } |
| 117 int ExecuteScriptAndExtractInt(const std::string& script) { |
| 118 int value = 0; |
| 119 EXPECT_TRUE(content::ExecuteScriptAndExtractInt( |
| 120 shell(), "domAutomationController.send(" + script + ")", &value)); |
| 121 return value; |
| 122 } |
| 123 void EnableWheelScrollLatching() { |
| 124 feature_list_.InitFromCommandLine( |
| 125 features::kTouchpadAndWheelScrollLatching.name, ""); |
| 126 } |
| 127 void DisableWheelScrollLatching() { |
| 128 feature_list_.InitFromCommandLine( |
| 129 "", features::kTouchpadAndWheelScrollLatching.name); |
| 130 } |
| 131 |
| 132 void WheelEventTargetTest() { |
| 133 LoadURL(); |
| 134 EXPECT_EQ(0, ExecuteScriptAndExtractInt("documentWheelEventCounter")); |
| 135 EXPECT_EQ(0, ExecuteScriptAndExtractInt("scrollableDivWheelEventCounter")); |
| 136 |
| 137 FrameWatcher frame_watcher(shell()->web_contents()); |
| 138 scoped_refptr<InputMsgWatcher> input_msg_watcher(new InputMsgWatcher( |
| 139 GetWidgetHost(), blink::WebInputEvent::kMouseWheel)); |
| 140 |
| 141 float scrollable_div_top = |
| 142 ExecuteScriptAndExtractInt("scrollableDiv.getBoundingClientRect().top"); |
| 143 float x = (ExecuteScriptAndExtractInt( |
| 144 "scrollableDiv.getBoundingClientRect().left") + |
| 145 ExecuteScriptAndExtractInt( |
| 146 "scrollableDiv.getBoundingClientRect().right")) / |
| 147 2; |
| 148 float y = 0.5 * scrollable_div_top; |
| 149 float delta_x = 0; |
| 150 float delta_y = -0.6 * scrollable_div_top; |
| 151 blink::WebMouseWheelEvent wheel_event = |
| 152 SyntheticWebMouseWheelEventBuilder::Build(x, y, x, y, delta_x, delta_y, |
| 153 0, true); |
| 154 |
| 155 wheel_event.phase = blink::WebMouseWheelEvent::kPhaseBegan; |
| 156 GetRouter()->RouteMouseWheelEvent(GetRootView(), &wheel_event, |
| 157 ui::LatencyInfo()); |
| 158 |
| 159 // Runs until we get the InputMsgAck callback. |
| 160 EXPECT_EQ(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, |
| 161 input_msg_watcher->WaitForAck()); |
| 162 |
| 163 while (ExecuteScriptAndExtractInt("document.scrollingElement.scrollTop") < |
| 164 -delta_y) { |
| 165 frame_watcher.WaitFrames(1); |
| 166 } |
| 167 |
| 168 EXPECT_EQ(0, ExecuteScriptAndExtractInt("scrollableDiv.scrollTop")); |
| 169 EXPECT_EQ(1, ExecuteScriptAndExtractInt("documentWheelEventCounter")); |
| 170 EXPECT_EQ(0, ExecuteScriptAndExtractInt("scrollableDivWheelEventCounter")); |
| 171 |
| 172 wheel_event.phase = blink::WebMouseWheelEvent::kPhaseChanged; |
| 173 GetRouter()->RouteMouseWheelEvent(GetRootView(), &wheel_event, |
| 174 ui::LatencyInfo()); |
| 175 |
| 176 // Runs until we get the InputMsgAck callback. |
| 177 EXPECT_EQ(INPUT_EVENT_ACK_STATE_NOT_CONSUMED, |
| 178 input_msg_watcher->WaitForAck()); |
| 179 |
| 180 if (wheel_scroll_latching_enabled_) { |
| 181 while (ExecuteScriptAndExtractInt("document.scrollingElement.scrollTop") < |
| 182 -2 * delta_y) { |
| 183 frame_watcher.WaitFrames(1); |
| 184 } |
| 185 |
| 186 EXPECT_EQ(0, ExecuteScriptAndExtractInt("scrollableDiv.scrollTop")); |
| 187 EXPECT_EQ(2, ExecuteScriptAndExtractInt("documentWheelEventCounter")); |
| 188 EXPECT_EQ(0, |
| 189 ExecuteScriptAndExtractInt("scrollableDivWheelEventCounter")); |
| 190 } else { // !wheel_scroll_latching_enabled_ |
| 191 while (ExecuteScriptAndExtractInt("scrollableDiv.scrollTop") < -delta_y) |
| 192 frame_watcher.WaitFrames(1); |
| 193 |
| 194 EXPECT_EQ(1, ExecuteScriptAndExtractInt("documentWheelEventCounter")); |
| 195 EXPECT_EQ(1, |
| 196 ExecuteScriptAndExtractInt("scrollableDivWheelEventCounter")); |
| 197 } |
| 198 } |
| 199 |
| 200 private: |
| 201 base::test::ScopedFeatureList feature_list_; |
| 202 bool wheel_scroll_latching_enabled_; |
| 203 }; |
| 204 |
| 205 class WheelScrollLatchingDisabledBrowserTest |
| 206 : public WheelScrollLatchingBrowserTest { |
| 207 public: |
| 208 WheelScrollLatchingDisabledBrowserTest() |
| 209 : WheelScrollLatchingBrowserTest(false) {} |
| 210 ~WheelScrollLatchingDisabledBrowserTest() override {} |
| 211 }; |
| 212 |
| 213 // Start scrolling by mouse wheel on the document: the wheel event will be sent |
| 214 // to the document's scrolling element, the scrollable div will be under the |
| 215 // cursor after applying the scrolling. Continue scrolling by mouse wheel, since |
| 216 // wheel scroll latching is enabled the wheel event will be still sent to the |
| 217 // document's scrolling element and the document's scrolling element will |
| 218 // continue scrolling. |
| 219 IN_PROC_BROWSER_TEST_F(WheelScrollLatchingBrowserTest, WheelEventTarget) { |
| 220 WheelEventTargetTest(); |
| 221 } |
| 222 |
| 223 // Start scrolling by mouse wheel on the document: the wheel event will be sent |
| 224 // to the document's scrolling element, the scrollable div will be under the |
| 225 // cursor after applying the scrolloffsets. Continue scrolling by mouse wheel, |
| 226 // since wheel scroll latching is disabled the wheel event will be still sent to |
| 227 // the scrollable div which is currently under the cursor. The div will start |
| 228 // scrolling. |
| 229 IN_PROC_BROWSER_TEST_F(WheelScrollLatchingDisabledBrowserTest, |
| 230 WheelEventTarget) { |
| 231 WheelEventTargetTest(); |
| 232 } |
| 233 |
| 234 } // namespace content |
OLD | NEW |