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 function createMediaXHR() {
13 var mediaURL
= MediaSourceUtil
.SEGMENT_INFO
.url
;
14 var xhr
= new XMLHttpRequest();
15 xhr
.open('GET', mediaURL
, true);
16 xhr
.responseType
= 'legacystream';
18 assert_equals(xhr
.responseType
, "legacystream", "Verify response type was set.");
23 function waitForLoadingState(test
, xhr
, callback
)
25 var eventHandler
= test
.step_func(function(e
)
27 if (e
.target
.readyState
< e
.target
.LOADING
)
29 xhr
.removeEventListener('readystatechange', eventHandler
);
32 xhr
.addEventListener('readystatechange', eventHandler
);
35 function appendStream(test
, sourceBuffer
, callback
)
37 var xhr
= createMediaXHR();
38 test
.failOnEvent(xhr
, 'error');
42 waitForLoadingState(test
, xhr
, function()
44 assert_true(xhr
.response
!= null, "xhr.response is not null");
46 test
.expectEvent(xhr
, "load", "XHR load completed.");
47 test
.expectEvent(xhr
, "loadend", "XHR load ended.");
49 test
.expectEvent(sourceBuffer
, "updatestart", "Append started.");
50 test
.expectEvent(sourceBuffer
, "update", "Append success.");
51 test
.expectEvent(sourceBuffer
, "updateend", "Append ended.");
52 sourceBuffer
.appendStream(xhr
.response
);
54 assert_true(sourceBuffer
.updating
, "updating attribute is true");
56 test
.waitForExpectedEvents(function()
58 assert_false(sourceBuffer
.updating
, "updating attribute is false");
64 function appendStreamTest(callback
, description
)
66 mediasource_test(function(test
, mediaElement
, mediaSource
)
68 var sourceBuffer
= mediaSource
.addSourceBuffer(MediaSourceUtil
.SEGMENT_INFO
.type
);
69 test
.failOnEvent(mediaElement
, 'error');
70 callback(test
, mediaElement
, mediaSource
, sourceBuffer
);
74 appendStreamTest(function(test
, mediaElement
, mediaSource
, sourceBuffer
)
76 test
.expectEvent(mediaElement
, "canplaythrough", "Reached HAVE_ENOUGH_DATA.");
77 appendStream(test
, sourceBuffer
, function() { test
.done(); });
78 }, "Test SourceBuffer.appendStream() event dispatching.");
80 appendStreamTest(function(test
, mediaElement
, mediaSource
, sourceBuffer
)
82 var xhr
= createMediaXHR();
83 test
.failOnEvent(xhr
, 'error');
85 waitForLoadingState(test
, xhr
, function()
87 var xhr2
= createMediaXHR();
89 waitForLoadingState(test
, xhr2
, function()
91 test
.expectEvent(sourceBuffer
, "updatestart", "Append started.");
92 test
.expectEvent(sourceBuffer
, "update", "Append success.");
93 test
.expectEvent(sourceBuffer
, "updateend", "Append ended.");
94 sourceBuffer
.appendStream(xhr
.response
);
96 assert_true(sourceBuffer
.updating
, "updating attribute is true");
98 assert_throws("InvalidStateError",
99 function() { sourceBuffer
.appendStream(xhr2
.response
); },
100 "appendStream() throws an exception because there is a pending append.");
102 assert_true(sourceBuffer
.updating
, "updating attribute is true");
104 test
.waitForExpectedEvents(function()
106 assert_false(sourceBuffer
.updating
, "updating attribute is false");
111 }, "Test SourceBuffer.appendStream() call during a pending appendStream().");
113 appendStreamTest(function(test
, mediaElement
, mediaSource
, sourceBuffer
)
115 var xhr
= createMediaXHR();
116 test
.failOnEvent(xhr
, 'error');
118 waitForLoadingState(test
, xhr
, function()
120 test
.expectEvent(sourceBuffer
, "updatestart", "Append started.");
121 test
.expectEvent(sourceBuffer
, "abort", "Append aborted.");
122 test
.expectEvent(sourceBuffer
, "updateend", "Append ended.");
123 sourceBuffer
.appendStream(xhr
.response
);
125 assert_true(sourceBuffer
.updating
, "updating attribute is true");
127 sourceBuffer
.abort();
129 assert_false(sourceBuffer
.updating
, "updating attribute is false");
131 test
.waitForExpectedEvents(function()
133 assert_false(sourceBuffer
.updating
, "updating attribute is false");
137 }, "Test SourceBuffer.abort() call during a pending appendStream().");
139 appendStreamTest(function(test
, mediaElement
, mediaSource
, sourceBuffer
)
141 appendStream(test
, sourceBuffer
, function()
143 test
.expectEvent(mediaSource
, "sourceended", "MediaSource sourceended event");
144 mediaSource
.endOfStream();
145 assert_equals(mediaSource
.readyState
, "ended", "MediaSource readyState is 'ended'");
147 test
.waitForExpectedEvents(function()
149 assert_equals(mediaSource
.readyState
, "ended", "MediaSource readyState is 'ended'");
151 var xhr2
= createMediaXHR();
153 waitForLoadingState(test
, xhr2
, function()
155 test
.expectEvent(mediaSource
, "sourceopen", "MediaSource sourceopen event");
156 test
.expectEvent(sourceBuffer
, "updatestart", "Append started.");
157 test
.expectEvent(sourceBuffer
, "update", "Append success.");
158 test
.expectEvent(sourceBuffer
, "updateend", "Append ended.");
159 sourceBuffer
.appendStream(xhr2
.response
);
161 assert_equals(mediaSource
.readyState
, "open", "MediaSource readyState is 'open'");
162 assert_true(sourceBuffer
.updating
, "updating attribute is true");
164 test
.waitForExpectedEvents(function()
166 assert_equals(mediaSource
.readyState
, "open", "MediaSource readyState is 'open'");
167 assert_false(sourceBuffer
.updating
, "updating attribute is false");
173 }, "Test SourceBuffer.appendStream() triggering an 'ended' to 'open' transition.");
175 appendStreamTest(function(test
, mediaElement
, mediaSource
, sourceBuffer
)
177 var xhr
= createMediaXHR();
178 test
.failOnEvent(xhr
, 'error');
180 waitForLoadingState(test
, xhr
, function()
182 test
.expectEvent(sourceBuffer
, "updatestart", "Append started.");
183 test
.expectEvent(sourceBuffer
, "abort", "Append aborted.");
184 test
.expectEvent(sourceBuffer
, "updateend", "Append ended.");
185 sourceBuffer
.appendStream(xhr
.response
);
187 assert_true(sourceBuffer
.updating
, "updating attribute is true");
188 assert_equals(mediaSource
.activeSourceBuffers
.length
, 0, "activeSourceBuffers.length");
190 test
.expectEvent(mediaSource
.sourceBuffers
, "removesourcebuffer", "sourceBuffers");
191 mediaSource
.removeSourceBuffer(sourceBuffer
);
193 assert_false(sourceBuffer
.updating
, "updating attribute is false");
195 var xhr2
= createMediaXHR();
196 test
.failOnEvent(xhr2
, 'error');
198 waitForLoadingState(test
, xhr2
, function()
200 assert_throws("InvalidStateError",
201 function() { sourceBuffer
.appendStream(xhr2
.response
); },
202 "appendStream() throws an exception because it isn't attached to the mediaSource anymore.");
204 test
.waitForExpectedEvents(function()
206 assert_false(sourceBuffer
.updating
, "updating attribute is false");
211 }, "Test MediaSource.removeSourceBuffer() call during a pending appendStream().");
213 appendStreamTest(function(test
, mediaElement
, mediaSource
, sourceBuffer
)
215 var xhr
= createMediaXHR();
216 test
.failOnEvent(xhr
, 'error');
218 waitForLoadingState(test
, xhr
, function()
220 test
.expectEvent(sourceBuffer
, "updatestart", "Append started.");
221 test
.expectEvent(sourceBuffer
, "update", "Append success.");
222 test
.expectEvent(sourceBuffer
, "updateend", "Append ended.");
223 sourceBuffer
.appendStream(xhr
.response
);
225 assert_true(sourceBuffer
.updating
, "updating attribute is true");
227 assert_throws("InvalidStateError",
228 function() { mediaSource
.duration
= 1.0; },
229 "set duration throws an exception when updating attribute is true.");
231 test
.waitForExpectedEvents(function()
233 assert_false(sourceBuffer
.updating
, "updating attribute is false");
237 }, "Test setting MediaSource.duration during a pending appendStream() for one of its SourceBuffers.");
239 appendStreamTest(function(test
, mediaElement
, mediaSource
, sourceBuffer
)
241 var xhr
= createMediaXHR();
242 test
.failOnEvent(xhr
, "error");
243 test
.failOnEvent(mediaSource
, "sourceended");
245 waitForLoadingState(test
, xhr
, function()
247 test
.expectEvent(sourceBuffer
, "updatestart", "Append started.");
248 test
.expectEvent(sourceBuffer
, "update", "Append success.");
249 test
.expectEvent(sourceBuffer
, "updateend", "Append ended.");
250 sourceBuffer
.appendStream(xhr
.response
);
252 assert_true(sourceBuffer
.updating
, "updating attribute is true");
254 assert_throws("InvalidStateError",
255 function() { mediaSource
.endOfStream(); },
256 "endOfStream() throws an exception when updating attribute is true.");
258 assert_equals(mediaSource
.readyState
, "open");
260 test
.waitForExpectedEvents(function()
262 assert_false(sourceBuffer
.updating
, "updating attribute is false");
263 assert_equals(mediaSource
.readyState
, "open");
267 }, "Test MediaSource.endOfStream() during a pending appendStream() for one of its SourceBuffers.");
269 appendStreamTest(function(test
, mediaElement
, mediaSource
, sourceBuffer
)
271 var xhr
= createMediaXHR();
272 test
.failOnEvent(xhr
, 'error');
274 waitForLoadingState(test
, xhr
, function()
276 test
.expectEvent(sourceBuffer
, "updatestart", "Append started.");
277 test
.expectEvent(sourceBuffer
, "update", "Append success.");
278 test
.expectEvent(sourceBuffer
, "updateend", "Append ended.");
279 sourceBuffer
.appendStream(xhr
.response
);
281 assert_true(sourceBuffer
.updating
, "updating attribute is true");
283 assert_throws("InvalidStateError",
284 function() { sourceBuffer
.timestampOffset
= 10.0; },
285 "set timestampOffset throws an exception when updating attribute is true.");
287 test
.waitForExpectedEvents(function()
289 assert_false(sourceBuffer
.updating
, "updating attribute is false");
293 }, "Test setting SourceBuffer.timestampOffset during a pending appendStream().");
295 appendStreamTest(function(test
, mediaElement
, mediaSource
, sourceBuffer
)
297 var xhr
= createMediaXHR();
298 test
.failOnEvent(xhr
, 'error');
300 waitForLoadingState(test
, xhr
, function()
302 test
.expectEvent(sourceBuffer
, "updatestart", "Append started.");
303 test
.expectEvent(sourceBuffer
, "update", "Append success.");
304 test
.expectEvent(sourceBuffer
, "updateend", "Append ended.");
305 sourceBuffer
.appendStream(xhr
.response
, 0);
307 assert_true(sourceBuffer
.updating
, "updating attribute is true");
309 test
.waitForExpectedEvents(function()
311 assert_false(sourceBuffer
.updating
, "updating attribute is false");
315 }, "Test appending a Stream with maxSize equal to 0.");
317 appendStreamTest(function(test
, mediaElement
, mediaSource
, sourceBuffer
)
319 var xhr
= createMediaXHR();
320 test
.failOnEvent(xhr
, 'error');
322 waitForLoadingState(test
, xhr
, function()
324 test
.expectEvent(sourceBuffer
, "updatestart", "Append started.");
325 test
.expectEvent(sourceBuffer
, "update", "Append success.");
326 test
.expectEvent(sourceBuffer
, "updateend", "Append ended.");
327 sourceBuffer
.appendStream(xhr
.response
, 10);
329 assert_true(sourceBuffer
.updating
, "updating attribute is true");
331 test
.waitForExpectedEvents(function()
333 assert_false(sourceBuffer
.updating
, "updating attribute is false");
337 }, "Test appending a Stream with maxSize value less than the size of the XHR response.");
339 appendStreamTest(function(test
, mediaElement
, mediaSource
, sourceBuffer
)
341 var xhr
= createMediaXHR();
342 test
.failOnEvent(xhr
, 'error');
344 waitForLoadingState(test
, xhr
, function()
346 test
.expectEvent(sourceBuffer
, "updatestart", "Append started.");
347 test
.expectEvent(sourceBuffer
, "update", "Append success.");
348 test
.expectEvent(sourceBuffer
, "updateend", "Append ended.");
349 sourceBuffer
.appendStream(xhr
.response
, 10 * 1024 * 1024);
351 assert_true(sourceBuffer
.updating
, "updating attribute is true");
353 test
.waitForExpectedEvents(function()
355 assert_false(sourceBuffer
.updating
, "updating attribute is false");
359 }, "Test appending a Stream with maxSize value greater than the size of the XHR response.");
361 appendStreamTest(function(test
, mediaElement
, mediaSource
, sourceBuffer
)
363 var xhr
= createMediaXHR();
364 test
.failOnEvent(xhr
, 'error');
366 waitForLoadingState(test
, xhr
, function()
368 test
.expectEvent(sourceBuffer
, "updatestart", "Append started.");
369 test
.expectEvent(sourceBuffer
, "update", "Append success.");
370 test
.expectEvent(sourceBuffer
, "updateend", "Append ended.");
371 sourceBuffer
.appendStream(xhr
.response
, "test");
373 assert_true(sourceBuffer
.updating
, "updating attribute is true");
375 test
.waitForExpectedEvents(function()
377 assert_false(sourceBuffer
.updating
, "updating attribute is false");
381 }, "Test appending a Stream with an invalid maxSize.");