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 = [ |
| 10 { func : moveToEnd, errorCount : 0, moved : null, elementToMove: fun
ction(video, current) { |
| 11 // Moving previous "source" element to end of list, it should be
processed again. |
| 12 return video.removeChild(current.previousSibling); |
| 13 } }, |
| 14 { func : moveToEnd, errorCount : 0, moved : null, elementToMove: fun
ction(video, current) { |
| 15 // Moving current "source" element, it should be processed again
. |
| 16 return video.removeChild(current); |
| 17 } }, |
| 18 { func : moveToEnd, errorCount : 0, moved : null, elementToMove: fun
ction(video, current) { |
| 19 // Moving next "source" element, it should be processed again. |
| 20 return video.removeChild(current.nextSibling); |
| 21 } }, |
| 22 { func : moveEarlier, errorCount : 0, moved : null, elementToMove: f
unction(video, current) { |
| 23 // Moving current "source" element to beginning of list, it shou
ld not be processed again. |
| 24 return video.removeChild(current); |
| 25 } }, |
| 26 { func : moveEarlier, errorCount : 0, moved : null, elementToMove: f
unction(video, current) { |
| 27 // Moving next "source" element to beginning of list, it should
never processed. |
| 28 return video.removeChild(current.nextSibling); |
| 29 } }, |
| 30 { func : moveEarlier, errorCount : 0, moved : null, elementToMove: f
unction(video, current) { |
| 31 // span inserted after current "source" element before it is rem
oved, processing should proceed normally. |
| 32 var span = document.createElement("span") |
| 33 span.appendChild(document.createTextNode("Your browser doesn't s
upport HTML5 video!")); |
| 34 video.insertBefore(span, current.nextSibling); |
| 35 return video.removeChild(current); |
| 36 } }, |
| 37 { func : moveEarlier, errorCount : 0, moved : null, elementToMove: f
unction(video, current) { |
| 38 // span inserted after next "source" element before it is remove
d, processing should proceed normally. |
| 39 var span = document.createElement("span") |
| 40 span.appendChild(document.createTextNode("Your browser doesn't s
upport HTML5 video!")); |
| 41 video.insertBefore(span, current.nextSibling.nextSibling); |
| 42 return video.removeChild(current.nextSibling); |
| 43 } } |
| 44 ]; |
10 | 45 |
11 var testInfo = | 46 var test = testInfo[i]; |
12 { | 47 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 | 48 |
26 function findCurrentSourceElement() | 49 // Add a bunch of source elements with bogus urls because we want to rea
rrange |
27 { | 50 // elements after the media engine begins processing sources, and we can
't predict |
28 var sources = video.getElementsByTagName('source'); | 51 // the delay between when the media element fires an "error" event and o
ur handler |
29 var currentSrc = video.currentSrc; | 52 // is called, but we need to guarantee that there are <source> elements
that |
30 var ndx; | 53 // haven't been processed when we run the test. |
31 for (ndx = 0; ndx < sources.length; ++ndx) { | 54 for (var index = 1; index <= 10; index++) |
32 if (sources[ndx].src == currentSrc) | 55 addSource(index); |
33 break; | 56 |
34 } | 57 function addSource(index) { |
35 if (ndx >= sources.length) { | 58 var source = document.createElement("source"); |
36 failTest(currentSrc + " not found in <source> list"); | 59 source.src = findMediaFile("video", index + "-" + Date.now()); |
37 return null; | 60 source.type = mimeTypeForExtension(source.src.split(".").pop()); |
38 } | 61 video.appendChild(source); |
39 return sources[ndx]; | 62 |
| 63 source.onerror = t.step_func(function(event) { |
| 64 test.func(event); |
| 65 }); |
| 66 } |
| 67 |
| 68 function findCurrentSourceElement() { |
| 69 var sources = video.childNodes; |
| 70 var currentSrc = video.currentSrc; |
| 71 var index; |
| 72 for (index = 0; index < sources.length; ++index) { |
| 73 if (sources[index].src == currentSrc) |
| 74 break; |
40 } | 75 } |
41 | 76 |
42 function moveEarlier(test, event) | 77 assert_less_than(index, sources.length, currentSrc + " not found in
<source> list"); |
43 { | 78 return sources[index]; |
44 if (test.done) | 79 } |
45 return; | |
46 | 80 |
47 switch (++test.errorCount) | 81 function moveEarlier(event) { |
48 { | 82 switch (++test.errorCount) { |
49 case 1: | 83 case 1: |
50 // Do nothing on the first error event | 84 // Do nothing on the first error event |
51 break; | 85 break; |
52 | 86 |
53 case 2: | 87 case 2: |
54 var current = findCurrentSourceElement(); | 88 var current = findCurrentSourceElement(); |
55 switch (test.iteration) | 89 test.moved = test.elementToMove(video, current); |
56 { | 90 assert_not_equals(test.moved, null); |
57 case 1: | 91 video.insertBefore(test.moved, video.firstChild); |
58 consoleWrite("Moving <b>current<" + "/b> <sou
rce> element to beginning of list, it should not be processed again."); | 92 break; |
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 | 93 |
84 testExpected(test.moved, null, '!='); | 94 default: |
85 video.insertBefore(test.moved, video.firstChild); | 95 // We should never get an error for the element we moved. |
86 break; | 96 assert_not_equals(event.target, test.moved); |
| 97 if (event.target.nextSibling == null) |
| 98 t.done(); |
| 99 break; |
| 100 } |
| 101 } |
87 | 102 |
88 default: | 103 function moveToEnd(event) { |
89 // We should never get an error for the element we moved
. | 104 switch (++test.errorCount) { |
90 if (event.target == test.moved) { | 105 case 1: |
91 failTest("Error fired for <source> moved to be
ginning of list."); | 106 // Do nothing on the first error event |
92 test.done = true; | 107 break; |
93 return; | 108 |
94 } else if (!event.target.nextSibling) { | 109 case 2: |
95 logResult(true, "<source> moved was not proces
sed"); | 110 var current = findCurrentSourceElement(); |
96 setTimeout(configureNextTest, 100); | 111 test.moved = test.elementToMove(video, current); |
97 } | 112 assert_not_equals(test.moved, null); |
98 break; | 113 video.appendChild(test.moved); |
99 } | 114 break; |
| 115 |
| 116 default: |
| 117 assert_true(event.target == test.moved || event.target.nextSibli
ng != null); |
| 118 if (event.target == test.moved) |
| 119 t.done(); |
| 120 break; |
100 } | 121 } |
101 | 122 } |
102 function moveToEnd(test, event) | 123 }); |
103 { | 124 } |
104 switch (++test.errorCount) | 125 </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 |