test_load.html (6659B)
1 <!DOCTYPE HTML> 2 <html> 3 <!-- 4 https://bugzilla.mozilla.org/show_bug.cgi?id=479859 5 --> 6 <head> 7 <title>Test for Bug 479859</title> 8 <script src="/tests/SimpleTest/SimpleTest.js"></script> 9 <link rel="stylesheet" type="text/css" href="/tests/SimpleTest/test.css"/> 10 <script type="application/javascript" src="manifest.js"></script> 11 </head> 12 <body> 13 <a target="_blank" href="https://bugzilla.mozilla.org/show_bug.cgi?id=479859">Mozilla Bug 479859</a> 14 <p id="display"></p> 15 <div id="content" style="display: none"> 16 17 </div> 18 <pre id="test"> 19 <script type="text/javascript"> 20 21 function log() { 22 //document.getElementById('log').innerHTML += "<p>" + msg + "</p>"; 23 } 24 25 // We don't track: progress, canplay, canplaythrough and stalled events, 26 // as these can be delivered out of order, and/or multiple times. 27 var gEventTypes = [ 'loadstart', 'abort', 'error', 'emptied', 'play', 28 'pause', 'loadedmetadata', 'loadeddata', 'waiting', 'playing', 'seeking', 29 'seeked', 'timeupdate', 'ended', 'ratechange', 'durationchange', 'volumechange' ]; 30 31 var gEventNum = 0; 32 var gTestNum = 0; 33 var gTestFileNum = 0; 34 var gExpectedEvents = null; 35 var gTest = null; 36 var gTestName = "?"; 37 38 function listener(evt) { 39 log('event ' + evt.type); 40 ok(gEventNum < gExpectedEvents.length, gTestName+" - corrent number of events received"); 41 var expected = (gEventNum < gExpectedEvents.length) ? gExpectedEvents[gEventNum] : "NoEvent"; 42 is(evt.type, expected, gTestName+" - events received in order"); 43 gEventNum++; 44 if (gEventNum == gExpectedEvents.length) { 45 setTimeout(nextTest, 0); 46 } 47 } 48 49 function source_error(evt) { 50 log('event source_error'); 51 ok(evt.type == "error", "Should only get error events here"); 52 ok(gEventNum < gExpectedEvents.length, gTestName+" - corrent number of events received"); 53 var expected = (gEventNum < gExpectedEvents.length) ? gExpectedEvents[gEventNum] : "NoEvent"; 54 is("source_error", expected, gTestName+" - events received in order"); 55 gEventNum++; 56 if (gEventNum == gExpectedEvents.length) { 57 setTimeout(nextTest, 0); 58 } 59 } 60 61 var gMedia = null; 62 63 function createMedia(tag) { 64 gMedia = document.createElement(tag); 65 gMedia.preload = "metadata"; 66 for (var i=0; i<gEventTypes.length; i++) { 67 gMedia.addEventListener(gEventTypes[i], listener); 68 } 69 } 70 71 function addSource(src, type) { 72 var s = document.createElement("source"); 73 s.addEventListener("error", source_error); 74 s.src = src; 75 s.type = type; 76 gMedia.appendChild(s); 77 return s; 78 } 79 80 function prependSource(src, type) { 81 var s = document.createElement("source"); 82 s.addEventListener("error", source_error); 83 s.src = src; 84 s.type = type; 85 gMedia.insertBefore(s, gMedia.firstChild); 86 return s; 87 } 88 89 var gTests = [ 90 { 91 // Test 0: adding video to doc, then setting src should load implicitly. 92 create(src) { 93 document.body.appendChild(gMedia); 94 gMedia.src = src; 95 }, 96 expectedEvents: ['loadstart', 'durationchange', 'loadedmetadata', 'loadeddata'] 97 }, { 98 // Test 1: adding video to doc, then adding source. 99 create(src, type) { 100 document.body.appendChild(gMedia); 101 addSource(src, type); 102 }, 103 expectedEvents: ['loadstart', 'durationchange', 'loadedmetadata', 'loadeddata'] 104 },{ 105 // Test 2: video with multiple source, the first of which are bad, we should load the last, 106 // and receive error events for failed loads on the source children. 107 create(src, type) { 108 document.body.appendChild(gMedia); 109 addSource("404a", type); 110 addSource("404b", type); 111 addSource(src, type); 112 }, 113 expectedEvents: ['loadstart', 'source_error', 'source_error', 'durationchange', 'loadedmetadata', 'loadeddata'] 114 }, { 115 // Test 3: video with bad src, good <source>, ensure that <source> aren't used. 116 create(src, type) { 117 gMedia.src = "404a"; 118 addSource(src, type); 119 document.body.appendChild(gMedia); 120 }, 121 expectedEvents: ['loadstart', 'error'] 122 }, { 123 // Test 4: video with only bad source, loading, then adding a good source 124 // - should resume load. 125 create(src, type) { 126 addSource("404a", type); 127 var s2 = addSource("404b", type); 128 s2.addEventListener("error", 129 function() { 130 // Should awaken waiting load, causing successful load. 131 addSource(src, type); 132 }); 133 document.body.appendChild(gMedia); 134 }, 135 expectedEvents: ['loadstart', 'source_error', 'source_error', 'durationchange', 'loadedmetadata', 'loadeddata'] 136 }, { 137 // Test 5: video with only 1 bad source, let it fail to load, then prepend 138 // a good <source> to the video, it shouldn't be selected, because the 139 // "pointer" should be after the last child - the bad source. 140 prepended: false, 141 create(src, type) { 142 var prepended = false; 143 addSource("404a", type); 144 var s2 = addSource("404b", type); 145 s2.addEventListener("error", 146 function() { 147 // Should awaken waiting load, causing successful load. 148 if (!prepended) { 149 prependSource(src, type); 150 prepended = true; 151 } 152 }); 153 document.body.appendChild(gMedia); 154 }, 155 expectedEvents: ['loadstart', 'source_error', 'source_error'] 156 }, { 157 // Test 6: (Bug 1165203) preload="none" then followed by an explicit 158 // call to load() should load metadata 159 create(src, type) { 160 gMedia.preload = "none"; 161 gMedia.src = src; 162 document.body.appendChild(gMedia); 163 addSource(src, type); 164 gMedia.load(); 165 }, 166 expectedEvents: ['emptied', 'loadstart', 'durationchange', 'loadedmetadata', 'loadeddata'] 167 } 168 ]; 169 170 function nextTest() { 171 if (gMedia) { 172 for (var i=0; i<gEventTypes.length; i++) { 173 gMedia.removeEventListener(gEventTypes[i], listener); 174 } 175 removeNodeAndSource(gMedia); 176 gMedia = null; 177 } 178 gEventNum = 0; 179 180 if (gTestNum == gTests.length) { 181 gTestNum = 0; 182 ++gTestFileNum; 183 if (gTestFileNum == gSmallTests.length) { 184 SimpleTest.finish(); 185 return; 186 } 187 } 188 189 var src = gSmallTests[gTestFileNum].name; 190 var type = gSmallTests[gTestFileNum].type; 191 192 var t = gTests[gTestNum]; 193 gTestNum++; 194 195 createMedia(type.match(/^audio\//) ? "audio" : "video"); 196 if (!gMedia.canPlayType(type)) { 197 // Unsupported type, skip to next test 198 nextTest(); 199 return; 200 } 201 202 gTestName = "Test " + src + " " + (gTestNum - 1); 203 log("Starting " + gTestName); 204 gExpectedEvents = t.expectedEvents; 205 206 t.create(src, type); 207 } 208 209 addLoadEvent(nextTest); 210 SimpleTest.waitForExplicitFinish(); 211 212 </script> 213 </pre> 214 215 <div id="log" style="font-size: small"></div> 216 </body> 217 </html>