1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/content/canvas/test/webgl-conformance/resources/js-test-pre.js Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,474 @@ 1.4 +/* 1.5 +Copyright (C) 2009 Apple Computer, Inc. All rights reserved. 1.6 + 1.7 +Redistribution and use in source and binary forms, with or without 1.8 +modification, are permitted provided that the following conditions 1.9 +are met: 1.10 +1. Redistributions of source code must retain the above copyright 1.11 + notice, this list of conditions and the following disclaimer. 1.12 +2. Redistributions in binary form must reproduce the above copyright 1.13 + notice, this list of conditions and the following disclaimer in the 1.14 + documentation and/or other materials provided with the distribution. 1.15 + 1.16 +THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY 1.17 +EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 1.18 +IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 1.19 +PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR 1.20 +CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, 1.21 +EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 1.22 +PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 1.23 +PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY 1.24 +OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 1.25 +(INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 1.26 +OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 1.27 +*/ 1.28 + 1.29 +// WebKit Specfic code. Add your own here. 1.30 +function initNonKhronosFramework(waitUntilDone) { 1.31 + if (window.layoutTestController) { 1.32 + layoutTestController.overridePreference("WebKitWebGLEnabled", "1"); 1.33 + layoutTestController.dumpAsText(); 1.34 + if (waitUntilDone) { 1.35 + layoutTestController.waitUntilDone(); 1.36 + } 1.37 + } 1.38 +} 1.39 + 1.40 +function nonKhronosFrameworkNotifyDone() { 1.41 + if (window.layoutTestController) { 1.42 + layoutTestController.notifyDone(); 1.43 + } 1.44 +} 1.45 + 1.46 +function reportTestResultsToHarness(success, msg) { 1.47 + if (window.parent.webglTestHarness) { 1.48 + window.parent.webglTestHarness.reportResults(success, msg); 1.49 + } 1.50 +} 1.51 + 1.52 +function notifyFinishedToHarness() { 1.53 + if (window.parent.webglTestHarness) { 1.54 + window.parent.webglTestHarness.notifyFinished(); 1.55 + } 1.56 +} 1.57 + 1.58 +function description(msg) 1.59 +{ 1.60 + // For MSIE 6 compatibility 1.61 + var span = document.createElement("span"); 1.62 + 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>'; 1.63 + var description = document.getElementById("description"); 1.64 + if (description.firstChild) 1.65 + description.replaceChild(span, description.firstChild); 1.66 + else 1.67 + description.appendChild(span); 1.68 +} 1.69 + 1.70 +function debug(msg) 1.71 +{ 1.72 + var span = document.createElement("span"); 1.73 + document.getElementById("console").appendChild(span); // insert it first so XHTML knows the namespace 1.74 + span.innerHTML = msg + '<br />'; 1.75 +} 1.76 + 1.77 +function escapeHTML(text) 1.78 +{ 1.79 + return text.replace(/&/g, "&").replace(/</g, "<"); 1.80 +} 1.81 + 1.82 +function testPassed(msg) 1.83 +{ 1.84 + reportTestResultsToHarness(true, msg); 1.85 + debug('<span><span class="pass">PASS</span> ' + escapeHTML(msg) + '</span>'); 1.86 +} 1.87 + 1.88 +function testFailed(msg) 1.89 +{ 1.90 + reportTestResultsToHarness(false, msg); 1.91 + debug('<span><span class="fail">FAIL</span> ' + escapeHTML(msg) + '</span>'); 1.92 + dump('FAIL: ' + msg + '\n'); 1.93 + 1.94 + var stack = (new Error).stack.split('\n'); 1.95 + if (!stack.length) { 1.96 + return; 1.97 + } 1.98 + 1.99 + dump('STACK TRACE: \n'); 1.100 + 1.101 + stack.pop(); 1.102 + var index = 0, frame, messages = new Array(); 1.103 + // Match all .html files and print out the line in them. 1.104 + while (stack.length && index != -1) { 1.105 + frame = stack.pop(); 1.106 + index = frame.indexOf(".html:"); 1.107 + if (index != -1) { 1.108 + messages.unshift(frame); 1.109 + } 1.110 + } 1.111 + 1.112 + // Print out the first stack frame in JS and then stop. 1.113 + if (stack.length) { 1.114 + messages.unshift(stack.pop()); 1.115 + } 1.116 + 1.117 + for (message in messages) { 1.118 + dump(messages[message] + '\n'); 1.119 + } 1.120 +} 1.121 + 1.122 +function testFailedRender(msg, ref, test, width, height) 1.123 +{ 1.124 + var refData; 1.125 + if (typeof ref.getImageData == 'function') { 1.126 + refData = ref.canvas.toDataURL(); 1.127 + } else { 1.128 + refData = arrayToURLData(ref, width, height); 1.129 + } 1.130 + 1.131 + var testData; 1.132 + if (typeof test.getImageData == 'function') { 1.133 + testData = test.canvas.toDataURL(); 1.134 + } else { 1.135 + testData = arrayToURLData(test, width, height); 1.136 + } 1.137 + 1.138 + testFailed(msg); 1.139 + 1.140 + var data = 'REFTEST TEST-DEBUG-INFO | ' + msg + ' | image comparison (==)\n' + 1.141 + 'REFTEST IMAGE 1 (TEST): ' + testData + '\n' + 1.142 + 'REFTEST IMAGE 2 (REFERENCE): ' + refData; 1.143 + dump('FAIL: ' + data + '\n'); 1.144 + dump('To view the differences between these image renderings, go to the following link: https://hg.mozilla.org/mozilla-central/raw-file/tip/layout/tools/reftest/reftest-analyzer.xhtml#log=' + 1.145 + encodeURIComponent(encodeURIComponent(data)) + '\n'); 1.146 +} 1.147 + 1.148 +function arrayToURLData(buf, width, height) 1.149 +{ 1.150 + var cv = document.createElement('canvas'); 1.151 + cv.height = height; 1.152 + cv.width = width; 1.153 + var ctx = cv.getContext('2d'); 1.154 + var imgd = ctx.getImageData(0, 0, width, height); 1.155 + for (i = 0; i < height * width; ++i) { 1.156 + offset = i * 4; 1.157 + for (j = 0; j < 4; j++) { 1.158 + imgd.data[offset + j] = buf[offset + j]; 1.159 + } 1.160 + } 1.161 + ctx.putImageData(imgd, 0, 0); 1.162 + return cv.toDataURL(); 1.163 +} 1.164 + 1.165 +function areArraysEqual(_a, _b) 1.166 +{ 1.167 + try { 1.168 + if (_a.length !== _b.length) 1.169 + return false; 1.170 + for (var i = 0; i < _a.length; i++) 1.171 + if (_a[i] !== _b[i]) 1.172 + return false; 1.173 + } catch (ex) { 1.174 + return false; 1.175 + } 1.176 + return true; 1.177 +} 1.178 + 1.179 +function isMinusZero(n) 1.180 +{ 1.181 + // the only way to tell 0 from -0 in JS is the fact that 1/-0 is 1.182 + // -Infinity instead of Infinity 1.183 + return n === 0 && 1/n < 0; 1.184 +} 1.185 + 1.186 +function isResultCorrect(_actual, _expected) 1.187 +{ 1.188 + if (_expected === 0) 1.189 + return _actual === _expected && (1/_actual) === (1/_expected); 1.190 + if (_actual === _expected) 1.191 + return true; 1.192 + if (typeof(_expected) == "number" && isNaN(_expected)) 1.193 + return typeof(_actual) == "number" && isNaN(_actual); 1.194 + if (Object.prototype.toString.call(_expected) == Object.prototype.toString.call([])) 1.195 + return areArraysEqual(_actual, _expected); 1.196 + return false; 1.197 +} 1.198 + 1.199 +function stringify(v) 1.200 +{ 1.201 + if (v === 0 && 1/v < 0) 1.202 + return "-0"; 1.203 + else return "" + v; 1.204 +} 1.205 + 1.206 +function evalAndLog(_a) 1.207 +{ 1.208 + if (typeof _a != "string") 1.209 + debug("WARN: tryAndLog() expects a string argument"); 1.210 + 1.211 + // Log first in case things go horribly wrong or this causes a sync event. 1.212 + debug(_a); 1.213 + 1.214 + var _av; 1.215 + try { 1.216 + _av = eval(_a); 1.217 + } catch (e) { 1.218 + testFailed(_a + " threw exception " + e); 1.219 + } 1.220 + return _av; 1.221 +} 1.222 + 1.223 +function shouldBe(_a, _b, quiet) 1.224 +{ 1.225 + if (typeof _a != "string" || typeof _b != "string") 1.226 + debug("WARN: shouldBe() expects string arguments"); 1.227 + var exception; 1.228 + var _av; 1.229 + try { 1.230 + _av = eval(_a); 1.231 + } catch (e) { 1.232 + exception = e; 1.233 + } 1.234 + var _bv = eval(_b); 1.235 + 1.236 + if (exception) 1.237 + testFailed(_a + " should be " + _bv + ". Threw exception " + exception); 1.238 + else if (isResultCorrect(_av, _bv)) { 1.239 + if (!quiet) { 1.240 + testPassed(_a + " is " + _b); 1.241 + } 1.242 + } else if (typeof(_av) == typeof(_bv)) 1.243 + testFailed(_a + " should be " + _bv + ". Was " + stringify(_av) + "."); 1.244 + else 1.245 + testFailed(_a + " should be " + _bv + " (of type " + typeof _bv + "). Was " + _av + " (of type " + typeof _av + ")."); 1.246 +} 1.247 + 1.248 +function shouldNotBe(_a, _b, quiet) 1.249 +{ 1.250 + if (typeof _a != "string" || typeof _b != "string") 1.251 + debug("WARN: shouldNotBe() expects string arguments"); 1.252 + var exception; 1.253 + var _av; 1.254 + try { 1.255 + _av = eval(_a); 1.256 + } catch (e) { 1.257 + exception = e; 1.258 + } 1.259 + var _bv = eval(_b); 1.260 + 1.261 + if (exception) 1.262 + testFailed(_a + " should not be " + _bv + ". Threw exception " + exception); 1.263 + else if (!isResultCorrect(_av, _bv)) { 1.264 + if (!quiet) { 1.265 + testPassed(_a + " is not " + _b); 1.266 + } 1.267 + } else 1.268 + testFailed(_a + " should not be " + _bv + "."); 1.269 +} 1.270 + 1.271 +function shouldBeTrue(_a) { shouldBe(_a, "true"); } 1.272 +function shouldBeFalse(_a) { shouldBe(_a, "false"); } 1.273 +function shouldBeNaN(_a) { shouldBe(_a, "NaN"); } 1.274 +function shouldBeNull(_a) { shouldBe(_a, "null"); } 1.275 + 1.276 +function shouldBeEqualToString(a, b) 1.277 +{ 1.278 + var unevaledString = '"' + b.replace(/"/g, "\"") + '"'; 1.279 + shouldBe(a, unevaledString); 1.280 +} 1.281 + 1.282 +function shouldEvaluateTo(actual, expected) { 1.283 + // A general-purpose comparator. 'actual' should be a string to be 1.284 + // evaluated, as for shouldBe(). 'expected' may be any type and will be 1.285 + // used without being eval'ed. 1.286 + if (expected == null) { 1.287 + // Do this before the object test, since null is of type 'object'. 1.288 + shouldBeNull(actual); 1.289 + } else if (typeof expected == "undefined") { 1.290 + shouldBeUndefined(actual); 1.291 + } else if (typeof expected == "function") { 1.292 + // All this fuss is to avoid the string-arg warning from shouldBe(). 1.293 + try { 1.294 + actualValue = eval(actual); 1.295 + } catch (e) { 1.296 + testFailed("Evaluating " + actual + ": Threw exception " + e); 1.297 + return; 1.298 + } 1.299 + shouldBe("'" + actualValue.toString().replace(/\n/g, "") + "'", 1.300 + "'" + expected.toString().replace(/\n/g, "") + "'"); 1.301 + } else if (typeof expected == "object") { 1.302 + shouldBeTrue(actual + " == '" + expected + "'"); 1.303 + } else if (typeof expected == "string") { 1.304 + shouldBe(actual, expected); 1.305 + } else if (typeof expected == "boolean") { 1.306 + shouldBe("typeof " + actual, "'boolean'"); 1.307 + if (expected) 1.308 + shouldBeTrue(actual); 1.309 + else 1.310 + shouldBeFalse(actual); 1.311 + } else if (typeof expected == "number") { 1.312 + shouldBe(actual, stringify(expected)); 1.313 + } else { 1.314 + debug(expected + " is unknown type " + typeof expected); 1.315 + shouldBeTrue(actual, "'" +expected.toString() + "'"); 1.316 + } 1.317 +} 1.318 + 1.319 +function shouldBeNonZero(_a) 1.320 +{ 1.321 + var exception; 1.322 + var _av; 1.323 + try { 1.324 + _av = eval(_a); 1.325 + } catch (e) { 1.326 + exception = e; 1.327 + } 1.328 + 1.329 + if (exception) 1.330 + testFailed(_a + " should be non-zero. Threw exception " + exception); 1.331 + else if (_av != 0) 1.332 + testPassed(_a + " is non-zero."); 1.333 + else 1.334 + testFailed(_a + " should be non-zero. Was " + _av); 1.335 +} 1.336 + 1.337 +function shouldBeNonNull(_a) 1.338 +{ 1.339 + var exception; 1.340 + var _av; 1.341 + try { 1.342 + _av = eval(_a); 1.343 + } catch (e) { 1.344 + exception = e; 1.345 + } 1.346 + 1.347 + if (exception) 1.348 + testFailed(_a + " should be non-null. Threw exception " + exception); 1.349 + else if (_av != null) 1.350 + testPassed(_a + " is non-null."); 1.351 + else 1.352 + testFailed(_a + " should be non-null. Was " + _av); 1.353 +} 1.354 + 1.355 +function shouldBeUndefined(_a) 1.356 +{ 1.357 + var exception; 1.358 + var _av; 1.359 + try { 1.360 + _av = eval(_a); 1.361 + } catch (e) { 1.362 + exception = e; 1.363 + } 1.364 + 1.365 + if (exception) 1.366 + testFailed(_a + " should be undefined. Threw exception " + exception); 1.367 + else if (typeof _av == "undefined") 1.368 + testPassed(_a + " is undefined."); 1.369 + else 1.370 + testFailed(_a + " should be undefined. Was " + _av); 1.371 +} 1.372 + 1.373 +function shouldBeDefined(_a) 1.374 +{ 1.375 + var exception; 1.376 + var _av; 1.377 + try { 1.378 + _av = eval(_a); 1.379 + } catch (e) { 1.380 + exception = e; 1.381 + } 1.382 + 1.383 + if (exception) 1.384 + testFailed(_a + " should be defined. Threw exception " + exception); 1.385 + else if (_av !== undefined) 1.386 + testPassed(_a + " is defined."); 1.387 + else 1.388 + testFailed(_a + " should be defined. Was " + _av); 1.389 +} 1.390 + 1.391 +function shouldBeGreaterThanOrEqual(_a, _b) { 1.392 + if (typeof _a != "string" || typeof _b != "string") 1.393 + debug("WARN: shouldBeGreaterThanOrEqual expects string arguments"); 1.394 + 1.395 + var exception; 1.396 + var _av; 1.397 + try { 1.398 + _av = eval(_a); 1.399 + } catch (e) { 1.400 + exception = e; 1.401 + } 1.402 + var _bv = eval(_b); 1.403 + 1.404 + if (exception) 1.405 + testFailed(_a + " should be >= " + _b + ". Threw exception " + exception); 1.406 + else if (typeof _av == "undefined" || _av < _bv) 1.407 + testFailed(_a + " should be >= " + _b + ". Was " + _av + " (of type " + typeof _av + ")."); 1.408 + else 1.409 + testPassed(_a + " is >= " + _b); 1.410 +} 1.411 + 1.412 +function shouldThrow(_a, _e) 1.413 +{ 1.414 + var exception; 1.415 + var _av; 1.416 + try { 1.417 + _av = eval(_a); 1.418 + } catch (e) { 1.419 + exception = e; 1.420 + } 1.421 + 1.422 + var _ev; 1.423 + if (_e) 1.424 + _ev = eval(_e); 1.425 + 1.426 + if (exception) { 1.427 + if (typeof _e == "undefined" || exception == _ev) 1.428 + testPassed(_a + " threw exception " + exception + "."); 1.429 + else 1.430 + testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Threw exception " + exception + "."); 1.431 + } else if (typeof _av == "undefined") 1.432 + testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was undefined."); 1.433 + else 1.434 + testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was " + _av + "."); 1.435 +} 1.436 + 1.437 +function assertMsg(assertion, msg) { 1.438 + if (assertion) { 1.439 + testPassed(msg); 1.440 + } else { 1.441 + testFailed(msg); 1.442 + } 1.443 +} 1.444 + 1.445 +function gc() { 1.446 + if (window.GCController) { 1.447 + window.GCController.collect(); 1.448 + return; 1.449 + } 1.450 + 1.451 + if (window.opera && window.opera.collect) { 1.452 + window.opera.collect(); 1.453 + return; 1.454 + } 1.455 + 1.456 + try { 1.457 + window.QueryInterface(Components.interfaces.nsIInterfaceRequestor) 1.458 + .getInterface(Components.interfaces.nsIDOMWindowUtils) 1.459 + .garbageCollect(); 1.460 + return; 1.461 + } catch(e) {} 1.462 + 1.463 + function gcRec(n) { 1.464 + if (n < 1) 1.465 + return {}; 1.466 + var temp = {i: "ab" + i + (i / 100000)}; 1.467 + temp += "foo"; 1.468 + gcRec(n-1); 1.469 + } 1.470 + for (var i = 0; i < 1000; i++) 1.471 + gcRec(10); 1.472 +} 1.473 + 1.474 +function finishTest() { 1.475 + debug('<br /><span class="pass">TEST COMPLETE</span>'); 1.476 + notifyFinishedToHarness(); 1.477 +}