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.layoutTestController) {
17 layoutTestController.dumpAsText();
18 layoutTestController.waitUntilDone();
21 function disableFullTestDetailsPrinting()
23 printFullTestDetails = false;
26 function enableFullTestDetailsPrinting()
28 printFullTestDetails = true;
33 if (!console && document.body) {
34 console = document.createElement('div');
35 document.body.appendChild(console);
40 function findMediaElement()
43 video = document.getElementsByTagName('video')[0];
49 function testAndEnd(testFuncString)
51 test(testFuncString, true);
54 function test(testFuncString, endit)
56 logResult(eval(testFuncString), "TEST(" + testFuncString + ")");
61 function testExpected(testFuncString, expected, comparison)
64 var observed = eval(testFuncString);
70 if (comparison === undefined)
76 case '<': success = observed < expected; break;
77 case '<=': success = observed <= expected; break;
78 case '>': success = observed > expected; break;
79 case '>=': success = observed >= expected; break;
80 case '!=': success = observed != expected; break;
81 case '==': success = observed == expected; break;
84 reportExpected(success, testFuncString, comparison, expected, observed)
89 function reportExpected(success, testFuncString, comparison, expected, observed)
93 var msg = "Test " + testNumber;
95 if (printFullTestDetails || !success)
96 msg = "EXPECTED (<em>" + testFuncString + " </em>" + comparison + " '<em>" + expected + "</em>')";
99 msg += ", OBSERVED '<em>" + observed + "</em>'";
101 logResult(success, msg);
104 function runSilently(testFuncString)
106 if (printFullTestDetails)
107 consoleWrite("RUN(" + testFuncString + ")");
109 eval(testFuncString);
111 if (!printFullTestDetails) {
112 // No details were printed previous, give some now.
113 // This will be helpful in case of error.
114 logResult(Failed, "Error in RUN(" + testFuncString + "):");
116 logResult(Failed, "<span style='color:red'>"+ex+"</span>");
120 function run(testFuncString)
122 consoleWrite("RUN(" + testFuncString + ")");
124 eval(testFuncString);
130 function waitForEventOnce(eventName, func, endit)
132 waitForEvent(eventName, func, endit, true)
135 function waitForEventAndEnd(eventName, funcString)
137 waitForEvent(eventName, funcString, true)
140 function waitForEvent(eventName, func, endit, oneTimeOnly)
142 function _eventCallback(event)
145 mediaElement.removeEventListener(eventName, _eventCallback, true);
147 consoleWrite("EVENT(" + eventName + ")");
156 mediaElement.addEventListener(eventName, _eventCallback, true);
159 function waitForEventTestAndEnd(eventName, testFuncString)
161 waitForEventAndTest(eventName, testFuncString, true);
164 function waitForEventAndFail(eventName)
166 waitForEventAndTest(eventName, "false", true);
169 function waitForEventAndTest(eventName, testFuncString, endit)
171 function _eventCallback(event)
173 logResult(eval(testFuncString), "EVENT(" + eventName + ") TEST(" + testFuncString + ")");
178 mediaElement.addEventListener(eventName, _eventCallback, true);
181 function testException(testString, exceptionString)
186 logResult(ex.code == eval(exceptionString), "TEST(" + testString + ") THROWS("+exceptionString+")");
190 var testEnded = false;
194 consoleWrite("END OF TEST");
196 if (window.layoutTestController)
197 layoutTestController.notifyDone();
200 function endTestLater()
202 setTimeout(endTest, 250);
205 function failTestIn(ms)
207 setTimeout(function () {
208 consoleWrite("FAIL: did not end fast enough");
213 function failTest(text)
215 logResult(Failed, text);
220 function logResult(success, text)
223 consoleWrite(text + " <span style='color:green'>OK</span>");
225 consoleWrite(text + " <span style='color:red'>FAIL</span>");
228 function consoleWrite(text)
232 logConsole().innerHTML += text + "<br>";
235 function relativeURL(url)
237 return url.substr(url.indexOf('/media/')+7);
241 function isInTimeRanges(ranges, time)
244 for (i = 0; i < ranges.length; ++i) {
245 if (time >= ranges.start(i) && time <= ranges.end(i)) {
252 function testTracks(expected)
254 tracks = video.textTracks;
255 testExpected("tracks.length", expected.length);
257 for (var i = 0; i < tracks.length; i++) {
258 consoleWrite("<br>*** Testing text track " + i);
261 for (j = 0; j < expected.tests.length; j++) {
262 var test = expected.tests[j];
263 testExpected("track." + test.property, test.values[i]);
268 function testCues(index, expected)
270 consoleWrite("<br>*** Testing text track " + index);
272 cues = video.textTracks[index].cues;
273 testExpected("cues.length", expected.length);
274 for (var i = 0; i < cues.length; i++) {
275 for (j = 0; j < expected.tests.length; j++) {
276 var test = expected.tests[j];
277 testExpected("cues[" + i + "]." + test.property, test.values[i]);
282 function allTestsEnded()
284 numberOfTrackTests--;
285 if (numberOfTrackTests == 0)
289 function enableAllTextTracks()
292 for (var i = 0; i < video.textTracks.length; i++) {
293 if (video.textTracks[i].mode == TextTrack.DISABLED)
294 video.textTracks[i].mode = TextTrack.HIDDEN;