2 ** Copyright (c) 2012 The Khronos Group Inc.
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:
12 ** The above copyright notice and this permission notice shall be included
13 ** in all copies or substantial portions of the Materials.
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.
25 var testHarnessInitialized = false;
27 var initNonKhronosFramework = function() {
28 if (testHarnessInitialized) {
31 testHarnessInitialized = true;
33 /* -- plaform specific code -- */
35 // WebKit Specific code. Add your code here.
36 if (window.testRunner && !window.layoutTestController) {
37 window.layoutTestController = window.testRunner;
40 if (window.layoutTestController) {
41 layoutTestController.overridePreference("WebKitWebGLEnabled", "1");
42 layoutTestController.dumpAsText();
43 layoutTestController.waitUntilDone();
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);
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;
59 /* -- end platform specific code --*/
62 this.initTestingHarness = function() {
63 initNonKhronosFramework();
67 function nonKhronosFrameworkNotifyDone() {
68 // WebKit Specific code. Add your code here.
69 if (window.layoutTestController) {
70 layoutTestController.notifyDone();
74 function reportTestResultsToHarness(success, msg) {
75 if (window.parent.webglTestHarness) {
76 window.parent.webglTestHarness.reportResults(window.location.pathname, success, msg);
80 function notifyFinishedToHarness() {
81 if (window.parent.webglTestHarness) {
82 window.parent.webglTestHarness.notifyFinished(window.location.pathname);
84 if (window.nonKhronosFrameworkNotifyDone) {
85 window.nonKhronosFrameworkNotifyDone();
89 function description(msg)
92 if (msg === undefined) {
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);
102 description.appendChild(span);
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 />';
112 function escapeHTML(text)
114 return text.replace(/&/g, "&").replace(/</g, "<");
117 function testPassed(msg)
119 reportTestResultsToHarness(true, msg);
120 debug('<span><span class="pass">PASS</span> ' + escapeHTML(msg) + '</span>');
123 function testFailed(msg)
125 reportTestResultsToHarness(false, msg);
126 debug('<span><span class="fail">FAIL</span> ' + escapeHTML(msg) + '</span>');
129 function areArraysEqual(_a, _b)
132 if (_a.length !== _b.length)
134 for (var i = 0; i < _a.length; i++)
143 function isMinusZero(n)
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;
150 function isResultCorrect(_actual, _expected)
153 return _actual === _expected && (1/_actual) === (1/_expected);
154 if (_actual === _expected)
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);
163 function stringify(v)
165 if (v === 0 && 1/v < 0)
170 function evalAndLog(_a)
172 if (typeof _a != "string")
173 debug("WARN: tryAndLog() expects a string argument");
175 // Log first in case things go horribly wrong or this causes a sync event.
182 testFailed(_a + " threw exception " + e);
187 function shouldBe(_a, _b, quiet)
189 if (typeof _a != "string" || typeof _b != "string")
190 debug("WARN: shouldBe() expects string arguments");
201 testFailed(_a + " should be " + _bv + ". Threw exception " + exception);
202 else if (isResultCorrect(_av, _bv)) {
204 testPassed(_a + " is " + _b);
206 } else if (typeof(_av) == typeof(_bv))
207 testFailed(_a + " should be " + _bv + ". Was " + stringify(_av) + ".");
209 testFailed(_a + " should be " + _bv + " (of type " + typeof _bv + "). Was " + _av + " (of type " + typeof _av + ").");
212 function shouldNotBe(_a, _b, quiet)
214 if (typeof _a != "string" || typeof _b != "string")
215 debug("WARN: shouldNotBe() expects string arguments");
226 testFailed(_a + " should not be " + _bv + ". Threw exception " + exception);
227 else if (!isResultCorrect(_av, _bv)) {
229 testPassed(_a + " is not " + _b);
232 testFailed(_a + " should not be " + _bv + ".");
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"); }
240 function shouldBeEqualToString(a, b)
242 var unevaledString = '"' + b.replace(/"/g, "\"") + '"';
243 shouldBe(a, unevaledString);
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().
258 actualValue = eval(actual);
260 testFailed("Evaluating " + actual + ": Threw exception " + e);
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'");
272 shouldBeTrue(actual);
274 shouldBeFalse(actual);
275 } else if (typeof expected == "number") {
276 shouldBe(actual, stringify(expected));
278 debug(expected + " is unknown type " + typeof expected);
279 shouldBeTrue(actual, "'" +expected.toString() + "'");
283 function shouldBeNonZero(_a)
294 testFailed(_a + " should be non-zero. Threw exception " + exception);
296 testPassed(_a + " is non-zero.");
298 testFailed(_a + " should be non-zero. Was " + _av);
301 function shouldBeNonNull(_a)
312 testFailed(_a + " should be non-null. Threw exception " + exception);
313 else if (_av != null)
314 testPassed(_a + " is non-null.");
316 testFailed(_a + " should be non-null. Was " + _av);
319 function shouldBeUndefined(_a)
330 testFailed(_a + " should be undefined. Threw exception " + exception);
331 else if (typeof _av == "undefined")
332 testPassed(_a + " is undefined.");
334 testFailed(_a + " should be undefined. Was " + _av);
337 function shouldBeDefined(_a)
348 testFailed(_a + " should be defined. Threw exception " + exception);
349 else if (_av !== undefined)
350 testPassed(_a + " is defined.");
352 testFailed(_a + " should be defined. Was " + _av);
355 function shouldBeGreaterThanOrEqual(_a, _b) {
356 if (typeof _a != "string" || typeof _b != "string")
357 debug("WARN: shouldBeGreaterThanOrEqual expects string arguments");
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 + ").");
373 testPassed(_a + " is >= " + _b);
376 function expectTrue(v, msg) {
384 function shouldThrow(_a, _e)
399 if (typeof _e == "undefined" || exception == _ev)
400 testPassed(_a + " threw exception " + exception + ".");
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.");
406 testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was " + _av + ".");
409 function shouldBeType(_a, _type) {
418 var _typev = eval(_type);
420 if(_typev === Number){
421 if(_av instanceof Number){
422 testPassed(_a + " is an instance of Number");
424 else if(typeof(_av) === 'number'){
425 testPassed(_a + " is an instance of Number");
428 testFailed(_a + " is not an instance of Number");
431 else if (_av instanceof _typev) {
432 testPassed(_a + " is an instance of " + _type);
434 testFailed(_a + " is not an instance of " + _type);
438 function assertMsg(assertion, msg) {
447 if (window.GCController) {
448 window.GCController.collect();
452 if (window.opera && window.opera.collect) {
453 window.opera.collect();
458 window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
459 .getInterface(Components.interfaces.nsIDOMWindowUtils)
467 var temp = {i: "ab" + i + (i / 100000)};
471 for (var i = 0; i < 1000; i++)
475 function finishTest() {
476 successfullyParsed = true;
477 var epilogue = document.createElement("script");
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;
484 if (src.substr(l - expectedBase.length) == expectedBase) {
485 basePath = src.substr(0, l - expectedBase.length);
489 epilogue.src = basePath + "js-test-post.js";
490 document.body.appendChild(epilogue);