1
2var video = null;
3var mediaElement = null;
4var console = null;
5var printFullTestDetails = true; // This is optionaly switched of by test whose tested values can differ. (see disableFullTestDetailsPrinting())
6var Failed = false;
7
8findMediaElement();
9logConsole();
10
11if (window.layoutTestController) {
12 layoutTestController.dumpAsText();
13 layoutTestController.waitUntilDone();
14}
15
16function disableFullTestDetailsPrinting()
17{
18 printFullTestDetails = false;
19}
20
21function enableFullTestDetailsPrinting()
22{
23 printFullTestDetails = true;
24}
25
26function logConsole()
27{
28 if (!console && document.body) {
29 console = document.createElement('div');
30 document.body.appendChild(console);
31 }
32 return console;
33}
34
35function findMediaElement()
36{
37 try {
38 video = document.getElementsByTagName('video')[0];
39 if (video)
40 mediaElement = video;
41 } catch (ex) { }
42}
43
44function testAndEnd(testFuncString)
45{
46 test(testFuncString, true);
47}
48
49function test(testFuncString, endit)
50{
51 logResult(eval(testFuncString), "TEST(" + testFuncString + ")");
52 if (endit)
53 endTest();
54}
55
56function testExpected(testFuncString, expected, comparison)
57{
58 try {
59 var observed = eval(testFuncString);
60 } catch (ex) {
61 consoleWrite(ex);
62 return;
63 }
64
65 if (comparison === undefined)
66 comparison = '==';
67
68 var success = false;
69 switch (comparison)
70 {
71 case '<': success = observed < expected; break;
72 case '<=': success = observed <= expected; break;
73 case '>': success = observed > expected; break;
74 case '>=': success = observed >= expected; break;
75 case '!=': success = observed != expected; break;
76 case '==': success = observed == expected; break;
77 }
78
79 reportExpected(success, testFuncString, comparison, expected, observed)
80}
81
82var testNumber = 0;
83
84function reportExpected(success, testFuncString, comparison, expected, observed)
85{
86 testNumber++;
87
88 var msg = "Test " + testNumber;
89
90 if (printFullTestDetails || !success)
91 msg = "EXPECTED (<em>" + testFuncString + " </em>" + comparison + " '<em>" + expected + "</em>')";
92
93 if (!success)
94 msg += ", OBSERVED '<em>" + observed + "</em>'";
95
96 logResult(success, msg);
97}
98
99function runSilently(testFuncString)
100{
101 if (printFullTestDetails)
102 consoleWrite("RUN(" + testFuncString + ")");
103 try {
104 eval(testFuncString);
105 } catch (ex) {
106 if (!printFullTestDetails) {
107 // No details were printed previous, give some now.
108 // This will be helpful in case of error.
109 logResult(Failed, "Error in RUN(" + testFuncString + "):");
110 }
111 logResult(Failed, "<span style='color:red'>"+ex+"</span>");
112 }
113}
114
115function run(testFuncString)
116{
117 consoleWrite("RUN(" + testFuncString + ")");
118 try {
119 eval(testFuncString);
120 } catch (ex) {
121 consoleWrite(ex);
122 }
123}
124
125function waitForEventAndEnd(eventName, funcString)
126{
127 waitForEvent(eventName, funcString, true)
128}
129
130function waitForEvent(eventName, func, endit)
131{
132 function _eventCallback(event)
133 {
134 consoleWrite("EVENT(" + eventName + ")");
135
136 if (func)
137 func(event);
138
139 if (endit)
140 endTest();
141 }
142
143 mediaElement.addEventListener(eventName, _eventCallback);
144}
145
146function waitForEventTestAndEnd(eventName, testFuncString)
147{
148 waitForEventAndTest(eventName, testFuncString, true);
149}
150
151function waitForEventAndFail(eventName)
152{
153 waitForEventAndTest(eventName, "false", true);
154}
155
156function waitForEventAndTest(eventName, testFuncString, endit)
157{
158 function _eventCallback(event)
159 {
160 logResult(eval(testFuncString), "EVENT(" + eventName + ") TEST(" + testFuncString + ")");
161 if (endit)
162 endTest();
163 }
164
165 mediaElement.addEventListener(eventName, _eventCallback);
166}
167
168function testException(testString, exceptionString)
169{
170 try {
171 eval(testString);
172 } catch (ex) {
173 logResult(ex.code == eval(exceptionString), "TEST(" + testString + ") THROWS("+exceptionString+")");
174 }
175}
176
177var testEnded = false;
178
179function endTest()
180{
181 consoleWrite("END OF TEST");
182 testEnded = true;
183 if (window.layoutTestController)
184 layoutTestController.notifyDone();
185}
186
187function endTestLater()
188{
189 setTimeout(endTest, 250);
190}
191
192function failTestIn(ms)
193{
194 setTimeout(function () {
195 consoleWrite("FAIL: did not end fast enough");
196 endTest();
197 }, ms);
198}
199
200function failTest(text)
201{
202 logResult(Failed, text);
203 endTest();
204}
205
206
207function logResult(success, text)
208{
209 if (success)
210 consoleWrite(text + " <span style='color:green'>OK</span>");
211 else
212 consoleWrite(text + " <span style='color:red'>FAIL</span>");
213}
214
215function consoleWrite(text)
216{
217 if (testEnded)
218 return;
219 logConsole().innerHTML += text + "<br>";
220}
221
222function relativeURL(url)
223{
224 return url.substr(url.indexOf('/media/')+7);
225}
226
227
228function isInTimeRanges(ranges, time)
229{
230 var i = 0;
231 for (i = 0; i < ranges.length; ++i) {
232 if (time >= ranges.start(i) && time <= ranges.end(i)) {
233 return true;
234 }
235 }
236 return false;
237}