/
video_cuj.go
565 lines (494 loc) · 16.6 KB
/
video_cuj.go
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
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
// Copyright 2020 The Chromium OS Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package ui
import (
"context"
"fmt"
"time"
"chromiumos/tast/common/perf"
"chromiumos/tast/ctxutil"
"chromiumos/tast/errors"
"chromiumos/tast/local/audio/crastestclient"
"chromiumos/tast/local/bundles/cros/ui/cuj"
"chromiumos/tast/local/chrome"
"chromiumos/tast/local/chrome/ash"
"chromiumos/tast/local/chrome/browser"
"chromiumos/tast/local/chrome/lacros"
"chromiumos/tast/local/chrome/uiauto"
"chromiumos/tast/local/chrome/uiauto/nodewith"
"chromiumos/tast/local/chrome/uiauto/pointer"
"chromiumos/tast/local/chrome/uiauto/role"
"chromiumos/tast/local/chrome/uiauto/touch"
"chromiumos/tast/local/chrome/webutil"
"chromiumos/tast/local/coords"
"chromiumos/tast/local/input"
"chromiumos/tast/local/ui"
"chromiumos/tast/testing"
"chromiumos/tast/testing/hwdep"
)
type videoCUJTestParam struct {
bt browser.Type
tablet bool
tracing bool
}
func init() {
testing.AddTest(&testing.Test{
Func: VideoCUJ,
LacrosStatus: testing.LacrosVariantExists,
Desc: "Measures the smoothess of switch between full screen video and a tab/app",
Contacts: []string{"xiyuan@chromium.org", "chromeos-perfmetrics-eng@google.com"},
Attr: []string{"group:crosbolt", "crosbolt_perbuild", "group:cuj"},
SoftwareDeps: []string{"chrome", "arc"},
HardwareDeps: hwdep.D(hwdep.InternalDisplay()),
Timeout: 45 * time.Minute,
Vars: []string{
"mute",
},
VarDeps: []string{
"ui.VideoCUJ.ytExperiments",
},
Params: []testing.Param{{
Name: "clamshell",
Fixture: "loggedInToCUJUser",
Val: videoCUJTestParam{
bt: browser.TypeAsh,
},
}, {
Name: "clamshell_trace",
Fixture: "loggedInToCUJUser",
Val: videoCUJTestParam{
bt: browser.TypeAsh,
tracing: true,
},
}, {
Name: "tablet",
Fixture: "loggedInToCUJUser",
Val: videoCUJTestParam{
bt: browser.TypeAsh,
tablet: true,
},
}, {
Name: "lacros",
Fixture: "loggedInToCUJUserLacros",
Val: videoCUJTestParam{
bt: browser.TypeLacros,
},
ExtraSoftwareDeps: []string{"lacros"},
}, {
Name: "lacros_tablet",
Fixture: "loggedInToCUJUserLacros",
Val: videoCUJTestParam{
bt: browser.TypeLacros,
tablet: true,
},
ExtraSoftwareDeps: []string{"lacros"},
}},
})
}
func VideoCUJ(ctx context.Context, s *testing.State) {
// PollOptions to allow blink layout to catch up. Interval at 5s is an
// arbitrary long interval for slow DUTs where blink layout could stay
// stale for a while (we have seen 3s on grunt in local test) and the default
// 100ms interval could take the intermediate layout as the stabilized final
// one.
layoutPollOptions := testing.PollOptions{
Interval: 5 * time.Second,
Timeout: 20 * time.Second,
}
// Shorten context a bit to allow for cleanup.
closeCtx := ctx
ctx, cancel := ctxutil.Shorten(ctx, 2*time.Second)
defer cancel()
testParam := s.Param().(videoCUJTestParam)
var cr *chrome.Chrome
var cs ash.ConnSource
if testParam.bt == browser.TypeAsh {
cr = s.FixtValue().(chrome.HasChrome).Chrome()
cs = cr
} else {
var l *lacros.Lacros
var err error
cr, l, cs, err = lacros.Setup(ctx, s.FixtValue(), testParam.bt)
if err != nil {
s.Fatal("Failed to initialize test: ", err)
}
defer lacros.CloseLacros(ctx, l)
}
tconn, err := cr.TestAPIConn(ctx)
if err != nil {
s.Fatal("Failed to connect to test API: ", err)
}
tabChecker, err := cuj.NewTabCrashChecker(ctx, tconn)
if err != nil {
s.Fatal("Failed to create TabCrashChecker: ", err)
}
if _, ok := s.Var("mute"); ok {
if err := crastestclient.Mute(ctx); err != nil {
s.Fatal("Failed to mute audio: ", err)
}
defer crastestclient.Unmute(closeCtx)
}
tabletMode := testParam.tablet
cleanup, err := ash.EnsureTabletModeEnabled(ctx, tconn, tabletMode)
if err != nil {
s.Fatal("Failed to ensure tablet/clamshell mode: ", err)
}
defer cleanup(closeCtx)
kb, err := input.Keyboard(ctx)
if err != nil {
s.Fatal("Failed to open the keyboard: ", err)
}
defer kb.Close()
var pc pointer.Context
var tsw *input.TouchscreenEventWriter
var stw *input.SingleTouchEventWriter
if tabletMode {
var err error
pc, err = pointer.NewTouch(ctx, tconn)
if err != nil {
s.Fatal("Failed to create a touch controller")
}
tsw, _, err = touch.NewTouchscreenAndConverter(ctx, tconn)
if err != nil {
s.Fatal("Failed to access to the touchscreen: ", err)
}
defer tsw.Close()
stw, err = tsw.NewSingleTouchWriter()
if err != nil {
s.Fatal("Failed to create the single touch writer: ", err)
}
} else {
pc = pointer.NewMouse(tconn)
}
defer pc.Close()
ac := uiauto.New(tconn)
configs := []cuj.MetricConfig{
cuj.NewCustomMetricConfig(
"Ash.Smoothness.PercentDroppedFrames_1sWindow", "percent",
perf.SmallerIsBetter, []int64{50, 80}),
cuj.NewCustomMetricConfig(
"Browser.Responsiveness.JankyIntervalsPerThirtySeconds3", "janks",
perf.SmallerIsBetter, []int64{0, 3}),
}
if tabletMode {
configs = append(configs,
cuj.NewLatencyMetricConfig("Ash.DragWindowFromShelf.PresentationTime"),
cuj.NewSmoothnessMetricConfig("Ash.Overview.AnimationSmoothness.Enter.TabletMode"),
cuj.NewSmoothnessMetricConfig("Ash.Overview.AnimationSmoothness.Exit.TabletMode"),
)
} else {
configs = append(configs,
cuj.NewSmoothnessMetricConfig("Ash.WindowCycleView.AnimationSmoothness.Container"),
)
}
recorder, err := cuj.NewRecorder(ctx, cr, nil, cuj.RecorderOptions{}, configs...)
if err != nil {
s.Fatal("Failed to create a recorder: ", err)
}
if testParam.tracing {
recorder.EnableTracing(s.OutDir())
}
defer recorder.Close(closeCtx)
webConn, err := cs.NewConn(ctx, ui.PerftestURL)
if err != nil {
s.Fatal("Failed to open web: ", err)
}
defer webConn.Close()
var webWinID int
if all, err := ash.GetAllWindows(ctx, tconn); err != nil {
s.Fatal("Failed to get all window: ", err)
} else if len(all) != 1 {
s.Fatalf("Expect 1 window, got %d", len(all))
} else {
webWinID = all[0].ID
}
s.Log("Open youtube Web")
ytConn, err := cs.NewConn(ctx,
"https://www.youtube.com/watch?v=by_xCK2Jo5c&absolute_experiments="+
s.RequiredVar("ui.VideoCUJ.ytExperiments"),
browser.WithNewWindow())
if err != nil {
s.Fatal("Failed to open youtube: ", err)
}
defer ytConn.Close()
var ytWinID int
if all, err := ash.GetAllWindows(ctx, tconn); err != nil {
s.Fatal("Failed to get all window: ", err)
} else if len(all) != 2 {
s.Fatalf("Expect 2 windows, got %d", len(all))
} else {
if webWinID == all[0].ID {
ytWinID = all[1].ID
} else {
ytWinID = all[0].ID
}
}
// Wait for <video> tag to show up.
if err := webutil.WaitForYoutubeVideo(ctx, ytConn, 0); err != nil {
s.Fatal("Failed to wait for video element: ", err)
}
// Hold alt a bit then tab to show the window cycle list.
altTab := func() error {
if err := kb.AccelPress(ctx, "Alt"); err != nil {
return errors.Wrap(err, "failed to press alt")
}
defer kb.AccelRelease(ctx, "Alt")
if err := testing.Sleep(ctx, 500*time.Millisecond); err != nil {
return errors.Wrap(err, "failed to wait")
}
if err := kb.Accel(ctx, "Tab"); err != nil {
return errors.Wrap(err, "failed to type tab")
}
if err := testing.Sleep(ctx, time.Second); err != nil {
return errors.Wrap(err, "failed to wait")
}
return nil
}
getYtElemBounds := func(sel string) (coords.Rect, error) {
var bounds coords.Rect
if err := ytConn.Eval(ctx, fmt.Sprintf(
`(function() {
var b = document.querySelector(%q).getBoundingClientRect();
return {
'left': Math.round(b.left),
'top': Math.round(b.top),
'width': Math.round(b.width),
'height': Math.round(b.height),
};
})()`,
sel), &bounds); err != nil {
return coords.Rect{}, errors.Wrapf(err, "failed to get bounds for selector %q", sel)
}
return bounds, nil
}
// Returns bounds of the element when the element does not change its bounds
// and is at the top (a tap/click should reach it).
getTappableYtElemBounds := func(sel string) (coords.Rect, error) {
var bounds coords.Rect
if err := testing.Poll(ctx, func(ctx context.Context) error {
if newBounds, err := getYtElemBounds(sel); err != nil {
return err
} else if newBounds != bounds {
bounds = newBounds
return errors.New("bounds are changing")
}
if bounds.Width == 0 || bounds.Height == 0 {
return errors.Errorf("bad bound for selector %q", sel)
}
var atTop bool
if err := ytConn.Eval(ctx, fmt.Sprintf(
`(function() {
var sel = document.querySelector(%q);
var el = document.elementFromPoint(%d, %d);
return sel.contains(el);
})()`,
sel, bounds.CenterPoint().X, bounds.CenterPoint().Y),
&atTop); err != nil {
return errors.Wrapf(err, "failed to check at top of selector %q", sel)
}
if !atTop {
return errors.Errorf("selector %q is not at top", sel)
}
return nil
}, &layoutPollOptions); err != nil {
return coords.Rect{}, err
}
return bounds, nil
}
tapYtElem := func(sel string) error {
bounds, err := getTappableYtElemBounds(sel)
if err != nil {
return err
}
ytWeb := nodewith.ClassName("WebContentsViewAura").Role(role.Window).NameContaining("YouTube")
location, err := ac.Location(ctx, ytWeb)
if err != nil {
return errors.Wrap(err, "failed to get YouTube WebContentsViewAura location")
}
tapPoint := bounds.CenterPoint().Add(location.TopLeft())
if err := pc.ClickAt(tapPoint)(ctx); err != nil {
return errors.Wrapf(err, "failed to tap selector %q", sel)
}
return nil
}
tapFullscreenButton := func() error {
if err := tapYtElem(`.ytp-fullscreen-button`); err != nil {
// The failure could be caused by promotion banner covering the button.
// It could happen in small screen devices. Attempt to dismiss the banner.
// Ignore the error since the banner might not be there.
if err := tapYtElem("ytd-button-renderer#dismiss-button"); err != nil {
s.Log("Failed to dismiss banner: ", err)
}
// Attempt to dismiss floating surveys that could cover the bottom-right
// and ignore the errors since the survey could not be there.
if err := tapYtElem("button[aria-label='Dismiss']"); err != nil {
s.Log("Failed to dismiss survey: ", err)
}
// Attempt to dismiss webfe served message box and ignore the errors
// since the message div could not be there.
if err := tapYtElem(".webfe-served-box"); err != nil {
s.Log("Failed to dismiss webfe served message: ", err)
}
// Tap the video to pause it to ensure the fullscreen button showing up.
if err := tapYtElem(`video`); err != nil {
return errors.Wrap(err, "failed to tap video to pause it")
}
// Wait for the button animation to finish. Otherwise, it is not tappable
// even though it has stable bounds and is at the top.
if err := testing.Sleep(ctx, 500*time.Millisecond); err != nil {
return errors.Wrap(err, "failed to wait for button animaiton")
}
// Tap fullscreen button again.
if err := tapYtElem(`.ytp-fullscreen-button`); err != nil {
return errors.Wrap(err, "failed to tap fullscreen button")
}
if err := tapYtElem(`video`); err != nil {
return errors.Wrap(err, "failed to tap video to resume it")
}
}
return nil
}
enterFullscreen := func() error {
if ytWin, err := ash.GetWindow(ctx, tconn, ytWinID); err != nil {
return errors.Wrap(err, "failed to get youtube window")
} else if ytWin.State == ash.WindowStateFullscreen {
return errors.New("alreay in fullscreen")
}
if err := tapFullscreenButton(); err != nil {
return err
}
// Wait for the 'video' element to be updated to fullscreen. This is needed
// because blink layout is updated asynchronously with the browser window
// bounds change. 'video' element is considered fullscreen when either its
// width or its height matches the screen width or height. This is because
// 'video' element is resized to keep video aspect ratio and not always
// filling the screen. The allowed difference of width/height is 2 because
// the difference is about 0.999 on "nocturne" and arbitrarily use 2 to
// handle that edge case.
if err := testing.Poll(ctx, func(ctx context.Context) error {
var fullscreen bool
if err := ytConn.Eval(ctx,
`(function() {
var b = document.querySelector('video').getBoundingClientRect();
return Math.abs(b.width - window.screen.width) < 2 ||
Math.abs(b.height - window.screen.height) < 2;
})()`,
&fullscreen); err != nil {
return errors.Wrap(err, "failed to check video size")
}
if !fullscreen {
return errors.New("video is not fullscreen")
}
return nil
}, &layoutPollOptions); err != nil {
return errors.Wrap(err, "failed to wait for blink layout")
}
// Wait for the browser window to be in fullscreen state.
if err := ash.WaitForCondition(ctx, tconn, func(w *ash.Window) bool {
return w.ID == ytWinID && w.State == ash.WindowStateFullscreen
}, &testing.PollOptions{Timeout: 5 * time.Second}); err != nil {
return errors.Wrap(err, "failed to wait for fullscreen")
}
return nil
}
if err := ash.CloseNotifications(ctx, tconn); err != nil {
s.Fatal("Failed to close ash notification: ", err)
}
s.Log("Make video fullscreen")
if err := enterFullscreen(); err != nil {
s.Fatal("Failed to enter fullscreen: ", err)
}
if err := recorder.RunFor(ctx, func(ctx context.Context) error {
s.Log("Switch away from fullscreen video")
if tabletMode {
if err := tapFullscreenButton(); err != nil {
return errors.Wrap(err, "failed to tap fullscreen button")
}
if err := ash.WaitForCondition(ctx, tconn, func(w *ash.Window) bool {
return w.ID == ytWinID && w.State != ash.WindowStateFullscreen
}, &testing.PollOptions{Timeout: 5 * time.Second}); err != nil {
return errors.Wrap(err, "failed to wait fullscreen exit")
}
if err := ash.DragToShowOverview(ctx, tsw, stw, tconn); err != nil {
return errors.Wrap(err, "failed to DragToShowOverview")
}
w, err := ash.GetWindow(ctx, tconn, webWinID)
if err != nil {
return errors.Wrap(err, "failed to find the other window")
}
if err := pc.ClickAt(w.OverviewInfo.Bounds.CenterPoint())(ctx); err != nil {
return errors.Wrap(err, "failed to tap the other window's overview item")
}
} else {
if err := altTab(); err != nil {
return errors.Wrap(err, "failed to alt-tab")
}
}
if err := ash.WaitForCondition(ctx, tconn, func(w *ash.Window) bool {
return w.ID == ytWinID && !w.IsActive && !w.IsAnimating
}, &testing.PollOptions{Timeout: 5 * time.Second}); err != nil {
return errors.Wrap(err, "failed to wait youtube window deactivate")
}
s.Log("Switch back to fullscreen video")
if tabletMode {
if err := ash.DragToShowOverview(ctx, tsw, stw, tconn); err != nil {
return errors.Wrap(err, "failed to DragToShowOverview")
}
ytWin, err := ash.GetWindow(ctx, tconn, ytWinID)
if err != nil {
return errors.Wrap(err, "failed to get youtube window")
}
if err := pc.ClickAt(ytWin.OverviewInfo.Bounds.CenterPoint())(ctx); err != nil {
return errors.Wrap(err, "failed to select youtube window")
}
if err := enterFullscreen(); err != nil {
return errors.Wrap(err, "failed to make video fullscreen")
}
} else {
if err := altTab(); err != nil {
return errors.Wrap(err, "failed to alt-tab")
}
}
if err := ash.WaitForCondition(ctx, tconn, func(w *ash.Window) bool {
return w.ID == ytWinID && w.IsActive && w.State == ash.WindowStateFullscreen
}, &testing.PollOptions{Timeout: 5 * time.Second}); err != nil {
return errors.Wrap(err, "failed to wait active fullscreen youtube window")
}
return nil
}, 30*time.Minute); err != nil {
s.Fatal("Failed: ", err)
}
// Get video smoothness.
s.Log("Get video smoothness")
var vs float64
if err := ytConn.Eval(ctx,
`(function() {
var q = document.querySelector("video").getVideoPlaybackQuality();
var d = q.droppedVideoFrames * 100 / q.totalVideoFrames;
return Math.round(100 - d);
})()`, &vs); err != nil {
s.Fatal("Failed to get video smoothness: ", err)
}
s.Log("Video smoothness: ", vs)
metricSuffix := "clamshell"
if tabletMode {
metricSuffix = "tablet"
}
// Before recording the metrics, check if there is any tab crashed.
if err := tabChecker.Check(ctx); err != nil {
s.Fatal("Tab renderer crashed: ", err)
}
pv := perf.NewValues()
pv.Set(perf.Metric{
Name: "VideoCUJ.VideoSmoothness." + metricSuffix,
Unit: "percent",
Direction: perf.BiggerIsBetter,
}, vs)
if err = recorder.Record(ctx, pv); err != nil {
s.Fatal("Failed to report: ", err)
}
if err := pv.Save(s.OutDir()); err != nil {
s.Error("Failed saving perf data: ", err)
}
}