Chromium Code Reviews| 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 "<meta name='viewport' content='width=device-width'/>" | |
| 40 "<style>" | |
| 41 "html, body {" | |
| 42 " margin: 0;" | |
| 43 "}" | |
| 44 ".spacer { height: 1000px; }" | |
| 45 "</style>" | |
| 46 "<div class=spacer></div>" | |
| 47 "<script>" | |
| 48 " document.addEventListener('wheel', function(e) { while(true) {} }, " | |
| 49 "{'passive': true});" | |
| 50 " document.addEventListener('touchstart', function(e) { while(true) {} }, " | |
| 51 "{'passive': true});" | |
| 52 " document.title='ready';" | |
| 53 "</script>"; | |
| 54 | |
| 55 } // namespace | |
| 56 | |
| 57 namespace content { | |
| 58 | |
| 59 class NonBlockingEventBrowserTest : public ContentBrowserTest { | |
| 60 public: | |
| 61 NonBlockingEventBrowserTest() {} | |
| 62 ~NonBlockingEventBrowserTest() override {} | |
| 63 | |
| 64 RenderWidgetHostImpl* GetWidgetHost() { | |
| 65 return RenderWidgetHostImpl::From( | |
| 66 shell()->web_contents()->GetRenderViewHost()->GetWidget()); | |
|
Charlie Reis
2016/02/16 22:25:22
This isn't safe in general, since a WebContents ca
dtapuska
2016/02/17 03:34:56
This pattern is used in other browser tests. The U
Charlie Reis
2016/02/17 06:34:13
Sure, but I brought it up because new code should
| |
| 67 } | |
| 68 | |
| 69 void OnSyntheticGestureCompleted(SyntheticGesture::Result result) { | |
| 70 EXPECT_EQ(SyntheticGesture::GESTURE_FINISHED, result); | |
| 71 } | |
| 72 | |
| 73 protected: | |
| 74 void LoadURL() { | |
| 75 const GURL data_url(kNonBlockingEventDataURL); | |
| 76 NavigateToURL(shell(), data_url); | |
| 77 | |
| 78 RenderWidgetHostImpl* host = GetWidgetHost(); | |
| 79 host->GetView()->SetSize(gfx::Size(400, 400)); | |
| 80 | |
| 81 base::string16 ready_title(base::ASCIIToUTF16("ready")); | |
| 82 TitleWatcher watcher(shell()->web_contents(), ready_title); | |
| 83 ignore_result(watcher.WaitAndGetTitle()); | |
| 84 | |
| 85 MainThreadFrameObserver main_thread_sync(host); | |
| 86 main_thread_sync.Wait(); | |
| 87 } | |
| 88 | |
| 89 // ContentBrowserTest: | |
| 90 void SetUpCommandLine(base::CommandLine* cmd) override { | |
| 91 // TODO(dtapuska): Remove this switch once wheel-gestures ships. | |
| 92 cmd->AppendSwitch(switches::kEnableExperimentalWebPlatformFeatures); | |
| 93 cmd->AppendSwitch(switches::kEnableWheelGestures); | |
| 94 } | |
| 95 | |
| 96 int ExecuteScriptAndExtractInt(const std::string& script) { | |
| 97 int value = 0; | |
| 98 EXPECT_TRUE(content::ExecuteScriptAndExtractInt( | |
| 99 shell()->web_contents(), "domAutomationController.send(" + script + ")", | |
| 100 &value)); | |
| 101 return value; | |
| 102 } | |
| 103 | |
| 104 int GetScrollTop() { | |
| 105 return ExecuteScriptAndExtractInt("document.scrollingElement.scrollTop"); | |
| 106 } | |
| 107 | |
| 108 void DoWheelScroll() { | |
| 109 EXPECT_EQ(0, GetScrollTop()); | |
| 110 | |
| 111 int scrollHeight = | |
| 112 ExecuteScriptAndExtractInt("document.documentElement.scrollHeight"); | |
| 113 EXPECT_EQ(1000, scrollHeight); | |
| 114 | |
| 115 scoped_refptr<FrameWatcher> frame_watcher(new FrameWatcher()); | |
| 116 GetWidgetHost()->GetProcess()->AddFilter(frame_watcher.get()); | |
| 117 scoped_refptr<InputMsgWatcher> input_msg_watcher( | |
| 118 new InputMsgWatcher(GetWidgetHost(), blink::WebInputEvent::MouseWheel)); | |
| 119 | |
| 120 GetWidgetHost()->ForwardWheelEvent( | |
| 121 SyntheticWebMouseWheelEventBuilder::Build(10, 10, 0, -53, 0, true)); | |
| 122 | |
| 123 // Runs until we get the InputMsgAck callback | |
| 124 EXPECT_EQ(INPUT_EVENT_ACK_STATE_SET_NON_BLOCKING, | |
| 125 input_msg_watcher->WaitForAck()); | |
| 126 frame_watcher->WaitFrames(1); | |
| 127 | |
| 128 // Expect that the compositor scrolled at least one pixel while the | |
| 129 // main thread was in a busy loop. | |
| 130 EXPECT_LT(0, frame_watcher->LastMetadata().root_scroll_offset.y()); | |
| 131 } | |
| 132 | |
| 133 void DoTouchScroll() { | |
| 134 EXPECT_EQ(0, GetScrollTop()); | |
| 135 | |
| 136 int scrollHeight = | |
| 137 ExecuteScriptAndExtractInt("document.documentElement.scrollHeight"); | |
| 138 EXPECT_EQ(1000, scrollHeight); | |
| 139 | |
| 140 scoped_refptr<FrameWatcher> frame_watcher(new FrameWatcher()); | |
| 141 GetWidgetHost()->GetProcess()->AddFilter(frame_watcher.get()); | |
| 142 | |
| 143 SyntheticSmoothScrollGestureParams params; | |
| 144 params.gesture_source_type = SyntheticGestureParams::TOUCH_INPUT; | |
| 145 params.anchor = gfx::PointF(50, 50); | |
| 146 params.distances.push_back(gfx::Vector2d(0, -45)); | |
| 147 | |
| 148 scoped_ptr<SyntheticSmoothScrollGesture> gesture( | |
| 149 new SyntheticSmoothScrollGesture(params)); | |
| 150 GetWidgetHost()->QueueSyntheticGesture( | |
| 151 std::move(gesture), | |
| 152 base::Bind(&NonBlockingEventBrowserTest::OnSyntheticGestureCompleted, | |
| 153 base::Unretained(this))); | |
| 154 | |
| 155 // Expect that the compositor scrolled at least one pixel while the | |
| 156 // main thread was in a busy loop. | |
| 157 while (frame_watcher->LastMetadata().root_scroll_offset.y() <= 0) | |
| 158 frame_watcher->WaitFrames(1); | |
| 159 } | |
| 160 | |
| 161 private: | |
| 162 DISALLOW_COPY_AND_ASSIGN(NonBlockingEventBrowserTest); | |
| 163 }; | |
| 164 | |
| 165 // Disabled on MacOS because it doesn't support wheel gestures | |
| 166 // just yet. | |
| 167 #if defined(OS_MACOSX) | |
| 168 #define MAYBE_MouseWheel DISABLED_MouseWheel | |
| 169 #else | |
| 170 #define MAYBE_MouseWheel MouseWheel | |
| 171 #endif | |
| 172 IN_PROC_BROWSER_TEST_F(NonBlockingEventBrowserTest, MAYBE_MouseWheel) { | |
| 173 LoadURL(); | |
| 174 DoWheelScroll(); | |
| 175 } | |
| 176 | |
| 177 // Disabled on MacOS because it doesn't support touch input. | |
| 178 #if defined(OS_MACOSX) | |
| 179 #define MAYBE_TouchStart DISABLED_TouchStart | |
| 180 #else | |
| 181 #define MAYBE_TouchStart TouchStart | |
| 182 #endif | |
| 183 IN_PROC_BROWSER_TEST_F(NonBlockingEventBrowserTest, MAYBE_TouchStart) { | |
| 184 LoadURL(); | |
| 185 DoTouchScroll(); | |
| 186 } | |
| 187 | |
| 188 } // namespace content | |
| OLD | NEW |