3 var mediaElement = document; // If not set, an event from any element will trigger a waitForEvent() callback.
5 var printFullTestDetails = true; // This is optionaly switched of by test whose tested values can differ. (see disableFullTestDetailsPrinting())
8 var track = null; // Current TextTrack being tested.
9 var cues = null; // Current TextTrackCueList being tested.
10 var numberOfTrackTests = 0;
11 var numberOfTracksLoaded = 0;
16 if (window.testRunner) {
17 // Some track element rendering tests require text pixel dump.
18 if (typeof requirePixelDump == "undefined")
19 testRunner.dumpAsText();
21 testRunner.waitUntilDone();
24 function disableFullTestDetailsPrinting()
26 printFullTestDetails = false;
29 function enableFullTestDetailsPrinting()
31 printFullTestDetails = true;
36 if (!console && document.body) {
37 console = document.createElement('div');
38 document.body.appendChild(console);
43 function findMediaElement()
46 video = document.getElementsByTagName('video')[0];
52 function testAndEnd(testFuncString)
54 test(testFuncString, true);
57 function test(testFuncString, endit)
59 logResult(eval(testFuncString), "TEST(" + testFuncString + ")");
64 function testExpected(testFuncString, expected, comparison)
67 var observed = eval(testFuncString);
73 if (comparison === undefined)
79 case '<': success = observed < expected; break;
80 case '<=': success = observed <= expected; break;
81 case '>': success = observed > expected; break;
82 case '>=': success = observed >= expected; break;
83 case '!=': success = observed != expected; break;
84 case '==': success = observed == expected; break;
85 case '===': success = observed === expected; break;
88 reportExpected(success, testFuncString, comparison, expected, observed)
91 function testArraysEqual(testFuncString, expected)
95 observed = eval(testFuncString);
101 testExpected(testFuncString + ".length", expected.length);
103 for (var i = 0; i < observed.length; i++) {
104 testExpected(testFuncString + "[" + i + "]", expected[i]);
110 function reportExpected(success, testFuncString, comparison, expected, observed)
114 var msg = "Test " + testNumber;
116 if (printFullTestDetails || !success)
117 msg = "EXPECTED (<em>" + testFuncString + " </em>" + comparison + " '<em>" + expected + "</em>')";
120 msg += ", OBSERVED '<em>" + observed + "</em>'";
122 logResult(success, msg);
125 function runSilently(testFuncString)
127 if (printFullTestDetails)
128 consoleWrite("RUN(" + testFuncString + ")");
130 eval(testFuncString);
132 if (!printFullTestDetails) {
133 // No details were printed previous, give some now.
134 // This will be helpful in case of error.
135 logResult(Failed, "Error in RUN(" + testFuncString + "):");
137 logResult(Failed, "<span style='color:red'>"+ex+"</span>");
141 function run(testFuncString)
143 consoleWrite("RUN(" + testFuncString + ")");
145 eval(testFuncString);
151 function waitForEventOnce(eventName, func, endit)
153 waitForEvent(eventName, func, endit, true)
156 function waitForEventAndEnd(eventName, funcString)
158 waitForEvent(eventName, funcString, true)
161 function waitForEvent(eventName, func, endit, oneTimeOnly, element)
164 element = mediaElement;
166 function _eventCallback(event)
169 element.removeEventListener(eventName, _eventCallback, true);
171 consoleWrite("EVENT(" + eventName + ")");
180 element.addEventListener(eventName, _eventCallback, true);
183 function waitForEventTestAndEnd(eventName, testFuncString)
185 waitForEventAndTest(eventName, testFuncString, true);
188 function waitForEventAndFail(eventName)
190 waitForEventAndTest(eventName, "false", true);
193 function waitForEventAndTest(eventName, testFuncString, endit)
195 function _eventCallback(event)
197 logResult(eval(testFuncString), "EVENT(" + eventName + ") TEST(" + testFuncString + ")");
202 mediaElement.addEventListener(eventName, _eventCallback, true);
205 function testException(testString, exceptionString)
210 logResult(ex.code == eval(exceptionString), "TEST(" + testString + ") THROWS("+exceptionString+")");
214 var testEnded = false;
218 consoleWrite("END OF TEST");
220 if (window.testRunner)
221 testRunner.notifyDone();
224 function endTestLater()
226 setTimeout(endTest, 250);
229 function failTestIn(ms)
231 setTimeout(function () {
232 consoleWrite("FAIL: did not end fast enough");
237 function failTest(text)
239 logResult(Failed, text);
244 function logResult(success, text)
247 consoleWrite(text + " <span style='color:green'>OK</span>");
249 consoleWrite(text + " <span style='color:red'>FAIL</span>");
252 function consoleWrite(text)
256 logConsole().innerHTML += text + "<br>";
259 function relativeURL(url)
261 return url.substr(url.indexOf('/media/')+7);
265 function isInTimeRanges(ranges, time)
268 for (i = 0; i < ranges.length; ++i) {
269 if (time >= ranges.start(i) && time <= ranges.end(i)) {
276 function testTracks(expected)
278 tracks = video.textTracks;
279 testExpected("tracks.length", expected.length);
281 for (var i = 0; i < tracks.length; i++) {
282 consoleWrite("<br>*** Testing text track " + i);
285 for (j = 0; j < expected.tests.length; j++) {
286 var test = expected.tests[j];
287 testExpected("track." + test.property, test.values[i]);
292 function testCues(index, expected)
294 consoleWrite("<br>*** Testing text track " + index);
296 cues = video.textTracks[index].cues;
297 testExpected("cues.length", expected.length);
298 for (var i = 0; i < cues.length; i++) {
299 for (j = 0; j < expected.tests.length; j++) {
300 var test = expected.tests[j];
301 testExpected("cues[" + i + "]." + test.property, test.values[i]);
306 function allTestsEnded()
308 numberOfTrackTests--;
309 if (numberOfTrackTests == 0)
313 function enableAllTextTracks()
316 for (var i = 0; i < video.textTracks.length; i++) {
317 if (video.textTracks[i].mode == "disabled")
318 video.textTracks[i].mode = "hidden";
322 var requiredEvents = [];
324 function waitForEventsAndCall(eventList, func)
326 function _eventCallback(event)
328 if (!requiredEvents.length)
331 var index = requiredEvents.indexOf(event.type);
335 requiredEvents.splice(index, 1);
336 if (requiredEvents.length)
343 for (var i = 0; i < eventList.length; i++) {
344 requiredEvents[i] = eventList[i][1];
345 eventList[i][0].addEventListener(requiredEvents[i], _eventCallback, true);