1 function _valToString(val)
3 if (val === undefined || val === null)
4 return '[' + typeof(val) + ']';
5 return val.toString() + '[' + typeof(val) + ']';
9 0:0, 1:1, 2:2, 3:3, 4:4, 5:5, 6:6, 7:7, 8:8, 9:9,
10 a:10, b:11, c:12, d:13, e:14, f:15,
11 A:10, B:11, C:12, D:13, E:14, F:15
13 function _hex2dec(hex)
15 return _hex2dec_table[hex.charAt(0)]*16 + _hex2dec_table[hex.charAt(1)];
19 var _asserted = false;
22 document.getElementById('d').appendChild(document.createElement('li')).appendChild(document.createTextNode(text));
30 function _assert(cond, text)
34 _fail('Failed assertion: ' + text);
37 function _assertSame(a, b, text_a, text_b)
41 _fail('Failed assertion ' + text_a + ' === ' + text_b +
42 ' (got ' + _valToString(a) + ', expected ' + _valToString(b) + ')');
45 function _assertDifferent(a, b, text_a, text_b)
49 _fail('Failed assertion ' + text_a + ' !== ' + text_b +
50 ' (got ' + _valToString(a) + ', expected not ' + _valToString(b) + ')');
53 function _assertEqual(a, b, text_a, text_b)
57 _fail('Failed assertion ' + text_a + ' == ' + text_b +
58 ' (got ' + _valToString(a) + ', expected ' + _valToString(b) + ')');
61 function _assertMatch(a, b, text_a, text_b)
65 _fail('Failed assertion ' + text_a + ' matches ' + text_b +
66 ' (got ' + _valToString(a) + ')');
70 var _manual_check = false;
72 function _requireManualCheck()
79 _fail('Aborted due to predicted crash');
82 var _getImageDataCalibrated = false;
83 var _getImageDataIsPremul, _getImageDataIsBGRA;
85 function _getPixel(canvas, x,y)
87 var ctx = canvas.getContext('2d');
88 if (ctx && typeof(ctx.getImageData) != 'undefined')
91 var imgdata = ctx.getImageData(x, y, 1, 1);
93 // probably a security exception caused by having drawn
94 // data: URLs onto the canvas
99 // Work around getImageData bugs, since we want the other tests to
100 // carry on working as well as possible
101 if (! _getImageDataCalibrated)
103 var c2 = document.createElement('canvas');
104 c2.width = c2.height = 1;
105 var ctx2 = c2.getContext('2d');
106 ctx2.fillStyle = 'rgba(0, 255, 255, 0.5)';
107 ctx2.fillRect(0, 0, 1, 1);
108 var data2 = ctx2.getImageData(0, 0, 1, 1).data;
110 // Firefox returns premultiplied alpha
112 if (data2[1] > 100 && data2[1] < 150)
113 _getImageDataIsPremul = true;
115 _getImageDataIsPremul = false;
117 // Opera Mini 4 Beta returns BGRA instead of RGBA
119 if (data2[0] > 250 && data2[2] < 5)
120 _getImageDataIsBGRA = true;
122 _getImageDataIsBGRA = false;
124 _getImageDataCalibrated = true;
127 // Undo the BGRA flipping
128 var rgba = (_getImageDataIsBGRA
129 ? [ imgdata.data[2], imgdata.data[1], imgdata.data[0], imgdata.data[3] ]
130 : [ imgdata.data[0], imgdata.data[1], imgdata.data[2], imgdata.data[3] ]);
132 if (! _getImageDataIsPremul)
135 // Undo the premultiplying
137 return [ 0, 0, 0, 0 ];
140 var a = rgba[3] / 255;
142 Math.round(rgba[0]/a),
143 Math.round(rgba[1]/a),
144 Math.round(rgba[2]/a),
151 try { ctx = canvas.getContext('opera-2dgame'); } catch (e) { /* Firefox throws */ }
152 if (ctx && typeof(ctx.getPixel) != 'undefined')
155 var c = ctx.getPixel(x, y);
157 // probably a security exception caused by having drawn
158 // data: URLs onto the canvas
163 var matches = /^rgba\((\d+), (\d+), (\d+), ([\d\.]+)\)$/.exec(c);
165 return [ matches[1], matches[2], matches[3], Math.round(matches[4]*255) ];
166 matches = /^#(..)(..)(..)$/.exec(c);
168 return [ _hex2dec(matches[1]), _hex2dec(matches[2]), _hex2dec(matches[3]), 255 ];
171 //_warn("(Can't test pixel value)");
172 _manual_check = true;
176 function _assertPixel(canvas, x,y, r,g,b,a, pos, colour)
179 var c = _getPixel(canvas, x,y);
180 if (c && ! (c[0] == r && c[1] == g && c[2] == b && c[3] == a))
181 _fail('Failed assertion: got pixel [' + c + '] at ('+x+','+y+'), expected ['+r+','+g+','+b+','+a+']');
184 function _assertPixelApprox(canvas, x,y, r,g,b,a, pos, colour, tolerance)
187 var c = _getPixel(canvas, x,y);
190 var diff = Math.max(Math.abs(c[0]-r), Math.abs(c[1]-g), Math.abs(c[2]-b), Math.abs(c[3]-a));
191 if (diff > tolerance)
192 _fail('Failed assertion: got pixel [' + c + '] at ('+x+','+y+'), expected ['+r+','+g+','+b+','+a+'] +/- '+tolerance);
196 function _addTest(test)
198 var deferred = false;
199 window.deferTest = function () { deferred = true; };
202 if (_failed) // test failed
204 document.documentElement.className += ' fail';
205 window._testStatus = ['fail', document.getElementById('d').innerHTML];
207 else if (_manual_check || !_asserted)
208 { // test case explicitly asked for a manual check, or no automatic assertions were performed
209 document.getElementById('d').innerHTML += '<li>Cannot automatically verify result';
210 document.documentElement.className += ' needs_check';
211 window._testStatus = ['check', document.getElementById('d').innerHTML];
213 else // test succeeded
215 document.getElementById('d').innerHTML += '<li>Passed';
216 document.documentElement.className += ' pass';
217 window._testStatus = ['pass', document.getElementById('d').innerHTML];
219 if (window.testRunner)
220 testRunner.notifyDone();
222 window.endTest = endTest;
223 window.wrapFunction = function (f)
229 f.apply(null, arguments);
233 _fail('Aborted with exception: ' + e.message);
239 window.onload = function ()
241 if (window.testRunner) {
242 testRunner.dumpAsText();
243 testRunner.waitUntilDone();
247 var canvas = document.getElementById('c');
248 var ctx = canvas.getContext('2d');
253 _fail('Aborted with exception: ' + e.message);
254 deferred = false; // cancel any deference