/
cast_utils_unit.js
298 lines (252 loc) · 9.73 KB
/
cast_utils_unit.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
/*! @license
* Shaka Player
* Copyright 2016 Google LLC
* SPDX-License-Identifier: Apache-2.0
*/
describe('CastUtils', () => {
const CastUtils = shaka.cast.CastUtils;
const FakeEvent = shaka.util.FakeEvent;
it('includes every Player member', () => {
const ignoredMembers = [
'constructor', // JavaScript added field
'getAdManager', // Handled specially
'getSharedConfiguration', // Handled specially
'getNetworkingEngine', // Handled specially
'getDrmEngine', // Handled specially
'getMediaElement', // Handled specially
'setMaxHardwareResolution',
'destroy', // Should use CastProxy.destroy instead
'drmInfo', // Too large to proxy
'getManifest', // Too large to proxy
'getManifestParserFactory', // Would not serialize.
'setVideoContainer',
// Test helper methods (not @export'd)
'createDrmEngine',
'createNetworkingEngine',
'createPlayhead',
'createMediaSourceEngine',
'createStreamingEngine',
];
const castMembers = CastUtils.PlayerVoidMethods
.concat(CastUtils.PlayerPromiseMethods)
.concat(Object.keys(CastUtils.PlayerGetterMethods))
.concat(Object.keys(CastUtils.PlayerGetterMethodsThatRequireLive));
// eslint-disable-next-line no-restricted-syntax
const allPlayerMembers = Object.getOwnPropertyNames(shaka.Player.prototype);
expect(
ignoredMembers.filter((member) => !allPlayerMembers.includes(member)))
.toEqual([]);
const playerMembers = allPlayerMembers.filter((name) => {
// Private members end with _.
return !ignoredMembers.includes(name) && !name.endsWith('_');
});
// To make debugging easier, don't check that they are equal; instead check
// that neither has any extra entries.
expect(castMembers.filter((name) => !playerMembers.includes(name)))
.toEqual([]);
expect(playerMembers.filter((name) => !castMembers.includes(name)))
.toEqual([]);
});
describe('serialize/deserialize', () => {
it('transfers infinite values and NaN', () => {
const orig = {
'nan': NaN,
'positive_infinity': Infinity,
'negative_infinity': -Infinity,
'null': null,
'true': true,
'false': false,
'one': 1,
'string': 'a string',
};
const serialized = CastUtils.serialize(orig);
// The object is turned into a string.
expect(typeof serialized).toBe('string');
// The deserialized object matches the original.
const deserialized = CastUtils.deserialize(serialized);
for (const k in orig) {
if (typeof orig[k] == 'number' && isNaN(orig[k])) {
expect(deserialized[k]).toBeNaN();
} else {
expect(deserialized[k]).toBe(orig[k]);
}
}
});
it('transfers real Events', () => {
const event = new CustomEvent('myEventType');
// Properties that can definitely be transferred.
const nativeProperties = [
'bubbles',
'type',
'cancelable',
'defaultPrevented',
];
const extraProperties = {
'key': 'value',
'true': true,
'one': 1,
};
for (const k in extraProperties) {
event[k] = extraProperties[k];
}
// The event is turned into a string.
const serialized = CastUtils.serialize(event);
expect(typeof serialized).toBe('string');
// The string is turned back into an object.
const deserialized = CastUtils.deserialize(serialized);
expect(typeof deserialized).toBe('object');
// The object can be used to construct a FakeEvent.
const fakeEvent = FakeEvent.fromRealEvent(deserialized);
// The fake event has the same type and properties as the original.
const asObj = /** @type {!Object} */ (fakeEvent);
for (const k of nativeProperties) {
expect(asObj[k]).toBe(event[k]);
}
for (const k in extraProperties) {
expect(asObj[k]).toBe(event[k]);
}
});
it('transfers dispatched FakeEvents', async () => {
/** @type {!FakeEvent} */
const event = new FakeEvent('custom');
// Properties that can definitely be transferred.
const nativeProperties = [
'bubbles',
'type',
'cancelable',
'defaultPrevented',
];
const extraProperties = {
'key': 'value',
'true': true,
'one': 1,
};
const asObj = /** @type {!Object} */ (event);
for (const k in extraProperties) {
asObj[k] = extraProperties[k];
}
/** @type {!shaka.util.FakeEventTarget} */
const target = new shaka.util.FakeEventTarget();
const p = new Promise((resolve) => {
target.addEventListener(event.type, resolve);
});
target.dispatchEvent(event);
await p;
// The event is turned into a string.
const serialized = CastUtils.serialize(event);
expect(typeof serialized).toBe('string');
// The string is turned back into an object.
const deserialized = CastUtils.deserialize(serialized);
expect(typeof deserialized).toBe('object');
// The deserialized event has the same type and properties as the
// original.
for (const k of nativeProperties) {
expect(deserialized[k]).toBe(asObj[k]);
}
for (const k in extraProperties) {
expect(deserialized[k]).toBe(asObj[k]);
}
});
describe('TimeRanges', () => {
/** @type {!HTMLVideoElement} */
let video;
/** @type {!shaka.util.EventManager} */
let eventManager;
/** @type {!shaka.media.MediaSourceEngine} */
let mediaSourceEngine;
beforeAll(() => {
video = shaka.test.UiUtils.createVideoElement();
document.body.appendChild(video);
});
beforeEach(async () => {
// The TimeRanges constructor cannot be used directly, so we load a clip
// to get ranges to use.
const fakeVideoStream = {
mimeType: 'video/mp4',
codecs: 'avc1.42c01e',
drmInfos: [],
};
const initSegmentUrl = '/base/test/test/assets/sintel-video-init.mp4';
const videoSegmentUrl =
'/base/test/test/assets/sintel-video-segment.mp4';
// Wait for the media source to be open.
eventManager = new shaka.util.EventManager();
eventManager.listen(video, 'error', onError);
function onError() {
fail('Error code ' + (video.error ? video.error.code : 0));
}
mediaSourceEngine = new shaka.media.MediaSourceEngine(
video,
new shaka.test.FakeClosedCaptionParser(),
new shaka.test.FakeTextDisplayer());
const ContentType = shaka.util.ManifestParserUtils.ContentType;
const initObject = new Map();
initObject.set(ContentType.VIDEO, fakeVideoStream);
await mediaSourceEngine.init(initObject, false);
const data = await shaka.test.Util.fetch(initSegmentUrl);
await mediaSourceEngine.appendBuffer(
ContentType.VIDEO, data, null, null,
/* hasClosedCaptions= */ false);
const data2 = await shaka.test.Util.fetch(videoSegmentUrl);
await mediaSourceEngine.appendBuffer(
ContentType.VIDEO, data2, null, null,
/* hasClosedCaptions= */ false);
});
afterEach(async () => {
eventManager.release();
if (mediaSourceEngine) {
await mediaSourceEngine.destroy();
}
// "unload" the video element.
video.removeAttribute('src');
video.load();
});
afterAll(() => {
document.body.removeChild(video);
});
it('deserialize into equivalent objects', () => {
const buffered = video.buffered;
// The test is less interesting if the ranges are empty.
expect(buffered.length).toBeGreaterThan(0);
// The TimeRanges object is turned into a string.
const serialized = CastUtils.serialize(buffered);
expect(typeof serialized).toBe('string');
// Expect the deserialized version to look like the original.
const deserialized = CastUtils.deserialize(serialized);
expect(deserialized.length).toBe(buffered.length);
expect(deserialized.start).toEqual(jasmine.any(Function));
expect(deserialized.end).toEqual(jasmine.any(Function));
const TimeRangesUtils = shaka.media.TimeRangesUtils;
expect(TimeRangesUtils.getBufferedInfo(deserialized))
.toEqual(TimeRangesUtils.getBufferedInfo(buffered));
});
}); // describe('TimeRanges')
it('transfers real Errors', () => {
let realError;
try {
// Cast undefined to "?" to convince the compiler to let us dereference
// it.
const foo = /** @type {?} */(undefined);
// Now this will generate a TypeError.
foo.bar = 'baz';
// We need to catch a real Error in this test, so we disable eslint on
// the next line.
// eslint-disable-next-line no-restricted-syntax
} catch (error) {
realError = error;
}
// The event is turned into a string.
const serialized = CastUtils.serialize(realError);
expect(typeof serialized).toBe('string');
// The string is turned back into an object.
const deserialized = CastUtils.deserialize(serialized);
expect(typeof deserialized).toBe('object');
// And that object should be an Error type.
expect(deserialized).toEqual(jasmine.any(Error));
// At least these basic properties should match.
expect(deserialized.type).toBe(realError.type);
expect(deserialized.message).toBe(realError.message);
expect(deserialized.stack).toBe(realError.stack);
});
}); // describe('serialize/deserialize')
}); // describe('CastUtils')