2 testRunner.dumpAsText(true);
4 function _valToString(val)
6 if (val === undefined || val === null)
7 return '[' + typeof(val) + ']';
8 return val.toString() + '[' + typeof(val) + ']';
12 var _asserted = false;
16 document.getElementById('d').appendChild(document.createElement('li')).appendChild(document.createTextNode(text));
25 function _assert(cond, text)
29 _fail('Failed assertion: ' + text);
32 function _assertSame(a, b, text_a, text_b)
36 _fail('Failed assertion ' + text_a + ' === ' + text_b +
37 ' (got ' + _valToString(a) + ', expected ' + _valToString(b) + ')');
40 function _assertDifferent(a, b, text_a, text_b)
44 _fail('Failed assertion ' + text_a + ' !== ' + text_b +
45 ' (got ' + _valToString(a) + ', expected not ' + _valToString(b) + ')');
48 function _assertEqual(a, b, text_a, text_b)
52 _fail('Failed assertion ' + text_a + ' == ' + text_b +
53 ' (got ' + _valToString(a) + ', expected ' + _valToString(b) + ')');
56 function _assertMatch(a, b, text_a, text_b)
60 _fail('Failed assertion ' + text_a + ' matches ' + text_b +
61 ' (got ' + _valToString(a) + ')');
65 var _manual_check = false;
67 function _requireManualCheck()
74 _fail('Aborted due to predicted crash');
77 function _getPixel(canvas, x,y)
81 var ctx = canvas.getContext('2d');
82 var imgdata = ctx.getImageData(x, y, 1, 1);
83 return [ imgdata.data[0], imgdata.data[1], imgdata.data[2], imgdata.data[3] ];
87 // probably a security exception caused by having drawn
88 // data: URLs onto the canvas
94 function _assertPixel(canvas, x,y, r,g,b,a, pos, colour)
97 var c = _getPixel(canvas, x,y);
98 if (c && ! (c[0] == r && c[1] == g && c[2] == b && c[3] == a))
99 _fail('Failed assertion: got pixel [' + c + '] at ('+x+','+y+'), expected ['+r+','+g+','+b+','+a+']');
102 function _assertPixelApprox(canvas, x,y, r,g,b,a, pos, colour, tolerance)
105 var c = _getPixel(canvas, x,y);
108 var diff = Math.max(Math.abs(c[0]-r), Math.abs(c[1]-g), Math.abs(c[2]-b), Math.abs(c[3]-a));
109 if (diff > tolerance)
110 _fail('Failed assertion: got pixel [' + c + '] at ('+x+','+y+'), expected ['+r+','+g+','+b+','+a+'] +/- '+tolerance);
114 function _addTest(test)
116 var deferred = false;
117 window.deferTest = function () { deferred = true; };
120 if (_failed) // test failed
122 document.documentElement.className += ' fail';
123 window._testStatus = ['fail', document.getElementById('d').innerHTML];
125 else if (_manual_check || !_asserted)
126 { // test case explicitly asked for a manual check, or no automatic assertions were performed
127 document.getElementById('d').innerHTML += '<li>Cannot automatically verify result';
128 document.documentElement.className += ' needs_check';
129 window._testStatus = ['check', document.getElementById('d').innerHTML];
131 else // test succeeded
133 document.getElementById('d').innerHTML += '<li>Passed';
134 document.documentElement.className += ' pass';
135 window._testStatus = ['pass', document.getElementById('d').innerHTML];
138 window.endTest = endTest;
139 window.wrapFunction = function (f)
145 f.apply(null, arguments);
149 _fail('Aborted with exception: ' + e.message);
155 window.onload = function ()
159 var canvas = document.getElementById('c');
160 var ctx = canvas.getContext('2d');
165 _fail('Aborted with exception: ' + e.message);
166 deferred = false; // cancel any deference
174 function _assertGreen(ctx, canvasWidth, canvasHeight)
176 var testColor = function(d, idx, expected) {
177 _assertEqual(d[idx], expected, "d[" + idx + "]", String(expected));
179 var imagedata = ctx.getImageData(0, 0, canvasWidth, canvasHeight);
180 var w = imagedata.width, h = imagedata.height, d = imagedata.data;
181 for (var i = 0; i < h; ++i) {
182 for (var j = 0; j < w; ++j) {
183 testColor(d, 4 * (w * i + j) + 0, 0);
184 testColor(d, 4 * (w * i + j) + 1, 255);
185 testColor(d, 4 * (w * i + j) + 2, 0);
186 testColor(d, 4 * (w * i + j) + 3, 255);