rendition-mixin.test.js
6.64 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
/* eslint-disable max-len */
import QUnit from 'qunit';
import RenditionMixin from '../src/rendition-mixin.js';
import videojs from 'video.js';
const makeMockPlaylist = function(options) {
options = options || {};
let playlist = {
segments: [],
attributes: {}
};
playlist.attributes.BANDWIDTH = options.bandwidth;
if ('width' in options) {
playlist.attributes.RESOLUTION = playlist.attributes.RESOLUTION || {};
playlist.attributes.RESOLUTION.width = options.width;
}
if ('height' in options) {
playlist.attributes.RESOLUTION = playlist.attributes.RESOLUTION || {};
playlist.attributes.RESOLUTION.height = options.height;
}
if ('excludeUntil' in options) {
playlist.excludeUntil = options.excludeUntil;
}
if ('uri' in options) {
playlist.uri = options.uri;
}
if ('disabled' in options) {
playlist.disabled = options.disabled;
}
return playlist;
};
const makeMockHlsHandler = function(playlistOptions) {
let mcp = {
fastQualityChange_: () => {
mcp.fastQualityChange_.calls++;
}
};
mcp.fastQualityChange_.calls = 0;
let hlsHandler = {
masterPlaylistController_: mcp
};
hlsHandler.playlists = new videojs.EventTarget();
hlsHandler.playlists.master = { playlists: [] };
playlistOptions.forEach((playlist, i) => {
hlsHandler.playlists.master.playlists[i] = makeMockPlaylist(playlist);
if (playlist.uri) {
hlsHandler.playlists.master.playlists[playlist.uri] =
hlsHandler.playlists.master.playlists[i];
}
});
return hlsHandler;
};
QUnit.module('Rendition Selector API Mixin');
QUnit.test('adds the representations API to HlsHandler', function(assert) {
let hlsHandler = makeMockHlsHandler([
{}
]);
RenditionMixin(hlsHandler);
assert.equal(typeof hlsHandler.representations, 'function',
'added the representations API');
});
QUnit.test('returns proper number of representations', function(assert) {
let hlsHandler = makeMockHlsHandler([
{}, {}, {}
]);
RenditionMixin(hlsHandler);
let renditions = hlsHandler.representations();
assert.equal(renditions.length, 3, 'number of renditions is 3');
});
QUnit.test('returns representations in playlist order', function(assert) {
let hlsHandler = makeMockHlsHandler([
{
bandwidth: 10
},
{
bandwidth: 20
},
{
bandwidth: 30
}
]);
RenditionMixin(hlsHandler);
let renditions = hlsHandler.representations();
assert.equal(renditions[0].bandwidth, 10, 'rendition has bandwidth 10');
assert.equal(renditions[1].bandwidth, 20, 'rendition has bandwidth 20');
assert.equal(renditions[2].bandwidth, 30, 'rendition has bandwidth 30');
});
QUnit.test('returns representations with width and height if present', function(assert) {
let hlsHandler = makeMockHlsHandler([
{
bandwidth: 10,
width: 100,
height: 200
},
{
bandwidth: 20,
width: 500,
height: 600
},
{
bandwidth: 30
}
]);
RenditionMixin(hlsHandler);
let renditions = hlsHandler.representations();
assert.equal(renditions[0].width, 100, 'rendition has a width of 100');
assert.equal(renditions[0].height, 200, 'rendition has a height of 200');
assert.equal(renditions[1].width, 500, 'rendition has a width of 500');
assert.equal(renditions[1].height, 600, 'rendition has a height of 600');
assert.equal(renditions[2].width, undefined, 'rendition has a width of undefined');
assert.equal(renditions[2].height, undefined, 'rendition has a height of undefined');
});
QUnit.test('incompatible playlists are not included in the representations list',
function(assert) {
let hlsHandler = makeMockHlsHandler([
{
bandwidth: 0,
excludeUntil: Infinity,
uri: 'media0.m3u8'
},
{
bandwidth: 0,
excludeUntil: 0,
uri: 'media1.m3u8'
},
{
bandwidth: 0,
excludeUntil: Date.now() + 999999,
uri: 'media2.m3u8'
},
{
bandwidth: 0,
excludeUntil: 1,
uri: 'media3.m3u8'
},
{
bandwidth: 0,
uri: 'media4.m3u8'
}
]);
RenditionMixin(hlsHandler);
let renditions = hlsHandler.representations();
assert.equal(renditions.length, 4, 'incompatible rendition not added');
assert.equal(renditions[0].id, 'media1.m3u8', 'rendition is enabled');
assert.equal(renditions[1].id, 'media2.m3u8', 'rendition is enabled');
assert.equal(renditions[2].id, 'media3.m3u8', 'rendition is enabled');
assert.equal(renditions[3].id, 'media4.m3u8', 'rendition is enabled');
});
QUnit.test('setting a representation to disabled sets disabled to true',
function(assert) {
let renditiondisabled = 0;
let hlsHandler = makeMockHlsHandler([
{
bandwidth: 0,
excludeUntil: 0,
uri: 'media0.m3u8'
},
{
bandwidth: 0,
excludeUntil: 0,
uri: 'media1.m3u8'
}
]);
let playlists = hlsHandler.playlists.master.playlists;
hlsHandler.playlists.on('renditiondisabled', function() {
renditiondisabled++;
});
RenditionMixin(hlsHandler);
let renditions = hlsHandler.representations();
assert.equal(renditiondisabled, 0, 'renditiondisabled event has not been triggered');
renditions[0].enabled(false);
assert.equal(renditiondisabled, 1, 'renditiondisabled event has been triggered');
assert.equal(playlists[0].disabled, true, 'rendition has been disabled');
assert.equal(playlists[1].disabled, undefined, 'rendition has not been disabled');
assert.equal(playlists[0].excludeUntil, 0,
'excludeUntil not touched when disabling a rendition');
assert.equal(playlists[1].excludeUntil, 0,
'excludeUntil not touched when disabling a rendition');
});
QUnit.test('changing the enabled state of a representation calls fastQualityChange_',
function(assert) {
let renditionEnabledEvents = 0;
let hlsHandler = makeMockHlsHandler([
{
bandwidth: 0,
disabled: true,
uri: 'media0.m3u8'
},
{
bandwidth: 0,
uri: 'media1.m3u8'
}
]);
let mpc = hlsHandler.masterPlaylistController_;
hlsHandler.playlists.on('renditionenabled', function() {
renditionEnabledEvents++;
});
RenditionMixin(hlsHandler);
let renditions = hlsHandler.representations();
assert.equal(mpc.fastQualityChange_.calls, 0, 'fastQualityChange_ was never called');
assert.equal(renditionEnabledEvents, 0,
'renditionenabled event has not been triggered');
renditions[0].enabled(true);
assert.equal(mpc.fastQualityChange_.calls, 1, 'fastQualityChange_ was called once');
assert.equal(renditionEnabledEvents, 1,
'renditionenabled event has been triggered once');
renditions[1].enabled(false);
assert.equal(mpc.fastQualityChange_.calls, 2, 'fastQualityChange_ was called twice');
});