test-helpers.js
7.93 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
import document from 'global/document';
import sinon from 'sinon';
import videojs from 'video.js';
import QUnit from 'qunit';
/* eslint-disable no-unused-vars */
// needed so MediaSource can be registered with videojs
import MediaSource from 'videojs-contrib-media-sources';
/* eslint-enable */
import testDataManifests from './test-manifests.js';
import xhrFactory from '../src/xhr';
import window from 'global/window';
// a SourceBuffer that tracks updates but otherwise is a noop
class MockSourceBuffer extends videojs.EventTarget {
constructor() {
super();
this.updates_ = [];
this.updating = false;
this.on('updateend', function() {
this.updating = false;
});
this.buffered = videojs.createTimeRanges();
this.duration_ = NaN;
Object.defineProperty(this, 'duration', {
get() {
return this.duration_;
},
set(duration) {
this.updates_.push({
duration
});
this.duration_ = duration;
}
});
}
abort() {
this.updates_.push({
abort: true
});
}
appendBuffer(bytes) {
this.updates_.push({
append: bytes
});
this.updating = true;
}
remove(start, end) {
this.updates_.push({
remove: [start, end]
});
}
}
class MockMediaSource extends videojs.EventTarget {
constructor() {
super();
this.readyState = 'closed';
this.on('sourceopen', function() {
this.readyState = 'open';
});
this.sourceBuffers = [];
this.duration = NaN;
this.seekable = videojs.createTimeRange();
}
addSeekableRange_(start, end) {
this.seekable = videojs.createTimeRange(start, end);
}
addSourceBuffer(mime) {
let sourceBuffer = new MockSourceBuffer();
sourceBuffer.mimeType_ = mime;
this.sourceBuffers.push(sourceBuffer);
return sourceBuffer;
}
endOfStream(error) {
this.readyState = 'closed';
this.error_ = error;
}
}
export const useFakeMediaSource = function() {
let RealMediaSource = videojs.MediaSource;
let realCreateObjectURL = window.URL.createObjectURL;
let id = 0;
videojs.MediaSource = MockMediaSource;
videojs.MediaSource.supportsNativeMediaSources =
RealMediaSource.supportsNativeMediaSources;
videojs.URL.createObjectURL = function() {
id++;
return 'blob:videojs-contrib-hls-mock-url' + id;
};
return {
restore() {
videojs.MediaSource = RealMediaSource;
videojs.URL.createObjectURL = realCreateObjectURL;
}
};
};
let fakeEnvironment = {
requests: [],
restore() {
this.clock.restore();
videojs.xhr.XMLHttpRequest = window.XMLHttpRequest;
this.xhr.restore();
['warn', 'error'].forEach((level) => {
if (this.log && this.log[level] && this.log[level].restore) {
if (QUnit) {
QUnit.equal(this.log[level].callCount, 0, `no unexpected logs on ${level}`);
}
this.log[level].restore();
}
});
}
};
export const useFakeEnvironment = function() {
fakeEnvironment.log = {};
['warn', 'error'].forEach((level) => {
// you can use .log[level].args to get args
sinon.stub(videojs.log, level);
fakeEnvironment.log[level] = videojs.log[level];
Object.defineProperty(videojs.log[level], 'calls', {
get() {
// reset callCount to 0 so they don't have to
let callCount = this.callCount;
this.callCount = 0;
return callCount;
}
});
});
fakeEnvironment.clock = sinon.useFakeTimers();
fakeEnvironment.xhr = sinon.useFakeXMLHttpRequest();
fakeEnvironment.requests.length = 0;
fakeEnvironment.xhr.onCreate = function(xhr) {
fakeEnvironment.requests.push(xhr);
};
videojs.xhr.XMLHttpRequest = fakeEnvironment.xhr;
return fakeEnvironment;
};
// patch over some methods of the provided tech so it can be tested
// synchronously with sinon's fake timers
export const mockTech = function(tech) {
if (tech.isMocked_) {
// make this function idempotent because HTML and Flash based
// playback have very different lifecycles. For HTML, the tech
// is available on player creation. For Flash, the tech isn't
// ready until the source has been loaded and one tick has
// expired.
return;
}
tech.isMocked_ = true;
tech.src_ = null;
tech.time_ = null;
tech.paused_ = !tech.autoplay();
tech.paused = function() {
return tech.paused_;
};
if (!tech.currentTime_) {
tech.currentTime_ = tech.currentTime;
}
tech.currentTime = function() {
return tech.time_ === null ? tech.currentTime_() : tech.time_;
};
tech.setSrc = function(src) {
tech.src_ = src;
};
tech.src = function(src) {
if (src !== null) {
return tech.setSrc(src);
}
return tech.src_ === null ? tech.src : tech.src_;
};
tech.currentSrc_ = tech.currentSrc;
tech.currentSrc = function() {
return tech.src_ === null ? tech.currentSrc_() : tech.src_;
};
tech.play_ = tech.play;
tech.play = function() {
tech.play_();
tech.paused_ = false;
tech.trigger('play');
};
tech.pause_ = tech.pause_;
tech.pause = function() {
tech.pause_();
tech.paused_ = true;
tech.trigger('pause');
};
tech.setCurrentTime = function(time) {
tech.time_ = time;
setTimeout(function() {
tech.trigger('seeking');
setTimeout(function() {
tech.trigger('seeked');
}, 1);
}, 1);
};
};
export const createPlayer = function(options) {
let video;
let player;
video = document.createElement('video');
video.className = 'video-js';
document.querySelector('#qunit-fixture').appendChild(video);
player = videojs(video, options || {
flash: {
swf: ''
}
});
player.buffered = function() {
return videojs.createTimeRange(0, 0);
};
mockTech(player.tech_);
return player;
};
export const openMediaSource = function(player, clock) {
// ensure the Flash tech is ready
player.tech_.triggerReady();
clock.tick(1);
// mock the tech *after* it has finished loading so that we don't
// mock a tech that will be unloaded on the next tick
mockTech(player.tech_);
player.tech_.hls.xhr = xhrFactory();
// simulate the sourceopen event
player.tech_.hls.mediaSource.readyState = 'open';
player.tech_.hls.mediaSource.dispatchEvent({
type: 'sourceopen',
swfId: player.tech_.el().id
});
};
export const standardXHRResponse = function(request, data) {
if (!request.url) {
return;
}
let contentType = 'application/json';
// contents off the global object
let manifestName = (/(?:.*\/)?(.*)\.m3u8/).exec(request.url);
if (manifestName) {
manifestName = manifestName[1];
} else {
manifestName = request.url;
}
if (/\.m3u8?/.test(request.url)) {
contentType = 'application/vnd.apple.mpegurl';
} else if (/\.ts/.test(request.url)) {
contentType = 'video/MP2T';
}
if (!data) {
data = testDataManifests[manifestName];
}
request.response = new Uint8Array(16).buffer;
request.respond(200, {'Content-Type': contentType}, data);
};
// return an absolute version of a page-relative URL
export const absoluteUrl = function(relativeUrl) {
return window.location.protocol + '//' +
window.location.host +
(window.location.pathname
.split('/')
.slice(0, -1)
.concat(relativeUrl)
.join('/')
);
};
export const playlistWithDuration = function(time, conf) {
let result = {
targetDuration: 10,
mediaSequence: conf && conf.mediaSequence ? conf.mediaSequence : 0,
discontinuityStarts: [],
segments: [],
endList: true
};
let count = Math.floor(time / 10);
let remainder = time % 10;
let i;
let isEncrypted = conf && conf.isEncrypted;
for (i = 0; i < count; i++) {
result.segments.push({
uri: i + '.ts',
resolvedUri: i + '.ts',
duration: 10
});
if (isEncrypted) {
result.segments[i].key = {
uri: i + '-key.php',
resolvedUri: i + '-key.php'
};
}
}
if (remainder) {
result.segments.push({
uri: i + '.ts',
duration: remainder
});
}
return result;
};