-
Notifications
You must be signed in to change notification settings - Fork 4.1k
Expand file tree
/
Copy pathtest-video-players-helper.js
More file actions
623 lines (564 loc) · 19.7 KB
/
test-video-players-helper.js
File metadata and controls
623 lines (564 loc) · 19.7 KB
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
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
import {removeElement} from '#core/dom';
import {detectIsAutoplaySupported} from '#core/dom/video';
import {toggleExperiment} from '#experiments';
import {Services} from '#service';
import {getData, listenOncePromise} from '#utils/event-helper';
import {
createFixtureIframe,
expectBodyToBecomeVisible,
poll,
} from '#testing/iframe';
import {
VideoAnalyticsEvents_Enum,
VideoEvents_Enum,
VideoInterface,
} from '../../src/video-interface';
function skipIfAutoplayUnsupported(win) {
return detectIsAutoplaySupported(win).then((isSupported) => {
if (!isSupported) {
this.skipTest();
}
});
}
export function runVideoPlayerIntegrationTests(
env,
createVideoElementFunc,
opt_experiment,
timeout = 2000
) {
/**
* @const {number} Height of the fixture iframe
*/
const FRAME_HEIGHT = 1000;
let fixtureGlobal;
let videoGlobal;
function createButton(r, action) {
const button = r.fixture.doc.createElement('button');
button.setAttribute('on', 'tap:myVideo.' + action);
r.fixture.doc.body.appendChild(button);
return button;
}
describe
.configure()
.skipIfPropertiesObfuscated()
.ifChrome()
.run('Video Interface', function () {
this.timeout(timeout);
it.skip('should override the video interface methods', function () {
this.timeout(timeout);
return getVideoPlayer({outsideView: false, autoplay: true})
.then((r) => {
return r.video.getImpl(false);
})
.then((impl) => {
const methods = Object.getOwnPropertyNames(
Object.getPrototypeOf(new VideoInterface())
);
expect(methods.length).to.be.above(1);
for (let i = 0; i < methods.length; i++) {
const methodName = methods[i];
expect(impl[methodName]).to.exist;
}
});
});
afterEach(cleanUp);
});
describe
.configure()
.ifChrome()
.run('Actions', function () {
this.timeout(timeout);
// TODO(cvializ, #14827): Fails on Chrome 66.
it.skip('should support mute, play, pause, unmute actions', function () {
return getVideoPlayer({outsideView: false, autoplay: false}).then(
(r) => {
// Create a action buttons
const playButton = createButton(r, 'play');
const pauseButton = createButton(r, 'pause');
const muteButton = createButton(r, 'mute');
const unmuteButton = createButton(r, 'unmute');
return listenOncePromise(r.video, VideoEvents_Enum.LOAD)
.then(() => {
const promise = listenOncePromise(
r.video,
VideoEvents_Enum.PLAYING
);
playButton.click();
return promise;
})
.then(() => {
const promise = listenOncePromise(
r.video,
VideoEvents_Enum.MUTED
);
muteButton.click();
return promise;
})
.then(() => {
const promise = listenOncePromise(
r.video,
VideoEvents_Enum.PAUSE
);
pauseButton.click();
return promise;
})
.then(() => {
const promise = listenOncePromise(
r.video,
VideoEvents_Enum.UNMUTED
);
unmuteButton.click();
return promise;
});
}
);
});
// Although these tests are not about autoplay, we can ony run them in
// browsers that do support autoplay, this is because a synthetic click
// event will not be considered a user-action and mobile browsers that
// don't support muted autoplay will block it. In real life, the click
// would be considered a user-initiated action, but no way to do that in a
// scripted test environment.
before(function () {
this.timeout(timeout);
return skipIfAutoplayUnsupported.call(this, window);
});
afterEach(cleanUp);
});
describe
.configure()
.skipIfPropertiesObfuscated()
.ifChrome()
.run('Analytics Triggers', function () {
this.timeout(timeout);
let video;
// TODO(cvializ, #14827): Fails on Chrome 66.
it.skip('should trigger play analytics on play', function () {
let playButton;
return getVideoPlayer({
outsideView: true,
autoplay: false,
})
.then(async (r) => {
video = r.video;
playButton = createButton(r, 'play');
const impl = await video.getImpl(false);
const viewport = impl.getViewport();
const promise = listenOncePromise(video, VideoEvents_Enum.LOAD);
viewport.scrollIntoView(video);
return promise;
})
.then(() => {
const promise = listenOncePromise(
video,
VideoAnalyticsEvents_Enum.PLAY
);
playButton.click();
return promise;
});
});
it.skip('should trigger pause analytics when the video pauses', function () {
let pauseButton;
return getVideoPlayer({
outsideView: false,
autoplay: true,
})
.then((r) => {
video = r.video;
pauseButton = createButton(r, 'pause');
return listenOncePromise(video, VideoEvents_Enum.PLAYING);
})
.then(() => {
const promise = listenOncePromise(
video,
VideoAnalyticsEvents_Enum.PAUSE
);
pauseButton.click();
return promise;
});
});
it.skip('should trigger session analytics when a session ends', function () {
let pauseButton;
return getVideoPlayer({
outsideView: false,
autoplay: true,
})
.then((r) => {
video = r.video;
const promise = listenOncePromise(video, VideoEvents_Enum.PLAYING);
pauseButton = createButton(r, 'pause');
return promise;
})
.then(() => {
const promise = listenOncePromise(
video,
VideoAnalyticsEvents_Enum.PAUSE
);
pauseButton.click();
return promise;
});
});
// TODO (#16154): this test times out on master
it.skip('should trigger session analytics when a visible session ends', function () {
let viewport;
return getVideoPlayer({
outsideView: true,
autoplay: true,
})
.then(async (r) => {
video = r.video;
const impl = await video.getImpl(false);
viewport = impl.getViewport();
// scroll to the bottom, make video fully visible
viewport.scrollIntoView(video);
return listenOncePromise(video, VideoEvents_Enum.PLAYING);
})
.then(() => {
// scroll to the bottom, make video fully visible
viewport.setScrollTop(0);
return listenOncePromise(
video,
VideoAnalyticsEvents_Enum.SESSION_VISIBLE
);
});
});
describe.skip('should trigger ended analytics', () => {
let player;
before(function () {
return getVideoPlayer({
outsideView: false,
autoplay: true,
}).then((r) => {
// TODO(cvializ): Better way to detect which classes implement
// methods needed for tracking?
const {tagName} = r.video;
if (
tagName !== 'AMP-VIDEO' &&
tagName !== 'AMP-TEST-FAKE-VIDEOPLAYER'
) {
this.skipTest();
return;
}
player = r;
});
});
it('when the video ends', function () {
video = player.video;
return listenOncePromise(video, VideoAnalyticsEvents_Enum.ENDED);
});
});
// TODO(cvializ, #14827): Fails on Chrome 66.
it.skip('should include current time, play state, etc.', function () {
let playButton;
let pauseButton;
return getVideoPlayer({
outsideView: false,
autoplay: false,
})
.then((r) => {
video = r.video;
playButton = createButton(r, 'play');
pauseButton = createButton(r, 'pause');
return listenOncePromise(video, VideoEvents_Enum.LOAD);
})
.then(() => {
const promise = listenOncePromise(video, VideoEvents_Enum.PLAYING);
playButton.click();
return promise;
})
.then(() => {
pauseButton.click();
return listenOncePromise(video, VideoAnalyticsEvents_Enum.PAUSE);
})
.then((event) => {
const details = getData(event);
const playedRanges = JSON.parse(details.playedRangesJson);
expect(details.autoplay).to.be.a('boolean');
expect(details.currentTime).to.be.a('number');
expect(details.duration).to.be.a('number');
expect(details.height).to.be.a('number');
expect(details.id).to.be.a('string');
expect(details.playedTotal).to.be.a('number');
expect(playedRanges).to.be.an('array');
expect(details.state).to.be.a('string');
expect(details.width).to.be.a('number');
});
});
it.skip('should trigger video-seconds-played when visibleand playing', () => {
let video;
let timer;
let pauseButton;
let playButton;
return getVideoPlayer({
outsideView: true,
autoplay: true,
})
.then((r) => {
timer = Services.timerFor(r.video.ownerDocument.defaultView);
video = r.video;
pauseButton = createButton(r, 'pause');
playButton = createButton(r, 'play');
return Promise.race([
listenOncePromise(
video,
VideoAnalyticsEvents_Enum.SECONDS_PLAYED
).then(() => Promise.reject('Triggered video-seconds-played')),
timer.promise(2000),
]);
})
.then(async () => {
const impl = await video.getImpl(false);
const viewport = impl.getViewport();
viewport.scrollIntoView(video);
return listenOncePromise(
video,
VideoAnalyticsEvents_Enum.SECONDS_PLAYED
);
})
.then(() => {
pauseButton.click();
return listenOncePromise(video, VideoEvents_Enum.PAUSE);
})
.then(() => {
return Promise.race([
listenOncePromise(
video,
VideoAnalyticsEvents_Enum.SECONDS_PLAYED
).then(() => Promise.reject('Triggered video-seconds-played')),
timer.promise(2000),
]);
})
.then(() => {
playButton.click();
return listenOncePromise(video, VideoEvents_Enum.PLAYING);
})
.then(() => {
return listenOncePromise(
video,
VideoAnalyticsEvents_Enum.SECONDS_PLAYED
);
});
});
afterEach(cleanUp);
});
const t = describe.configure().ifChrome().skipIfPropertiesObfuscated();
t.run('Autoplay', function () {
this.timeout(timeout);
describe('play/pause', () => {
it.skip('should play when in view port initially', () => {
return getVideoPlayer({outsideView: false, autoplay: true}).then(
(r) => {
return listenOncePromise(r.video, VideoEvents_Enum.PLAYING);
}
);
});
it.skip('should not play when initially outside viewport', () => {
return getVideoPlayer({outsideView: true, autoplay: true}).then((r) => {
const timer = Services.timerFor(r.video.ownerDocument.defaultView);
const p = listenOncePromise(r.video, VideoEvents_Enum.PLAYING).then(
() => {
return Promise.reject('should not have autoplayed');
}
);
// we have to wait to ensure play is NOT called.
return Promise.race([timer.promise(1000), p]);
});
});
// TODO(aghassemi, #9379): Flaky on Safari 9.
// TODO(aghassemi): Flaky on Chrome 67 on Windows 7.
it.skip('should play/pause when video enters/exits viewport', function () {
let video;
let viewport;
return getVideoPlayer({outsideView: true, autoplay: true})
.then(async (r) => {
video = r.video;
const impl = await video.getImpl(false);
viewport = impl.getViewport();
// scroll to the bottom, make video fully visible
const p = listenOncePromise(video, VideoEvents_Enum.PLAYING);
viewport.scrollIntoView(video);
return p;
})
.then(() => {
// scroll back to top, make video not visible
const p = listenOncePromise(video, VideoEvents_Enum.PAUSE);
viewport.setScrollTop(0);
return p;
});
});
});
describe('Animated Icon', () => {
// TODO(amphtml): Unskip when #9379 is fixed.
it.skip('should create an animated icon overlay', () => {
let video;
let viewport;
let icon;
return getVideoPlayer({outsideView: true, autoplay: true})
.then((r) => {
video = r.video;
return poll('animation icon', () => {
return !!video.querySelector('i-amphtml-video-eq');
});
})
.then(async () => {
icon = video.querySelector('i-amphtml-video-eq');
expect(icon).to.exist;
// animation should be paused since video is not played yet
expect(isAnimationPaused(icon)).to.be.true;
const impl = await video.getImpl(false);
viewport = impl.getViewport();
// scroll to the bottom, make video fully visible so it autoplays
viewport.scrollIntoView(video);
return waitForAnimationPlay(icon).then(() => {
expect(isAnimationPaused(icon)).to.be.false;
});
});
function isAnimationPaused(iconElement) {
const animElement = iconElement.querySelector('.amp-video-eq-1-1');
const win = iconElement.ownerDocument.defaultView;
const cs = win.getComputedStyle(animElement);
const isPaused =
cs.getPropertyValue('animation-play-state') == 'paused' ||
cs.getPropertyValue('-webkit-animation-play-state') == 'paused' ||
cs.getPropertyValue('animation-name') == 'none' ||
cs.getPropertyValue('-webkit-animation-name') == 'none';
return isPaused;
}
function waitForAnimationPlay(iconElement) {
return poll(
'animation play',
() => {
return iconElement.classList.contains('amp-video-eq-play');
},
undefined,
timeout
);
}
});
});
before(function () {
this.timeout(timeout);
// Skip autoplay tests if browser does not support autoplay.
return skipIfAutoplayUnsupported.call(this, window);
});
afterEach(cleanUp);
});
describe
.configure()
.ifChrome()
.run('Rotate-to-fullscreen', function () {
this.timeout(timeout);
let video;
let playButton;
let autoFullscreen;
let isInLandscapeStub;
// TODO(alanorozco): Fails on Chrome 66.
it.skip('should enter fullscreen on rotation', function () {
function mockLandscape(isLandscape) {
if (!autoFullscreen) {
const manager = Services.videoManagerForDoc(video.getAmpDoc());
autoFullscreen = manager.getAutoFullscreenManagerForTesting_();
}
if (!isInLandscapeStub) {
isInLandscapeStub = env.sandbox.stub(
autoFullscreen,
'isInLandscape'
);
}
isInLandscapeStub.returns(isLandscape);
}
return getVideoPlayer({
autoplay: false,
outsideView: true,
'rotate-to-fullscreen': true,
})
.then(async (r) => {
video = r.video;
playButton = createButton(r, 'play');
mockLandscape(false);
const whenLoaded = listenOncePromise(video, VideoEvents_Enum.LOAD);
const impl = await video.getImpl(false);
const viewport = impl.getViewport();
viewport.scrollIntoView(video);
return whenLoaded;
})
.then(() => {
const whenPlaying = listenOncePromise(
video,
VideoEvents_Enum.PLAYING
);
playButton.click();
return whenPlaying;
})
.then(async () => {
const impl = await video.getImpl(false);
const enter = env.sandbox.stub(impl, 'fullscreenEnter');
mockLandscape(true);
autoFullscreen.onRotation_();
return poll('fullscreen enter', () => enter.called);
});
});
// Although these tests are not about autoplay, we can ony run them in
// browsers that do support autoplay, this is because a synthetic click
// event will not be considered a user-action and mobile browsers that
// don't support muted autoplay will block it. In real life, the click
// would be considered a user-initiated action, but no way to do that in a
// scripted test environment.
before(function () {
this.timeout(timeout);
// Skip autoplay tests if browser does not support autoplay.
return skipIfAutoplayUnsupported.call(this, window);
});
});
function getVideoPlayer(options) {
options = options || {};
const top = options.outsideView ? '126vh' : '0';
let fixture;
return createFixtureIframe('test/fixtures/video-players.html', FRAME_HEIGHT)
.then((f) => {
fixture = f;
if (opt_experiment) {
toggleExperiment(fixture.win, opt_experiment, true);
}
return expectBodyToBecomeVisible(fixture.win, timeout);
})
.then(() => {
const video = createVideoElementFunc(fixture);
const sizer = fixture.doc.createElement('div');
const whenVideoRegistered = video
.signals()
.whenSignal(VideoEvents_Enum.REGISTERED)
.then(() => ({video, fixture}));
['autoplay', 'dock', 'rotate-to-fullscreen'].forEach((option) => {
if (options[option]) {
video.setAttribute(option, '');
}
});
video.setAttribute('id', 'myVideo');
video.setAttribute('controls', '');
video.setAttribute('layout', 'fixed');
video.setAttribute('width', '300');
video.setAttribute('height', '300');
video.style.position = 'absolute';
video.style.top = top;
sizer.position = 'relative';
sizer.style.height = '200vh';
fixtureGlobal = fixture;
videoGlobal = video;
fixture.doc.body.appendChild(sizer);
fixture.doc.body.appendChild(video);
return whenVideoRegistered;
});
}
function cleanUp() {
try {
if (fixtureGlobal) {
if (opt_experiment) {
toggleExperiment(fixtureGlobal.win, opt_experiment, false);
}
removeElement(videoGlobal);
removeElement(fixtureGlobal.iframe);
}
} catch (e) {}
}
}