| OLD | NEW |
| (Empty) | |
| 1 function _valToString(val) |
| 2 { |
| 3 if (val === undefined || val === null) |
| 4 return '[' + typeof(val) + ']'; |
| 5 return val.toString() + '[' + typeof(val) + ']'; |
| 6 } |
| 7 |
| 8 function _assert(cond, text) |
| 9 { |
| 10 assert_true(!!cond, text); |
| 11 } |
| 12 |
| 13 function _assertSame(a, b, text_a, text_b) |
| 14 { |
| 15 var msg = text_a + ' === ' + text_b + ' (got ' + _valToString(a) + |
| 16 ', expected ' + _valToString(b) + ')'; |
| 17 assert_equals(a, b, msg); |
| 18 } |
| 19 |
| 20 function _assertDifferent(a, b, text_a, text_b) |
| 21 { |
| 22 var msg = text_a + ' !== ' + text_b + ' (got ' + _valToString(a) + |
| 23 ', expected not ' + _valToString(b) + ')'; |
| 24 assert_not_equals(a, b, msg); |
| 25 } |
| 26 |
| 27 |
| 28 function _getPixel(canvas, x,y) |
| 29 { |
| 30 var ctx = canvas.getContext('2d'); |
| 31 var imgdata = ctx.getImageData(x, y, 1, 1); |
| 32 return [ imgdata.data[0], imgdata.data[1], imgdata.data[2], imgdata.data[3]
]; |
| 33 } |
| 34 |
| 35 function _assertPixel(canvas, x,y, r,g,b,a, pos, colour) |
| 36 { |
| 37 var c = _getPixel(canvas, x,y); |
| 38 assert_equals(c[0], r, 'Red channel of the pixel at (' + x + ', ' + y + ')')
; |
| 39 assert_equals(c[1], g, 'Green channel of the pixel at (' + x + ', ' + y + ')
'); |
| 40 assert_equals(c[2], b, 'Blue channel of the pixel at (' + x + ', ' + y + ')'
); |
| 41 assert_equals(c[3], a, 'Alpha channel of the pixel at (' + x + ', ' + y + ')
'); |
| 42 } |
| 43 |
| 44 function _assertPixelApprox(canvas, x,y, r,g,b,a, pos, colour, tolerance) |
| 45 { |
| 46 var c = _getPixel(canvas, x,y); |
| 47 assert_approx_equals(c[0], r, tolerance, 'Red channel of the pixel at (' + x
+ ', ' + y + ')'); |
| 48 assert_approx_equals(c[1], g, tolerance, 'Green channel of the pixel at (' +
x + ', ' + y + ')'); |
| 49 assert_approx_equals(c[2], b, tolerance, 'Blue channel of the pixel at (' +
x + ', ' + y + ')'); |
| 50 assert_approx_equals(c[3], a, tolerance, 'Alpha channel of the pixel at (' +
x + ', ' + y + ')'); |
| 51 } |
| 52 |
| 53 function _addTest(testFn) |
| 54 { |
| 55 var deferred = false; |
| 56 window.deferTest = function () { deferred = true; }; |
| 57 on_event(window, "load", function() |
| 58 { |
| 59 t.step(function() { |
| 60 var canvas = document.getElementById('c'); |
| 61 var ctx = canvas.getContext('2d'); |
| 62 t.step(testFn, window, canvas, ctx); |
| 63 }); |
| 64 |
| 65 if (!deferred) { |
| 66 t.done(); |
| 67 } |
| 68 }); |
| 69 } |
| 70 |
| 71 function _assertGreen(ctx, canvasWidth, canvasHeight) |
| 72 { |
| 73 var testColor = function(d, idx, expected) { |
| 74 assert_equals(d[idx], expected, "d[" + idx + "]", String(expected)); |
| 75 }; |
| 76 var imagedata = ctx.getImageData(0, 0, canvasWidth, canvasHeight); |
| 77 var w = imagedata.width, h = imagedata.height, d = imagedata.data; |
| 78 for (var i = 0; i < h; ++i) { |
| 79 for (var j = 0; j < w; ++j) { |
| 80 testColor(d, 4 * (w * i + j) + 0, 0); |
| 81 testColor(d, 4 * (w * i + j) + 1, 255); |
| 82 testColor(d, 4 * (w * i + j) + 2, 0); |
| 83 testColor(d, 4 * (w * i + j) + 3, 255); |
| 84 } |
| 85 } |
| 86 } |
| OLD | NEW |