Bug 845676 - Remove most of the assertion annotations in the content/media mochitests...
[gecko.git] / content / media / test / test_preload_actions.html
blob09d31ce49f3aa0b8c704a2e537949738917af2ec
1 <!DOCTYPE HTML>
2 <html>
3 <!--
4 https://bugzilla.mozilla.org/show_bug.cgi?id=548523
5 -->
6 <head>
7 <title>Test for Bug 548523</title>
8 <script type="application/javascript" src="/tests/SimpleTest/SimpleTest.js"></script>
9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/>
10 <script type="text/javascript" src="manifest.js"></script>
11 <script type="text/javascript" src="use_large_cache.js"></script>
12 </head>
13 <body>
14 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=548523">Mozilla Bug 548523</a>
15 <p id="display"></p>
16 <div id="content" style="display: none">
18 </div>
19 <!-- <button onClick="SimpleTest.finish();">Finish</button> -->
20 <div>Tests complete: <span id="log" style="font-size: small;"></span></div>
21 <pre id="test">
22 <script type="application/javascript">
24 /** Test for Bug 548523 **/
26 var manager = new MediaTestManager;
28 manager.onFinished = function() {
29 is(gotLoadEvent, true, "Should not have delayed the load event indefinitely");
32 var test = getPlayableVideo(gSeekTests);
33 var baseName = test.name;
34 var gTest = test;
35 var bogusSrc = "bogus.duh";
36 var bogusType = "video/bogus";
37 var gotLoadEvent = false;
38 var finished = false;
40 addLoadEvent(function() {gotLoadEvent=true;});
42 function log(m) {
43 var l = document.getElementById("log");
44 l.innerHTML += m;
47 function maybeFinish(v, n) {
48 if (v._finished) {
49 return;
51 v._finished = true;
52 log(n + ",");
53 if (v.parentNode) {
54 v.parentNode.removeChild(v);
56 manager.finished(v.token);
59 function filename(uri) {
60 return uri.substr(uri.lastIndexOf("/")+1);
63 // Every test must have a setup(v) function, and must call maybeFinish() when test is complete.
64 var tests = [
66 // 1. Add preload:none video with src to document. Load should halt at NETWORK_IDLE and HAVE_NOTHING,
67 // after receiving a suspend event. Should not receive loaded events until after we call load().
68 // Note the suspend event is explictly sent by our "stop the load" code, but other tests can't rely
69 // on it for the preload:metadata case, as there can be multiple suspend events when loading metadata.
70 suspend:
71 function(e) {
72 var v = e.target;
73 is(v._gotLoadStart, true, "(1) Must get loadstart.");
74 is(v._gotLoadedMetaData, false, "(1) Must not get loadedmetadata.");
75 is(v.readyState, v.HAVE_NOTHING, "(1) ReadyState must be HAVE_NOTHING");
76 is(v.networkState, v.NETWORK_IDLE, "(1) NetworkState must be NETWORK_IDLE");
77 maybeFinish(v, 1);
80 setup:
81 function(v) {
82 v._gotLoadStart = false;
83 v._gotLoadedMetaData = false;
84 v.preload = "none";
85 v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
86 v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
87 v.addEventListener("suspend", this.suspend, false);
88 v.src = test.name;
89 document.body.appendChild(v); // Causes implicit load, which will be halted due to preload:none.
93 // 2. Add preload:metadata video with src to document. Should halt with NETWORK_IDLE, HAVE_CURRENT_DATA
94 // after suspend event and after loadedmetadata.
95 loadeddata:
96 function(e) {
97 var v = e.target;
98 is(v._gotLoadStart, true, "(2) Must get loadstart.");
99 is(v._gotLoadedMetaData, true, "(2) Must get loadedmetadata.");
100 ok(v.readyState >= v.HAVE_CURRENT_DATA, "(2) ReadyState must be >= HAVE_CURRENT_DATA");
101 is(v.networkState, v.NETWORK_IDLE, "(2) NetworkState must be NETWORK_IDLE");
102 maybeFinish(v, 2);
105 setup:
106 function(v) {
107 v._gotLoadStart = false;
108 v._gotLoadedMetaData = false;
109 v.preload = "metadata";
110 v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
111 v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
112 v.addEventListener("loadeddata", this.loadeddata, false);
113 v.src = test.name;
114 document.body.appendChild(v); // Causes implicit load, which will be halted after
115 // metadata due to preload:metadata.
119 // 3. Add preload:auto to document. Should receive canplaythrough eventually.
120 canplaythrough:
121 function(e) {
122 var v = e.target;
123 is(v._gotLoadStart, true, "(3) Must get loadstart.");
124 is(v._gotLoadedMetaData, true, "(3) Must get loadedmetadata.");
125 maybeFinish(v, 3);
128 setup:
129 function(v) {
130 v._gotLoadStart = false;
131 v._gotLoadedMetaData = false;
132 v.preload = "auto";
133 v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
134 v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
135 v.addEventListener("canplaythrough", this.canplaythrough, false);
136 v.src = test.name; // Causes implicit load.
137 document.body.appendChild(v);
141 // 4. Add preload:none video to document. Call play(), should load then play through.
142 suspend:
143 function(e) {
144 var v = e.target;
145 if (v._gotSuspend) {
146 return; // We can receive multiple suspend events, like the one after download completes.
148 v._gotSuspend = true;
149 is(v._gotLoadStart, true, "(4) Must get loadstart.");
150 is(v._gotLoadedMetaData, false, "(4) Must not get loadedmetadata.");
151 is(v.readyState, v.HAVE_NOTHING, "(4) ReadyState must be HAVE_NOTHING");
152 is(v.networkState, v.NETWORK_IDLE, "(4) NetworkState must be NETWORK_IDLE");
153 v.play(); // Should load and play through.
156 ended:
157 function(e) {
158 ok(true, "(4) Got playback ended");
159 maybeFinish(e.target, 4);
162 setup:
163 function(v) {
164 v._gotLoadStart = false;
165 v._gotLoadedMetaData = false;
166 v._gotSuspend = false;
167 v.preload = "none";
168 v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
169 v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
170 v.addEventListener("suspend", this.suspend, false);
171 v.addEventListener("ended", this.ended, false);
172 v.src = test.name;
173 document.body.appendChild(v);
177 // 5. preload:none video without resource, add to document, will implicitly start a
178 // preload:none load. Add a src, it shouldn't load.
179 suspend:
180 function(e) {
181 var v = e.target;
182 is(v._gotLoadStart, true, "(5) Must get loadstart.");
183 is(v._gotLoadedMetaData, false, "(5) Must not get loadedmetadata.");
184 is(v.readyState, v.HAVE_NOTHING, "(5) ReadyState must be HAVE_NOTHING");
185 is(v.networkState, v.NETWORK_IDLE, "(5) NetworkState must be NETWORK_IDLE");
186 maybeFinish(v, 5);
189 setup:
190 function(v) {
191 v._gotLoadStart = false;
192 v._gotLoadedMetaData = false;
193 v.preload = "none";
194 v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
195 v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
196 v.addEventListener("suspend", this.suspend, false);
197 document.body.appendChild(v); // Causes implicit load, which will be halted due to no resource.
198 v.src = test.name; // Load should start, and halt at preload:none.
202 // 6. preload:none video without resource, add to document, will implicitly start a
203 // preload:none load. Add a source, it shouldn't load.
204 suspend:
205 function(e) {
206 var v = e.target;
207 is(v._gotLoadStart, true, "(6) Must get loadstart.");
208 is(v._gotLoadedMetaData, false, "(6) Must not get loadedmetadata.");
209 is(v.readyState, v.HAVE_NOTHING, "(6) ReadyState must be HAVE_NOTHING");
210 is(v.networkState, v.NETWORK_IDLE, "(6) NetworkState must be NETWORK_IDLE");
211 maybeFinish(v, 6);
214 setup:
215 function(v) {
216 v._gotLoadStart = false;
217 v._gotLoadedMetaData = false;
218 v.preload = "none";
219 v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
220 v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
221 v.addEventListener("suspend", this.suspend, false);
222 document.body.appendChild(v); // Causes implicit load, which will be halted due to no resource.
223 var s = document.createElement("source");
224 s.src = test.name;
225 s.type = test.type;
226 v.appendChild(s); // Load should start, and halt at preload:none.
230 // 7. create a preload:none document with multiple sources, the first of which is invalid.
231 // Add to document, then play. It should load and play through the second source.
232 suspend:
233 function(e) {
234 var v = e.target;
235 if (v._gotSuspend)
236 return; // We can receive multiple suspend events, like the one after download completes.
237 v._gotSuspend = true;
238 is(v._gotLoadStart, true, "(7) Must get loadstart.");
239 is(v._gotLoadedMetaData, false, "(7) Must not get loadedmetadata.");
240 is(v.readyState, v.HAVE_NOTHING, "(7) ReadyState must be HAVE_NOTHING");
241 is(v.networkState, v.NETWORK_IDLE, "(7) NetworkState must be NETWORK_IDLE");
242 v.play(); // Should load and play through.
245 ended:
246 function(e) {
247 ok(true, "(7) Got playback ended");
248 var v = e.target;
249 is(v._gotErrorEvent, true, "(7) Should get error event from first source load failure");
250 maybeFinish(v, 7);
253 setup:
254 function(v) {
255 v._gotLoadStart = false;
256 v._gotLoadedMetaData = false;
257 v.preload = "none";
258 v._gotErrorEvent = false;
259 v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
260 v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
261 v.addEventListener("suspend", this.suspend, false);
262 v.addEventListener("ended", this.ended, false);
263 var s1 = document.createElement("source");
264 s1.src = "not-a-real-file.404"
265 s1.type = test.type;
266 s1.addEventListener("error", function(e){v._gotErrorEvent = true;}, false);
267 v.appendChild(s1);
268 var s2 = document.createElement("source");
269 s2.src = test.name;
270 s2.type = test.type;
271 v.appendChild(s2);
272 document.body.appendChild(v); // Causes implicit load, which will be halt at preload:none on the second resource.
276 // 8. Change preload value from none to metadata should cause metadata to be loaded.
277 loadeddata:
278 function(e) {
279 var v = e.target;
280 is(v._gotLoadedMetaData, true, "(8) Must get loadedmetadata.");
281 ok(v.readyState >= v.HAVE_CURRENT_DATA, "(8) ReadyState must be >= HAVE_CURRENT_DATA on suspend.");
282 is(v.networkState, v.NETWORK_IDLE, "(8) NetworkState must be NETWORK_IDLE when load is halted");
283 maybeFinish(v, 8);
286 setup:
287 function(v) {
288 v._gotLoadedMetaData = false;
289 v.preload = "none";
290 v.addEventListener("loadstart", function(e){v.preload = "metadata";}, false);
291 v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
292 v.addEventListener("loadeddata", this.loadeddata, false);
293 v.src = test.name; // Causes implicit load.
294 document.body.appendChild(v);
298 // 9. Change preload value from metadata to auto should cause entire media to be loaded.
299 // For some reason we don't always receive the canplaythrough event, particuarly on this test.
300 // We've disabled this test until bug 568402 is fixed.
301 canplaythrough:
302 function(e) {
303 var v = e.target;
304 is(v._gotLoadStart, true, "(9) Must get loadstart.");
305 is(v._gotLoadedMetaData, true, "(9) Must get loadedmetadata.");
306 maybeFinish(v, 9);
309 setup:
310 function(v) {
311 v._gotLoadStart = false;
312 v._gotLoadedMetaData = false;
313 v.preload = "metadata";
314 v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
315 v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
316 v.addEventListener("loadeddata", function(){v.preload = "auto"}, false);
317 v.addEventListener("canplaythrough", this.canplaythrough, false);
318 v.src = test.name; // Causes implicit load.
319 document.body.appendChild(v);
321 },*/
323 // 10. Change preload value from none to auto should cause entire media to be loaded.
324 canplaythrough:
325 function(e) {
326 var v = e.target;
327 is(v._gotLoadedMetaData, true, "(10) Must get loadedmetadata.");
328 maybeFinish(v, 10);
331 setup:
332 function(v) {
333 v._gotLoadedMetaData = false;
334 v.preload = "none";
335 v.addEventListener("loadstart", function(e){v.preload = "auto";}, false);
336 v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
337 v.addEventListener("canplaythrough", this.canplaythrough, false);
338 v.src = test.name; // Causes implicit load.
339 document.body.appendChild(v);
343 // 11. Change preload value from none to metadata should cause metadata to load.
344 loadeddata:
345 function(e) {
346 var v = e.target;
347 is(v._gotLoadedMetaData, true, "(11) Must get loadedmetadata.");
348 ok(v.readyState >= v.HAVE_CURRENT_DATA, "(11) ReadyState must be >= HAVE_CURRENT_DATA.");
349 is(v.networkState, v.NETWORK_IDLE, "(11) NetworkState must be NETWORK_IDLE.");
350 maybeFinish(v, 11);
353 setup:
354 function(v) {
355 v._gotLoadedMetaData = false;
356 v.preload = "none";
357 v.addEventListener("loadstart", function(e){v.preload = "metadata";}, false);
358 v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
359 v.addEventListener("loadeddata", this.loadeddata, false);
360 v.src = test.name; // Causes implicit load.
361 document.body.appendChild(v);
365 // 12. Change preload value from auto to metadata after load started,
366 // should still do full load, should not halt after metadata only.
367 canplaythrough:
368 function(e) {
369 var v = e.target;
370 is(v._gotLoadedMetaData, true, "(12) Must get loadedmetadata.");
371 is(v._gotLoadStart, true, "(12) Must get loadstart.");
372 maybeFinish(v, 12);
375 setup:
376 function(v) {
377 v._gotLoadStart = false;
378 v._gotLoadedMetaData = false;
379 v.preload = "auto";
380 v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
381 v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
382 v.addEventListener("canplaythrough", this.canplaythrough, false);
383 v.src = test.name; // Causes implicit load.
384 document.body.appendChild(v);
385 v.preload = "metadata";
389 // 13. Change preload value from auto to none after specifying a src
390 // should load according to preload none, no buffering should have taken place
391 suspend:
392 function(e) {
393 var v = e.target;
394 is(v._gotLoadStart, true, "(13) Must get loadstart.");
395 is(v._gotLoadedMetaData, false, "(13) Must not get loadedmetadata.");
396 is(v.readyState, v.HAVE_NOTHING, "(13) ReadyState must be HAVE_NOTHING");
397 is(v.networkState, v.NETWORK_IDLE, "(13) NetworkState must be NETWORK_IDLE");
398 maybeFinish(v, 13);
401 setup:
402 function(v) {
403 v._gotLoadStart = false;
404 v._gotLoadedMetaData = false;
405 v.preload = "auto";
406 v.src = test.name;
407 v.preload = "none";
408 v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
409 v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
410 v.addEventListener("suspend", this.suspend, false);
411 document.body.appendChild(v); // Causes implicit load, should load according to preload none
412 var s = document.createElement("source");
416 // 14. Add preload:metadata video with src to document. Play(), should play through.
417 loadeddata:
418 function(e) {
419 var v = e.target;
420 is(v._gotLoadStart, true, "(14) Must get loadstart.");
421 is(v._gotLoadedMetaData, true, "(14) Must get loadedmetadata.");
422 ok(v.readyState >= v.HAVE_CURRENT_DATA, "(14) ReadyState must be >= HAVE_CURRENT_DATA");
423 is(v.networkState, v.NETWORK_IDLE, "(14) NetworkState must be NETWORK_IDLE");
424 v.play();
427 ended:
428 function(e) {
429 ok(true, "(14) Got playback ended");
430 var v = e.target;
431 maybeFinish(v, 14);
434 setup:
435 function(v) {
436 v._gotLoadStart = false;
437 v._gotLoadedMetaData = false;
438 v.preload = "metadata";
439 v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
440 v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
441 v.addEventListener("ended", this.ended, false);
442 v.addEventListener("loadeddata", this.loadeddata, false);
443 v.src = test.name;
444 document.body.appendChild(v); // Causes implicit load, which will be halted after
445 // metadata due to preload:metadata.
449 // 15. Autoplay should override preload:none.
450 ended:
451 function(e) {
452 ok(true, "(15) Got playback ended.");
453 var v = e.target;
454 maybeFinish(v, 15);
457 setup:
458 function(v) {
459 v._gotLoadStart = false;
460 v._gotLoadedMetaData = false;
461 v.preload = "none";
462 v.autoplay = true;
463 v.addEventListener("loadstart", function(e){v._gotLoadStart = true;}, false);
464 v.addEventListener("loadedmetadata", function(e){v._gotLoadedMetaData = true;}, false);
465 v.addEventListener("ended", this.ended, false);
466 v.src = test.name; // Causes implicit load.
467 document.body.appendChild(v);
471 // 16. Autoplay should override preload:metadata.
472 ended:
473 function(e) {
474 ok(true, "(16) Got playback ended.");
475 var v = e.target;
476 maybeFinish(v, 16);
479 setup:
480 function(v) {
481 v.preload = "metadata";
482 v.autoplay = true;
483 v.addEventListener("ended", this.ended, false);
484 v.src = test.name; // Causes implicit load.
485 document.body.appendChild(v);
489 // 17. On a preload:none video, adding autoplay should disable preload none, i.e. don't break autoplay!
490 ended:
491 function(e) {
492 ok(true, "(17) Got playback ended.");
493 var v = e.target;
494 maybeFinish(v, 17);
497 setup:
498 function(v) {
499 v.addEventListener("ended", this.ended, false);
500 v.preload = "none";
501 document.body.appendChild(v); // Causes implicit load, which will be halted due to preload:none.
502 v.autoplay = true;
503 v.src = test.name;
507 // 18. On a preload='none' video, call play() before load algorithms's sync
508 // has run, the play() call should override preload='none'.
509 ended:
510 function(e) {
511 ok(true, "(18) Got playback ended.");
512 var v = e.target;
513 maybeFinish(v, 18);
516 setup:
517 function(v) {
518 v.addEventListener("ended", this.ended, false);
519 v.preload = "none";
520 v.src = test.name; // Schedules async section to continue load algorithm.
521 document.body.appendChild(v);
522 v.play(); // Should cause preload:none to be overridden.
526 // 19. Set preload='auto' on first video source then switching preload='none' and swapping the video source to another.
527 // The second video should not start playing as it's preload state has been changed to 'none' from 'auto'
528 loadedmetadata: function(e) {
529 var v = e.target;
530 is(v.preload === "auto", true, "(19) preload is initially auto");
531 setTimeout(function() {
532 // set preload state to none and switch video sources
533 v.preload="none";
534 v.src = test.name + "?asdf";
535 setTimeout(function() {
536 is(v.readyState === 0, true, "(19) no buffering has taken place");
537 maybeFinish(v, 19);
538 }, 2000);
539 }, 2000);
543 setup:
544 function(v) {
545 var that = this;
546 v.preload = "auto";
547 v.src = test.name;
548 // add a listener for when the video has loaded, so we know preload auto has worked
549 v.addEventListener( "loadedmetadata", this.loadedmetadata, false);
550 document.body.appendChild(v);
555 var iterationCount = 0;
556 function startTest(test, token) {
557 if (test == tests[0]) {
558 ++iterationCount;
560 if (iterationCount == 2) {
561 // Do this series of tests on logically different resources
562 test.name = baseName + "?" + Math.floor(Math.random()*100000);
564 var v = document.createElement("video");
565 v.token = token;
566 test.setup(v);
567 manager.started(token);
570 var twiceTests = tests.concat(tests);
571 manager.runTests(twiceTests, startTest);
573 </script>
574 </pre>
575 </body>
576 </html>