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 (!consoleDiv && document.body) {
37 consoleDiv = document.createElement('div');
38 document.body.appendChild(consoleDiv);
43 function findMediaElement()
46 video = document.getElementsByTagName('video')[0];
50 audio = document.getElementsByTagName('audio')[0];
57 function testAndEnd(testFuncString)
59 test(testFuncString, true);
62 function test(testFuncString, endit)
64 logResult(eval(testFuncString), "TEST(" + testFuncString + ")");
69 function testExpected(testFuncString, expected, comparison)
72 var observed = eval(testFuncString);
78 if (comparison === undefined)
84 case '<': success = observed < expected; break;
85 case '<=': success = observed <= expected; break;
86 case '>': success = observed > expected; break;
87 case '>=': success = observed >= expected; break;
88 case '!=': success = observed != expected; break;
89 case '==': success = observed == expected; break;
90 case '===': success = observed === expected; break;
93 reportExpected(success, testFuncString, comparison, expected, observed)
96 function testArraysEqual(testFuncString, expected)
100 observed = eval(testFuncString);
106 testExpected(testFuncString + ".length", expected.length);
108 for (var i = 0; i < observed.length; i++) {
109 testExpected(testFuncString + "[" + i + "]", expected[i]);
115 function reportExpected(success, testFuncString, comparison, expected, observed)
119 var msg = "Test " + testNumber;
121 if (printFullTestDetails || !success)
122 msg = "EXPECTED (<em>" + testFuncString + " </em>" + comparison + " '<em>" + expected + "</em>')";
125 msg += ", OBSERVED '<em>" + observed + "</em>'";
127 logResult(success, msg);
130 function runSilently(testFuncString)
132 if (printFullTestDetails)
133 consoleWrite("RUN(" + testFuncString + ")");
135 eval(testFuncString);
137 if (!printFullTestDetails) {
138 // No details were printed previous, give some now.
139 // This will be helpful in case of error.
140 logResult(Failed, "Error in RUN(" + testFuncString + "):");
142 logResult(Failed, "<span style='color:red'>"+ex+"</span>");
146 function run(testFuncString)
148 consoleWrite("RUN(" + testFuncString + ")");
150 eval(testFuncString);
156 function waitForEventOnce(eventName, func, endit, doNotLog)
158 waitForEvent(eventName, func, endit, true, null, doNotLog)
161 function waitForEventAndEnd(eventName, funcString)
163 waitForEvent(eventName, funcString, true, false, null, false)
166 function waitForEvent(eventName, func, endit, oneTimeOnly, element, doNotLog)
169 element = mediaElement;
171 function _eventCallback(event)
174 element.removeEventListener(eventName, _eventCallback, true);
177 consoleWrite("EVENT(" + eventName + ")");
186 element.addEventListener(eventName, _eventCallback, true);
189 function waitForEventTestAndEnd(eventName, testFuncString)
191 waitForEventAndTest(eventName, testFuncString, true);
194 function waitForEventAndFail(eventName)
196 waitForEventAndTest(eventName, "false", true);
199 function waitForEventAndTest(eventName, testFuncString, endit)
201 function _eventCallback(event)
203 logResult(eval(testFuncString), "EVENT(" + eventName + ") TEST(" + testFuncString + ")");
208 mediaElement.addEventListener(eventName, _eventCallback, true);
211 function testDOMException(testString, exceptionString)
218 logResult(exception instanceof DOMException && exception.code === eval(exceptionString),
219 "TEST(" + testString + ") THROWS(" + exceptionString + ": " + exception.message + ")");
222 function testException(testString, exceptionString) {
228 logResult(exception !== undefined && exception == eval(exceptionString),
229 "TEST(" + testString + ") THROWS(" + exceptionString + ")");
232 var testEnded = false;
236 consoleWrite("END OF TEST");
238 if (window.testRunner)
239 testRunner.notifyDone();
242 function endTestLater()
244 setTimeout(endTest, 250);
247 function failTestIn(ms)
249 setTimeout(function () {
250 consoleWrite("FAIL: did not end fast enough");
255 function failTest(text)
257 logResult(Failed, text);
261 function logResult(success, text)
264 consoleWrite(text + " <span style='color:green'>OK</span>");
266 consoleWrite(text + " <span style='color:red'>FAIL</span>");
269 function consoleWrite(text)
273 var span = document.createElement("span");
274 logConsole().appendChild(span);
275 span.innerHTML = text + '<br>';
278 function relativeURL(url)
280 return url.substr(url.lastIndexOf('/media/')+7);
284 function isInTimeRanges(ranges, time)
287 for (i = 0; i < ranges.length; ++i) {
288 if (time >= ranges.start(i) && time <= ranges.end(i)) {
295 function testTracks(expected)
297 tracks = video.textTracks;
298 testExpected("tracks.length", expected.length);
300 for (var i = 0; i < tracks.length; i++) {
301 consoleWrite("<br>*** Testing text track " + i);
304 for (j = 0; j < expected.tests.length; j++) {
305 var test = expected.tests[j];
306 testExpected("track." + test.property, test.values[i]);
311 function testCues(index, expected)
313 consoleWrite("<br>*** Testing text track " + index);
315 cues = video.textTracks[index].cues;
316 testExpected("cues.length", expected.length);
317 for (var i = 0; i < cues.length; i++) {
318 for (j = 0; j < expected.tests.length; j++) {
319 var test = expected.tests[j];
320 testExpected("cues[" + i + "]." + test.property, test.values[i]);
325 function allTestsEnded()
327 numberOfTrackTests--;
328 if (numberOfTrackTests == 0)
332 function enableAllTextTracks()
335 for (var i = 0; i < video.textTracks.length; i++) {
336 if (video.textTracks[i].mode == "disabled")
337 video.textTracks[i].mode = "hidden";
341 var requiredEvents = [];
343 function waitForEventsAndCall(eventList, func)
345 function _eventCallback(event)
347 if (!requiredEvents.length)
350 var index = requiredEvents.indexOf(event.type);
354 requiredEvents.splice(index, 1);
355 if (requiredEvents.length)
362 for (var i = 0; i < eventList.length; i++) {
363 requiredEvents[i] = eventList[i][1];
364 eventList[i][0].addEventListener(requiredEvents[i], _eventCallback, true);