OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 <utility> |
| 6 |
| 7 #include "base/auto_reset.h" |
| 8 #include "base/bind.h" |
| 9 #include "base/command_line.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/run_loop.h" |
| 12 #include "base/strings/utf_string_conversions.h" |
| 13 #include "build/build_config.h" |
| 14 #include "content/browser/renderer_host/input/synthetic_smooth_scroll_gesture.h" |
| 15 #include "content/browser/renderer_host/render_widget_host_impl.h" |
| 16 #include "content/browser/renderer_host/render_widget_host_view_base.h" |
| 17 #include "content/browser/web_contents/web_contents_impl.h" |
| 18 #include "content/common/input/synthetic_web_input_event_builders.h" |
| 19 #include "content/common/input_messages.h" |
| 20 #include "content/public/browser/render_view_host.h" |
| 21 #include "content/public/browser/render_widget_host_view.h" |
| 22 #include "content/public/common/content_switches.h" |
| 23 #include "content/public/test/browser_test_utils.h" |
| 24 #include "content/public/test/content_browser_test.h" |
| 25 #include "content/public/test/content_browser_test_utils.h" |
| 26 #include "content/public/test/test_utils.h" |
| 27 #include "content/shell/browser/shell.h" |
| 28 #include "third_party/WebKit/public/web/WebInputEvent.h" |
| 29 #include "ui/events/event_switches.h" |
| 30 #include "ui/events/latency_info.h" |
| 31 |
| 32 using blink::WebInputEvent; |
| 33 |
| 34 namespace { |
| 35 |
| 36 const char kNonBlockingEventDataURL[] = |
| 37 "data:text/html;charset=utf-8," |
| 38 "<!DOCTYPE html>" |
| 39 "<style>" |
| 40 "html, body {" |
| 41 " margin: 0;" |
| 42 "}" |
| 43 ".spacer { height: 1000px; }" |
| 44 "</style>" |
| 45 "<div class=spacer></div>" |
| 46 "<script>" |
| 47 " document.addEventListener('wheel', function(e) { while(true) {} }, " |
| 48 "{'passive': true});" |
| 49 " document.addEventListener('touchstart', function(e) { while(true) {} }, " |
| 50 "{'passive': true});" |
| 51 " document.title='ready';" |
| 52 "</script>"; |
| 53 |
| 54 } // namespace |
| 55 |
| 56 namespace content { |
| 57 |
| 58 class NonBlockingEventBrowserTest : public ContentBrowserTest { |
| 59 public: |
| 60 NonBlockingEventBrowserTest() {} |
| 61 ~NonBlockingEventBrowserTest() override {} |
| 62 |
| 63 RenderWidgetHostImpl* GetWidgetHost() { |
| 64 return RenderWidgetHostImpl::From( |
| 65 shell()->web_contents()->GetRenderViewHost()->GetWidget()); |
| 66 } |
| 67 |
| 68 void OnSyntheticGestureCompleted(SyntheticGesture::Result result) { |
| 69 EXPECT_EQ(SyntheticGesture::GESTURE_FINISHED, result); |
| 70 } |
| 71 |
| 72 protected: |
| 73 void LoadURL() { |
| 74 const GURL data_url(kNonBlockingEventDataURL); |
| 75 NavigateToURL(shell(), data_url); |
| 76 |
| 77 RenderWidgetHostImpl* host = GetWidgetHost(); |
| 78 host->GetView()->SetSize(gfx::Size(400, 400)); |
| 79 |
| 80 base::string16 ready_title(base::ASCIIToUTF16("ready")); |
| 81 TitleWatcher watcher(shell()->web_contents(), ready_title); |
| 82 ignore_result(watcher.WaitAndGetTitle()); |
| 83 |
| 84 MainThreadFrameObserver main_thread_sync(host); |
| 85 main_thread_sync.Wait(); |
| 86 } |
| 87 |
| 88 // ContentBrowserTest: |
| 89 void SetUpCommandLine(base::CommandLine* cmd) override { |
| 90 // TODO(dtapuska): Remove this switch once wheel-gestures ships. |
| 91 cmd->AppendSwitch(switches::kEnableExperimentalWebPlatformFeatures); |
| 92 cmd->AppendSwitch(switches::kEnableWheelGestures); |
| 93 } |
| 94 |
| 95 int ExecuteScriptAndExtractInt(const std::string& script) { |
| 96 int value = 0; |
| 97 EXPECT_TRUE(content::ExecuteScriptAndExtractInt( |
| 98 shell()->web_contents(), "domAutomationController.send(" + script + ")", |
| 99 &value)); |
| 100 return value; |
| 101 } |
| 102 |
| 103 int GetScrollTop() { |
| 104 return ExecuteScriptAndExtractInt("document.scrollingElement.scrollTop"); |
| 105 } |
| 106 |
| 107 void DoWheelScroll() { |
| 108 EXPECT_EQ(0, GetScrollTop()); |
| 109 |
| 110 int scrollHeight = |
| 111 ExecuteScriptAndExtractInt("document.documentElement.scrollHeight"); |
| 112 EXPECT_EQ(1000, scrollHeight); |
| 113 |
| 114 scoped_refptr<FrameWatcher> frame_watcher(new FrameWatcher()); |
| 115 GetWidgetHost()->GetProcess()->AddFilter(frame_watcher.get()); |
| 116 scoped_refptr<InputMsgWatcher> input_msg_watcher( |
| 117 new InputMsgWatcher(GetWidgetHost(), blink::WebInputEvent::MouseWheel)); |
| 118 |
| 119 GetWidgetHost()->ForwardWheelEvent( |
| 120 SyntheticWebMouseWheelEventBuilder::Build(10, 10, 0, -53, 0, true)); |
| 121 |
| 122 // Runs until we get the InputMsgAck callback |
| 123 EXPECT_EQ(INPUT_EVENT_ACK_STATE_SET_NON_BLOCKING, |
| 124 input_msg_watcher->WaitForAck()); |
| 125 frame_watcher->WaitFrames(1); |
| 126 |
| 127 // Expect that the compositor scrolled at least one pixel while the |
| 128 // main thread was in a busy loop. |
| 129 EXPECT_LT(0, frame_watcher->LastMetadata().root_scroll_offset.y()); |
| 130 } |
| 131 |
| 132 void DoTouchScroll() { |
| 133 EXPECT_EQ(0, GetScrollTop()); |
| 134 |
| 135 int scrollHeight = |
| 136 ExecuteScriptAndExtractInt("document.documentElement.scrollHeight"); |
| 137 EXPECT_EQ(1000, scrollHeight); |
| 138 |
| 139 scoped_refptr<FrameWatcher> frame_watcher(new FrameWatcher()); |
| 140 GetWidgetHost()->GetProcess()->AddFilter(frame_watcher.get()); |
| 141 |
| 142 SyntheticSmoothScrollGestureParams params; |
| 143 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; |
| 144 params.anchor = gfx::PointF(50, 50); |
| 145 params.distances.push_back(gfx::Vector2d(0, -45)); |
| 146 |
| 147 scoped_ptr<SyntheticSmoothScrollGesture> gesture( |
| 148 new SyntheticSmoothScrollGesture(params)); |
| 149 GetWidgetHost()->QueueSyntheticGesture( |
| 150 std::move(gesture), |
| 151 base::Bind(&NonBlockingEventBrowserTest::OnSyntheticGestureCompleted, |
| 152 base::Unretained(this))); |
| 153 |
| 154 // Expect that the compositor scrolled at least one pixel while the |
| 155 // main thread was in a busy loop. |
| 156 while (frame_watcher->LastMetadata().root_scroll_offset.y() <= 0) |
| 157 frame_watcher->WaitFrames(1); |
| 158 } |
| 159 |
| 160 private: |
| 161 DISALLOW_COPY_AND_ASSIGN(NonBlockingEventBrowserTest); |
| 162 }; |
| 163 |
| 164 IN_PROC_BROWSER_TEST_F(NonBlockingEventBrowserTest, MouseWheel) { |
| 165 LoadURL(); |
| 166 DoWheelScroll(); |
| 167 } |
| 168 |
| 169 IN_PROC_BROWSER_TEST_F(NonBlockingEventBrowserTest, TouchStart) { |
| 170 LoadURL(); |
| 171 DoTouchScroll(); |
| 172 } |
| 173 |
| 174 } // namespace content |
OLD | NEW |