4 <title>moving <source> element test</title>
5 <script src=video-test.js></script>
6 <script src=media-file.js></script>
14 { fcn : moveToEnd, errorCount : 0, moved : null, done : false, iteration : 1},
15 { fcn : moveToEnd, errorCount : 0, moved : null, done : false, iteration : 2},
16 { fcn : moveToEnd, errorCount : 0, moved : null, done : false, iteration : 3},
17 { fcn : moveEarlier, errorCount : 0, moved : null, iteration : 1 },
18 { fcn : moveEarlier, errorCount : 0, moved : null, iteration : 2 },
19 { fcn : moveEarlier, errorCount : 0, moved : null, iteration : 3 },
20 { fcn : moveEarlier, errorCount : 0, moved : null, iteration : 4 }
24 function findCurrentSourceElement()
26 var sources = video.getElementsByTagName('source');
27 var currentSrc = video.currentSrc;
29 for (ndx = 0; ndx < sources.length; ++ndx) {
30 if (sources[ndx].src == currentSrc)
33 if (ndx >= sources.length) {
34 failTest(currentSrc + " not found in <source> list");
40 function moveEarlier(test, event)
45 switch (++test.errorCount)
48 // Do nothing on the first error event
52 var current = findCurrentSourceElement();
53 switch (test.iteration)
56 consoleWrite("Moving <b>current<" + "/b> <source> element to beginning of list, it should not be processed again.");
57 test.moved = video.removeChild(current);
60 consoleWrite("Moving <b>next<" + "/b> <source> element to beginning of list, it should never processed.");
61 test.moved = video.removeChild(current.nextSibling);
64 consoleWrite("<span> inserted after <b>current<" + "/b> <source> element before it is removed, processing should proceed normally.");
65 var span = document.createElement("span")
66 span.appendChild(document.createTextNode("Your browser doesn't support HTML5 video!"));
67 video.insertBefore(span, current.nextSibling);
68 test.moved = video.removeChild(current);
71 consoleWrite("<span> inserted after <b>next<" + "/b> <source> element before it is removed, processing should proceed normally.");
72 var span = document.createElement("span")
73 span.appendChild(document.createTextNode("Your browser doesn't support HTML5 video!"));
74 video.insertBefore(span, current.nextSibling.nextSibling);
75 test.moved = video.removeChild(current.nextSibling);
78 failTest("Malformed test data!");
82 testExpected(test.moved, null, '!=');
83 video.insertBefore(test.moved, video.firstChild);
87 // We should never get an error for the element we moved.
88 if (event.target == test.moved) {
89 failTest("Error fired for <source> moved to beginning of list.");
92 } else if (!event.target.nextSibling) {
93 logResult(true, "<source> moved was not processed");
94 setTimeout(configureNextTest, 100);
100 function moveToEnd(test, event)
102 switch (++test.errorCount)
105 // Do nothing on the first error event
109 var current = findCurrentSourceElement();
110 switch (test.iteration)
113 consoleWrite("Moving <b>previous<" + "/b> <source> element to end of list, it should be processed again.");
114 test.moved = video.removeChild(current.previousSibling);
117 consoleWrite("Moving <b>current<" + "/b> <source> element, it should be processed again.");
118 test.moved = video.removeChild(current);
121 consoleWrite("Moving <b>next<" + "/b> <source> element, it should be processed again.");
122 test.moved = video.removeChild(current.nextSibling);
125 failTest("Malformed test data!");
129 testExpected(test.moved, null, '!=');
130 video.appendChild(test.moved);
134 if (event.target == test.moved) {
135 logResult(true, "<source> moved was processed a second time.");
136 setTimeout(configureNextTest, 100);
137 } else if (!event.target.nextSibling) {
138 // We should never reach the end of the source list since the tests stops
139 // when the error fires for the moved element.
140 failTest("Error never fired for <source> moved!");
146 function runOneTest(evt)
148 var test = testInfo.tests[testInfo.current];
152 function addSource(index)
154 var source = document.createElement('source');
155 source.src = findMediaFile("video", index + "-" + Date.now());
156 source.type = mimeTypeForExtension(source.src.split('.').pop());
157 video.appendChild(source);
160 function runNextTest()
163 if (++testInfo.current >= testInfo.tests.length) {
164 consoleWrite("PASS<br>");
169 testInfo.errorCount = 0;
170 video = mediaElement = document.createElement('video');
171 document.body.appendChild(video);
173 // Add a bunch of source elements with bogus urls because we want to rearrange elements
174 // after the media engine begins processing sources, and we can't predict the delay
175 // between when the media element fires an 'error' event and our handler is called,
176 // but we need to guarantee that there are <source> elements that haven't been processed
177 // when we run the test.
178 for (var ndx = 1; ndx <= 10; ndx++)
182 function configureNextTest()
184 var videos = document.querySelectorAll('video');
185 for (var ndx = 0; ndx < videos.length; ++ndx)
186 videos[ndx].parentNode.removeChild(videos[ndx]);
187 video = mediaElement = null;
188 setTimeout(runNextTest, 100);
193 document.addEventListener("error", runOneTest, true);
201 <div>Test to make sure a <source> moved after the media element begins processing
202 is handled correctly.</div>
203 <script>setup()</script>