michael@0: /*
michael@0: Copyright (C) 2009 Apple Computer, Inc. All rights reserved.
michael@0:
michael@0: Redistribution and use in source and binary forms, with or without
michael@0: modification, are permitted provided that the following conditions
michael@0: are met:
michael@0: 1. Redistributions of source code must retain the above copyright
michael@0: notice, this list of conditions and the following disclaimer.
michael@0: 2. Redistributions in binary form must reproduce the above copyright
michael@0: notice, this list of conditions and the following disclaimer in the
michael@0: documentation and/or other materials provided with the distribution.
michael@0:
michael@0: THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
michael@0: EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
michael@0: IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
michael@0: PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
michael@0: CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
michael@0: EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
michael@0: PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
michael@0: PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
michael@0: OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
michael@0: (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
michael@0: OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
michael@0: */
michael@0:
michael@0: // WebKit Specfic code. Add your own here.
michael@0: function initNonKhronosFramework(waitUntilDone) {
michael@0: if (window.layoutTestController) {
michael@0: layoutTestController.overridePreference("WebKitWebGLEnabled", "1");
michael@0: layoutTestController.dumpAsText();
michael@0: if (waitUntilDone) {
michael@0: layoutTestController.waitUntilDone();
michael@0: }
michael@0: }
michael@0: }
michael@0:
michael@0: function nonKhronosFrameworkNotifyDone() {
michael@0: if (window.layoutTestController) {
michael@0: layoutTestController.notifyDone();
michael@0: }
michael@0: }
michael@0:
michael@0: function reportTestResultsToHarness(success, msg) {
michael@0: if (window.parent.webglTestHarness) {
michael@0: window.parent.webglTestHarness.reportResults(success, msg);
michael@0: }
michael@0: }
michael@0:
michael@0: function notifyFinishedToHarness() {
michael@0: if (window.parent.webglTestHarness) {
michael@0: window.parent.webglTestHarness.notifyFinished();
michael@0: }
michael@0: }
michael@0:
michael@0: function description(msg)
michael@0: {
michael@0: // For MSIE 6 compatibility
michael@0: var span = document.createElement("span");
michael@0: span.innerHTML = '
' + msg + '
On success, you will see a series of "PASS" messages, followed by "TEST COMPLETE".
';
michael@0: var description = document.getElementById("description");
michael@0: if (description.firstChild)
michael@0: description.replaceChild(span, description.firstChild);
michael@0: else
michael@0: description.appendChild(span);
michael@0: }
michael@0:
michael@0: function debug(msg)
michael@0: {
michael@0: var span = document.createElement("span");
michael@0: document.getElementById("console").appendChild(span); // insert it first so XHTML knows the namespace
michael@0: span.innerHTML = msg + '
';
michael@0: }
michael@0:
michael@0: function escapeHTML(text)
michael@0: {
michael@0: return text.replace(/&/g, "&").replace(/PASS ' + escapeHTML(msg) + '');
michael@0: }
michael@0:
michael@0: function testFailed(msg)
michael@0: {
michael@0: reportTestResultsToHarness(false, msg);
michael@0: debug('FAIL ' + escapeHTML(msg) + '');
michael@0: dump('FAIL: ' + msg + '\n');
michael@0:
michael@0: var stack = (new Error).stack.split('\n');
michael@0: if (!stack.length) {
michael@0: return;
michael@0: }
michael@0:
michael@0: dump('STACK TRACE: \n');
michael@0:
michael@0: stack.pop();
michael@0: var index = 0, frame, messages = new Array();
michael@0: // Match all .html files and print out the line in them.
michael@0: while (stack.length && index != -1) {
michael@0: frame = stack.pop();
michael@0: index = frame.indexOf(".html:");
michael@0: if (index != -1) {
michael@0: messages.unshift(frame);
michael@0: }
michael@0: }
michael@0:
michael@0: // Print out the first stack frame in JS and then stop.
michael@0: if (stack.length) {
michael@0: messages.unshift(stack.pop());
michael@0: }
michael@0:
michael@0: for (message in messages) {
michael@0: dump(messages[message] + '\n');
michael@0: }
michael@0: }
michael@0:
michael@0: function testFailedRender(msg, ref, test, width, height)
michael@0: {
michael@0: var refData;
michael@0: if (typeof ref.getImageData == 'function') {
michael@0: refData = ref.canvas.toDataURL();
michael@0: } else {
michael@0: refData = arrayToURLData(ref, width, height);
michael@0: }
michael@0:
michael@0: var testData;
michael@0: if (typeof test.getImageData == 'function') {
michael@0: testData = test.canvas.toDataURL();
michael@0: } else {
michael@0: testData = arrayToURLData(test, width, height);
michael@0: }
michael@0:
michael@0: testFailed(msg);
michael@0:
michael@0: var data = 'REFTEST TEST-DEBUG-INFO | ' + msg + ' | image comparison (==)\n' +
michael@0: 'REFTEST IMAGE 1 (TEST): ' + testData + '\n' +
michael@0: 'REFTEST IMAGE 2 (REFERENCE): ' + refData;
michael@0: dump('FAIL: ' + data + '\n');
michael@0: 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=' +
michael@0: encodeURIComponent(encodeURIComponent(data)) + '\n');
michael@0: }
michael@0:
michael@0: function arrayToURLData(buf, width, height)
michael@0: {
michael@0: var cv = document.createElement('canvas');
michael@0: cv.height = height;
michael@0: cv.width = width;
michael@0: var ctx = cv.getContext('2d');
michael@0: var imgd = ctx.getImageData(0, 0, width, height);
michael@0: for (i = 0; i < height * width; ++i) {
michael@0: offset = i * 4;
michael@0: for (j = 0; j < 4; j++) {
michael@0: imgd.data[offset + j] = buf[offset + j];
michael@0: }
michael@0: }
michael@0: ctx.putImageData(imgd, 0, 0);
michael@0: return cv.toDataURL();
michael@0: }
michael@0:
michael@0: function areArraysEqual(_a, _b)
michael@0: {
michael@0: try {
michael@0: if (_a.length !== _b.length)
michael@0: return false;
michael@0: for (var i = 0; i < _a.length; i++)
michael@0: if (_a[i] !== _b[i])
michael@0: return false;
michael@0: } catch (ex) {
michael@0: return false;
michael@0: }
michael@0: return true;
michael@0: }
michael@0:
michael@0: function isMinusZero(n)
michael@0: {
michael@0: // the only way to tell 0 from -0 in JS is the fact that 1/-0 is
michael@0: // -Infinity instead of Infinity
michael@0: return n === 0 && 1/n < 0;
michael@0: }
michael@0:
michael@0: function isResultCorrect(_actual, _expected)
michael@0: {
michael@0: if (_expected === 0)
michael@0: return _actual === _expected && (1/_actual) === (1/_expected);
michael@0: if (_actual === _expected)
michael@0: return true;
michael@0: if (typeof(_expected) == "number" && isNaN(_expected))
michael@0: return typeof(_actual) == "number" && isNaN(_actual);
michael@0: if (Object.prototype.toString.call(_expected) == Object.prototype.toString.call([]))
michael@0: return areArraysEqual(_actual, _expected);
michael@0: return false;
michael@0: }
michael@0:
michael@0: function stringify(v)
michael@0: {
michael@0: if (v === 0 && 1/v < 0)
michael@0: return "-0";
michael@0: else return "" + v;
michael@0: }
michael@0:
michael@0: function evalAndLog(_a)
michael@0: {
michael@0: if (typeof _a != "string")
michael@0: debug("WARN: tryAndLog() expects a string argument");
michael@0:
michael@0: // Log first in case things go horribly wrong or this causes a sync event.
michael@0: debug(_a);
michael@0:
michael@0: var _av;
michael@0: try {
michael@0: _av = eval(_a);
michael@0: } catch (e) {
michael@0: testFailed(_a + " threw exception " + e);
michael@0: }
michael@0: return _av;
michael@0: }
michael@0:
michael@0: function shouldBe(_a, _b, quiet)
michael@0: {
michael@0: if (typeof _a != "string" || typeof _b != "string")
michael@0: debug("WARN: shouldBe() expects string arguments");
michael@0: var exception;
michael@0: var _av;
michael@0: try {
michael@0: _av = eval(_a);
michael@0: } catch (e) {
michael@0: exception = e;
michael@0: }
michael@0: var _bv = eval(_b);
michael@0:
michael@0: if (exception)
michael@0: testFailed(_a + " should be " + _bv + ". Threw exception " + exception);
michael@0: else if (isResultCorrect(_av, _bv)) {
michael@0: if (!quiet) {
michael@0: testPassed(_a + " is " + _b);
michael@0: }
michael@0: } else if (typeof(_av) == typeof(_bv))
michael@0: testFailed(_a + " should be " + _bv + ". Was " + stringify(_av) + ".");
michael@0: else
michael@0: testFailed(_a + " should be " + _bv + " (of type " + typeof _bv + "). Was " + _av + " (of type " + typeof _av + ").");
michael@0: }
michael@0:
michael@0: function shouldNotBe(_a, _b, quiet)
michael@0: {
michael@0: if (typeof _a != "string" || typeof _b != "string")
michael@0: debug("WARN: shouldNotBe() expects string arguments");
michael@0: var exception;
michael@0: var _av;
michael@0: try {
michael@0: _av = eval(_a);
michael@0: } catch (e) {
michael@0: exception = e;
michael@0: }
michael@0: var _bv = eval(_b);
michael@0:
michael@0: if (exception)
michael@0: testFailed(_a + " should not be " + _bv + ". Threw exception " + exception);
michael@0: else if (!isResultCorrect(_av, _bv)) {
michael@0: if (!quiet) {
michael@0: testPassed(_a + " is not " + _b);
michael@0: }
michael@0: } else
michael@0: testFailed(_a + " should not be " + _bv + ".");
michael@0: }
michael@0:
michael@0: function shouldBeTrue(_a) { shouldBe(_a, "true"); }
michael@0: function shouldBeFalse(_a) { shouldBe(_a, "false"); }
michael@0: function shouldBeNaN(_a) { shouldBe(_a, "NaN"); }
michael@0: function shouldBeNull(_a) { shouldBe(_a, "null"); }
michael@0:
michael@0: function shouldBeEqualToString(a, b)
michael@0: {
michael@0: var unevaledString = '"' + b.replace(/"/g, "\"") + '"';
michael@0: shouldBe(a, unevaledString);
michael@0: }
michael@0:
michael@0: function shouldEvaluateTo(actual, expected) {
michael@0: // A general-purpose comparator. 'actual' should be a string to be
michael@0: // evaluated, as for shouldBe(). 'expected' may be any type and will be
michael@0: // used without being eval'ed.
michael@0: if (expected == null) {
michael@0: // Do this before the object test, since null is of type 'object'.
michael@0: shouldBeNull(actual);
michael@0: } else if (typeof expected == "undefined") {
michael@0: shouldBeUndefined(actual);
michael@0: } else if (typeof expected == "function") {
michael@0: // All this fuss is to avoid the string-arg warning from shouldBe().
michael@0: try {
michael@0: actualValue = eval(actual);
michael@0: } catch (e) {
michael@0: testFailed("Evaluating " + actual + ": Threw exception " + e);
michael@0: return;
michael@0: }
michael@0: shouldBe("'" + actualValue.toString().replace(/\n/g, "") + "'",
michael@0: "'" + expected.toString().replace(/\n/g, "") + "'");
michael@0: } else if (typeof expected == "object") {
michael@0: shouldBeTrue(actual + " == '" + expected + "'");
michael@0: } else if (typeof expected == "string") {
michael@0: shouldBe(actual, expected);
michael@0: } else if (typeof expected == "boolean") {
michael@0: shouldBe("typeof " + actual, "'boolean'");
michael@0: if (expected)
michael@0: shouldBeTrue(actual);
michael@0: else
michael@0: shouldBeFalse(actual);
michael@0: } else if (typeof expected == "number") {
michael@0: shouldBe(actual, stringify(expected));
michael@0: } else {
michael@0: debug(expected + " is unknown type " + typeof expected);
michael@0: shouldBeTrue(actual, "'" +expected.toString() + "'");
michael@0: }
michael@0: }
michael@0:
michael@0: function shouldBeNonZero(_a)
michael@0: {
michael@0: var exception;
michael@0: var _av;
michael@0: try {
michael@0: _av = eval(_a);
michael@0: } catch (e) {
michael@0: exception = e;
michael@0: }
michael@0:
michael@0: if (exception)
michael@0: testFailed(_a + " should be non-zero. Threw exception " + exception);
michael@0: else if (_av != 0)
michael@0: testPassed(_a + " is non-zero.");
michael@0: else
michael@0: testFailed(_a + " should be non-zero. Was " + _av);
michael@0: }
michael@0:
michael@0: function shouldBeNonNull(_a)
michael@0: {
michael@0: var exception;
michael@0: var _av;
michael@0: try {
michael@0: _av = eval(_a);
michael@0: } catch (e) {
michael@0: exception = e;
michael@0: }
michael@0:
michael@0: if (exception)
michael@0: testFailed(_a + " should be non-null. Threw exception " + exception);
michael@0: else if (_av != null)
michael@0: testPassed(_a + " is non-null.");
michael@0: else
michael@0: testFailed(_a + " should be non-null. Was " + _av);
michael@0: }
michael@0:
michael@0: function shouldBeUndefined(_a)
michael@0: {
michael@0: var exception;
michael@0: var _av;
michael@0: try {
michael@0: _av = eval(_a);
michael@0: } catch (e) {
michael@0: exception = e;
michael@0: }
michael@0:
michael@0: if (exception)
michael@0: testFailed(_a + " should be undefined. Threw exception " + exception);
michael@0: else if (typeof _av == "undefined")
michael@0: testPassed(_a + " is undefined.");
michael@0: else
michael@0: testFailed(_a + " should be undefined. Was " + _av);
michael@0: }
michael@0:
michael@0: function shouldBeDefined(_a)
michael@0: {
michael@0: var exception;
michael@0: var _av;
michael@0: try {
michael@0: _av = eval(_a);
michael@0: } catch (e) {
michael@0: exception = e;
michael@0: }
michael@0:
michael@0: if (exception)
michael@0: testFailed(_a + " should be defined. Threw exception " + exception);
michael@0: else if (_av !== undefined)
michael@0: testPassed(_a + " is defined.");
michael@0: else
michael@0: testFailed(_a + " should be defined. Was " + _av);
michael@0: }
michael@0:
michael@0: function shouldBeGreaterThanOrEqual(_a, _b) {
michael@0: if (typeof _a != "string" || typeof _b != "string")
michael@0: debug("WARN: shouldBeGreaterThanOrEqual expects string arguments");
michael@0:
michael@0: var exception;
michael@0: var _av;
michael@0: try {
michael@0: _av = eval(_a);
michael@0: } catch (e) {
michael@0: exception = e;
michael@0: }
michael@0: var _bv = eval(_b);
michael@0:
michael@0: if (exception)
michael@0: testFailed(_a + " should be >= " + _b + ". Threw exception " + exception);
michael@0: else if (typeof _av == "undefined" || _av < _bv)
michael@0: testFailed(_a + " should be >= " + _b + ". Was " + _av + " (of type " + typeof _av + ").");
michael@0: else
michael@0: testPassed(_a + " is >= " + _b);
michael@0: }
michael@0:
michael@0: function shouldThrow(_a, _e)
michael@0: {
michael@0: var exception;
michael@0: var _av;
michael@0: try {
michael@0: _av = eval(_a);
michael@0: } catch (e) {
michael@0: exception = e;
michael@0: }
michael@0:
michael@0: var _ev;
michael@0: if (_e)
michael@0: _ev = eval(_e);
michael@0:
michael@0: if (exception) {
michael@0: if (typeof _e == "undefined" || exception == _ev)
michael@0: testPassed(_a + " threw exception " + exception + ".");
michael@0: else
michael@0: testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Threw exception " + exception + ".");
michael@0: } else if (typeof _av == "undefined")
michael@0: testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was undefined.");
michael@0: else
michael@0: testFailed(_a + " should throw " + (typeof _e == "undefined" ? "an exception" : _ev) + ". Was " + _av + ".");
michael@0: }
michael@0:
michael@0: function assertMsg(assertion, msg) {
michael@0: if (assertion) {
michael@0: testPassed(msg);
michael@0: } else {
michael@0: testFailed(msg);
michael@0: }
michael@0: }
michael@0:
michael@0: function gc() {
michael@0: if (window.GCController) {
michael@0: window.GCController.collect();
michael@0: return;
michael@0: }
michael@0:
michael@0: if (window.opera && window.opera.collect) {
michael@0: window.opera.collect();
michael@0: return;
michael@0: }
michael@0:
michael@0: try {
michael@0: window.QueryInterface(Components.interfaces.nsIInterfaceRequestor)
michael@0: .getInterface(Components.interfaces.nsIDOMWindowUtils)
michael@0: .garbageCollect();
michael@0: return;
michael@0: } catch(e) {}
michael@0:
michael@0: function gcRec(n) {
michael@0: if (n < 1)
michael@0: return {};
michael@0: var temp = {i: "ab" + i + (i / 100000)};
michael@0: temp += "foo";
michael@0: gcRec(n-1);
michael@0: }
michael@0: for (var i = 0; i < 1000; i++)
michael@0: gcRec(10);
michael@0: }
michael@0:
michael@0: function finishTest() {
michael@0: debug('
TEST COMPLETE');
michael@0: notifyFinishedToHarness();
michael@0: }