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 kJankyPageURL[] = | |
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: 10000px; }" | |
45 "</style>" | |
46 "<div class=spacer></div>" | |
47 "<script>" | |
48 " function jank(millis)" | |
49 " {" | |
50 " var end = performance.now() + millis;" | |
51 " while(performance.now() < end) {};" | |
52 " }" | |
53 " window.mouseMoveCount = 0;" | |
54 " window.touchMoveCount = 0;" | |
55 " function mouseMoveCounter(e) {" | |
56 " window.mouseMoveCount++;" | |
57 " window.lastMouseMoveEvent = e;" | |
58 " }" | |
59 " function touchMoveCounter(e) {" | |
60 " window.touchMoveCount++;" | |
61 " window.lastTouchMoveEvent = e;" | |
62 " }" | |
63 " document.addEventListener('mousemove', mouseMoveCounter);" | |
tdresser
2016/08/04 18:02:22
I'd be tempted just to make these method anonymous
dtapuska
2016/08/08 15:34:03
Done.
| |
64 " document.addEventListener('touchmove', touchMoveCounter, {passive: " | |
65 "true});" | |
66 " document.addEventListener('click', function(e) { jank(100); });" | |
67 " document.title='ready';" | |
68 "</script>"; | |
69 | |
70 } // namespace | |
71 | |
72 namespace content { | |
73 | |
74 class MainThreadEventQueueBrowserTest : public ContentBrowserTest { | |
75 public: | |
76 MainThreadEventQueueBrowserTest() {} | |
77 ~MainThreadEventQueueBrowserTest() override {} | |
78 | |
79 RenderWidgetHostImpl* GetWidgetHost() { | |
80 return RenderWidgetHostImpl::From( | |
81 shell()->web_contents()->GetRenderViewHost()->GetWidget()); | |
82 } | |
83 | |
84 void OnSyntheticGestureCompleted(SyntheticGesture::Result result) { | |
85 EXPECT_EQ(SyntheticGesture::GESTURE_FINISHED, result); | |
86 } | |
87 | |
88 protected: | |
89 void LoadURL(const char* page_data) { | |
90 const GURL data_url(page_data); | |
91 NavigateToURL(shell(), data_url); | |
92 | |
93 RenderWidgetHostImpl* host = GetWidgetHost(); | |
94 host->GetView()->SetSize(gfx::Size(400, 400)); | |
95 | |
96 base::string16 ready_title(base::ASCIIToUTF16("ready")); | |
97 TitleWatcher watcher(shell()->web_contents(), ready_title); | |
98 ignore_result(watcher.WaitAndGetTitle()); | |
99 | |
100 MainThreadFrameObserver main_thread_sync(host); | |
101 main_thread_sync.Wait(); | |
102 } | |
103 | |
104 int ExecuteScriptAndExtractInt(const std::string& script) { | |
105 int value = 0; | |
106 EXPECT_TRUE(content::ExecuteScriptAndExtractInt( | |
107 shell(), "domAutomationController.send(" + script + ")", &value)); | |
108 return value; | |
109 } | |
110 | |
111 void DoMouseMove() { | |
112 // Send a click event to cause some jankiness. | |
113 SimulateMouseClick(shell()->web_contents(), 0, | |
114 blink::WebPointerProperties::ButtonLeft); | |
tdresser
2016/08/04 18:02:21
Why not just call jank() explicitly here?
dtapuska
2016/08/08 15:34:03
Because ExecuteScript is synchronous and I want to
| |
115 scoped_refptr<InputMsgWatcher> input_msg_watcher( | |
116 new InputMsgWatcher(GetWidgetHost(), blink::WebInputEvent::MouseMove)); | |
117 GetWidgetHost()->ForwardMouseEvent(SyntheticWebMouseEventBuilder::Build( | |
118 blink::WebInputEvent::MouseMove, 10, 10, 0)); | |
119 GetWidgetHost()->ForwardMouseEvent(SyntheticWebMouseEventBuilder::Build( | |
120 blink::WebInputEvent::MouseMove, 15, 15, 0)); | |
121 GetWidgetHost()->ForwardMouseEvent(SyntheticWebMouseEventBuilder::Build( | |
122 blink::WebInputEvent::MouseMove, 20, 25, 0)); | |
123 | |
124 // Runs until we get the InputMsgAck callback | |
tdresser
2016/08/04 18:02:22
Missing .
dtapuska
2016/08/08 15:34:03
Done.
| |
125 EXPECT_EQ(INPUT_EVENT_ACK_STATE_CONSUMED, input_msg_watcher->WaitForAck()); | |
126 | |
127 int mouse_move_count = 0; | |
128 while ((mouse_move_count = | |
129 ExecuteScriptAndExtractInt("window.mouseMoveCount")) <= 0) { | |
tdresser
2016/08/04 18:02:22
I'd find this easier to read as:
int mouse_move_c
dtapuska
2016/08/08 15:34:03
Done. It isn't a real busy loop as ExecuteScriptAn
| |
130 } | |
131 EXPECT_EQ(1, mouse_move_count); | |
132 | |
133 int last_mouse_x = | |
134 ExecuteScriptAndExtractInt("window.lastMouseMoveEvent.x"); | |
135 int last_mouse_y = | |
136 ExecuteScriptAndExtractInt("window.lastMouseMoveEvent.y"); | |
137 EXPECT_EQ(20, last_mouse_x); | |
138 EXPECT_EQ(25, last_mouse_y); | |
139 } | |
140 | |
141 void DoTouchMove() { | |
142 SyntheticWebTouchEvent kEvents[4]; | |
143 kEvents[0].PressPoint(10, 10); | |
144 kEvents[1].PressPoint(10, 10); | |
145 kEvents[1].MovePoint(0, 20, 20); | |
146 kEvents[2].PressPoint(10, 10); | |
147 kEvents[2].MovePoint(0, 30, 30); | |
148 kEvents[3].PressPoint(10, 10); | |
149 kEvents[3].MovePoint(0, 35, 40); | |
150 | |
151 // Send a click event to cause some jankiness. | |
152 SimulateMouseClick(shell()->web_contents(), 0, | |
153 blink::WebPointerProperties::ButtonLeft); | |
154 scoped_refptr<InputMsgWatcher> input_msg_watcher( | |
155 new InputMsgWatcher(GetWidgetHost(), blink::WebInputEvent::TouchMove)); | |
156 | |
157 for (const auto& event : kEvents) | |
158 GetWidgetHost()->ForwardEmulatedTouchEvent(event); | |
159 | |
160 // Runs until we get the InputMsgAck callback | |
tdresser
2016/08/04 18:02:22
Missing .
dtapuska
2016/08/08 15:34:03
Done.
| |
161 EXPECT_EQ(INPUT_EVENT_ACK_STATE_SET_NON_BLOCKING, | |
162 input_msg_watcher->WaitForAck()); | |
163 | |
164 int touch_move_count = 0; | |
165 while ((touch_move_count = | |
166 ExecuteScriptAndExtractInt("window.touchMoveCount")) <= 0) { | |
167 } | |
168 EXPECT_EQ(1, touch_move_count); | |
169 | |
170 int last_touch_x = ExecuteScriptAndExtractInt( | |
171 "window.lastTouchMoveEvent.touches[0].pageX"); | |
172 int last_touch_y = ExecuteScriptAndExtractInt( | |
173 "window.lastTouchMoveEvent.touches[0].pageY"); | |
174 EXPECT_EQ(35, last_touch_x); | |
175 EXPECT_EQ(40, last_touch_y); | |
176 } | |
177 | |
178 private: | |
179 DISALLOW_COPY_AND_ASSIGN(MainThreadEventQueueBrowserTest); | |
180 }; | |
181 | |
182 IN_PROC_BROWSER_TEST_F(MainThreadEventQueueBrowserTest, MouseMove) { | |
183 LoadURL(kJankyPageURL); | |
184 DoMouseMove(); | |
185 } | |
186 | |
187 // Disabled on MacOS because it doesn't support touch input. | |
188 #if defined(OS_MACOSX) | |
189 #define MAYBE_TouchMove DISABLED_TouchMove | |
190 #else | |
191 #define MAYBE_TouchMove TouchMove | |
192 #endif | |
193 IN_PROC_BROWSER_TEST_F(MainThreadEventQueueBrowserTest, MAYBE_TouchMove) { | |
194 LoadURL(kJankyPageURL); | |
195 DoTouchMove(); | |
196 } | |
197 | |
198 } // namespace content | |
OLD | NEW |