Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / canvas / philip / tests.js
blob34907f602a4f40a124a0216889db3e3d3faf8cfc
1 function _valToString(val)
3 if (val === undefined || val === null)
4 return '[' + typeof(val) + ']';
5 return val.toString() + '[' + typeof(val) + ']';
8 var _failed = false;
9 var _asserted = false;
11 function _warn(text)
13 document.getElementById('d').appendChild(document.createElement('li')).appendChild(document.createTextNode(text));
16 function _fail(text)
18 _warn(text);
19 _failed = true;
22 function _assert(cond, text)
24 _asserted = true;
25 if (! cond)
26 _fail('Failed assertion: ' + text);
29 function _assertSame(a, b, text_a, text_b)
31 _asserted = true;
32 if (a !== b)
33 _fail('Failed assertion ' + text_a + ' === ' + text_b +
34 ' (got ' + _valToString(a) + ', expected ' + _valToString(b) + ')');
37 function _assertDifferent(a, b, text_a, text_b)
39 _asserted = true;
40 if (a === b)
41 _fail('Failed assertion ' + text_a + ' !== ' + text_b +
42 ' (got ' + _valToString(a) + ', expected not ' + _valToString(b) + ')');
45 function _assertEqual(a, b, text_a, text_b)
47 _asserted = true;
48 if (a != b)
49 _fail('Failed assertion ' + text_a + ' == ' + text_b +
50 ' (got ' + _valToString(a) + ', expected ' + _valToString(b) + ')');
53 function _assertMatch(a, b, text_a, text_b)
55 _asserted = true;
56 if (! a.match(b))
57 _fail('Failed assertion ' + text_a + ' matches ' + text_b +
58 ' (got ' + _valToString(a) + ')');
62 var _manual_check = false;
64 function _requireManualCheck()
66 _manual_check = true;
69 function _crash()
71 _fail('Aborted due to predicted crash');
74 function _getPixel(canvas, x,y)
76 try
78 var ctx = canvas.getContext('2d');
79 var imgdata = ctx.getImageData(x, y, 1, 1);
80 return [ imgdata.data[0], imgdata.data[1], imgdata.data[2], imgdata.data[3] ];
82 catch (e)
84 // probably a security exception caused by having drawn
85 // data: URLs onto the canvas
86 _manual_check = true;
87 return undefined;
91 function _assertPixel(canvas, x,y, r,g,b,a, pos, colour)
93 _asserted = true;
94 var c = _getPixel(canvas, x,y);
95 if (c && ! (c[0] == r && c[1] == g && c[2] == b && c[3] == a))
96 _fail('Failed assertion: got pixel [' + c + '] at ('+x+','+y+'), expected ['+r+','+g+','+b+','+a+']');
99 function _assertPixelApprox(canvas, x,y, r,g,b,a, pos, colour, tolerance)
101 _asserted = true;
102 var c = _getPixel(canvas, x,y);
103 if (c)
105 var diff = Math.max(Math.abs(c[0]-r), Math.abs(c[1]-g), Math.abs(c[2]-b), Math.abs(c[3]-a));
106 if (diff > tolerance)
107 _fail('Failed assertion: got pixel [' + c + '] at ('+x+','+y+'), expected ['+r+','+g+','+b+','+a+'] +/- '+tolerance);
111 function _addTest(test)
113 var deferred = false;
114 window.deferTest = function () { deferred = true; };
115 function endTest()
117 if (_failed) // test failed
119 document.documentElement.className += ' fail';
120 window._testStatus = ['fail', document.getElementById('d').innerHTML];
122 else if (_manual_check || !_asserted)
123 { // test case explicitly asked for a manual check, or no automatic assertions were performed
124 document.getElementById('d').innerHTML += '<li>Cannot automatically verify result';
125 document.documentElement.className += ' needs_check';
126 window._testStatus = ['check', document.getElementById('d').innerHTML];
128 else // test succeeded
130 document.getElementById('d').innerHTML += '<li>Passed';
131 document.documentElement.className += ' pass';
132 window._testStatus = ['pass', document.getElementById('d').innerHTML];
134 if (window.testRunner)
135 testRunner.notifyDone();
137 window.endTest = endTest;
138 window.wrapFunction = function (f)
140 return function()
144 f.apply(null, arguments);
146 catch (e)
148 _fail('Aborted with exception: ' + e.message);
150 endTest();
154 window.onload = function ()
156 if (window.testRunner) {
157 testRunner.dumpAsText();
158 testRunner.waitUntilDone();
162 var canvas = document.getElementById('c');
163 var ctx = canvas.getContext('2d');
164 test(canvas, ctx);
166 catch (e)
168 _fail('Aborted with exception: ' + e.message);
169 deferred = false; // cancel any deference
172 if (! deferred)
173 endTest();