4 <script src=
"/w3c/resources/testharness.js"></script>
5 <script src=
"/w3c/resources/testharnessreport.js"></script>
6 <script src=
"mediasource-util.js"></script>
7 <link rel='stylesheet' href='/w3c/resources/testharness.css'
>
12 mediasource_test(function(test
, mediaElement
, mediaSource
)
14 var sourceBuffer
= mediaSource
.addSourceBuffer(MediaSourceUtil
.AUDIO_VIDEO_TYPE
);
16 assert_throws("InvalidAccessError", function()
18 sourceBuffer
.remove(-1, 2);
22 }, "Test remove with a negative start.");
24 mediasource_test(function(test
, mediaElement
, mediaSource
)
26 var sourceBuffer
= mediaSource
.addSourceBuffer(MediaSourceUtil
.AUDIO_VIDEO_TYPE
);
28 [ undefined, NaN
, Infinity
, -Infinity
].forEach(function(item
)
30 assert_throws(new TypeError(), function()
32 sourceBuffer
.remove(item
, 2);
37 }, "Test remove with non-finite start.");
39 mediasource_test(function(test
, mediaElement
, mediaSource
)
41 var sourceBuffer
= mediaSource
.addSourceBuffer(MediaSourceUtil
.AUDIO_VIDEO_TYPE
);
43 mediaSource
.duration
= 10;
45 assert_throws("InvalidAccessError", function()
47 sourceBuffer
.remove(11, 12);
51 }, "Test remove with a start beyond the duration.");
53 mediasource_test(function(test
, mediaElement
, mediaSource
)
55 var sourceBuffer
= mediaSource
.addSourceBuffer(MediaSourceUtil
.AUDIO_VIDEO_TYPE
);
57 mediaSource
.duration
= 10;
59 assert_throws("InvalidAccessError", function()
61 sourceBuffer
.remove(2, 1);
65 }, "Test remove with a start larger than the end.");
68 mediasource_test(function(test
, mediaElement
, mediaSource
)
70 var sourceBuffer
= mediaSource
.addSourceBuffer(MediaSourceUtil
.AUDIO_VIDEO_TYPE
);
72 assert_throws("InvalidAccessError", function()
74 sourceBuffer
.remove(0, Number
.NEGATIVE_INFINITY
);
78 }, "Test remove with a NEGATIVE_INFINITY end.");
80 mediasource_test(function(test
, mediaElement
, mediaSource
)
82 var sourceBuffer
= mediaSource
.addSourceBuffer(MediaSourceUtil
.AUDIO_VIDEO_TYPE
);
84 assert_throws("InvalidAccessError", function()
86 sourceBuffer
.remove(0, Number
.NaN
);
90 }, "Test remove with a NaN end.");
92 mediasource_test(function(test
, mediaElement
, mediaSource
)
94 var sourceBuffer
= mediaSource
.addSourceBuffer(MediaSourceUtil
.AUDIO_VIDEO_TYPE
);
96 mediaSource
.duration
= 10;
98 mediaSource
.removeSourceBuffer(sourceBuffer
);
100 assert_throws("InvalidStateError", function()
102 sourceBuffer
.remove(1, 2);
106 }, "Test remove after SourceBuffer removed from mediaSource.");
109 mediasource_test(function(test
, mediaElement
, mediaSource
)
111 var sourceBuffer
= mediaSource
.addSourceBuffer(MediaSourceUtil
.AUDIO_VIDEO_TYPE
);
113 mediaSource
.duration
= 10;
115 test
.expectEvent(sourceBuffer
, "updatestart");
116 test
.expectEvent(sourceBuffer
, "update");
117 test
.expectEvent(sourceBuffer
, "updateend");
118 sourceBuffer
.remove(1, 2);
120 assert_true(sourceBuffer
.updating
, "updating");
122 assert_throws("InvalidStateError", function()
124 sourceBuffer
.remove(3, 4);
127 test
.waitForExpectedEvents(function()
131 }, "Test remove while update pending.");
133 mediasource_test(function(test
, mediaElement
, mediaSource
)
135 var sourceBuffer
= mediaSource
.addSourceBuffer(MediaSourceUtil
.AUDIO_VIDEO_TYPE
);
137 mediaSource
.duration
= 10;
139 test
.expectEvent(sourceBuffer
, "updatestart");
140 test
.expectEvent(sourceBuffer
, "abort");
141 test
.expectEvent(sourceBuffer
, "updateend");
142 sourceBuffer
.remove(1, 2);
144 assert_true(sourceBuffer
.updating
, "updating");
146 sourceBuffer
.abort();
148 assert_false(sourceBuffer
.updating
, "updating");
150 test
.waitForExpectedEvents(function()
154 }, "Test aborting a remove operation.");
156 mediasource_testafterdataloaded(function(test
, mediaElement
, mediaSource
, segmentInfo
, sourceBuffer
, mediaData
)
158 sourceBuffer
.appendBuffer(mediaData
);
160 test
.expectEvent(sourceBuffer
, "updatestart");
161 test
.expectEvent(sourceBuffer
, "update");
162 test
.expectEvent(sourceBuffer
, "updateend");
164 test
.waitForExpectedEvents(function()
166 assert_less_than(mediaSource
.duration
, 10)
168 mediaSource
.duration
= 10;
170 sourceBuffer
.remove(mediaSource
.duration
, mediaSource
.duration
+ 2);
172 assert_true(sourceBuffer
.updating
, "updating");
173 test
.expectEvent(sourceBuffer
, "updatestart");
174 test
.expectEvent(sourceBuffer
, "update");
175 test
.expectEvent(sourceBuffer
, "updateend");
178 test
.waitForExpectedEvents(function()
183 }, "Test remove with a start at the duration.");
185 mediasource_testafterdataloaded(function(test
, mediaElement
, mediaSource
, segmentInfo
, sourceBuffer
, mediaData
)
187 test
.expectEvent(sourceBuffer
, "updatestart");
188 test
.expectEvent(sourceBuffer
, "update");
189 test
.expectEvent(sourceBuffer
, "updateend");
190 sourceBuffer
.appendBuffer(mediaData
);
192 test
.waitForExpectedEvents(function()
194 mediaSource
.endOfStream();
196 assert_equals(mediaSource
.readyState
, "ended");
198 test
.expectEvent(sourceBuffer
, "updatestart");
199 test
.expectEvent(sourceBuffer
, "update");
200 test
.expectEvent(sourceBuffer
, "updateend");
201 sourceBuffer
.remove(1, 2);
203 assert_true(sourceBuffer
.updating
, "updating");
204 assert_equals(mediaSource
.readyState
, "open");
207 test
.waitForExpectedEvents(function()
209 assert_false(sourceBuffer
.updating
, "updating");
212 }, "Test remove transitioning readyState from 'ended' to 'open'.");
214 function removeAppendedDataTests(callback
, description
)
216 mediasource_testafterdataloaded(function(test
, mediaElement
, mediaSource
, segmentInfo
, sourceBuffer
, mediaData
)
218 test
.expectEvent(sourceBuffer
, "updatestart");
219 test
.expectEvent(sourceBuffer
, "update");
220 test
.expectEvent(sourceBuffer
, "updateend");
221 sourceBuffer
.appendBuffer(mediaData
);
223 test
.waitForExpectedEvents(function()
225 var bufferedRangeEnd
= segmentInfo
.bufferedRangeEndBeforeEndOfStream
.toFixed(3);
226 var subType
= MediaSourceUtil
.getSubType(segmentInfo
.type
);
228 assertBufferedEquals(sourceBuffer
, "{ [0.000, " + bufferedRangeEnd
+ ") }", "Initial buffered range.");
229 callback(test
, sourceBuffer
, bufferedRangeEnd
, subType
);
234 function removeAndCheckBufferedRanges(test
, sourceBuffer
, start
, end
, expected
)
236 test
.expectEvent(sourceBuffer
, "updatestart");
237 test
.expectEvent(sourceBuffer
, "update");
238 test
.expectEvent(sourceBuffer
, "updateend");
239 sourceBuffer
.remove(start
, end
);
241 test
.waitForExpectedEvents(function()
243 assertBufferedEquals(sourceBuffer
, expected
, "Buffered ranges after remove().");
248 removeAppendedDataTests(function(test
, sourceBuffer
, bufferedRangeEnd
, subType
)
250 removeAndCheckBufferedRanges(test
, sourceBuffer
, 0, Number
.POSITIVE_INFINITY
, "{ }");
251 }, "Test removing all appended data.");
253 removeAppendedDataTests(function(test
, sourceBuffer
, bufferedRangeEnd
, subType
)
256 webm
: ("{ [3.187, " + bufferedRangeEnd
+ ") }"),
257 mp4
: ("{ [3.021, " + bufferedRangeEnd
+ ") }"),
260 // Note: Range doesn't start exactly at the end of the remove range because there isn't
261 // a keyframe there. The resulting range starts at the first keyframe >= the end time.
262 removeAndCheckBufferedRanges(test
, sourceBuffer
, 0, 3, expectations
[subType
]);
263 }, "Test removing beginning of appended data.");
265 removeAppendedDataTests(function(test
, sourceBuffer
, bufferedRangeEnd
, subType
)
268 webm
: ("{ [0.000, 1.012) [3.187, " + bufferedRangeEnd
+ ") }"),
269 mp4
: ("{ [0.000, 1.022) [3.021, " + bufferedRangeEnd
+ ") }"),
272 // Note: The first resulting range ends slightly after start because the removal algorithm only removes
273 // frames with a timestamp >= the start time. If a frame starts before and ends after the remove() start
274 // timestamp, then it stays in the buffer.
275 removeAndCheckBufferedRanges(test
, sourceBuffer
, 1, 3, expectations
[subType
]);
276 }, "Test removing the middle of appended data.");
278 removeAppendedDataTests(function(test
, sourceBuffer
, bufferedRangeEnd
, subType
)
281 webm
: "{ [0.000, 1.012) }",
282 mp4
: "{ [0.000, 1.022) }",
285 // Using MAX_VALUE rather than POSITIVE_INFINITY here due to lack
286 // of 'unrestricted' on end parameter. (See comment in SourceBuffer.idl)
287 removeAndCheckBufferedRanges(test
, sourceBuffer
, 1, Number
.MAX_VALUE
, expectations
[subType
]);
288 }, "Test removing the end of appended data.");