upload tizen1.0 source
[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.layoutTestController) {
17     layoutTestController.dumpAsText();
18     layoutTestController.waitUntilDone();
19 }
20
21 function disableFullTestDetailsPrinting()
22 {
23     printFullTestDetails = false;
24 }
25
26 function enableFullTestDetailsPrinting()
27 {
28     printFullTestDetails = true;
29 }
30
31 function logConsole()
32 {
33     if (!console && document.body) {
34         console = document.createElement('div');
35         document.body.appendChild(console);
36     }
37     return console;
38 }
39
40 function findMediaElement()
41 {
42     try {
43         video = document.getElementsByTagName('video')[0];
44         if (video)
45             mediaElement = video;
46     } catch (ex) { }
47 }
48
49 function testAndEnd(testFuncString)
50 {
51     test(testFuncString, true);
52 }
53
54 function test(testFuncString, endit)
55 {
56     logResult(eval(testFuncString), "TEST(" + testFuncString + ")");
57     if (endit)
58         endTest();
59 }
60
61 function testExpected(testFuncString, expected, comparison)
62 {
63     try {
64         var observed = eval(testFuncString);
65     } catch (ex) {
66         consoleWrite(ex);
67         return;
68     }
69
70     if (comparison === undefined)
71         comparison = '==';
72
73     var success = false;
74     switch (comparison)
75     {
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;
82     }
83
84     reportExpected(success, testFuncString, comparison, expected, observed)
85 }
86
87 var testNumber = 0;
88
89 function reportExpected(success, testFuncString, comparison, expected, observed)
90 {
91     testNumber++;
92
93     var msg = "Test " + testNumber;
94
95     if (printFullTestDetails || !success)
96         msg = "EXPECTED (<em>" + testFuncString + " </em>" + comparison + " '<em>" + expected + "</em>')";
97
98     if (!success)
99         msg +=  ", OBSERVED '<em>" + observed + "</em>'";
100
101     logResult(success, msg);
102 }
103
104 function runSilently(testFuncString)
105 {
106     if (printFullTestDetails)
107         consoleWrite("RUN(" + testFuncString + ")");
108     try {
109         eval(testFuncString);
110     } catch (ex) {
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 + "):");
115         }
116         logResult(Failed, "<span style='color:red'>"+ex+"</span>");
117     }
118 }
119
120 function run(testFuncString)
121 {
122     consoleWrite("RUN(" + testFuncString + ")");
123     try {
124         eval(testFuncString);
125     } catch (ex) {
126         consoleWrite(ex);
127     }
128 }
129
130 function waitForEventOnce(eventName, func, endit)
131 {
132     waitForEvent(eventName, func, endit, true)
133 }
134
135 function waitForEventAndEnd(eventName, funcString)
136 {
137     waitForEvent(eventName, funcString, true)
138 }
139
140 function waitForEvent(eventName, func, endit, oneTimeOnly)
141 {
142     function _eventCallback(event)
143     {
144         if (oneTimeOnly)
145             mediaElement.removeEventListener(eventName, _eventCallback, true);
146
147         consoleWrite("EVENT(" + eventName + ")");
148
149         if (func)
150             func(event);
151
152         if (endit)
153             endTest();
154     }
155
156     mediaElement.addEventListener(eventName, _eventCallback, true);
157 }
158
159 function waitForEventTestAndEnd(eventName, testFuncString)
160 {
161     waitForEventAndTest(eventName, testFuncString, true);
162 }
163
164 function waitForEventAndFail(eventName)
165 {
166     waitForEventAndTest(eventName, "false", true);
167 }
168
169 function waitForEventAndTest(eventName, testFuncString, endit)
170 {
171     function _eventCallback(event)
172     {
173         logResult(eval(testFuncString), "EVENT(" + eventName + ") TEST(" + testFuncString + ")");
174         if (endit)
175             endTest();
176     }
177
178     mediaElement.addEventListener(eventName, _eventCallback, true);
179 }
180
181 function testException(testString, exceptionString)
182 {
183     try {
184         eval(testString);
185     } catch (ex) {
186         logResult(ex.code == eval(exceptionString), "TEST(" + testString + ") THROWS("+exceptionString+")");
187     }
188 }
189
190 var testEnded = false;
191
192 function endTest()
193 {
194     consoleWrite("END OF TEST");
195     testEnded = true;
196     if (window.layoutTestController)
197         layoutTestController.notifyDone();
198 }
199
200 function endTestLater()
201 {
202     setTimeout(endTest, 250);
203 }
204
205 function failTestIn(ms)
206 {
207     setTimeout(function () {
208         consoleWrite("FAIL: did not end fast enough");
209         endTest();
210     }, ms);
211 }
212
213 function failTest(text)
214 {
215     logResult(Failed, text);
216     endTest();
217 }
218
219
220 function logResult(success, text)
221 {
222     if (success)
223         consoleWrite(text + " <span style='color:green'>OK</span>");
224     else
225         consoleWrite(text + " <span style='color:red'>FAIL</span>");
226 }
227
228 function consoleWrite(text)
229 {
230     if (testEnded)
231         return;
232     logConsole().innerHTML += text + "<br>";
233 }
234
235 function relativeURL(url)
236 {
237     return url.substr(url.indexOf('/media/')+7);
238 }
239
240
241 function isInTimeRanges(ranges, time)
242 {
243     var i = 0;
244     for (i = 0; i < ranges.length; ++i) {
245         if (time >= ranges.start(i) && time <= ranges.end(i)) {
246           return true;
247         }
248     }
249     return false;
250 }
251
252 function testTracks(expected)
253 {
254     tracks = video.textTracks;
255     testExpected("tracks.length", expected.length);
256
257     for (var i = 0; i < tracks.length; i++) {
258         consoleWrite("<br>*** Testing text track " + i);
259
260         track = tracks[i];
261         for (j = 0; j < expected.tests.length; j++) {
262             var test = expected.tests[j];
263             testExpected("track." + test.property, test.values[i]);
264         }
265     }
266 }
267
268 function testCues(index, expected)
269 {
270     consoleWrite("<br>*** Testing text track " + index);
271
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]);
278         }
279     }
280 }
281
282 function allTestsEnded()
283 {
284     numberOfTrackTests--;
285     if (numberOfTrackTests == 0)
286         endTest();
287 }
288
289 function enableAllTextTracks()
290 {
291     findMediaElement();
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;
295     }
296 }