videojs-mobile-ui 0.8.0 → 0.9.0-beta.3

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
@@ -9,6 +9,8 @@ import plugin from '../src/plugin';
9
9
 
10
10
  const Player = videojs.getComponent('Player');
11
11
 
12
+ const merge = videojs.obj ? videojs.obj.merge : videojs.mergeOptions;
13
+
12
14
  QUnit.test('the environment is sane', function(assert) {
13
15
  assert.strictEqual(typeof Array.isArray, 'function', 'es5 exists');
14
16
  assert.strictEqual(typeof sinon, 'object', 'sinon exists');
@@ -16,28 +18,31 @@ QUnit.test('the environment is sane', function(assert) {
16
18
  assert.strictEqual(typeof plugin, 'function', 'plugin is a function');
17
19
  });
18
20
 
19
- QUnit.module('videojs-mobile-ui', {
21
+ const beforeEach = function() {
20
22
 
21
- beforeEach() {
23
+ // Mock the environment's timers because certain things - particularly
24
+ // player readiness - are asynchronous in video.js 5. This MUST come
25
+ // before any player is created; otherwise, timers could get created
26
+ // with the actual timer methods!
27
+ this.clock = sinon.useFakeTimers();
22
28
 
23
- // Mock the environment's timers because certain things - particularly
24
- // player readiness - are asynchronous in video.js 5. This MUST come
25
- // before any player is created; otherwise, timers could get created
26
- // with the actual timer methods!
27
- this.clock = sinon.useFakeTimers();
29
+ this.fixture = document.getElementById('qunit-fixture');
30
+ this.video = document.createElement('video');
31
+ this.fixture.appendChild(this.video);
28
32
 
29
- this.fixture = document.getElementById('qunit-fixture');
30
- this.video = document.createElement('video');
31
- this.fixture.appendChild(this.video);
32
- this.player = videojs(this.video);
33
- },
33
+ this.player = videojs(this.video);
34
+ };
34
35
 
35
- afterEach() {
36
- if (!this.player.isDisposed()) {
37
- this.player.dispose();
38
- }
39
- this.clock.restore();
36
+ const afterEach = function() {
37
+ if (!this.player.isDisposed()) {
38
+ this.player.dispose();
40
39
  }
40
+ this.clock.restore();
41
+ };
42
+
43
+ QUnit.module('videojs-mobile-ui', {
44
+ beforeEach,
45
+ afterEach
41
46
  });
42
47
 
43
48
  QUnit.test('registers itself with video.js', function(assert) {
@@ -49,6 +54,14 @@ QUnit.test('registers itself with video.js', function(assert) {
49
54
  );
50
55
  });
51
56
 
57
+ QUnit.test('initialises without errors', function(assert) {
58
+ this.player.mobileUi({ forceForTesting: true });
59
+
60
+ this.clock.tick(1);
61
+
62
+ assert.expect(0);
63
+ });
64
+
52
65
  QUnit.test('inserts element before control bar', function(assert) {
53
66
 
54
67
  this.player.mobileUi({forceForTesting: true});
@@ -84,7 +97,7 @@ QUnit.test('iOS event listeners', function(assert) {
84
97
 
85
98
  const oldBrowser = videojs.browser;
86
99
 
87
- videojs.browser = videojs.mergeOptions(videojs.browser, {
100
+ videojs.browser = merge(videojs.browser, {
88
101
  IS_IOS: true,
89
102
  IS_ANDROID: false
90
103
  });
@@ -124,7 +137,7 @@ QUnit[testOrSkip]('Android event listeners', function(assert) {
124
137
 
125
138
  const oldBrowser = videojs.browser;
126
139
 
127
- videojs.browser = videojs.mergeOptions(videojs.browser, {
140
+ videojs.browser = merge(videojs.browser, {
128
141
  IS_IOS: false,
129
142
  IS_ANDROID: true
130
143
  });
@@ -156,7 +169,7 @@ QUnit[testOrSkip]('Android event listeners skipped if disabled', function(assert
156
169
 
157
170
  const oldBrowser = videojs.browser;
158
171
 
159
- videojs.browser = videojs.mergeOptions(videojs.browser, {
172
+ videojs.browser = merge(videojs.browser, {
160
173
  IS_IOS: false,
161
174
  IS_ANDROID: true
162
175
  });
@@ -178,3 +191,98 @@ QUnit[testOrSkip]('Android event listeners skipped if disabled', function(assert
178
191
 
179
192
  videojs.browser = oldBrowser;
180
193
  });
194
+
195
+ QUnit.test('Adds disable-end class if disableOnEnd option is true', function(assert) {
196
+ this.player.mobileUi({
197
+ forceForTesting: true,
198
+ touchControls: { disableOnEnd: true }
199
+ });
200
+
201
+ this.clock.tick(1);
202
+
203
+ assert.ok(this.player.hasClass('vjs-mobile-ui-disable-end'), 'Class added via option');
204
+ });
205
+
206
+ QUnit.test('Adds disable-end class if endscreen plugin is present', function(assert) {
207
+ this.player.endscreen = () => {};
208
+
209
+ this.player.mobileUi({
210
+ forceForTesting: true,
211
+ touchControls: { disableOnEnd: false }
212
+ });
213
+
214
+ this.clock.tick(1);
215
+
216
+ assert.ok(this.player.hasClass('vjs-mobile-ui-disable-end'), 'Class added via endscreen detection');
217
+ });
218
+
219
+ QUnit.module('TouchOverlay', {
220
+ beforeEach,
221
+ afterEach
222
+ });
223
+
224
+ QUnit.test('TouchOverlay: double tap right seeks forward', function(assert) {
225
+ // Setup
226
+ this.player.mobileUi({ forceForTesting: true });
227
+ this.clock.tick(1);
228
+
229
+ const touchOverlay = this.player.getChild('TouchOverlay');
230
+ const touchEl = touchOverlay.el_;
231
+ let currentTimeCache = 0;
232
+
233
+ // Mock bounding rect so clicks have a defined "right" side
234
+ // Width is 100, so > 60 is right side
235
+ sinon.stub(touchEl, 'getBoundingClientRect').returns({
236
+ left: 0,
237
+ width: 100
238
+ });
239
+
240
+ this.player.currentTime = (time) => {
241
+ if (time === undefined) {
242
+ return currentTimeCache;
243
+ }
244
+ currentTimeCache = time;
245
+ return currentTimeCache;
246
+ };
247
+
248
+ this.player.duration(60);
249
+ this.player.currentTime(10);
250
+
251
+ // Trigger first tap
252
+ touchOverlay.handleTap({
253
+ target: touchEl,
254
+ preventDefault: () => {},
255
+ changedTouches: [{ clientX: 90 }]
256
+ });
257
+
258
+ // Trigger second tap (double tap)
259
+ touchOverlay.handleTap({
260
+ target: touchEl,
261
+ preventDefault: () => {},
262
+ changedTouches: [{ clientX: 90 }]
263
+ });
264
+
265
+ // Fast forward debounce timer (default tapTimeout is 300ms)
266
+ this.clock.tick(310);
267
+ assert.equal(this.player.currentTime(), 20, 'Seeked forward 10 seconds (default)');
268
+
269
+ // Advance enough for requestAnimationFrame to trigger
270
+ this.clock.tick(50);
271
+ assert.ok(touchOverlay.hasClass('skip'), 'Skip animation class added');
272
+ });
273
+
274
+ QUnit.test('TouchOverlay: single tap toggles play/pause visibility', function(assert) {
275
+ this.player.mobileUi({ forceForTesting: true });
276
+ this.clock.tick(1);
277
+
278
+ const touchOverlay = this.player.getChild('TouchOverlay');
279
+
280
+ // Trigger single tap
281
+ touchOverlay.handleTap({
282
+ target: touchOverlay.el_,
283
+ preventDefault: () => {},
284
+ changedTouches: [{ clientX: 50 }]
285
+ });
286
+
287
+ assert.ok(touchOverlay.hasClass('show-play-toggle'), 'Play toggle is visible after single tap');
288
+ });
@@ -0,0 +1,365 @@
1
+ import document from 'global/document';
2
+ import window from 'global/window';
3
+
4
+ import QUnit from 'qunit';
5
+ import sinon from 'sinon';
6
+ import videojs from 'video.js';
7
+
8
+ import plugin from '../src/plugin';
9
+
10
+ const createTouch = (el, opts) => {
11
+ if ('Touch' in window) {
12
+ opts.identifier = Date.now();
13
+ opts.target = el;
14
+
15
+ return new window.Touch(opts);
16
+ }
17
+
18
+ return opts;
19
+ };
20
+
21
+ const createTouchEvent = (el, evt, opts) => {
22
+ if ('TouchEvent' in window) {
23
+ opts.identifier = Date.now();
24
+ opts.target = el;
25
+
26
+ opts.touches = opts.touches.map((t) => {
27
+ return createTouch(el, t);
28
+ });
29
+ opts.changedTouches = opts.changedTouches.map((t) => {
30
+ return createTouch(el, t);
31
+ });
32
+
33
+ return new window.TouchEvent(evt, opts);
34
+ }
35
+
36
+ opts.type = evt;
37
+
38
+ return opts;
39
+ };
40
+
41
+ const skipWithoutTouch = 'TouchEvent' in window ? 'test' : 'skip';
42
+
43
+ QUnit.test('the environment is sane', function(assert) {
44
+ assert.strictEqual(typeof Array.isArray, 'function', 'es5 exists');
45
+ assert.strictEqual(typeof sinon, 'object', 'sinon exists');
46
+ assert.strictEqual(typeof videojs, 'function', 'videojs exists');
47
+ assert.strictEqual(typeof plugin, 'function', 'plugin is a function');
48
+ });
49
+
50
+ QUnit.module('swipeFullscreen', {
51
+
52
+ beforeEach() {
53
+ this.clock = sinon.useFakeTimers();
54
+ this.fixture = document.getElementById('qunit-fixture');
55
+ this.video = document.createElement('video');
56
+ this.fixture.appendChild(this.video);
57
+ this.player = videojs(this.video);
58
+ },
59
+
60
+ afterEach() {
61
+ if (!this.player.isDisposed()) {
62
+ this.player.dispose();
63
+ }
64
+ this.clock.restore();
65
+ }
66
+ });
67
+
68
+ QUnit.test('swipeToFullscreen: adds CSS class when enabled', function(assert) {
69
+ this.player.mobileUi({
70
+ fullscreen: {
71
+ swipeToFullscreen: true,
72
+ swipeFromFullscreen: false,
73
+ swipeThreshold: 50
74
+ },
75
+ forceForTesting: true
76
+ });
77
+
78
+ this.clock.tick(1);
79
+
80
+ assert.true(this.player.hasClass('using-fs-swipe-up'), 'adds using-fs-swipe-up class');
81
+ assert.false(this.player.hasClass('using-fs-swipe-down'), 'does not add using-fs-swipe-down class');
82
+ });
83
+
84
+ QUnit.test('swipeFromFullscreen: adds CSS class when enabled', function(assert) {
85
+ this.player.mobileUi({
86
+ fullscreen: {
87
+ swipeToFullscreen: false,
88
+ swipeFromFullscreen: true,
89
+ swipeThreshold: 50
90
+ },
91
+ forceForTesting: true
92
+ });
93
+
94
+ this.clock.tick(1);
95
+
96
+ assert.true(this.player.hasClass('using-fs-swipe-down'), 'adds using-fs-swipe-down class');
97
+ assert.false(this.player.hasClass('using-fs-swipe-up'), 'does not add using-fs-swipe-up class');
98
+ });
99
+
100
+ QUnit[skipWithoutTouch]('touch start outside fullscreen', function(assert) {
101
+ this.player.mobileUi({
102
+ fullscreen: {
103
+ swipeToFullscreen: true,
104
+ swipeFromFullscreen: true,
105
+ swipeThreshold: 50
106
+ },
107
+ forceForTesting: true
108
+ });
109
+
110
+ this.clock.tick(1);
111
+
112
+ const touchEvent = createTouchEvent(this.player.el(), 'touchstart', {
113
+ bubbles: true,
114
+ cancelable: true,
115
+ touches: [],
116
+ changedTouches: [{ clientY: 100 }]
117
+ });
118
+
119
+ this.player.el().dispatchEvent(touchEvent);
120
+
121
+ // Test that the event listener is properly set up
122
+ assert.expect(0);
123
+ });
124
+
125
+ QUnit[skipWithoutTouch]('prevents swiping when feature disabled', function(assert) {
126
+ this.player.mobileUi({
127
+ fullscreen: {
128
+ swipeToFullscreen: false,
129
+ swipeFromFullscreen: false,
130
+ swipeThreshold: 50
131
+ },
132
+ forceForTesting: true
133
+ });
134
+
135
+ this.clock.tick(1);
136
+
137
+ sinon.stub(this.player, 'requestFullscreen');
138
+
139
+ const touchStart = createTouchEvent(this.player.el(), 'touchstart', {
140
+ bubbles: true,
141
+ cancelable: true,
142
+ touches: [],
143
+ changedTouches: [{ clientY: 100 }]
144
+ });
145
+
146
+ const touchMove = createTouchEvent(this.player.el(), 'touchmove', {
147
+ bubbles: true,
148
+ cancelable: true,
149
+ touches: [{ clientY: 50 }],
150
+ changedTouches: []
151
+ });
152
+
153
+ const touchEnd = createTouchEvent(this.player.el(), 'touchend', {
154
+ bubbles: true,
155
+ cancelable: true,
156
+ touches: [],
157
+ changedTouches: [{ clientY: 50 }]
158
+ });
159
+
160
+ this.player.el().dispatchEvent(touchStart);
161
+ this.player.el().dispatchEvent(touchMove);
162
+ this.player.el().dispatchEvent(touchEnd);
163
+
164
+ assert.notOk(this.player.requestFullscreen.called, 'requestFullscreen not called when swipe disabled');
165
+ });
166
+
167
+ QUnit[skipWithoutTouch]('scales element on touch move', function(assert) {
168
+ this.player.mobileUi({
169
+ fullscreen: {
170
+ swipeToFullscreen: true,
171
+ swipeFromFullscreen: true,
172
+ swipeThreshold: 50
173
+ },
174
+ forceForTesting: true
175
+ });
176
+
177
+ this.clock.tick(1);
178
+
179
+ const techEl = this.player.tech_.el();
180
+
181
+ const touchStart = createTouchEvent(this.player.el(), 'touchstart', {
182
+ bubbles: true,
183
+ cancelable: true,
184
+ touches: [],
185
+ changedTouches: [{ clientY: 100 }]
186
+ });
187
+
188
+ const touchMove = createTouchEvent(this.player.el(), 'touchmove', {
189
+ bubbles: true,
190
+ cancelable: true,
191
+ touches: [{ clientY: 50 }],
192
+ changedTouches: []
193
+ });
194
+
195
+ this.player.el().dispatchEvent(touchStart);
196
+ this.player.el().dispatchEvent(touchMove);
197
+
198
+ assert.ok(techEl.style.transform.includes('scale('), 'applies scale transform on touch move');
199
+ });
200
+
201
+ QUnit[skipWithoutTouch]('enters fullscreen on swipe up', function(assert) {
202
+ this.player.mobileUi({
203
+ fullscreen: {
204
+ swipeToFullscreen: true,
205
+ swipeFromFullscreen: true,
206
+ swipeThreshold: 50
207
+ },
208
+ forceForTesting: true
209
+ });
210
+
211
+ this.clock.tick(1);
212
+
213
+ sinon.stub(this.player, 'isFullscreen').returns(false);
214
+ sinon.stub(this.player, 'requestFullscreen').returns(Promise.resolve());
215
+
216
+ const touchStart = createTouchEvent(this.player.el(), 'touchstart', {
217
+ bubbles: true,
218
+ cancelable: true,
219
+ touches: [],
220
+ changedTouches: [{ clientY: 200 }]
221
+ });
222
+
223
+ const touchEnd = createTouchEvent(this.player.el(), 'touchend', {
224
+ bubbles: true,
225
+ cancelable: true,
226
+ touches: [],
227
+ changedTouches: [{ clientY: 100 }]
228
+ });
229
+
230
+ this.player.el().dispatchEvent(touchStart);
231
+ this.player.el().dispatchEvent(touchEnd);
232
+
233
+ assert.true(this.player.requestFullscreen.called, 'requestFullscreen called on swipe up above threshold');
234
+ });
235
+
236
+ QUnit[skipWithoutTouch]('exits fullscreen on swipe down', function(assert) {
237
+ this.player.mobileUi({
238
+ fullscreen: {
239
+ swipeToFullscreen: true,
240
+ swipeFromFullscreen: true,
241
+ swipeThreshold: 50
242
+ },
243
+ forceForTesting: true
244
+ });
245
+
246
+ this.clock.tick(1);
247
+
248
+ sinon.stub(this.player, 'isFullscreen').returns(true);
249
+ sinon.stub(this.player, 'exitFullscreen');
250
+
251
+ const touchStart = createTouchEvent(this.player.el(), 'touchstart', {
252
+ bubbles: true,
253
+ cancelable: true,
254
+ touches: [],
255
+ changedTouches: [{ clientY: 100 }]
256
+ });
257
+
258
+ const touchEnd = createTouchEvent(this.player.el(), 'touchend', {
259
+ bubbles: true,
260
+ cancelable: true,
261
+ touches: [],
262
+ changedTouches: [{ clientY: 200 }]
263
+ });
264
+
265
+ this.player.el().dispatchEvent(touchStart);
266
+ this.player.el().dispatchEvent(touchEnd);
267
+
268
+ assert.true(this.player.exitFullscreen.called, 'exitFullscreen called on swipe down below threshold');
269
+ });
270
+
271
+ QUnit[skipWithoutTouch]('respects swipe threshold', function(assert) {
272
+ this.player.mobileUi({
273
+ fullscreen: {
274
+ swipeToFullscreen: true,
275
+ swipeFromFullscreen: true,
276
+ swipeThreshold: 100
277
+ },
278
+ forceForTesting: true
279
+ });
280
+
281
+ this.clock.tick(1);
282
+
283
+ sinon.stub(this.player, 'isFullscreen').returns(false);
284
+ sinon.stub(this.player, 'requestFullscreen').returns(Promise.resolve());
285
+
286
+ const touchStart = createTouchEvent(this.player.el(), 'touchstart', {
287
+ bubbles: true,
288
+ cancelable: true,
289
+ touches: [],
290
+ changedTouches: [{ clientY: 200 }]
291
+ });
292
+
293
+ // Small swipe (30px) - below 100px threshold
294
+ const touchEnd = createTouchEvent(this.player.el(), 'touchend', {
295
+ bubbles: true,
296
+ cancelable: true,
297
+ touches: [],
298
+ changedTouches: [{ clientY: 170 }]
299
+ });
300
+
301
+ this.player.el().dispatchEvent(touchStart);
302
+ this.player.el().dispatchEvent(touchEnd);
303
+
304
+ assert.notOk(this.player.requestFullscreen.called, 'requestFullscreen not called when swipe below threshold');
305
+ });
306
+
307
+ QUnit[skipWithoutTouch]('handles touchcancel event', function(assert) {
308
+ this.player.mobileUi({
309
+ fullscreen: {
310
+ swipeToFullscreen: true,
311
+ swipeFromFullscreen: true,
312
+ swipeThreshold: 50
313
+ },
314
+ forceForTesting: true
315
+ });
316
+
317
+ this.clock.tick(1);
318
+
319
+ sinon.stub(this.player, 'isFullscreen').returns(false);
320
+ sinon.stub(this.player, 'requestFullscreen').returns(Promise.resolve());
321
+
322
+ const touchStart = createTouchEvent(this.player.el(), 'touchstart', {
323
+ bubbles: true,
324
+ cancelable: true,
325
+ touches: [],
326
+ changedTouches: [{ clientY: 200 }]
327
+ });
328
+
329
+ const touchCancel = createTouchEvent(this.player.el(), 'touchcancel', {
330
+ bubbles: true,
331
+ cancelable: true,
332
+ touches: [],
333
+ changedTouches: [{ clientY: 100 }],
334
+ type: 'touchcancel'
335
+ });
336
+
337
+ this.player.el().dispatchEvent(touchStart);
338
+ this.player.el().dispatchEvent(touchCancel);
339
+
340
+ assert.notOk(this.player.requestFullscreen.called, 'requestFullscreen not called on touchcancel');
341
+ });
342
+
343
+ QUnit.test('cleans up event listeners on dispose', function(assert) {
344
+ this.player.mobileUi({
345
+ fullscreen: {
346
+ swipeToFullscreen: true,
347
+ swipeFromFullscreen: true,
348
+ swipeThreshold: 50
349
+ },
350
+ forceForTesting: true
351
+ });
352
+
353
+ this.clock.tick(1);
354
+
355
+ const rELSpy = sinon.spy(this.player.el(), 'removeEventListener');
356
+
357
+ this.player.dispose();
358
+
359
+ this.clock.tick(10);
360
+
361
+ assert.ok(rELSpy.calledWith('touchstart'), 'removeEventListener called during dispose');
362
+
363
+ rELSpy.restore();
364
+ });
365
+