Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 <!doctype HTML> | 1 <!DOCTYPE html> |
| 2 <html> | 2 <title>Test to make sure a "source" moved after the media element begins process ing is handled correctly.</title> |
| 3 <head> | 3 <script src="../resources/testharness.js"></script> |
| 4 <title>moving <source> element test</title> | 4 <script src="../resources/testharnessreport.js"></script> |
| 5 <!-- TODO(foolip): Convert test to testharness.js. crbug.com/588956 | 5 <script src="media-file.js"></script> |
| 6 (Please avoid writing new tests using video-test.js) --> | 6 <script> |
| 7 <script src=video-test.js></script> | 7 for(var i = 0; i < 7; i++) { |
| 8 <script src=media-file.js></script> | 8 async_test(function(t) { |
| 9 <script> | 9 var testInfo = [ |
|
fs
2016/07/05 13:33:04
Optionally, this could be written using forEach:
Srirama
2016/07/05 14:55:28
You mean to avoid the for loop above? Problem is t
fs
2016/07/05 15:03:35
It's possible that "this" could be used since we'r
| |
| 10 { func : moveToEnd, errorCount : 0, moved : null, iteration : 1}, | |
| 11 { func : moveToEnd, errorCount : 0, moved : null, iteration : 2}, | |
| 12 { func : moveToEnd, errorCount : 0, moved : null, iteration : 3}, | |
| 13 { func : moveEarlier, errorCount : 0, moved : null, iteration : 1 }, | |
| 14 { func : moveEarlier, errorCount : 0, moved : null, iteration : 2 }, | |
| 15 { func : moveEarlier, errorCount : 0, moved : null, iteration : 3 }, | |
| 16 { func : moveEarlier, errorCount : 0, moved : null, iteration : 4 } | |
|
fs
2016/07/05 13:33:04
I wonder if this test would be slighty easier to c
Srirama
2016/07/05 14:55:28
Done. Looks much better now, thanks.
| |
| 17 ]; | |
| 10 | 18 |
| 11 var testInfo = | 19 var test = testInfo[i]; |
| 12 { | 20 var video = document.createElement("video"); |
| 13 current : -1, | |
| 14 tests : | |
| 15 [ | |
| 16 { fcn : moveToEnd, errorCount : 0, moved : null, done : fals e, iteration : 1}, | |
| 17 { fcn : moveToEnd, errorCount : 0, moved : null, done : fals e, iteration : 2}, | |
| 18 { fcn : moveToEnd, errorCount : 0, moved : null, done : fals e, iteration : 3}, | |
| 19 { fcn : moveEarlier, errorCount : 0, moved : null, iteration : 1 }, | |
| 20 { fcn : moveEarlier, errorCount : 0, moved : null, iteration : 2 }, | |
| 21 { fcn : moveEarlier, errorCount : 0, moved : null, iteration : 3 }, | |
| 22 { fcn : moveEarlier, errorCount : 0, moved : null, iteration : 4 } | |
| 23 ] | |
| 24 }; | |
| 25 | 21 |
| 26 function findCurrentSourceElement() | 22 // Add a bunch of source elements with bogus urls because we want to rea rrange |
| 27 { | 23 // elements after the media engine begins processing sources, and we can 't predict |
| 28 var sources = video.getElementsByTagName('source'); | 24 // the delay between when the media element fires an "error" event and o ur handler |
| 29 var currentSrc = video.currentSrc; | 25 // is called, but we need to guarantee that there are <source> elements that |
| 30 var ndx; | 26 // haven't been processed when we run the test. |
| 31 for (ndx = 0; ndx < sources.length; ++ndx) { | 27 for (var index = 1; index <= 10; index++) |
| 32 if (sources[ndx].src == currentSrc) | 28 addSource(index); |
| 29 | |
| 30 function addSource(index) { | |
| 31 var source = document.createElement("source"); | |
| 32 source.src = findMediaFile("video", index + "-" + Date.now()); | |
| 33 source.type = mimeTypeForExtension(source.src.split(".").pop()); | |
| 34 video.appendChild(source); | |
| 35 | |
| 36 source.onerror = t.step_func(function(event) { | |
| 37 test.func(event); | |
| 38 }); | |
| 39 } | |
| 40 | |
| 41 function findCurrentSourceElement() { | |
| 42 var sources = video.childNodes; | |
| 43 var currentSrc = video.currentSrc; | |
| 44 var index; | |
| 45 for (index = 0; index < sources.length; ++index) { | |
| 46 if (sources[index].src == currentSrc) | |
| 47 break; | |
| 48 } | |
| 49 | |
| 50 assert_less_than(index, sources.length, currentSrc + " not found in <source> list"); | |
| 51 return sources[index]; | |
| 52 } | |
| 53 | |
| 54 function moveEarlier(event) { | |
| 55 switch (++test.errorCount) { | |
| 56 case 1: | |
| 57 // Do nothing on the first error event | |
| 58 break; | |
| 59 | |
| 60 case 2: | |
| 61 var current = findCurrentSourceElement(); | |
| 62 switch (test.iteration) | |
| 63 { | |
| 64 case 1: | |
| 65 // Moving current "source" element to beginning of list, it should not be processed again. | |
| 66 test.moved = video.removeChild(current); | |
| 67 break; | |
| 68 case 2: | |
| 69 // Moving next "source" element to beginning of list, it should never processed. | |
| 70 test.moved = video.removeChild(current.nextSibling); | |
| 71 break; | |
| 72 case 3: | |
| 73 // span inserted after current "source" element before i t is removed, processing should proceed normally. | |
| 74 var span = document.createElement("span") | |
| 75 span.appendChild(document.createTextNode("Your browser d oesn't support HTML5 video!")); | |
| 76 video.insertBefore(span, current.nextSibling); | |
| 77 test.moved = video.removeChild(current); | |
| 78 break; | |
| 79 case 4: | |
| 80 // span inserted after next "source" element before it i s removed, processing should proceed normally. | |
| 81 var span = document.createElement("span") | |
| 82 span.appendChild(document.createTextNode("Your browser d oesn't support HTML5 video!")); | |
| 83 video.insertBefore(span, current.nextSibling.nextSibling ); | |
| 84 test.moved = video.removeChild(current.nextSibling); | |
| 85 break; | |
| 86 default: | |
| 87 assert_unreached("Malformed test data!"); | |
| 33 break; | 88 break; |
| 34 } | 89 } |
| 35 if (ndx >= sources.length) { | 90 |
| 36 failTest(currentSrc + " not found in <source> list"); | 91 assert_not_equals(test.moved, null); |
| 37 return null; | 92 video.insertBefore(test.moved, video.firstChild); |
| 38 } | 93 break; |
| 39 return sources[ndx]; | 94 |
| 95 default: | |
| 96 // We should never get an error for the element we moved. | |
| 97 assert_not_equals(event.target, test.moved); | |
| 98 if (event.target.nextSibling == null) | |
| 99 t.done(); | |
| 100 break; | |
| 40 } | 101 } |
| 102 } | |
| 41 | 103 |
| 42 function moveEarlier(test, event) | 104 function moveToEnd(event) { |
| 43 { | 105 switch (++test.errorCount) { |
| 44 if (test.done) | 106 case 1: |
| 45 return; | 107 // Do nothing on the first error event |
| 108 break; | |
| 46 | 109 |
| 47 switch (++test.errorCount) | 110 case 2: |
| 111 var current = findCurrentSourceElement(); | |
| 112 switch (test.iteration) | |
| 48 { | 113 { |
| 49 case 1: | 114 case 1: |
| 50 // Do nothing on the first error event | 115 // Moving previous "source" element to end of list, it s hould be processed again. |
| 116 test.moved = video.removeChild(current.previousSibling); | |
| 51 break; | 117 break; |
| 52 | |
| 53 case 2: | 118 case 2: |
| 54 var current = findCurrentSourceElement(); | 119 // Moving current "source" element, it should be process ed again. |
| 55 switch (test.iteration) | 120 test.moved = video.removeChild(current); |
| 56 { | |
| 57 case 1: | |
| 58 consoleWrite("Moving <b>current<" + "/b> <sou rce> element to beginning of list, it should not be processed again."); | |
| 59 test.moved = video.removeChild(current); | |
| 60 break; | |
| 61 case 2: | |
| 62 consoleWrite("Moving <b>next<" + "/b> <source > element to beginning of list, it should never processed."); | |
| 63 test.moved = video.removeChild(current.nextSibli ng); | |
| 64 break; | |
| 65 case 3: | |
| 66 consoleWrite("<span> inserted after <b>cur rent<" + "/b> <source> element before it is removed, processing should pro ceed normally."); | |
| 67 var span = document.createElement("span") | |
| 68 span.appendChild(document.createTextNode("Your b rowser doesn't support HTML5 video!")); | |
| 69 video.insertBefore(span, current.nextSibling); | |
| 70 test.moved = video.removeChild(current); | |
| 71 break; | |
| 72 case 4: | |
| 73 consoleWrite("<span> inserted after <b>nex t<" + "/b> <source> element before it is removed, processing should procee d normally."); | |
| 74 var span = document.createElement("span") | |
| 75 span.appendChild(document.createTextNode("Your b rowser doesn't support HTML5 video!")); | |
| 76 video.insertBefore(span, current.nextSibling.nex tSibling); | |
| 77 test.moved = video.removeChild(current.nextSibli ng); | |
| 78 break; | |
| 79 default: | |
| 80 failTest("Malformed test data!"); | |
| 81 break; | |
| 82 } | |
| 83 | |
| 84 testExpected(test.moved, null, '!='); | |
| 85 video.insertBefore(test.moved, video.firstChild); | |
| 86 break; | 121 break; |
| 87 | 122 case 3: |
| 123 // Moving next "source" element, it should be processed again. | |
| 124 test.moved = video.removeChild(current.nextSibling); | |
| 125 break; | |
| 88 default: | 126 default: |
| 89 // We should never get an error for the element we moved . | 127 assert_unreached("Malformed test data!"); |
| 90 if (event.target == test.moved) { | |
| 91 failTest("Error fired for <source> moved to be ginning of list."); | |
| 92 test.done = true; | |
| 93 return; | |
| 94 } else if (!event.target.nextSibling) { | |
| 95 logResult(true, "<source> moved was not proces sed"); | |
| 96 setTimeout(configureNextTest, 100); | |
| 97 } | |
| 98 break; | 128 break; |
| 99 } | 129 } |
| 130 | |
| 131 assert_not_equals(test.moved, null); | |
| 132 video.appendChild(test.moved); | |
| 133 break; | |
| 134 | |
| 135 default: | |
| 136 assert_true(event.target == test.moved || event.target.nextSibli ng != null); | |
| 137 if (event.target == test.moved) | |
| 138 t.done(); | |
| 139 break; | |
| 100 } | 140 } |
| 101 | 141 } |
| 102 function moveToEnd(test, event) | 142 }); |
| 103 { | 143 } |
| 104 switch (++test.errorCount) | 144 </script> |
| 105 { | |
| 106 case 1: | |
| 107 // Do nothing on the first error event | |
| 108 break; | |
| 109 | |
| 110 case 2: | |
| 111 var current = findCurrentSourceElement(); | |
| 112 switch (test.iteration) | |
| 113 { | |
| 114 case 1: | |
| 115 consoleWrite("Moving <b>previous<" + "/b> <so urce> element to end of list, it should be processed again."); | |
| 116 test.moved = video.removeChild(current.previousS ibling); | |
| 117 break; | |
| 118 case 2: | |
| 119 consoleWrite("Moving <b>current<" + "/b> <sou rce> element, it should be processed again."); | |
| 120 test.moved = video.removeChild(current); | |
| 121 break; | |
| 122 case 3: | |
| 123 consoleWrite("Moving <b>next<" + "/b> <source > element, it should be processed again."); | |
| 124 test.moved = video.removeChild(current.nextSibli ng); | |
| 125 break; | |
| 126 default: | |
| 127 failTest("Malformed test data!"); | |
| 128 break; | |
| 129 } | |
| 130 | |
| 131 testExpected(test.moved, null, '!='); | |
| 132 video.appendChild(test.moved); | |
| 133 break; | |
| 134 | |
| 135 default: | |
| 136 if (event.target == test.moved) { | |
| 137 logResult(true, "<source> moved was processed a second time."); | |
| 138 setTimeout(configureNextTest, 100); | |
| 139 } else if (!event.target.nextSibling) { | |
| 140 // We should never reach the end of the source list since the tests stops | |
| 141 // when the error fires for the moved element. | |
| 142 failTest("Error never fired for <source> moved !"); | |
| 143 } | |
| 144 break; | |
| 145 } | |
| 146 } | |
| 147 | |
| 148 function runOneTest(evt) | |
| 149 { | |
| 150 var test = testInfo.tests[testInfo.current]; | |
| 151 test.fcn(test, evt); | |
| 152 } | |
| 153 | |
| 154 function addSource(index) | |
| 155 { | |
| 156 var source = document.createElement('source'); | |
| 157 source.src = findMediaFile("video", index + "-" + Date.now()); | |
| 158 source.type = mimeTypeForExtension(source.src.split('.').pop()); | |
| 159 video.appendChild(source); | |
| 160 } | |
| 161 | |
| 162 function runNextTest() | |
| 163 { | |
| 164 consoleWrite(""); | |
| 165 if (++testInfo.current >= testInfo.tests.length) { | |
| 166 consoleWrite("PASS<br>"); | |
| 167 endTest(); | |
| 168 return; | |
| 169 } | |
| 170 | |
| 171 testInfo.errorCount = 0; | |
| 172 video = mediaElement = document.createElement('video'); | |
| 173 document.body.appendChild(video); | |
| 174 | |
| 175 // Add a bunch of source elements with bogus urls because we wan t to rearrange elements | |
| 176 // after the media engine begins processing sources, and we can' t predict the delay | |
| 177 // between when the media element fires an 'error' event and our handler is called, | |
| 178 // but we need to guarantee that there are <source> elements tha t haven't been processed | |
| 179 // when we run the test. | |
| 180 for (var ndx = 1; ndx <= 10; ndx++) | |
| 181 addSource(ndx); | |
| 182 } | |
| 183 | |
| 184 function configureNextTest() | |
| 185 { | |
| 186 var videos = document.querySelectorAll('video'); | |
| 187 for (var ndx = 0; ndx < videos.length; ++ndx) | |
| 188 videos[ndx].parentNode.removeChild(videos[ndx]); | |
| 189 video = mediaElement = null; | |
| 190 setTimeout(runNextTest, 100); | |
| 191 } | |
| 192 | |
| 193 function setup() | |
| 194 { | |
| 195 document.addEventListener("error", runOneTest, true); | |
| 196 configureNextTest(); | |
| 197 } | |
| 198 | |
| 199 </script> | |
| 200 </head> | |
| 201 | |
| 202 <body> | |
| 203 <div>Test to make sure a <source> moved after the media element be gins processing | |
| 204 is handled correctly.</div> | |
| 205 <script>setup()</script> | |
| 206 </body> | |
| 207 </html> | |
| OLD | NEW |