Upstream version 7.36.149.0
[platform/framework/web/crosswalk.git] / src / third_party / webgl / src / sdk / tests / resources / js-test-pre.js
1 /*
2 ** Copyright (c) 2012 The Khronos Group Inc.
3 **
4 ** Permission is hereby granted, free of charge, to any person obtaining a
5 ** copy of this software and/or associated documentation files (the
6 ** "Materials"), to deal in the Materials without restriction, including
7 ** without limitation the rights to use, copy, modify, merge, publish,
8 ** distribute, sublicense, and/or sell copies of the Materials, and to
9 ** permit persons to whom the Materials are furnished to do so, subject to
10 ** the following conditions:
11 **
12 ** The above copyright notice and this permission notice shall be included
13 ** in all copies or substantial portions of the Materials.
14 **
15 ** THE MATERIALS ARE PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
16 ** EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
17 ** MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
18 ** IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
19 ** CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
20 ** TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
21 ** MATERIALS OR THE USE OR OTHER DEALINGS IN THE MATERIALS.
22 */
23
24 (function() {
25   var testHarnessInitialized = false;
26
27   var initNonKhronosFramework = function() {
28     if (testHarnessInitialized) {
29       return;
30     }
31     testHarnessInitialized = true;
32
33     /* -- plaform specific code -- */
34
35     // WebKit Specific code. Add your code here.
36     if (window.testRunner && !window.layoutTestController) {
37       window.layoutTestController = window.testRunner;
38     }
39
40     if (window.layoutTestController) {
41       layoutTestController.overridePreference("WebKitWebGLEnabled", "1");
42       layoutTestController.dumpAsText();
43       layoutTestController.waitUntilDone();
44     }
45     if (window.internals) {
46       // The WebKit testing system compares console output.
47       // Because the output of the WebGL Tests is GPU dependent
48       // we turn off console messages.
49       window.console.log = function() { };
50       window.console.error = function() { };
51       window.internals.settings.setWebGLErrorsToConsoleEnabled(false);
52
53       // RAF doesn't work in LayoutTests. Disable it so the tests will
54       // use setTimeout instead.
55       window.requestAnimationFrame = undefined;
56       window.webkitRequestAnimationFrame = undefined;
57     }
58
59     /* -- end platform specific code --*/
60   }
61
62   this.initTestingHarness = function() {
63     initNonKhronosFramework();
64   }
65 }());
66
67 function nonKhronosFrameworkNotifyDone() {
68   // WebKit Specific code. Add your code here.
69   if (window.layoutTestController) {
70     layoutTestController.notifyDone();
71   }
72 }
73
74 function reportTestResultsToHarness(success, msg) {
75   if (window.parent.webglTestHarness) {
76     window.parent.webglTestHarness.reportResults(window.location.pathname, success, msg);
77   }
78 }
79
80 function notifyFinishedToHarness() {
81   if (window.parent.webglTestHarness) {
82     window.parent.webglTestHarness.notifyFinished(window.location.pathname);
83   }
84   if (window.nonKhronosFrameworkNotifyDone) {
85     window.nonKhronosFrameworkNotifyDone();
86   }
87 }
88
89 function description(msg)
90 {
91     initTestingHarness();
92     if (msg === undefined) {
93       msg = document.title;
94     }
95     // For MSIE 6 compatibility
96     var span = document.createElement("span");
97     span.innerHTML = '<p>' + msg + '</p><p>On success, you will see a series of "<span class="pass">PASS</span>" messages, followed by "<span class="pass">TEST COMPLETE</span>".</p>';
98     var description = document.getElementById("description");
99     if (description.firstChild)
100         description.replaceChild(span, description.firstChild);
101     else
102         description.appendChild(span);
103 }
104
105 function debug(msg)
106 {
107     var span = document.createElement("span");
108     document.getElementById("console").appendChild(span); // insert it first so XHTML knows the namespace
109     span.innerHTML = msg + '<br />';
110 }
111
112 function escapeHTML(text)
113 {
114     return text.replace(/&/g, "&amp;").replace(/</g, "&lt;");
115 }
116
117 function testPassed(msg)
118 {
119     reportTestResultsToHarness(true, msg);
120     debug('<span><span class="pass">PASS</span> ' + escapeHTML(msg) + '</span>');
121 }
122
123 function testFailed(msg)
124 {
125     reportTestResultsToHarness(false, msg);
126     debug('<span><span class="fail">FAIL</span> ' + escapeHTML(msg) + '</span>');
127 }
128
129 function areArraysEqual(_a, _b)
130 {
131     try {
132         if (_a.length !== _b.length)
133             return false;
134         for (var i = 0; i < _a.length; i++)
135             if (_a[i] !== _b[i])
136                 return false;
137     } catch (ex) {
138         return false;
139     }
140     return true;
141 }
142
143 function isMinusZero(n)
144 {
145     // the only way to tell 0 from -0 in JS is the fact that 1/-0 is
146     // -Infinity instead of Infinity
147     return n === 0 && 1/n < 0;
148 }
149
150 function isResultCorrect(_actual, _expected)
151 {
152     if (_expected === 0)
153         return _actual === _expected && (1/_actual) === (1/_expected);
154     if (_actual === _expected)
155         return true;
156     if (typeof(_expected) == "number" && isNaN(_expected))
157         return typeof(_actual) == "number" && isNaN(_actual);
158     if (Object.prototype.toString.call(_expected) == Object.prototype.toString.call([]))
159         return areArraysEqual(_actual, _expected);
160     return false;
161 }
162
163 function stringify(v)
164 {
165     if (v === 0 && 1/v < 0)
166         return "-0";
167     else return "" + v;
168 }
169
170 function evalAndLog(_a)
171 {
172   if (typeof _a != "string")
173     debug("WARN: tryAndLog() expects a string argument");
174
175   // Log first in case things go horribly wrong or this causes a sync event.
176   debug(_a);
177
178   var _av;
179   try {
180      _av = eval(_a);
181   } catch (e) {
182     testFailed(_a + " threw exception " + e);
183   }
184   return _av;
185 }
186
187 function shouldBe(_a, _b, quiet)
188 {
189     if (typeof _a != "string" || typeof _b != "string")
190         debug("WARN: shouldBe() expects string arguments");
191     var exception;
192     var _av;
193     try {
194         _av = eval(_a);
195     } catch (e) {
196         exception = e;
197     }
198     var _bv = eval(_b);
199
200     if (exception)
201         testFailed(_a + " should be " + _bv + ". Threw exception " + exception);
202     else if (isResultCorrect(_av, _bv)) {
203         if (!quiet) {
204             testPassed(_a + " is " + _b);
205         }
206     } else if (typeof(_av) == typeof(_bv))
207         testFailed(_a + " should be " + _bv + ". Was " + stringify(_av) + ".");
208     else
209         testFailed(_a + " should be " + _bv + " (of type " + typeof _bv + "). Was " + _av + " (of type " + typeof _av + ").");
210 }
211
212 function shouldNotBe(_a, _b, quiet)
213 {
214     if (typeof _a != "string" || typeof _b != "string")
215         debug("WARN: shouldNotBe() expects string arguments");
216     var exception;
217     var _av;
218     try {
219         _av = eval(_a);
220     } catch (e) {
221         exception = e;
222     }
223     var _bv = eval(_b);
224
225     if (exception)
226         testFailed(_a + " should not be " + _bv + ". Threw exception " + exception);
227     else if (!isResultCorrect(_av, _bv)) {
228         if (!quiet) {
229             testPassed(_a + " is not " + _b);
230         }
231     } else
232         testFailed(_a + " should not be " + _bv + ".");
233 }
234
235 function shouldBeTrue(_a) { shouldBe(_a, "true"); }
236 function shouldBeFalse(_a) { shouldBe(_a, "false"); }
237 function shouldBeNaN(_a) { shouldBe(_a, "NaN"); }
238 function shouldBeNull(_a) { shouldBe(_a, "null"); }
239
240 function shouldBeEqualToString(a, b)
241 {
242   var unevaledString = '"' + b.replace(/"/g, "\"") + '"';
243   shouldBe(a, unevaledString);
244 }
245
246 function shouldEvaluateTo(actual, expected) {
247   // A general-purpose comparator.  'actual' should be a string to be
248   // evaluated, as for shouldBe(). 'expected' may be any type and will be
249   // used without being eval'ed.
250   if (expected == null) {
251     // Do this before the object test, since null is of type 'object'.
252     shouldBeNull(actual);
253   } else if (typeof expected == "undefined") {
254     shouldBeUndefined(actual);
255   } else if (typeof expected == "function") {
256     // All this fuss is to avoid the string-arg warning from shouldBe().
257     try {
258       actualValue = eval(actual);
259     } catch (e) {
260       testFailed("Evaluating " + actual + ": Threw exception " + e);
261       return;
262     }
263     shouldBe("'" + actualValue.toString().replace(/\n/g, "") + "'",
264              "'" + expected.toString().replace(/\n/g, "") + "'");
265   } else if (typeof expected == "object") {
266     shouldBeTrue(actual + " == '" + expected + "'");
267   } else if (typeof expected == "string") {
268     shouldBe(actual, expected);
269   } else if (typeof expected == "boolean") {
270     shouldBe("typeof " + actual, "'boolean'");
271     if (expected)
272       shouldBeTrue(actual);
273     else
274       shouldBeFalse(actual);
275   } else if (typeof expected == "number") {
276     shouldBe(actual, stringify(expected));
277   } else {
278     debug(expected + " is unknown type " + typeof expected);
279     shouldBeTrue(actual, "'"  +expected.toString() + "'");
280   }
281 }
282
283 function shouldBeNonZero(_a)
284 {
285   var exception;
286   var _av;
287   try {
288      _av = eval(_a);
289   } catch (e) {
290      exception = e;
291   }
292
293   if (exception)
294     testFailed(_a + " should be non-zero. Threw exception " + exception);
295   else if (_av != 0)
296     testPassed(_a + " is non-zero.");
297   else
298     testFailed(_a + " should be non-zero. Was " + _av);
299 }
300
301 function shouldBeNonNull(_a)
302 {
303   var exception;
304   var _av;
305   try {
306      _av = eval(_a);
307   } catch (e) {
308      exception = e;
309   }
310
311   if (exception)
312     testFailed(_a + " should be non-null. Threw exception " + exception);
313   else if (_av != null)
314     testPassed(_a + " is non-null.");
315   else
316     testFailed(_a + " should be non-null. Was " + _av);
317 }
318
319 function shouldBeUndefined(_a)
320 {
321   var exception;
322   var _av;
323   try {
324      _av = eval(_a);
325   } catch (e) {
326      exception = e;
327   }
328
329   if (exception)
330     testFailed(_a + " should be undefined. Threw exception " + exception);
331   else if (typeof _av == "undefined")
332     testPassed(_a + " is undefined.");
333   else
334     testFailed(_a + " should be undefined. Was " + _av);
335 }
336
337 function shouldBeDefined(_a)
338 {
339   var exception;
340   var _av;
341   try {
342      _av = eval(_a);
343   } catch (e) {
344      exception = e;
345   }
346
347   if (exception)
348     testFailed(_a + " should be defined. Threw exception " + exception);
349   else if (_av !== undefined)
350     testPassed(_a + " is defined.");
351   else
352     testFailed(_a + " should be defined. Was " + _av);
353 }
354
355 function shouldBeGreaterThanOrEqual(_a, _b) {
356     if (typeof _a != "string" || typeof _b != "string")
357         debug("WARN: shouldBeGreaterThanOrEqual expects string arguments");
358
359     var exception;
360     var _av;
361     try {
362         _av = eval(_a);
363     } catch (e) {
364         exception = e;
365     }
366     var _bv = eval(_b);
367
368     if (exception)
369         testFailed(_a + " should be >= " + _b + ". Threw exception " + exception);
370     else if (typeof _av == "undefined" || _av < _bv)
371         testFailed(_a + " should be >= " + _b + ". Was " + _av + " (of type " + typeof _av + ").");
372     else
373         testPassed(_a + " is >= " + _b);
374 }
375
376 function expectTrue(v, msg) {
377   if (v) {
378     testPassed(msg);
379   } else {
380     testFailed(msg);
381   }
382 }
383
384 function shouldThrow(_a, _e)
385 {
386   var exception;
387   var _av;
388   try {
389      _av = eval(_a);
390   } catch (e) {
391      exception = e;
392   }
393
394   var _ev;
395   if (_e)
396       _ev =  eval(_e);
397
398   if (exception) {
399     if (typeof _e == "undefined" || exception == _ev)
400       testPassed(_a + " threw exception " + exception + ".");
401     else
402       testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Threw exception " + exception + ".");
403   } else if (typeof _av == "undefined")
404     testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was undefined.");
405   else
406     testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was " + _av + ".");
407 }
408
409 function shouldBeType(_a, _type) {
410     var exception;
411     var _av;
412     try {
413         _av = eval(_a);
414     } catch (e) {
415         exception = e;
416     }
417
418     var _typev = eval(_type);
419
420     if(_typev === Number){
421         if(_av instanceof Number){
422             testPassed(_a + " is an instance of Number");
423         }
424         else if(typeof(_av) === 'number'){
425             testPassed(_a + " is an instance of Number");
426         }
427         else{
428             testFailed(_a + " is not an instance of Number");
429         }
430     }
431     else if (_av instanceof _typev) {
432         testPassed(_a + " is an instance of " + _type);
433     } else {
434         testFailed(_a + " is not an instance of " + _type);
435     }
436 }
437
438 function assertMsg(assertion, msg) {
439     if (assertion) {
440         testPassed(msg);
441     } else {
442         testFailed(msg);
443     }
444 }
445
446 function gc() {
447     if (window.GCController) {
448         window.GCController.collect();
449         return;
450     }
451
452     if (window.opera && window.opera.collect) {
453         window.opera.collect();
454         return;
455     }
456
457     try {
458         window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
459               .getInterface(Components.interfaces.nsIDOMWindowUtils)
460               .garbageCollect();
461         return;
462     } catch(e) {}
463
464     function gcRec(n) {
465         if (n < 1)
466             return {};
467         var temp = {i: "ab" + i + (i / 100000)};
468         temp += "foo";
469         gcRec(n-1);
470     }
471     for (var i = 0; i < 1000; i++)
472         gcRec(10);
473 }
474
475 function finishTest() {
476   successfullyParsed = true;
477   var epilogue = document.createElement("script");
478   var basePath = "";
479   var expectedBase = "js-test-pre.js";
480   var scripts = document.getElementsByTagName('script');
481   for (var script, i = 0; script = scripts[i]; i++) {
482     var src = script.src;
483     var l = src.length;
484     if (src.substr(l - expectedBase.length) == expectedBase) {
485       basePath = src.substr(0, l - expectedBase.length);
486       break;
487     }
488   }
489   epilogue.src = basePath + "js-test-post.js";
490   document.body.appendChild(epilogue);
491 }
492