-
Notifications
You must be signed in to change notification settings - Fork 105
/
MediaControls-test.js
1009 lines (832 loc) · 39.7 KB
/
MediaControls-test.js
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
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* eslint-disable no-unused-expressions */
import MediaControls from '../MediaControls';
import fullscreen from '../../../Fullscreen';
import Settings from '../Settings';
import Scrubber from '../Scrubber';
import * as util from '../../../util';
let mediaControls;
let stubs;
let clock;
const PLAYING_CLASS = 'bp-media-is-playing';
const CRAWLER =
'<div class="bp-media-crawler-wrapper"><div class="bp-crawler"><div></div><div></div><div></div></div></div>';
const sandbox = sinon.sandbox.create();
describe('lib/viewers/media/MediaControls', () => {
before(() => {
fixture.setBase('src/lib');
});
beforeEach(() => {
stubs = {};
fixture.load('viewers/media/__tests__/MediaControls-test.html');
});
afterEach(() => {
fixture.cleanup();
sandbox.verifyAndRestore();
stubs = null;
if (typeof mediaControls.destroy() === 'function') {
mediaControls.destroy();
}
mediaControls = null;
});
describe('constructor()', () => {
beforeEach(() => {
stubs.insertTemplate = sandbox.stub(util, 'insertTemplate');
mediaControls = new MediaControls(
document.getElementById('test-controls-container'),
{ duration: 1210 },
{
set: () => {},
has: () => {},
get: () => {},
unset: () => {}
}
);
});
it('should insert the DOM template', () => {
expect(stubs.insertTemplate).to.be.called;
});
it('should set the duration', () => {
expect(mediaControls.durationEl.textContent).to.equal('20:10');
});
it('should set labels on the appropriate elements', () => {
expect(mediaControls.playButtonEl.getAttribute('title')).to.equal(__('media_play'));
expect(mediaControls.playButtonEl.getAttribute('aria-label')).to.equal(__('media_play'));
expect(mediaControls.volButtonEl.getAttribute('title')).to.equal(__('media_mute'));
expect(mediaControls.volButtonEl.getAttribute('aria-label')).to.equal(__('media_mute'));
expect(mediaControls.fullscreenButtonEl.getAttribute('title')).to.equal(__('enter_fullscreen'));
expect(mediaControls.fullscreenButtonEl.getAttribute('aria-label')).to.equal(__('enter_fullscreen'));
expect(mediaControls.settingsButtonEl.getAttribute('title')).to.equal(__('media_settings'));
expect(mediaControls.settingsButtonEl.getAttribute('aria-label')).to.equal(__('media_settings'));
expect(mediaControls.subtitlesButtonEl.getAttribute('title')).to.equal(__('media_subtitles_cc'));
expect(mediaControls.subtitlesButtonEl.getAttribute('aria-label')).to.equal(__('media_subtitles_cc'));
expect(mediaControls.timeScrubberEl.getAttribute('aria-valuenow')).to.equal('0');
expect(mediaControls.timeScrubberEl.getAttribute('aria-valuetext')).to.equal('0:00 of 20:10');
expect(mediaControls.volScrubberEl.getAttribute('aria-valuenow')).to.equal('100');
expect(mediaControls.volScrubberEl.getAttribute('aria-valuetext')).to.equal('100% Volume');
});
});
beforeEach(() => {
mediaControls = new MediaControls(
document.getElementById('test-controls-container'),
{ duration: 10 },
{
set: () => {},
has: () => {},
get: () => {},
unset: () => {}
}
);
});
describe('destroy()', () => {
beforeEach(() => {
stubs.removeAllListeners = sandbox.stub(mediaControls, 'removeAllListeners');
stubs.removeVolumeScrubberWrapperExpansionHandlers = sandbox.stub(
mediaControls,
'removeVolumeScrubberWrapperExpansionHandlers'
);
stubs.removeEventListener = sandbox.stub(document, 'removeEventListener');
stubs.removeActivationListener = sandbox.stub(util, 'removeActivationListener');
stubs.genericEl = {
getHandleEl: sandbox.stub().returns({
removeEventListener: sandbox.stub()
}),
getConvertedEl: sandbox.stub().returns({
removeEventListener: sandbox.stub()
}),
destroy: sandbox.stub(),
removeListener: sandbox.stub(),
removeEventListener: sandbox.stub()
};
});
it('should remove all listeners', () => {
mediaControls.destroy();
expect(stubs.removeAllListeners).to.be.called;
expect(stubs.removeVolumeScrubberWrapperExpansionHandlers).to.be.called;
expect(stubs.removeEventListener).to.be.calledWith('mouseup', mediaControls.timeScrubbingStopHandler);
expect(stubs.removeEventListener).to.be.calledWith('mousemove', mediaControls.filmstripShowHandler);
});
it('should remove time scrubber event listeners if it exists', () => {
mediaControls.timeScrubber = false;
mediaControls.destroy();
expect(mediaControls.timeScrubber).to.be.false;
mediaControls.timeScrubber = stubs.genericEl;
mediaControls.destroy();
expect(stubs.genericEl.getHandleEl().removeEventListener).to.be.calledWith(
'mousedown',
mediaControls.timeScrubbingStartHandler
);
expect(stubs.genericEl.getConvertedEl().removeEventListener).to.be.calledWith(
'mousemove',
mediaControls.filmstripShowHandler
);
expect(stubs.genericEl.getConvertedEl().removeEventListener).to.be.calledWith(
'mouseleave',
mediaControls.filmstripHideHandler
);
expect(stubs.genericEl.destroy).to.be.called;
expect(mediaControls.timeScrubber).to.equal(undefined);
});
it('should destroy the volume scrubber', () => {
mediaControls.volScrubber = stubs.genericEl;
mediaControls.destroy();
expect(stubs.genericEl.destroy).to.be.called;
expect(mediaControls.volScrubber).to.equal(undefined);
});
it('should remove listeners and destroy the settings object', () => {
mediaControls.settings = stubs.genericEl;
mediaControls.destroy();
expect(stubs.genericEl.removeListener).to.be.calledWith('quality', mediaControls.handleQuality);
expect(stubs.genericEl.removeListener).to.be.calledWith('speed', mediaControls.handleRate);
expect(stubs.genericEl.destroy);
expect(mediaControls.settings).to.equal(undefined);
});
it('should remove event listeners from the fullscreen, play, volume, scrubber, and settings button elements', () => {
mediaControls.fullscreenButtonEl = stubs.genericEl;
mediaControls.settingsButtonEl = stubs.genericEl;
mediaControls.volButtonEl = stubs.genericEl;
mediaControls.playButtonEl = stubs.genericEl;
mediaControls.subtitlesButtonEl = stubs.genericEl;
mediaControls.wrapperEl = stubs.genericEl;
mediaControls.destroy();
expect(stubs.removeActivationListener).to.be.calledWith(stubs.genericEl, mediaControls.togglePlayHandler);
expect(stubs.removeActivationListener).to.be.calledWith(stubs.genericEl, mediaControls.toggleMuteHandler);
expect(stubs.removeActivationListener).to.be.calledWith(
stubs.genericEl,
mediaControls.toggleFullscreenHandler
);
expect(stubs.removeActivationListener).to.be.calledWith(
stubs.genericEl,
mediaControls.toggleSettingsHandler
);
expect(stubs.removeActivationListener).to.be.calledWith(
stubs.genericEl,
mediaControls.toggleSubtitlesHandler
);
});
});
describe('handleRate()', () => {
it('should emit the ratechange event', () => {
stubs.emit = sandbox.stub(mediaControls, 'emit');
mediaControls.handleRate();
expect(stubs.emit).to.be.calledWith('ratechange');
});
});
describe('handleQuality()', () => {
it('should emit the qualitychange event', () => {
stubs.emit = sandbox.stub(mediaControls, 'emit');
mediaControls.handleQuality();
expect(stubs.emit).to.be.calledWith('qualitychange');
});
});
describe('handleSubtitle()', () => {
it('should emit the subtitlechange event', () => {
stubs.emit = sandbox.stub(mediaControls, 'emit');
mediaControls.handleSubtitle();
expect(stubs.emit).to.be.calledWith('subtitlechange');
});
});
describe('setupSettings()', () => {
it('should create a settings obect and bind listeners', () => {
const settingsStub = sandbox.stub(Settings.prototype, 'addListener');
mediaControls.setupSettings();
expect(mediaControls.settings instanceof Settings);
expect(settingsStub).to.be.calledWith('quality', mediaControls.handleQuality);
expect(settingsStub).to.be.calledWith('speed', mediaControls.handleRate);
});
});
describe('setupScrubbers()', () => {
beforeEach(() => {
stubs.on = sandbox.stub(Scrubber.prototype, 'on');
});
it('should create a new scrubber and value change handler for time', () => {
mediaControls.setupScrubbers();
expect(mediaControls.timeScrubber instanceof Scrubber);
expect(stubs.on).to.be.calledWith('valuechange');
});
it('should create a new scrubber and value change handler for volume', () => {
mediaControls.setupScrubbers();
expect(mediaControls.volumeScrubber instanceof Scrubber);
expect(stubs.on).to.be.calledWith('valuechange');
});
});
describe('getTimeFromScrubber()', () => {
it('should compute the right time', () => {
mediaControls.mediaEl = {
duration: 100
};
mediaControls.setupScrubbers();
sandbox.stub(mediaControls.timeScrubber, 'getValue').returns(0.3);
const time = mediaControls.getTimeFromScrubber();
expect(time).to.equal(30);
});
});
describe('formatTime()', () => {
it('should correctly format 3 hours', () => {
const result = mediaControls.formatTime(10800);
expect(result).to.equal('3:00:00');
});
it('should correctly format the time', () => {
const result = mediaControls.formatTime(11211);
expect(result).to.equal('3:06:51');
});
it('should correctly format when double-digit minutes', () => {
const result = mediaControls.formatTime(705);
expect(result).to.equal('11:45');
});
it('should correctly format when single-digit minutes', () => {
const result = mediaControls.formatTime(105);
expect(result).to.equal('1:45');
});
it('should correctly format when 0 minutes', () => {
const result = mediaControls.formatTime(9);
expect(result).to.equal('0:09');
});
it('should correctly format 0 seconds', () => {
const result = mediaControls.formatTime(0);
expect(result).to.equal('0:00');
});
});
describe('setDuration()', () => {
beforeEach(() => {
mediaControls.durationEl = {
textContent: ''
};
stubs.formatTime = sandbox.stub(mediaControls, 'formatTime');
});
it('should set the text content of the duration element', () => {
mediaControls.setDuration(10800);
expect(stubs.formatTime).to.be.calledWith(10800);
});
it('should set the text content to 0 if there is no time', () => {
mediaControls.setDuration(undefined);
expect(stubs.formatTime).to.be.calledWith(0);
});
});
describe('setTimeCode()', () => {
beforeEach(() => {
mediaControls.mediaEl = {
textContent: '',
duration: 500
};
mediaControls.durationEl = {
textContent: '8:20'
};
mediaControls.setupScrubbers();
stubs.setValue = sandbox.stub(mediaControls.timeScrubber, 'setValue');
stubs.formatTime = sandbox.stub(mediaControls, 'formatTime').returns('4:10');
});
it('should set the value of the time scrubber and update the timecode El', () => {
mediaControls.setTimeCode(250);
expect(stubs.setValue).to.be.calledWith(0.5);
expect(stubs.formatTime).to.be.calledWith(250);
});
it('should set correct aria values', () => {
mediaControls.setTimeCode(250);
expect(mediaControls.timeScrubberEl.getAttribute('aria-valuenow')).to.equal('250');
expect(mediaControls.timeScrubberEl.getAttribute('aria-valuetext')).to.equal('4:10 of 8:20');
});
it('should set the value with 0 if no time is passed in', () => {
mediaControls.setTimeCode(undefined);
expect(stubs.setValue).to.be.calledWith(0);
expect(stubs.formatTime).to.be.calledWith(0);
});
});
describe('updateProgress()', () => {
it('should correctly set the buffered value of the time scrubber', () => {
mediaControls.mediaEl = {
buffered: {
length: 5,
end: sandbox.stub().returns(1)
},
duration: 1
};
mediaControls.setupScrubbers();
stubs.setBufferedValueStub = sandbox.stub(mediaControls.timeScrubber, 'setBufferedValue');
mediaControls.updateProgress();
expect(stubs.setBufferedValueStub).to.be.calledWith(1);
});
});
describe('toggleMute()', () => {
it('should emit a togglemute message', () => {
stubs.emit = sandbox.stub(mediaControls, 'emit');
mediaControls.toggleMute();
expect(stubs.emit).to.be.calledWith('togglemute');
});
});
describe('togglePlay()', () => {
it('should emit a toggleplayback message', () => {
stubs.emit = sandbox.stub(mediaControls, 'emit');
mediaControls.togglePlay();
expect(stubs.emit).to.be.calledWith('toggleplayback');
});
});
describe('toggleSubtitles()', () => {
it('should emit a togglesubtitles message', () => {
sandbox.stub(mediaControls.settings, 'toggleSubtitles');
stubs.emit = sandbox.stub(mediaControls, 'emit');
mediaControls.toggleSubtitles();
expect(stubs.emit).to.be.calledWith('togglesubtitles');
expect(mediaControls.settings.toggleSubtitles).to.be.called;
});
});
describe('toggleFullscreen()', () => {
beforeEach(() => {
stubs.emit = sandbox.stub(mediaControls, 'emit');
stubs.setFullscreenLabel = sandbox.stub(mediaControls, 'setFullscreenLabel');
});
it('should emit a togglefullscreen message', () => {
mediaControls.toggleFullscreen();
expect(stubs.emit).to.be.calledWith('togglefullscreen');
});
it('should set the fullscreen label', () => {
mediaControls.toggleFullscreen();
expect(stubs.setFullscreenLabel).to.be.called;
});
});
describe('setFullscreenLabel()', () => {
beforeEach(() => {
stubs.isFullscreen = sandbox.stub(fullscreen, 'isFullscreen');
stubs.setLabel = sandbox.stub(mediaControls, 'setLabel');
});
it('should set the label to exit fullscreen if in fullscreen', () => {
stubs.isFullscreen.returns(true);
mediaControls.setFullscreenLabel();
expect(stubs.setLabel).to.be.calledWith(mediaControls.fullscreenButtonEl, __('exit_fullscreen'));
});
it('should set the label to enter fullscreen if it\'s not fullscreen', () => {
stubs.isFullscreen.returns(false);
mediaControls.setFullscreenLabel();
expect(stubs.setLabel).to.be.calledWith(mediaControls.fullscreenButtonEl, __('enter_fullscreen'));
});
});
describe('toggleSettings()', () => {
beforeEach(() => {
stubs.show = sandbox.stub(mediaControls.settings, 'show');
stubs.hide = sandbox.stub(mediaControls.settings, 'hide');
stubs.isVisible = sandbox.stub(mediaControls, 'isSettingsVisible');
});
it('should hide the settings if they are visible', () => {
stubs.isVisible.returns(true);
mediaControls.toggleSettings();
expect(mediaControls.settings.hide).to.be.called;
});
it('should show the settings if they are hidden', () => {
stubs.isVisible.returns(false);
mediaControls.toggleSettings();
expect(mediaControls.settings.show).to.be.called;
});
});
describe('setLabel()', () => {
it('should set the aria label and the title of the given label', () => {
const el = document.createElement('button');
mediaControls.setLabel(el, 'test');
expect(el.getAttribute('title')).to.equal('test');
expect(el.getAttribute('aria-label')).to.equal('test');
});
});
describe('isSettingsVisible()', () => {
it('should return true if the settings exist and are visible', () => {
stubs.isVisible = sandbox.stub(mediaControls.settings, 'isVisible').returns(true);
const result = mediaControls.isSettingsVisible();
expect(stubs.isVisible).to.be.called;
expect(result).to.equal(true);
stubs.isVisible.returns(false);
const falseResult = mediaControls.isSettingsVisible();
expect(falseResult).to.equal(false);
});
});
describe('showPauseIcon()', () => {
it('should add the playing class to the wrapper el and update the label', () => {
stubs.setLabel = sandbox.stub(mediaControls, 'setLabel');
mediaControls.showPauseIcon();
expect(mediaControls.wrapperEl.classList.contains(PLAYING_CLASS)).to.be.true;
expect(stubs.setLabel).to.be.calledWith(mediaControls.playButtonEl, __('media_pause'));
});
});
describe('showPlayIcon()', () => {
it('should remove the playing class to the wrapper el and update the label', () => {
stubs.setLabel = sandbox.stub(mediaControls, 'setLabel');
mediaControls.showPlayIcon();
expect(mediaControls.wrapperEl.classList.contains(PLAYING_CLASS)).to.be.false;
expect(stubs.setLabel).to.be.calledWith(mediaControls.playButtonEl, __('media_play'));
});
});
describe('updateVolumeIcon()', () => {
beforeEach(() => {
mediaControls.setupScrubbers();
stubs.setValue = sandbox.stub(mediaControls.volScrubber, 'setValue');
stubs.setLabel = sandbox.stub(mediaControls, 'setLabel');
});
it('should remove all volume level classes and add the correct one', () => {
mediaControls.className = 'bp-media-volume-icon-is-low';
mediaControls.updateVolumeIcon(1);
expect(mediaControls.volButtonEl.classList.contains('bp-media-volume-icon-is-low')).to.be.false;
expect(mediaControls.volButtonEl.classList.contains('bp-media-volume-icon-is-high')).to.be.true;
});
it('set the new value of the volume scrubber', () => {
mediaControls.updateVolumeIcon(1);
expect(stubs.setValue).to.be.calledWith(1);
});
it('should set the correct volume button level', () => {
mediaControls.updateVolumeIcon(1);
expect(stubs.setLabel).to.be.calledWith(mediaControls.volButtonEl, __('media_mute'));
mediaControls.updateVolumeIcon(0);
expect(stubs.setLabel).to.be.calledWith(mediaControls.volButtonEl, __('media_unmute'));
});
it('set the correct aria values', () => {
mediaControls.updateVolumeIcon(0.31);
expect(mediaControls.volScrubberEl.getAttribute('aria-valuenow')).to.equal('31');
expect(mediaControls.volScrubberEl.getAttribute('aria-valuetext')).to.equal('31% Volume');
});
});
describe('attachEventHandlers()', () => {
beforeEach(() => {
stubs.wrapperAddEventListener = sandbox.stub(mediaControls.wrapperEl, 'addEventListener');
stubs.addActivationListener = sandbox.stub(util, 'addActivationListener');
stubs.addListener = sandbox.stub(fullscreen, 'addListener');
});
it('should add the correct event Liseners', () => {
mediaControls.attachEventHandlers();
expect(stubs.wrapperAddEventListener).to.be.calledWith('mouseenter', mediaControls.mouseenterHandler);
expect(stubs.wrapperAddEventListener).to.be.calledWith('mouseleave', mediaControls.mouseleaveHandler);
expect(stubs.addActivationListener).to.be.calledWith(
mediaControls.playButtonEl,
mediaControls.togglePlayHandler
);
expect(stubs.addActivationListener).to.be.calledWith(
mediaControls.volButtonEl,
mediaControls.toggleMuteHandler
);
expect(stubs.addActivationListener).to.be.calledWith(
mediaControls.fullscreenButtonEl,
mediaControls.toggleFullscreenHandler
);
expect(stubs.addActivationListener).to.be.calledWith(
mediaControls.settingsButtonEl,
mediaControls.toggleSettingsHandler
);
expect(stubs.addActivationListener).to.be.calledWith(
mediaControls.subtitlesButtonEl,
mediaControls.toggleSubtitlesHandler
);
expect(stubs.addListener).to.be.called;
});
});
describe('mouseenterHandler()', () => {
it('should set preventHiding to true and show the controls', () => {
stubs.show = sandbox.stub(mediaControls, 'show');
mediaControls.mouseenterHandler();
expect(mediaControls.preventHiding).to.equal(true);
expect(stubs.show).to.be.called;
});
});
describe('mouseleaveHandler()', () => {
it('should allow hiding via setHiding and show the controls', () => {
stubs.show = sandbox.stub(mediaControls, 'show');
mediaControls.mouseleaveHandler();
expect(mediaControls.preventHiding).to.equal(false);
expect(stubs.show).to.be.called;
});
});
describe('show()', () => {
beforeEach(() => {
stubs.showControlClass = 'bp-media-controls-is-visible';
stubs.timeout = 2001;
stubs.hide = sandbox.stub(mediaControls, 'hide');
clock = sinon.useFakeTimers();
});
afterEach(() => {
clock.restore();
});
it('should add the controls class to the wrapper element', () => {
mediaControls.show();
expect(mediaControls.wrapperEl.parentNode.classList.contains(stubs.showControlClass)).to.be.true;
});
it('should add the auto hide timeout', () => {
mediaControls.show();
expect(mediaControls.autoHideTimeout).to.not.equal(undefined);
});
it('should hide the controls after a timeout', () => {
mediaControls.show();
clock.tick(stubs.timeout);
expect(stubs.hide).to.be.called;
});
});
describe('hide()', () => {
beforeEach(() => {
stubs.isSettingsVisible = sandbox.stub(mediaControls, 'isSettingsVisible');
stubs.filmstripHideHandler = sandbox.stub(mediaControls, 'filmstripHideHandler');
stubs.show = sandbox.stub(mediaControls, 'show');
});
it('should should call show and do nothing else if the prevent hiding is true', () => {
mediaControls.preventHiding = true;
mediaControls.hide();
expect(stubs.show).to.be.called;
});
it('should should call show and do nothing else if the settings are visible', () => {
mediaControls.preventHiding = false;
stubs.isSettingsVisible.returns(true);
mediaControls.hide();
expect(stubs.show).to.be.called;
});
it('should remove the show controls class and hide the filmstrip if the wrapper element and parent exist', () => {
mediaControls.preventHiding = false;
stubs.isSettingsVisible.returns(false);
mediaControls.hide();
expect(stubs.show).to.not.be.called;
expect(mediaControls.wrapperEl.parentNode.classList.contains('bp-media-controls-is-visible')).to.be.false;
expect(stubs.filmstripHideHandler).to.be.called;
});
});
describe('toggle()', () => {
beforeEach(() => {
stubs.isVisible = sandbox.stub(mediaControls, 'isVisible');
stubs.hide = sandbox.stub(mediaControls, 'hide');
stubs.show = sandbox.stub(mediaControls, 'show');
});
it('should hide the settings and remove preventHiding if the controls are visible', () => {
stubs.isVisible.returns(true);
mediaControls.toggle();
expect(stubs.hide).to.be.called;
expect(mediaControls.preventHiding).to.be.false;
});
it('should show the controls if they are not visible', () => {
stubs.isVisible.returns(false);
mediaControls.toggle();
expect(stubs.show).to.be.called;
});
});
describe('isVisible()', () => {
beforeEach(() => {
stubs.wrapperParent = document.createElement('div')
mediaControls.wrapperEl = stubs.wrapperParent.appendChild(document.createElement('div'));
});
it('should return false if the controls show class is missing', () => {
const result = mediaControls.isVisible();
expect(result).to.be.false;
});
it('should return true if the controls show class is present', () => {
stubs.wrapperParent.classList.add('bp-media-controls-is-visible');
const result = mediaControls.isVisible();
expect(result).to.be.true;
});
});
describe('resizeTimeScrubber()', () => {
it('should resize the time scrubber', () => {
mediaControls.setupScrubbers();
stubs.resize = sandbox.stub(mediaControls.timeScrubber, 'resize');
mediaControls.resizeTimeScrubber();
expect(stubs.resize).to.be.called;
});
});
describe('setFilmstrip()', () => {
it('should set the filmstrip source to the provided URL', () => {
mediaControls.filmstripEl = {
src: ''
};
mediaControls.filmstripUrl = 'testurl';
mediaControls.setFilmstrip();
expect(mediaControls.filmstripEl.src).to.equal('testurl');
});
});
describe('initFilmstrip()', () => {
beforeEach(() => {
stubs.status = {
getPromise: sandbox.stub().returns(Promise.resolve())
};
mediaControls.setupScrubbers();
stubs.handleElAddEventListener = sandbox.stub(mediaControls.timeScrubber.getHandleEl(), 'addEventListener');
stubs.getConvertedElAddEventListener = sandbox.stub(
mediaControls.timeScrubber.getConvertedEl(),
'addEventListener'
);
mediaControls.timeScrubberEl = {
addEventListener: sandbox.stub()
}
stubs.setFilmstrip = sandbox.stub(mediaControls, 'setFilmstrip');
});
it('should give the correct class and content to the filmstrip container', () => {
mediaControls.initFilmstrip('url', stubs.status, '380', 1);
expect(mediaControls.filmstripContainerEl.classList.contains('bp-media-filmstrip-container')).to.be.true;
expect(mediaControls.filmstripContainerEl.innerHTML.includes(CRAWLER)).to.be.true;
});
it('should give the correct class to the filmstrip', () => {
mediaControls.initFilmstrip('url', stubs.status, '380', 1);
expect(mediaControls.filmstripEl.classList.contains('bp-media-filmstrip')).to.be.true;
});
it('should give the correct class to the filmstrip time element', () => {
mediaControls.initFilmstrip('url', stubs.status, '380', 1);
expect(mediaControls.filmstripTimeEl.classList.contains('bp-media-filmstrip-timecode')).to.be.true;
});
it('should add the correct eventListeners to the handle and converted time scrubber elements', () => {
mediaControls.initFilmstrip('url', stubs.status, '380', 1);
expect(stubs.handleElAddEventListener).to.be.calledWith(
'mousedown',
mediaControls.timeScrubbingStartHandler
);
expect(stubs.getConvertedElAddEventListener).to.be.calledWith(
'mousemove',
mediaControls.filmstripShowHandler
);
expect(stubs.getConvertedElAddEventListener).to.be.calledWith(
'mouseleave',
mediaControls.filmstripHideHandler
);
});
it('should add the touchstart eventListener if touch is detected', () => {
mediaControls.hasTouch = true;
mediaControls.initFilmstrip('url', stubs.status, '380', 1);
expect(mediaControls.timeScrubberEl.addEventListener).to.be.calledWith('touchstart', mediaControls.timeScrubbingStartHandler)
});
it('should add the onload function to the filmstrip', () => {
mediaControls.initFilmstrip('url', stubs.status, '380', 1);
expect(typeof mediaControls.filmstripEl.onload === 'function');
});
});
describe('timeScrubbingStartHandler()', () => {
it('should set isScrubbing to true, preventHiding to true, and add show and stop handlers', () => {
stubs.addEventListener = sandbox.stub(document, 'addEventListener');
mediaControls.timeScrubbingStartHandler();
expect(mediaControls.isScrubbing).to.equal(true);
expect(mediaControls.preventHiding).to.equal(true);
expect(stubs.addEventListener).to.be.calledWith('mouseup', mediaControls.timeScrubbingStopHandler);
expect(stubs.addEventListener).to.be.calledWith('mousemove', mediaControls.filmstripShowHandler);
});
it('should add show and stop touch events if touch is present', () => {
mediaControls.hasTouch = true;
stubs.addEventListener = sandbox.stub(document, 'addEventListener');
mediaControls.timeScrubbingStartHandler();
expect(stubs.addEventListener).to.be.calledWith('touchend', mediaControls.timeScrubbingStopHandler);
expect(stubs.addEventListener).to.be.calledWith('touchmove', mediaControls.show);
});
});
describe('timeScrubbingStopHandler()', () => {
beforeEach(() => {
mediaControls.setupScrubbers();
stubs.event = {
target: mediaControls.timeScrubberEl.firstChild
};
stubs.removeEventListener = sandbox.stub(document, 'removeEventListener');
mediaControls.filmstripContainerEl = document.createElement('div');
});
it('should set isScrubbing to false, preventHiding to false, and remove show and stop handlers', () => {
mediaControls.timeScrubbingStopHandler(stubs.event);
expect(mediaControls.isScrubbing).to.equal(false);
expect(mediaControls.preventHiding).to.equal(false);
expect(stubs.removeEventListener).to.be.calledWith('mouseup', mediaControls.timeScrubbingStopHandler);
expect(stubs.removeEventListener).to.be.calledWith('mousemove', mediaControls.filmstripShowHandler);
});
it('should remove touch show and stop handlers if touch is present', () => {
mediaControls.hasTouch = true;
mediaControls.timeScrubbingStopHandler(stubs.event);
expect(stubs.removeEventListener).to.be.calledWith('touchend', mediaControls.timeScrubbingStopHandler);
expect(stubs.removeEventListener).to.be.calledWith('touchmove', mediaControls.show);
});
it('should hide the filmstrip if it is not being hovered over', () => {
stubs.event.target = document.createElement('div');
mediaControls.timeScrubbingStopHandler(stubs.event);
expect(mediaControls.filmstripContainerEl.style.display).equal('none');
});
});
describe('filmstripShowHandler()', () => {
beforeEach(() => {
stubs.getBoundingClientRect = sandbox.stub(mediaControls.containerEl, 'getBoundingClientRect').returns({
left: 0,
width: 260
});
stubs.event = {
pageX: 100
};
stubs.isSettingsVisible = sandbox.stub(mediaControls, 'isSettingsVisible');
stubs.formatTime = sandbox.stub(mediaControls, 'formatTime');
mediaControls.mediaEl = {
duration: 3600
};
stubs.status = {
getPromise: sandbox.stub().returns(Promise.resolve())
};
mediaControls.initFilmstrip('url', stubs.status, '380', 1);
sandbox.stub(mediaControls, 'computeFilmstripPositions').returns({
time: 10,
left: -100,
top: -180,
containerLeft: 20
});
});
it('should do nothing if the settings are visible', () => {
stubs.isSettingsVisible.returns(true);
mediaControls.filmstripShowHandler(stubs.event);
expect(stubs.getBoundingClientRect).to.not.be.called;
});
it('should correctly style the filmstrip and format the time element', () => {
stubs.isSettingsVisible.returns(false);
mediaControls.filmstripShowHandler(stubs.event);
expect(mediaControls.filmstripEl.style.left).to.equal('-100px');
expect(mediaControls.filmstripEl.style.top).to.equal('-180px');
expect(mediaControls.filmstripContainerEl.style.display).to.equal('block');
expect(mediaControls.filmstripContainerEl.style.left).to.equal('20px');
expect(stubs.formatTime).to.be.calledWith(10);
});
});
describe('computeFilmstripPositions()', () => {
it('should compute correct positions when filmstrip not ready', () => {
mediaControls.mediaEl = {
duration: 100
};
mediaControls.filmstripInterval = 1;
sandbox.stub(mediaControls.timeScrubber, 'computeScrubberPosition').returns(0.25);
const positions = mediaControls.computeFilmstripPositions(400, 200, 1000, null);
expect(positions.time).to.equal(25);
expect(positions.left).to.equal(0);
expect(positions.top).to.equal(0);
expect(positions.containerLeft).to.equal(120);
});
it('should compute correct horizontal offset into filmstrip', () => {
mediaControls.mediaEl = {
duration: 100
};
mediaControls.filmstripInterval = 1;
sandbox.stub(mediaControls.timeScrubber, 'computeScrubberPosition').returns(0.2);
const positions = mediaControls.computeFilmstripPositions(400, 200, 1000, 16000);
expect(positions.time).to.equal(20);
expect(positions.left).to.equal(-3200);
expect(positions.top).to.equal(0);
expect(positions.containerLeft).to.equal(120);
});
it('should compute correct vertical offset into filmstrip', () => {
mediaControls.mediaEl = {
duration: 1100
};
mediaControls.filmstripInterval = 1;
sandbox.stub(mediaControls.timeScrubber, 'computeScrubberPosition').returns(0.2);
const positions = mediaControls.computeFilmstripPositions(400, 200, 1000, 16000);
expect(positions.time).to.equal(220);
expect(positions.left).to.equal(-3200);
expect(positions.top).to.equal(-180);
expect(positions.containerLeft).to.equal(120);
});
it('should compute correct offset into filmstrip with different interval', () => {
mediaControls.mediaEl = {
duration: 2000
};
mediaControls.filmstripInterval = 5;
sandbox.stub(mediaControls.timeScrubber, 'computeScrubberPosition').returns(0.6);
const positions = mediaControls.computeFilmstripPositions(800, 200, 1000, 16000);
expect(positions.time).to.equal(1200);
expect(positions.left).to.equal(-6400);
expect(positions.top).to.equal(-180);
expect(positions.containerLeft).to.equal(520);
});
it('should compute correct container position when hovering near left boundary', () => {
mediaControls.mediaEl = {
duration: 100
};
mediaControls.filmstripInterval = 1;
sandbox.stub(mediaControls.timeScrubber, 'computeScrubberPosition').returns(0.01);
const positions = mediaControls.computeFilmstripPositions(210, 200, 1000, 16000);
expect(positions.time).to.equal(1);
expect(positions.left).to.equal(-160);
expect(positions.top).to.equal(0);
expect(positions.containerLeft).to.equal(0);
});
it('should compute correct container position when hovering near right boundary', () => {
mediaControls.mediaEl = {
duration: 100
};
mediaControls.filmstripInterval = 1;
sandbox.stub(mediaControls.timeScrubber, 'computeScrubberPosition').returns(0.99);
const positions = mediaControls.computeFilmstripPositions(1190, 200, 1000, 16000);
expect(positions.time).to.equal(99);
expect(positions.left).to.equal(-15840);
expect(positions.top).to.equal(0);
expect(positions.containerLeft).to.equal(840);
});
});
describe('filmstripHideHandler()', () => {
beforeEach(() => {
stubs.status = {
getPromise: sandbox.stub().returns(Promise.resolve())
};
mediaControls.initFilmstrip('url', stubs.status, '380', 1);
mediaControls.isScrubbing = false;
});
it('should hide the filmstrip container if not scrubbing', () => {
mediaControls.filmstripHideHandler();
expect(mediaControls.filmstripContainerEl.style.display).to.equal('none');
});
it('should do nothing if scrubbing', () => {
mediaControls.isScrubbing = true;
mediaControls.filmstripHideHandler();
expect(mediaControls.filmstripContainerEl.style.display).to.equal('');
});
});
describe('initSubtitles()', () => {