1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
content / browser / renderer_host / input / mouse_latency_browsertest.cc [blame]
// Copyright 2017 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <memory>
#include <string>
#include <utility>
#include <vector>
#include "base/functional/bind.h"
#include "base/run_loop.h"
#include "base/test/test_timeouts.h"
#include "base/test/values_test_util.h"
#include "build/build_config.h"
#include "content/browser/renderer_host/render_widget_host_factory.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/browser/site_instance_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/input/synthetic_gesture.h"
#include "content/common/input/synthetic_gesture_controller.h"
#include "content/common/input/synthetic_gesture_params.h"
#include "content/common/input/synthetic_gesture_target.h"
#include "content/common/input/synthetic_pointer_action.h"
#include "content/common/input/synthetic_smooth_move_gesture.h"
#include "content/common/input/synthetic_smooth_scroll_gesture.h"
#include "content/common/input/synthetic_tap_gesture.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/tracing_controller.h"
#include "content/public/test/browser_test.h"
#include "content/public/test/browser_test_utils.h"
#include "content/public/test/content_browser_test.h"
#include "content/public/test/content_browser_test_utils.h"
#include "content/shell/browser/shell.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "testing/gmock/include/gmock/gmock.h"
namespace {
const char kDataURL[] =
"data:text/html;charset=utf-8,"
"<!DOCTYPE html>"
"<html>"
"<head>"
"<title>Mouse event trace events reported.</title>"
"<script>"
" let i=0;"
" document.addEventListener('mousemove', () => {"
" var end = performance.now() + 20;"
" while(performance.now() < end);"
" document.body.style.backgroundColor = 'rgb(' + (i++) + ',0,0)'"
" });"
" document.addEventListener('wheel', (e) => {"
" if (!e.cancelable)"
" return;"
" var end = performance.now() + 50;"
" while(performance.now() < end);"
" });"
"</script>"
"<style>"
"body {"
" height:3000px;"
// Prevent text selection logic from triggering, as it makes the test flaky.
" user-select: none;"
"}"
"</style>"
"</head>"
"<body>"
"</body>"
"</html>";
} // namespace
namespace content {
// This class listens for terminated latency info events. It listens
// for both the mouse event ack and the gpu swap buffers event since
// the event could occur in either.
class TracingRenderWidgetHost : public RenderWidgetHostImpl {
public:
TracingRenderWidgetHost(FrameTree* frame_tree,
RenderWidgetHostDelegate* delegate,
viz::FrameSinkId frame_sink_id,
base::SafeRef<SiteInstanceGroup> site_instance_group,
int32_t routing_id,
bool hidden,
bool renderer_initiated_creation)
: RenderWidgetHostImpl(frame_tree,
/*self_owned=*/false,
frame_sink_id,
delegate,
std::move(site_instance_group),
routing_id,
hidden,
renderer_initiated_creation,
std::make_unique<FrameTokenMessageQueue>()) {}
void OnMouseEventAck(
const input::MouseEventWithLatencyInfo& event,
blink::mojom::InputEventResultSource ack_source,
blink::mojom::InputEventResultState ack_result) override {
RenderWidgetHostImpl::OnMouseEventAck(event, ack_source, ack_result);
}
};
class TracingRenderWidgetHostFactory : public RenderWidgetHostFactory {
public:
TracingRenderWidgetHostFactory() {
RenderWidgetHostFactory::RegisterFactory(this);
}
TracingRenderWidgetHostFactory(const TracingRenderWidgetHostFactory&) =
delete;
TracingRenderWidgetHostFactory& operator=(
const TracingRenderWidgetHostFactory&) = delete;
~TracingRenderWidgetHostFactory() override {
RenderWidgetHostFactory::UnregisterFactory();
}
std::unique_ptr<RenderWidgetHostImpl> CreateRenderWidgetHost(
FrameTree* frame_tree,
RenderWidgetHostDelegate* delegate,
viz::FrameSinkId frame_sink_id,
base::SafeRef<SiteInstanceGroup> site_instance_group,
int32_t routing_id,
bool hidden,
bool renderer_initiated_creation) override {
return std::make_unique<TracingRenderWidgetHost>(
frame_tree, delegate, frame_sink_id, std::move(site_instance_group),
routing_id, hidden, renderer_initiated_creation);
}
};
class MouseLatencyBrowserTest : public ContentBrowserTest {
public:
MouseLatencyBrowserTest() {}
MouseLatencyBrowserTest(const MouseLatencyBrowserTest&) = delete;
MouseLatencyBrowserTest& operator=(const MouseLatencyBrowserTest&) = delete;
~MouseLatencyBrowserTest() override {}
RenderWidgetHostImpl* GetWidgetHost() {
return RenderWidgetHostImpl::From(shell()
->web_contents()
->GetPrimaryMainFrame()
->GetRenderViewHost()
->GetWidget());
}
void OnSyntheticGestureCompleted(SyntheticGesture::Result result) {
EXPECT_EQ(SyntheticGesture::GESTURE_FINISHED, result);
runner_->Quit();
}
void OnTraceDataCollected(std::unique_ptr<std::string> trace_data_string) {
trace_data_ = base::test::ParseJson(*trace_data_string);
runner_->Quit();
}
protected:
void LoadURL() {
const GURL data_url(kDataURL);
EXPECT_TRUE(NavigateToURL(shell(), data_url));
RenderWidgetHostImpl* host = GetWidgetHost();
host->GetView()->SetSize(gfx::Size(400, 400));
}
// Generate mouse events for a synthetic click at |point|.
void DoSyncClick(const gfx::PointF& position) {
SyntheticTapGestureParams params;
params.gesture_source_type = content::mojom::GestureSourceType::kMouseInput;
params.position = position;
params.duration_ms = 100;
std::unique_ptr<SyntheticTapGesture> gesture(
new SyntheticTapGesture(params));
GetWidgetHost()->QueueSyntheticGesture(
std::move(gesture),
base::BindOnce(&MouseLatencyBrowserTest::OnSyntheticGestureCompleted,
base::Unretained(this)));
// Runs until we get the OnSyntheticGestureCompleted callback
runner_ = std::make_unique<base::RunLoop>();
runner_->Run();
}
// Generate mouse events drag from |position|.
void DoSyncCoalescedMoves(const gfx::PointF position,
const gfx::Vector2dF& delta1,
const gfx::Vector2dF& delta2) {
SyntheticSmoothMoveGestureParams params;
params.input_type = SyntheticSmoothMoveGestureParams::MOUSE_DRAG_INPUT;
params.start_point.SetPoint(position.x(), position.y());
params.distances.push_back(delta1);
params.distances.push_back(delta2);
std::unique_ptr<SyntheticSmoothMoveGesture> gesture(
new SyntheticSmoothMoveGesture(params));
GetWidgetHost()->QueueSyntheticGesture(
std::move(gesture),
base::BindOnce(&MouseLatencyBrowserTest::OnSyntheticGestureCompleted,
base::Unretained(this)));
// Runs until we get the OnSyntheticGestureCompleted callback
runner_ = std::make_unique<base::RunLoop>();
runner_->Run();
}
// Generate mouse wheel scroll.
void DoSyncCoalescedMouseWheel(const gfx::PointF position,
const gfx::Vector2dF& delta) {
SyntheticSmoothScrollGestureParams params;
params.gesture_source_type = content::mojom::GestureSourceType::kMouseInput;
params.anchor = position;
params.distances.push_back(delta);
GetWidgetHost()->QueueSyntheticGesture(
std::make_unique<SyntheticSmoothScrollGesture>(params),
base::BindOnce(&MouseLatencyBrowserTest::OnSyntheticGestureCompleted,
base::Unretained(this)));
// Runs until we get the OnSyntheticGestureCompleted callback
runner_ = std::make_unique<base::RunLoop>();
runner_->Run();
}
void StartTracing() {
base::trace_event::TraceConfig trace_config(
"{"
"\"enable_argument_filter\":false,"
"\"enable_systrace\":false,"
"\"included_categories\":["
"\"latencyInfo\""
"],"
"\"record_mode\":\"record-until-full\""
"}");
base::RunLoop run_loop;
ASSERT_TRUE(TracingController::GetInstance()->StartTracing(
trace_config, run_loop.QuitClosure()));
run_loop.Run();
}
const base::Value& StopTracing() {
bool success = TracingController::GetInstance()->StopTracing(
TracingController::CreateStringEndpoint(
base::BindOnce(&MouseLatencyBrowserTest::OnTraceDataCollected,
base::Unretained(this))));
EXPECT_TRUE(success);
// Runs until we get the OnTraceDataCollected callback, which populates
// trace_data_;
runner_ = std::make_unique<base::RunLoop>();
runner_->Run();
return trace_data_;
}
std::string ShowTraceEventsWithId(const std::string& id_to_show,
const base::Value::List* traceEvents) {
std::stringstream stream;
for (const base::Value& traceEvent_value : *traceEvents) {
if (!traceEvent_value.is_dict())
continue;
const base::Value::Dict& traceEvent = traceEvent_value.GetDict();
const std::string* id = traceEvent.FindString("id");
if (!id)
continue;
if (*id == id_to_show)
stream << traceEvent;
}
return stream.str();
}
void AssertTraceIdsBeginAndEnd(const base::Value& trace_data,
const std::string& trace_event_name) {
const base::Value::Dict* trace_data_dict = trace_data.GetIfDict();
ASSERT_TRUE(trace_data_dict);
const base::Value::List* traceEvents =
trace_data_dict->FindList("traceEvents");
ASSERT_TRUE(traceEvents);
std::map<std::string, int> trace_ids;
for (const base::Value& traceEvent_value : *traceEvents) {
ASSERT_TRUE(traceEvent_value.is_dict());
const base::Value::Dict& traceEvent = traceEvent_value.GetDict();
const std::string* name = traceEvent.FindString("name");
ASSERT_TRUE(name);
if (*name != trace_event_name)
continue;
const std::string* id = traceEvent.FindString("id");
if (id)
++trace_ids[*id];
}
for (auto i : trace_ids) {
// Each trace id should show up once for the begin, and once for the end.
EXPECT_EQ(2, i.second) << ShowTraceEventsWithId(i.first, traceEvents);
}
}
private:
std::unique_ptr<base::RunLoop> runner_;
base::Value trace_data_;
TracingRenderWidgetHostFactory widget_factory_;
};
// Ensures that LatencyInfo async slices are reported correctly for MouseUp and
// MouseDown events in the case where no swap is generated.
// Disabled on Android because we don't support synthetic mouse input on
// Android (crbug.com/723618).
// Disabled on due to flakiness (https://crbug.com/800303, https://crbug.com/815363).
IN_PROC_BROWSER_TEST_F(MouseLatencyBrowserTest,
DISABLED_MouseDownAndUpRecordedWithoutSwap) {
LoadURL();
auto filter = std::make_unique<InputMsgWatcher>(
GetWidgetHost(), blink::WebInputEvent::Type::kMouseUp);
StartTracing();
DoSyncClick(gfx::PointF(100, 100));
EXPECT_EQ(blink::mojom::InputEventResultState::kNotConsumed,
filter->GetAckStateWaitIfNecessary());
const base::Value& trace_data = StopTracing();
const base::Value::Dict* trace_data_dict = trace_data.GetIfDict();
ASSERT_TRUE(trace_data_dict);
const base::Value::List* traceEvents =
trace_data_dict->FindList("traceEvents");
ASSERT_TRUE(traceEvents);
std::vector<std::string> trace_event_names;
for (const base::Value& traceEvent_value : *traceEvents) {
ASSERT_TRUE(traceEvent_value.is_dict());
const base::Value::Dict& traceEvent = traceEvent_value.GetDict();
const std::string* name = traceEvent.FindString("name");
ASSERT_TRUE(name);
if (*name != "InputLatency::MouseUp" && *name != "InputLatency::MouseDown")
continue;
trace_event_names.push_back(*name);
}
// We see two events per async slice, a begin and an end.
EXPECT_THAT(trace_event_names,
testing::UnorderedElementsAre(
"InputLatency::MouseDown", "InputLatency::MouseDown",
"InputLatency::MouseUp", "InputLatency::MouseUp"));
}
// Ensures that LatencyInfo async slices are reported correctly for MouseMove
// events in the case where events are coalesced. (crbug.com/771165).
// Disabled on Android because we don't support synthetic mouse input on Android
// (crbug.com/723618).
// http://crbug.com/801629 : Flaky on multiple platforms.
IN_PROC_BROWSER_TEST_F(MouseLatencyBrowserTest,
DISABLED_CoalescedMouseMovesCorrectlyTerminated) {
LoadURL();
StartTracing();
DoSyncCoalescedMoves(gfx::PointF(100, 100), gfx::Vector2dF(150, 150),
gfx::Vector2dF(250, 250));
// The following wait is the upper bound for gpu swap completed callback. It
// is two frames to account for double buffering.
MainThreadFrameObserver observer(GetWidgetHost());
observer.Wait();
observer.Wait();
const base::Value& trace_data = StopTracing();
AssertTraceIdsBeginAndEnd(trace_data, "InputLatency::MouseMove");
}
// TODO(crbug.com/41436535): This is flaky on multiple platforms.
IN_PROC_BROWSER_TEST_F(MouseLatencyBrowserTest,
DISABLED_CoalescedMouseWheelsCorrectlyTerminated) {
LoadURL();
StartTracing();
DoSyncCoalescedMouseWheel(gfx::PointF(100, 100), gfx::Vector2dF(0, -100));
const base::Value& trace_data = StopTracing();
AssertTraceIdsBeginAndEnd(trace_data, "InputLatency::MouseWheel");
}
} // namespace content