1 function _valToString(val
)
3 if (val
=== undefined || val
=== null)
4 return '[' + typeof(val
) + ']';
5 return val
.toString() + '[' + typeof(val
) + ']';
13 document
.getElementById('d').appendChild(document
.createElement('li')).appendChild(document
.createTextNode(text
));
22 function _assert(cond
, text
)
26 _fail('Failed assertion: ' + text
);
29 function _assertSame(a
, b
, text_a
, text_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
)
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
)
49 _fail('Failed assertion ' + text_a
+ ' == ' + text_b
+
50 ' (got ' + _valToString(a
) + ', expected ' + _valToString(b
) + ')');
53 function _assertMatch(a
, b
, text_a
, text_b
)
57 _fail('Failed assertion ' + text_a
+ ' matches ' + text_b
+
58 ' (got ' + _valToString(a
) + ')');
62 var _manual_check
= false;
64 function _requireManualCheck()
71 _fail('Aborted due to predicted crash');
74 function _getPixel(canvas
, x
,y
)
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] ];
84 // probably a security exception caused by having drawn
85 // data: URLs onto the canvas
91 function _assertPixel(canvas
, x
,y
, r
,g
,b
,a
, pos
, colour
)
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
)
102 var c
= _getPixel(canvas
, x
,y
);
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; };
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
)
144 f
.apply(null, arguments
);
148 _fail('Aborted with exception: ' + e
.message
);
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');
168 _fail('Aborted with exception: ' + e
.message
);
169 deferred
= false; // cancel any deference