Implement Focus UI
[framework/web/webkit-efl.git] / LayoutTests / media / video-test.js
1
2 var video = null;
3 var mediaElement = document; // If not set, an event from any element will trigger a waitForEvent() callback.
4 var console = null;
5 var printFullTestDetails = true; // This is optionaly switched of by test whose tested values can differ. (see disableFullTestDetailsPrinting())
6 var Failed = false;
7
8 var track = null; // Current TextTrack being tested.
9 var cues = null; // Current TextTrackCueList being tested.
10 var numberOfTrackTests = 0;
11 var numberOfTracksLoaded = 0;
12
13 findMediaElement();
14 logConsole();
15
16 if (window.testRunner) {
17     // Some track element rendering tests require text pixel dump.
18     if (typeof requirePixelDump == "undefined")
19         testRunner.dumpAsText();
20
21     testRunner.waitUntilDone();
22 }
23
24 function disableFullTestDetailsPrinting()
25 {
26     printFullTestDetails = false;
27 }
28
29 function enableFullTestDetailsPrinting()
30 {
31     printFullTestDetails = true;
32 }
33
34 function logConsole()
35 {
36     if (!console && document.body) {
37         console = document.createElement('div');
38         document.body.appendChild(console);
39     }
40     return console;
41 }
42
43 function findMediaElement()
44 {
45     try {
46         video = document.getElementsByTagName('video')[0];
47         if (video)
48             mediaElement = video;
49     } catch (ex) { }
50 }
51
52 function testAndEnd(testFuncString)
53 {
54     test(testFuncString, true);
55 }
56
57 function test(testFuncString, endit)
58 {
59     logResult(eval(testFuncString), "TEST(" + testFuncString + ")");
60     if (endit)
61         endTest();
62 }
63
64 function testExpected(testFuncString, expected, comparison)
65 {
66     try {
67         var observed = eval(testFuncString);
68     } catch (ex) {
69         consoleWrite(ex);
70         return;
71     }
72
73     if (comparison === undefined)
74         comparison = '==';
75
76     var success = false;
77     switch (comparison)
78     {
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;
86     }
87
88     reportExpected(success, testFuncString, comparison, expected, observed)
89 }
90
91 function testArraysEqual(testFuncString, expected)
92 {
93     var observed;
94     try {
95         observed = eval(testFuncString);
96     } catch (ex) {
97         consoleWrite(ex);
98         return;
99     }
100   
101     testExpected(testFuncString + ".length", expected.length);
102
103     for (var i = 0; i < observed.length; i++) {
104         testExpected(testFuncString + "[" + i + "]", expected[i]);
105     }
106 }
107
108 var testNumber = 0;
109
110 function reportExpected(success, testFuncString, comparison, expected, observed)
111 {
112     testNumber++;
113
114     var msg = "Test " + testNumber;
115
116     if (printFullTestDetails || !success)
117         msg = "EXPECTED (<em>" + testFuncString + " </em>" + comparison + " '<em>" + expected + "</em>')";
118
119     if (!success)
120         msg +=  ", OBSERVED '<em>" + observed + "</em>'";
121
122     logResult(success, msg);
123 }
124
125 function runSilently(testFuncString)
126 {
127     if (printFullTestDetails)
128         consoleWrite("RUN(" + testFuncString + ")");
129     try {
130         eval(testFuncString);
131     } catch (ex) {
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 + "):");
136         }
137         logResult(Failed, "<span style='color:red'>"+ex+"</span>");
138     }
139 }
140
141 function run(testFuncString)
142 {
143     consoleWrite("RUN(" + testFuncString + ")");
144     try {
145         eval(testFuncString);
146     } catch (ex) {
147         consoleWrite(ex);
148     }
149 }
150
151 function waitForEventOnce(eventName, func, endit)
152 {
153     waitForEvent(eventName, func, endit, true)
154 }
155
156 function waitForEventAndEnd(eventName, funcString)
157 {
158     waitForEvent(eventName, funcString, true)
159 }
160
161 function waitForEvent(eventName, func, endit, oneTimeOnly, element)
162 {
163     if (!element)
164         element = mediaElement;
165
166     function _eventCallback(event)
167     {
168         if (oneTimeOnly)
169             element.removeEventListener(eventName, _eventCallback, true);
170
171         consoleWrite("EVENT(" + eventName + ")");
172
173         if (func)
174             func(event);
175
176         if (endit)
177             endTest();
178     }
179
180     element.addEventListener(eventName, _eventCallback, true);
181 }
182
183 function waitForEventTestAndEnd(eventName, testFuncString)
184 {
185     waitForEventAndTest(eventName, testFuncString, true);
186 }
187
188 function waitForEventAndFail(eventName)
189 {
190     waitForEventAndTest(eventName, "false", true);
191 }
192
193 function waitForEventAndTest(eventName, testFuncString, endit)
194 {
195     function _eventCallback(event)
196     {
197         logResult(eval(testFuncString), "EVENT(" + eventName + ") TEST(" + testFuncString + ")");
198         if (endit)
199             endTest();
200     }
201
202     mediaElement.addEventListener(eventName, _eventCallback, true);
203 }
204
205 function testException(testString, exceptionString)
206 {
207     try {
208         eval(testString);
209     } catch (ex) {
210         logResult(ex.code == eval(exceptionString), "TEST(" + testString + ") THROWS("+exceptionString+")");
211     }
212 }
213
214 var testEnded = false;
215
216 function endTest()
217 {
218     consoleWrite("END OF TEST");
219     testEnded = true;
220     if (window.testRunner)
221         testRunner.notifyDone();
222 }
223
224 function endTestLater()
225 {
226     setTimeout(endTest, 250);
227 }
228
229 function failTestIn(ms)
230 {
231     setTimeout(function () {
232         consoleWrite("FAIL: did not end fast enough");
233         endTest();
234     }, ms);
235 }
236
237 function failTest(text)
238 {
239     logResult(Failed, text);
240     endTest();
241 }
242
243
244 function logResult(success, text)
245 {
246     if (success)
247         consoleWrite(text + " <span style='color:green'>OK</span>");
248     else
249         consoleWrite(text + " <span style='color:red'>FAIL</span>");
250 }
251
252 function consoleWrite(text)
253 {
254     if (testEnded)
255         return;
256     logConsole().innerHTML += text + "<br>";
257 }
258
259 function relativeURL(url)
260 {
261     return url.substr(url.indexOf('/media/')+7);
262 }
263
264
265 function isInTimeRanges(ranges, time)
266 {
267     var i = 0;
268     for (i = 0; i < ranges.length; ++i) {
269         if (time >= ranges.start(i) && time <= ranges.end(i)) {
270           return true;
271         }
272     }
273     return false;
274 }
275
276 function testTracks(expected)
277 {
278     tracks = video.textTracks;
279     testExpected("tracks.length", expected.length);
280
281     for (var i = 0; i < tracks.length; i++) {
282         consoleWrite("<br>*** Testing text track " + i);
283
284         track = tracks[i];
285         for (j = 0; j < expected.tests.length; j++) {
286             var test = expected.tests[j];
287             testExpected("track." + test.property, test.values[i]);
288         }
289     }
290 }
291
292 function testCues(index, expected)
293 {
294     consoleWrite("<br>*** Testing text track " + index);
295
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]);
302         }
303     }
304 }
305
306 function allTestsEnded()
307 {
308     numberOfTrackTests--;
309     if (numberOfTrackTests == 0)
310         endTest();
311 }
312
313 function enableAllTextTracks()
314 {
315     findMediaElement();
316     for (var i = 0; i < video.textTracks.length; i++) {
317         if (video.textTracks[i].mode == "disabled")
318             video.textTracks[i].mode = "hidden";
319     }
320 }
321
322 var requiredEvents = [];
323
324 function waitForEventsAndCall(eventList, func)
325 {
326     function _eventCallback(event)
327     {
328         if (!requiredEvents.length)
329             return;
330
331         var index = requiredEvents.indexOf(event.type);
332         if (index < 0)
333             return;
334
335         requiredEvents.splice(index, 1);
336         if (requiredEvents.length)
337             return;
338
339         func();
340     }
341
342     requiredEvents = [];
343     for (var i = 0; i < eventList.length; i++) {
344         requiredEvents[i] = eventList[i][1];
345         eventList[i][0].addEventListener(requiredEvents[i], _eventCallback, true);
346     }
347 }