Upstream version 10.39.225.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 _logToConsole(msg)
90 {
91     if (window.console)
92       window.console.log(msg);
93 }
94
95 var _jsTestPreVerboseLogging = false;
96
97 function enableJSTestPreVerboseLogging()
98 {
99     _jsTestPreVerboseLogging = true;
100 }
101
102 function description(msg)
103 {
104     initTestingHarness();
105     if (msg === undefined) {
106       msg = document.title;
107     }
108     // For MSIE 6 compatibility
109     var span = document.createElement("span");
110     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>';
111     var description = document.getElementById("description");
112     if (description.firstChild)
113         description.replaceChild(span, description.firstChild);
114     else
115         description.appendChild(span);
116     if (_jsTestPreVerboseLogging) {
117         _logToConsole(msg);
118     }
119 }
120
121 function _addSpan(contents)
122 {
123     var span = document.createElement("span");
124     document.getElementById("console").appendChild(span); // insert it first so XHTML knows the namespace
125     span.innerHTML = contents + '<br />';
126 }
127
128 function debug(msg)
129 {
130     _addSpan(msg);
131     if (_jsTestPreVerboseLogging) {
132         _logToConsole(msg);
133     }
134 }
135
136 function escapeHTML(text)
137 {
138     return text.replace(/&/g, "&amp;").replace(/</g, "&lt;");
139 }
140
141 function testPassed(msg)
142 {
143     reportTestResultsToHarness(true, msg);
144     _addSpan('<span><span class="pass">PASS</span> ' + escapeHTML(msg) + '</span>');
145     if (_jsTestPreVerboseLogging) {
146         _logToConsole('PASS ' + msg);
147     }
148 }
149
150 function testFailed(msg)
151 {
152     reportTestResultsToHarness(false, msg);
153     _addSpan('<span><span class="fail">FAIL</span> ' + escapeHTML(msg) + '</span>');
154     _logToConsole('FAIL ' + msg);
155 }
156
157 function areArraysEqual(_a, _b)
158 {
159     try {
160         if (_a.length !== _b.length)
161             return false;
162         for (var i = 0; i < _a.length; i++)
163             if (_a[i] !== _b[i])
164                 return false;
165     } catch (ex) {
166         return false;
167     }
168     return true;
169 }
170
171 function isMinusZero(n)
172 {
173     // the only way to tell 0 from -0 in JS is the fact that 1/-0 is
174     // -Infinity instead of Infinity
175     return n === 0 && 1/n < 0;
176 }
177
178 function isResultCorrect(_actual, _expected)
179 {
180     if (_expected === 0)
181         return _actual === _expected && (1/_actual) === (1/_expected);
182     if (_actual === _expected)
183         return true;
184     if (typeof(_expected) == "number" && isNaN(_expected))
185         return typeof(_actual) == "number" && isNaN(_actual);
186     if (Object.prototype.toString.call(_expected) == Object.prototype.toString.call([]))
187         return areArraysEqual(_actual, _expected);
188     return false;
189 }
190
191 function stringify(v)
192 {
193     if (v === 0 && 1/v < 0)
194         return "-0";
195     else return "" + v;
196 }
197
198 function evalAndLog(_a)
199 {
200   if (typeof _a != "string")
201     debug("WARN: tryAndLog() expects a string argument");
202
203   // Log first in case things go horribly wrong or this causes a sync event.
204   debug(_a);
205
206   var _av;
207   try {
208      _av = eval(_a);
209   } catch (e) {
210     testFailed(_a + " threw exception " + e);
211   }
212   return _av;
213 }
214
215 function shouldBe(_a, _b, quiet)
216 {
217     if (typeof _a != "string" || typeof _b != "string")
218         debug("WARN: shouldBe() expects string arguments");
219     var exception;
220     var _av;
221     try {
222         _av = eval(_a);
223     } catch (e) {
224         exception = e;
225     }
226     var _bv = eval(_b);
227
228     if (exception)
229         testFailed(_a + " should be " + _bv + ". Threw exception " + exception);
230     else if (isResultCorrect(_av, _bv)) {
231         if (!quiet) {
232             testPassed(_a + " is " + _b);
233         }
234     } else if (typeof(_av) == typeof(_bv))
235         testFailed(_a + " should be " + _bv + ". Was " + stringify(_av) + ".");
236     else
237         testFailed(_a + " should be " + _bv + " (of type " + typeof _bv + "). Was " + _av + " (of type " + typeof _av + ").");
238 }
239
240 function shouldNotBe(_a, _b, quiet)
241 {
242     if (typeof _a != "string" || typeof _b != "string")
243         debug("WARN: shouldNotBe() expects string arguments");
244     var exception;
245     var _av;
246     try {
247         _av = eval(_a);
248     } catch (e) {
249         exception = e;
250     }
251     var _bv = eval(_b);
252
253     if (exception)
254         testFailed(_a + " should not be " + _bv + ". Threw exception " + exception);
255     else if (!isResultCorrect(_av, _bv)) {
256         if (!quiet) {
257             testPassed(_a + " is not " + _b);
258         }
259     } else
260         testFailed(_a + " should not be " + _bv + ".");
261 }
262
263 function shouldBeTrue(_a) { shouldBe(_a, "true"); }
264 function shouldBeFalse(_a) { shouldBe(_a, "false"); }
265 function shouldBeNaN(_a) { shouldBe(_a, "NaN"); }
266 function shouldBeNull(_a) { shouldBe(_a, "null"); }
267
268 function shouldBeEqualToString(a, b)
269 {
270   var unevaledString = '"' + b.replace(/"/g, "\"") + '"';
271   shouldBe(a, unevaledString);
272 }
273
274 function shouldEvaluateTo(actual, expected) {
275   // A general-purpose comparator.  'actual' should be a string to be
276   // evaluated, as for shouldBe(). 'expected' may be any type and will be
277   // used without being eval'ed.
278   if (expected == null) {
279     // Do this before the object test, since null is of type 'object'.
280     shouldBeNull(actual);
281   } else if (typeof expected == "undefined") {
282     shouldBeUndefined(actual);
283   } else if (typeof expected == "function") {
284     // All this fuss is to avoid the string-arg warning from shouldBe().
285     try {
286       actualValue = eval(actual);
287     } catch (e) {
288       testFailed("Evaluating " + actual + ": Threw exception " + e);
289       return;
290     }
291     shouldBe("'" + actualValue.toString().replace(/\n/g, "") + "'",
292              "'" + expected.toString().replace(/\n/g, "") + "'");
293   } else if (typeof expected == "object") {
294     shouldBeTrue(actual + " == '" + expected + "'");
295   } else if (typeof expected == "string") {
296     shouldBe(actual, expected);
297   } else if (typeof expected == "boolean") {
298     shouldBe("typeof " + actual, "'boolean'");
299     if (expected)
300       shouldBeTrue(actual);
301     else
302       shouldBeFalse(actual);
303   } else if (typeof expected == "number") {
304     shouldBe(actual, stringify(expected));
305   } else {
306     debug(expected + " is unknown type " + typeof expected);
307     shouldBeTrue(actual, "'"  +expected.toString() + "'");
308   }
309 }
310
311 function shouldBeNonZero(_a)
312 {
313   var exception;
314   var _av;
315   try {
316      _av = eval(_a);
317   } catch (e) {
318      exception = e;
319   }
320
321   if (exception)
322     testFailed(_a + " should be non-zero. Threw exception " + exception);
323   else if (_av != 0)
324     testPassed(_a + " is non-zero.");
325   else
326     testFailed(_a + " should be non-zero. Was " + _av);
327 }
328
329 function shouldBeNonNull(_a)
330 {
331   var exception;
332   var _av;
333   try {
334      _av = eval(_a);
335   } catch (e) {
336      exception = e;
337   }
338
339   if (exception)
340     testFailed(_a + " should be non-null. Threw exception " + exception);
341   else if (_av != null)
342     testPassed(_a + " is non-null.");
343   else
344     testFailed(_a + " should be non-null. Was " + _av);
345 }
346
347 function shouldBeUndefined(_a)
348 {
349   var exception;
350   var _av;
351   try {
352      _av = eval(_a);
353   } catch (e) {
354      exception = e;
355   }
356
357   if (exception)
358     testFailed(_a + " should be undefined. Threw exception " + exception);
359   else if (typeof _av == "undefined")
360     testPassed(_a + " is undefined.");
361   else
362     testFailed(_a + " should be undefined. Was " + _av);
363 }
364
365 function shouldBeDefined(_a)
366 {
367   var exception;
368   var _av;
369   try {
370      _av = eval(_a);
371   } catch (e) {
372      exception = e;
373   }
374
375   if (exception)
376     testFailed(_a + " should be defined. Threw exception " + exception);
377   else if (_av !== undefined)
378     testPassed(_a + " is defined.");
379   else
380     testFailed(_a + " should be defined. Was " + _av);
381 }
382
383 function shouldBeGreaterThanOrEqual(_a, _b) {
384     if (typeof _a != "string" || typeof _b != "string")
385         debug("WARN: shouldBeGreaterThanOrEqual expects string arguments");
386
387     var exception;
388     var _av;
389     try {
390         _av = eval(_a);
391     } catch (e) {
392         exception = e;
393     }
394     var _bv = eval(_b);
395
396     if (exception)
397         testFailed(_a + " should be >= " + _b + ". Threw exception " + exception);
398     else if (typeof _av == "undefined" || _av < _bv)
399         testFailed(_a + " should be >= " + _b + ". Was " + _av + " (of type " + typeof _av + ").");
400     else
401         testPassed(_a + " is >= " + _b);
402 }
403
404 function expectTrue(v, msg) {
405   if (v) {
406     testPassed(msg);
407   } else {
408     testFailed(msg);
409   }
410 }
411
412 function shouldThrow(_a, _e)
413 {
414   var exception;
415   var _av;
416   try {
417      _av = eval(_a);
418   } catch (e) {
419      exception = e;
420   }
421
422   var _ev;
423   if (_e)
424       _ev =  eval(_e);
425
426   if (exception) {
427     if (typeof _e == "undefined" || exception == _ev)
428       testPassed(_a + " threw exception " + exception + ".");
429     else
430       testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Threw exception " + exception + ".");
431   } else if (typeof _av == "undefined")
432     testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was undefined.");
433   else
434     testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was " + _av + ".");
435 }
436
437 function shouldBeType(_a, _type) {
438     var exception;
439     var _av;
440     try {
441         _av = eval(_a);
442     } catch (e) {
443         exception = e;
444     }
445
446     var _typev = eval(_type);
447
448     if(_typev === Number){
449         if(_av instanceof Number){
450             testPassed(_a + " is an instance of Number");
451         }
452         else if(typeof(_av) === 'number'){
453             testPassed(_a + " is an instance of Number");
454         }
455         else{
456             testFailed(_a + " is not an instance of Number");
457         }
458     }
459     else if (_av instanceof _typev) {
460         testPassed(_a + " is an instance of " + _type);
461     } else {
462         testFailed(_a + " is not an instance of " + _type);
463     }
464 }
465
466 function assertMsg(assertion, msg) {
467     if (assertion) {
468         testPassed(msg);
469     } else {
470         testFailed(msg);
471     }
472 }
473
474 function gc() {
475     if (window.GCController) {
476         window.GCController.collect();
477         return;
478     }
479
480     if (window.opera && window.opera.collect) {
481         window.opera.collect();
482         return;
483     }
484
485     try {
486         window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
487               .getInterface(Components.interfaces.nsIDOMWindowUtils)
488               .garbageCollect();
489         return;
490     } catch(e) {}
491
492     function gcRec(n) {
493         if (n < 1)
494             return {};
495         var temp = {i: "ab" + i + (i / 100000)};
496         temp += "foo";
497         gcRec(n-1);
498     }
499     for (var i = 0; i < 1000; i++)
500         gcRec(10);
501 }
502
503 function finishTest() {
504   successfullyParsed = true;
505   var epilogue = document.createElement("script");
506   var basePath = "";
507   var expectedBase = "js-test-pre.js";
508   var scripts = document.getElementsByTagName('script');
509   for (var script, i = 0; script = scripts[i]; i++) {
510     var src = script.src;
511     var l = src.length;
512     if (src.substr(l - expectedBase.length) == expectedBase) {
513       basePath = src.substr(0, l - expectedBase.length);
514       break;
515     }
516   }
517   epilogue.src = basePath + "js-test-post.js";
518   document.body.appendChild(epilogue);
519 }
520