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
399
400
content / browser / renderer_host / input / composited_scrolling_browsertest.cc [blame]
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include <tuple>
#include <utility>
#include "base/command_line.h"
#include "base/functional/bind.h"
#include "base/numerics/angle_conversions.h"
#include "base/run_loop.h"
#include "base/strings/utf_string_conversions.h"
#include "base/test/metrics/histogram_tester.h"
#include "base/test/scoped_feature_list.h"
#include "build/build_config.h"
#include "cc/base/features.h"
#include "content/browser/renderer_host/render_widget_host_impl.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/input/synthetic_gesture.h"
#include "content/common/input/synthetic_gesture_params.h"
#include "content/common/input/synthetic_smooth_scroll_gesture.h"
#include "content/common/input/synthetic_smooth_scroll_gesture_params.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/common/content_switches.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/public/test/hit_test_region_observer.h"
#include "content/public/test/test_utils.h"
#include "content/shell/browser/shell.h"
#include "third_party/blink/public/common/features.h"
#include "third_party/blink/public/common/switches.h"
namespace {
const char kCompositedScrollingDataURL[] =
"data:text/html;charset=utf-8,"
"<!DOCTYPE html>"
"<meta name='viewport' content='width=device-width'/>"
"<style>"
"%23scroller {"
" width:500px;"
" height:500px;"
" overflow:scroll;"
" transform: rotateX(-30deg);"
"}"
"%23content {"
" background-color:red;"
" width:1000px;"
" height:1000px;"
"}"
"</style>"
"<div id='scroller'>"
" <div id='content'>"
" </div>"
"</div>"
"<script>"
" document.title='ready';"
"</script>";
} // namespace
namespace content {
class CompositedScrollingBrowserTest : public ContentBrowserTest {
public:
CompositedScrollingBrowserTest() {
// Disable scroll resampling because this is checking scroll distance.
scoped_feature_list_.InitAndDisableFeature(
blink::features::kResamplingScrollEvents);
}
CompositedScrollingBrowserTest(const CompositedScrollingBrowserTest&) =
delete;
CompositedScrollingBrowserTest& operator=(
const CompositedScrollingBrowserTest&) = delete;
~CompositedScrollingBrowserTest() override = default;
void SetUpCommandLine(base::CommandLine* cmd) override {
cmd->AppendSwitch(blink::switches::kEnablePreferCompositingToLCDText);
}
RenderWidgetHostImpl* GetWidgetHost() {
return RenderWidgetHostImpl::From(shell()
->web_contents()
->GetPrimaryMainFrame()
->GetRenderViewHost()
->GetWidget());
}
void OnSyntheticGestureCompleted(SyntheticGesture::Result result) {
EXPECT_EQ(SyntheticGesture::GESTURE_FINISHED, result);
runner_->Quit();
}
protected:
void LoadURL(const char* url) {
const GURL data_url(url);
EXPECT_TRUE(NavigateToURL(shell(), data_url));
RenderWidgetHostImpl* host = GetWidgetHost();
HitTestRegionObserver observer(GetWidgetHost()->GetFrameSinkId());
host->GetView()->SetSize(gfx::Size(400, 400));
std::u16string ready_title(u"ready");
TitleWatcher watcher(shell()->web_contents(), ready_title);
std::ignore = watcher.WaitAndGetTitle();
// Wait for the hit test data to be ready after initiating URL loading
// before returning
observer.WaitForHitTestData();
}
double GetScrollTop() {
return EvalJs(shell(), "document.getElementById(\"scroller\").scrollTop")
.ExtractDouble();
}
// Generate touch events for a synthetic scroll from |point| for |distance|.
// Returns the distance scrolled.
double DoScroll(content::mojom::GestureSourceType type,
const gfx::Point& point,
const gfx::Vector2d& distance) {
SyntheticSmoothScrollGestureParams params;
params.gesture_source_type = type;
params.anchor = gfx::PointF(point);
params.distances.push_back(-distance);
runner_ = new MessageLoopRunner();
std::unique_ptr<SyntheticSmoothScrollGesture> gesture(
new SyntheticSmoothScrollGesture(params));
GetWidgetHost()->QueueSyntheticGesture(
std::move(gesture),
base::BindOnce(
&CompositedScrollingBrowserTest::OnSyntheticGestureCompleted,
base::Unretained(this)));
// Runs until we get the OnSyntheticGestureCompleted callback
runner_->Run();
runner_ = nullptr;
return GetScrollTop();
}
double DoTouchScroll(const gfx::Point& point, const gfx::Vector2d& distance) {
return DoScroll(content::mojom::GestureSourceType::kTouchInput, point,
distance);
}
double DoWheelScroll(const gfx::Point& point, const gfx::Vector2d& distance) {
return DoScroll(content::mojom::GestureSourceType::kMouseInput, point,
distance);
}
private:
base::test::ScopedFeatureList scoped_feature_list_;
scoped_refptr<MessageLoopRunner> runner_;
};
// Verify transforming a scroller doesn't prevent it from scrolling. See
// crbug.com/543655 for a case where this was broken.
// Disabled on MacOS because it doesn't support touch input.
// Disabled on Android due to flakiness, see https://crbug.com/376668.
// Flaky on Windows: crbug.com/804009
#if BUILDFLAG(IS_MAC) || BUILDFLAG(IS_ANDROID) || BUILDFLAG(IS_WIN)
#define MAYBE_Scroll3DTransformedScroller DISABLED_Scroll3DTransformedScroller
#else
#define MAYBE_Scroll3DTransformedScroller Scroll3DTransformedScroller
#endif
IN_PROC_BROWSER_TEST_F(CompositedScrollingBrowserTest,
MAYBE_Scroll3DTransformedScroller) {
LoadURL(kCompositedScrollingDataURL);
ASSERT_EQ(0, GetScrollTop());
double scroll_distance =
DoTouchScroll(gfx::Point(50, 150), gfx::Vector2d(0, 100));
// The scroll distance is increased due to the rotation of the scroller.
EXPECT_NEAR(100 / std::cos(base::DegToRad(30.0f)), scroll_distance, 1.0f);
}
static constexpr unsigned kCompositedScroll = 1 << 0;
static constexpr unsigned kRasterInducingScroll = 1 << 1;
class CompositedScrollingMetricTest
: public CompositedScrollingBrowserTest,
public testing::WithParamInterface<unsigned> {
public:
CompositedScrollingMetricTest() {
if (RasterInducingScrollEnabled()) {
scoped_feature_list_.InitAndEnableFeature(
blink::features::kRasterInducingScroll);
} else {
scoped_feature_list_.InitAndDisableFeature(
blink::features::kRasterInducingScroll);
}
}
void SetUpCommandLine(base::CommandLine* cmd) override {
if (CompositedScrollEnabled()) {
cmd->AppendSwitch(blink::switches::kEnablePreferCompositingToLCDText);
} else {
cmd->AppendSwitch(blink::switches::kDisablePreferCompositingToLCDText);
}
}
static bool CompositedScrollEnabled() {
return GetParam() & kCompositedScroll;
}
static bool RasterInducingScrollEnabled() {
return GetParam() & kRasterInducingScroll;
}
const char* kWheelHistogramName = "Renderer4.ScrollingThread.Wheel";
const char* kTouchHistogramName = "Renderer4.ScrollingThread.Touch";
// These values are defined in input_handler_proxy.cc and match the enum in
// histograms.xml.
enum ScrollingThreadStatus {
kScrollingOnCompositor = 0,
kScrollingOnCompositorBlockedOnMain = 1,
kScrollingOnMain = 2,
kMaxValue = kScrollingOnMain,
};
base::test::ScopedFeatureList scoped_feature_list_;
};
INSTANTIATE_TEST_SUITE_P(
All,
CompositedScrollingMetricTest,
::testing::Values(0,
kCompositedScroll,
kRasterInducingScroll,
kCompositedScroll | kRasterInducingScroll),
[](const ::testing::TestParamInfo<unsigned>& info) {
return base::StringPrintf(
"%s%s",
(info.param & kCompositedScroll) ? "Composited" : "NonComposited",
(info.param & kRasterInducingScroll) ? "_RasterInducingScroll" : "");
});
// Tests the functionality of the histogram tracking composited vs main thread
// scrolls.
IN_PROC_BROWSER_TEST_P(CompositedScrollingMetricTest,
RecordCorrectScrollingThread) {
LoadURL(R"HTML(
data:text/html;charset=utf-8,
<!DOCTYPE html>
<style>
%23scroller {
width: 300px;
height: 300px;
overflow: auto;
}
%23space {
width: 1000px;
height: 1000px;
}
</style>
<div id='scroller'>
<div id='space'></div>
</div>
<script>
document.title = 'ready';
</script>
)HTML");
base::HistogramTester histograms;
DoTouchScroll(gfx::Point(50, 50), gfx::Vector2d(0, 100));
DoTouchScroll(gfx::Point(50, 50), gfx::Vector2d(0, -100));
DoWheelScroll(gfx::Point(50, 50), gfx::Vector2d(0, 100));
content::FetchHistogramsFromChildProcesses();
base::HistogramBase::Sample expected_bucket =
CompositedScrollEnabled() ? kScrollingOnCompositor
// TODO(crbug.com/329115115): For now we still need main-thread
// hit-testing in RasterInducingScroll for non-composited scroller.
: RasterInducingScrollEnabled() ? kScrollingOnCompositorBlockedOnMain
: kScrollingOnMain;
histograms.ExpectUniqueSample(kTouchHistogramName, expected_bucket, 2);
histograms.ExpectUniqueSample(kWheelHistogramName, expected_bucket, 1);
}
// Tests the composited vs main thread scrolling histogram in the presence of
// blocking event handlers.
IN_PROC_BROWSER_TEST_P(CompositedScrollingMetricTest, BlockingEventHandlers) {
LoadURL(R"HTML(
data:text/html;charset=utf-8,
<!DOCTYPE html>
<style>
%23scroller {
width: 300px;
height: 300px;
overflow: auto;
}
%23space {
width: 1000px;
height: 1000px;
}
</style>
<div id='scroller'>
<div id='space'></div>
</div>
<script>
const scroller = document.getElementById('scroller');
scroller.addEventListener('wheel', (e) => { }, {passive: false});
scroller.addEventListener('touchstart', (e) => { }, {passive: false});
onload = () => {
document.title = 'ready';
}
</script>
)HTML");
base::HistogramTester histograms;
DoTouchScroll(gfx::Point(50, 50), gfx::Vector2d(0, 100));
DoTouchScroll(gfx::Point(50, 50), gfx::Vector2d(0, -100));
DoWheelScroll(gfx::Point(50, 50), gfx::Vector2d(0, 100));
content::FetchHistogramsFromChildProcesses();
base::HistogramBase::Sample expected_bucket =
CompositedScrollEnabled() ? kScrollingOnCompositorBlockedOnMain
: kScrollingOnMain;
histograms.ExpectUniqueSample(kTouchHistogramName, expected_bucket, 2);
histograms.ExpectUniqueSample(kWheelHistogramName, expected_bucket, 1);
}
// Tests the composited vs main thread scrolling histogram in the presence of
// passive event handlers. These should behave the same as the case without any
// event handlers at all.
// TODO(crbug.com/335028963): Re-enable this test
#if BUILDFLAG(IS_MAC)
#define MAYBE_PassiveEventHandlers DISABLED_PassiveEventHandlers
#else
#define MAYBE_PassiveEventHandlers PassiveEventHandlers
#endif
IN_PROC_BROWSER_TEST_P(CompositedScrollingMetricTest,
MAYBE_PassiveEventHandlers) {
LoadURL(R"HTML(
data:text/html;charset=utf-8,
<!DOCTYPE html>
<style>
%23scroller {
width: 300px;
height: 300px;
overflow: auto;
}
%23space {
width: 1000px;
height: 1000px;
}
</style>
<div id='scroller'>
<div id='space'></div>
</div>
<script>
const scroller = document.getElementById('scroller');
scroller.addEventListener('wheel', (e) => { }, {passive: true});
scroller.addEventListener('touchstart', (e) => { }, {passive: true});
onload = () => {
document.title = 'ready';
}
</script>
)HTML");
base::HistogramTester histograms;
DoTouchScroll(gfx::Point(50, 50), gfx::Vector2d(0, 100));
DoTouchScroll(gfx::Point(50, 50), gfx::Vector2d(0, -100));
DoWheelScroll(gfx::Point(50, 50), gfx::Vector2d(0, 100));
content::FetchHistogramsFromChildProcesses();
base::HistogramBase::Sample expected_bucket =
CompositedScrollEnabled() ? kScrollingOnCompositor
// TODO(crbug.com/329115115): For now we still need main-thread
// hit-testing in RasterInducingScroll for non-composited scroller.
: RasterInducingScrollEnabled() ? kScrollingOnCompositorBlockedOnMain
: kScrollingOnMain;
histograms.ExpectUniqueSample(kTouchHistogramName, expected_bucket, 2);
histograms.ExpectUniqueSample(kWheelHistogramName, expected_bucket, 1);
}
} // namespace content