ranges.test.js
10.7 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
import Ranges from '../src/ranges';
import {createTimeRanges} from 'video.js';
import QUnit from 'qunit';
let rangesEqual = (rangeOne, rangeTwo) => {
if (!rangeOne || !rangeTwo) {
return false;
}
if (rangeOne.length !== rangeTwo.length) {
return false;
}
for (let i = 0; i < rangeOne.length; i++) {
if (rangeOne.start(i) !== rangeTwo.start(i) ||
rangeOne.end(i) !== rangeTwo.end(i)) {
return false;
}
}
return true;
};
QUnit.module('TimeRanges Utilities');
QUnit.test('finds the overlapping time range', function() {
let range = Ranges.findRange(createTimeRanges([[0, 5], [6, 12]]), 3);
QUnit.equal(range.length, 1, 'found one range');
QUnit.equal(range.end(0), 5, 'inside the first buffered region');
range = Ranges.findRange(createTimeRanges([[0, 5], [6, 12]]), 6);
QUnit.equal(range.length, 1, 'found one range');
QUnit.equal(range.end(0), 12, 'inside the second buffered region');
});
QUnit.module('Buffer Inpsection');
QUnit.test('detects time range end-point changed by updates', function() {
let edge;
// Single-range changes
edge = Ranges.findSoleUncommonTimeRangesEnd(createTimeRanges([[0, 10]]),
createTimeRanges([[0, 11]]));
QUnit.strictEqual(edge, 11, 'detected a forward addition');
edge = Ranges.findSoleUncommonTimeRangesEnd(createTimeRanges([[5, 10]]),
createTimeRanges([[0, 10]]));
QUnit.strictEqual(edge, null, 'ignores backward addition');
edge = Ranges.findSoleUncommonTimeRangesEnd(createTimeRanges([[5, 10]]),
createTimeRanges([[0, 11]]));
QUnit.strictEqual(edge, 11,
'detected a forward addition & ignores a backward addition');
edge = Ranges.findSoleUncommonTimeRangesEnd(createTimeRanges([[0, 10]]),
createTimeRanges([[0, 9]]));
QUnit.strictEqual(edge, null,
'ignores a backwards addition resulting from a shrinking range');
edge = Ranges.findSoleUncommonTimeRangesEnd(createTimeRanges([[0, 10]]),
createTimeRanges([[2, 7]]));
QUnit.strictEqual(edge, null,
'ignores a forward & backwards addition resulting from a shrinking ' +
'range');
edge = Ranges.findSoleUncommonTimeRangesEnd(createTimeRanges([[2, 10]]),
createTimeRanges([[0, 7]]));
QUnit.strictEqual(
edge,
null,
'ignores a forward & backwards addition resulting from a range shifted backward'
);
edge = Ranges.findSoleUncommonTimeRangesEnd(createTimeRanges([[2, 10]]),
createTimeRanges([[5, 15]]));
QUnit.strictEqual(edge, 15,
'detected a forwards addition resulting from a range shifted foward');
// Multiple-range changes
edge = Ranges.findSoleUncommonTimeRangesEnd(createTimeRanges([[0, 10]]),
createTimeRanges([[0, 11], [12, 15]]));
QUnit.strictEqual(edge, null, 'ignores multiple new forward additions');
edge = Ranges.findSoleUncommonTimeRangesEnd(createTimeRanges([[0, 10], [20, 40]]),
createTimeRanges([[20, 50]]));
QUnit.strictEqual(edge, 50, 'detected a forward addition & ignores range removal');
edge = Ranges.findSoleUncommonTimeRangesEnd(createTimeRanges([[0, 10], [20, 40]]),
createTimeRanges([[0, 50]]));
QUnit.strictEqual(edge, 50, 'detected a forward addition & ignores merges');
edge = Ranges.findSoleUncommonTimeRangesEnd(createTimeRanges([[0, 10], [20, 40]]),
createTimeRanges([[0, 40]]));
QUnit.strictEqual(edge, null, 'ignores merges');
// Empty input
edge = Ranges.findSoleUncommonTimeRangesEnd(createTimeRanges(),
createTimeRanges([[0, 11]]));
QUnit.strictEqual(edge, 11, 'handle an empty original TimeRanges object');
edge = Ranges.findSoleUncommonTimeRangesEnd(createTimeRanges([[0, 11]]),
createTimeRanges());
QUnit.strictEqual(edge, null, 'handle an empty update TimeRanges object');
// Null input
edge = Ranges.findSoleUncommonTimeRangesEnd(null, createTimeRanges([[0, 11]]));
QUnit.strictEqual(edge, 11, 'treat null original buffer as an empty TimeRanges object');
edge = Ranges.findSoleUncommonTimeRangesEnd(createTimeRanges([[0, 11]]), null);
QUnit.strictEqual(edge, null, 'treat null update buffer as an empty TimeRanges object');
});
QUnit.module('Segment Percent Buffered Calculations');
QUnit.test('calculates the percent buffered for segments in the simple case', function() {
let segmentStart = 10;
let segmentDuration = 10;
let currentTime = 0;
let buffered = createTimeRanges([[15, 19]]);
let percentBuffered = Ranges.getSegmentBufferedPercent(
segmentStart,
segmentDuration,
currentTime,
buffered);
QUnit.equal(percentBuffered, 40, 'calculated the buffered amount correctly');
});
QUnit.test('consider the buffer before currentTime to be filled if the ' +
'segement begins at or before the currentTime', function() {
let segmentStart = 10;
let segmentDuration = 10;
let currentTime = 15;
let buffered = createTimeRanges([[15, 19]]);
let percentBuffered = Ranges.getSegmentBufferedPercent(
segmentStart,
segmentDuration,
currentTime,
buffered);
QUnit.equal(percentBuffered, 90, 'calculated the buffered amount correctly');
});
QUnit.test('does not consider the buffer before currentTime as filled if the segment ' +
'begins after the currentTime', function() {
let segmentStart = 10;
let segmentDuration = 10;
let currentTime = 18;
let buffered = createTimeRanges([[19, 30]]);
let percentBuffered = Ranges.getSegmentBufferedPercent(
segmentStart,
segmentDuration,
currentTime,
buffered);
QUnit.equal(percentBuffered, 10, 'calculated the buffered amount correctly');
});
QUnit.test('calculates the percent buffered for segments with multiple buffered ' +
'regions', function() {
let segmentStart = 10;
let segmentDuration = 10;
let currentTime = 0;
let buffered = createTimeRanges([[0, 11], [12, 19]]);
let percentBuffered = Ranges.getSegmentBufferedPercent(
segmentStart,
segmentDuration,
currentTime,
buffered);
QUnit.equal(percentBuffered, 80, 'calculated the buffered amount correctly');
});
QUnit.test('calculates the percent buffered for segments with multiple buffered ' +
'regions taking into account currentTime', function() {
let segmentStart = 10;
let segmentDuration = 10;
let currentTime = 12;
let buffered = createTimeRanges([[0, 11], [12, 19]]);
let percentBuffered = Ranges.getSegmentBufferedPercent(
segmentStart,
segmentDuration,
currentTime,
buffered);
QUnit.equal(percentBuffered, 90, 'calculated the buffered amount correctly');
});
QUnit.test('calculates the percent buffered as 0 for zero-length segments', function() {
let segmentStart = 10;
let segmentDuration = 0;
let currentTime = 0;
let buffered = createTimeRanges([[0, 19]]);
let percentBuffered = Ranges.getSegmentBufferedPercent(
segmentStart,
segmentDuration,
currentTime,
buffered);
QUnit.equal(percentBuffered, 0, 'calculated the buffered amount correctly');
});
QUnit.test('calculates the percent buffered as 0 for segments that do not overlap ' +
'buffered regions taking into account currentTime', function() {
let segmentStart = 10;
let segmentDuration = 10;
let currentTime = 19;
let buffered = createTimeRanges([[20, 30]]);
let percentBuffered = Ranges.getSegmentBufferedPercent(
segmentStart,
segmentDuration,
currentTime,
buffered);
QUnit.equal(percentBuffered, 0, 'calculated the buffered amount correctly');
});
QUnit.test('calculates the percent buffered for segments ' +
'that end before currentTime', function() {
let segmentStart = 10;
let segmentDuration = 10;
let currentTime = 19.6;
let buffered = createTimeRanges([[0, 19.5]]);
let percentBuffered = Ranges.getSegmentBufferedPercent(
segmentStart,
segmentDuration,
currentTime,
buffered);
QUnit.equal(percentBuffered, 95, 'calculated the buffered amount correctly');
});
QUnit.test('finds next range', function() {
QUnit.equal(Ranges.findNextRange(createTimeRanges(), 10).length,
0,
'does not find next range in empty buffer');
QUnit.equal(Ranges.findNextRange(createTimeRanges([[0, 20]]), 10).length,
0,
'does not find next range when no next ranges');
QUnit.equal(Ranges.findNextRange(createTimeRanges([[0, 20]]), 30).length,
0,
'does not find next range when current time later than buffer');
QUnit.equal(Ranges.findNextRange(createTimeRanges([[10, 20]]), 10).length,
0,
'does not find next range when current time is at beginning of buffer');
QUnit.equal(Ranges.findNextRange(createTimeRanges([[10, 20]]), 11).length,
0,
'does not find next range when current time in middle of buffer');
QUnit.equal(Ranges.findNextRange(createTimeRanges([[10, 20]]), 20).length,
0,
'does not find next range when current time is at end of buffer');
QUnit.ok(rangesEqual(Ranges.findNextRange(createTimeRanges([[10, 20]]), 0),
createTimeRanges([[10, 20]])),
'finds next range when buffer comes after time');
QUnit.ok(rangesEqual(Ranges.findNextRange(createTimeRanges([[10, 20], [25, 35]]), 22),
createTimeRanges([[25, 35]])),
'finds next range when time between buffers');
QUnit.ok(rangesEqual(Ranges.findNextRange(createTimeRanges([[10, 20], [25, 35]]), 15),
createTimeRanges([[25, 35]])),
'finds next range when time in previous buffer');
});
QUnit.test('finds gaps within ranges', function() {
QUnit.equal(Ranges.findGaps(createTimeRanges()).length,
0,
'does not find gap in empty buffer');
QUnit.equal(Ranges.findGaps(createTimeRanges([[0, 10]])).length,
0,
'does not find gap in single buffer');
QUnit.equal(Ranges.findGaps(createTimeRanges([[1, 10]])).length,
0,
'does not find gap at start of buffer');
QUnit.ok(rangesEqual(Ranges.findGaps(createTimeRanges([[0, 10], [11, 20]])),
createTimeRanges([[10, 11]])),
'finds a single gap');
QUnit.ok(rangesEqual(Ranges.findGaps(createTimeRanges([[0, 10], [11, 20], [22, 30]])),
createTimeRanges([[10, 11], [20, 22]])),
'finds multiple gaps');
});