| OLD | NEW |
| 1 <script src="../../resources/js-test.js"></script> | 1 <script src="../../resources/testharness.js"></script> |
| 2 <script src="../../resources/testharnessreport.js"></script> |
| 2 <canvas id="canvas" width="100" height="100"></canvas><br/> | 3 <canvas id="canvas" width="100" height="100"></canvas><br/> |
| 3 <script src="canvas-putImageData.js"></script> | 4 <script> |
| 5 |
| 6 function fillRect(imageData, x, y, width, height, r, g, b, a) |
| 7 { |
| 8 var bytesPerRow = imageData.width * 4; |
| 9 var data =imageData.data; |
| 10 for (var i = 0; i < height; i++) { |
| 11 var rowOrigin = (y+i) * bytesPerRow; |
| 12 rowOrigin += x * 4; |
| 13 for (var j = 0; j < width; j++) { |
| 14 var position = rowOrigin + j * 4; |
| 15 data[position + 0] = r; |
| 16 data[position + 1] = g; |
| 17 data[position + 2] = b; |
| 18 data[position + 3] = a; |
| 19 } |
| 20 } |
| 21 } |
| 22 |
| 23 var canvas = document.getElementById("canvas"); |
| 24 var context = canvas.getContext("2d"); |
| 25 |
| 26 if (!context.createImageData) |
| 27 context.createImageData = function(w, h) { |
| 28 var data = this.getImageData(0, 0, w, h); |
| 29 for (var i = 0; i < data.data.length; i++) |
| 30 data.data[i] = 0; |
| 31 } |
| 32 |
| 33 var buffer = context.createImageData(100, 100); |
| 34 // Fill top left corner |
| 35 fillRect(buffer, 0, 0, 50, 50, 0, 128, 0, 255); |
| 36 |
| 37 function checkPixelColor(x, y, color) |
| 38 { |
| 39 data = context.getImageData(x, y, 1, 1).data; |
| 40 for (i = 0; i < 4; i++) |
| 41 if (data[i] != color [i]) |
| 42 return false; |
| 43 return true; |
| 44 } |
| 45 |
| 46 function pixelShouldBe(x, y, color) |
| 47 { |
| 48 assert_array_equals(context.getImageData(x, y, 1, 1).data, color); |
| 49 } |
| 50 |
| 51 var pixelTests = [ |
| 52 // 0 - 6 |
| 53 ['PixelTestBasicDrawing 1', 25, 25, [0, 128, 0, 255]], |
| 54 ['PixelTestBasicDrawing 2', 49, 0, [0, 128, 0, 255]], |
| 55 ['PixelTestBasicDrawing 3', 0, 49, [0, 128, 0, 255]], |
| 56 ['PixelTestBasicDrawing 4', 49, 49, [0, 128, 0, 255]], |
| 57 ['PixelTestBasicDrawing 5', 50, 0, [0, 0, 0, 0]], |
| 58 ['PixelTestBasicDrawing 6', 0, 50, [0, 0, 0, 0]], |
| 59 ['PixelTestBasicDrawing 7', 50, 50, [0, 0, 0, 0]], |
| 60 // 7 - 11 |
| 61 ['PixelTestPositionedDrawing 1', 0, 50, [0, 128, 0, 255]], |
| 62 ['PixelTestPositionedDrawing 2', 25, 75, [0, 128, 0, 255]], |
| 63 ['PixelTestPositionedDrawing 3', 49, 50, [0, 128, 0, 255]], |
| 64 ['PixelTestPositionedDrawing 4', 0, 99, [0, 128, 0, 255]], |
| 65 ['PixelTestPositionedDrawing 5', 49, 99, [0, 128, 0, 255]], |
| 66 // 12 - 15 |
| 67 ['PixelTestTranslation 1', 50, 50, [0, 128, 0, 255]], |
| 68 ['PixelTestTranslation 2', 75, 75, [0, 128, 0, 255]], |
| 69 ['PixelTestTranslation 3', 99, 99, [0, 128, 0, 255]], |
| 70 ['PixelTestTranslation 4', 50, 49, [0, 0, 0, 0]], |
| 71 // 16 - 27 |
| 72 ['PixelTestRegionIntersectLeftEdge 1', 0, 25, [0, 128, 0, 255]], |
| 73 ['PixelTestRegionIntersectLeftEdge 2', 0, 50, [0, 128, 0, 255]], |
| 74 ['PixelTestRegionIntersectLeftEdge 3', 0, 75, [0, 128, 0, 255]], |
| 75 ['PixelTestRegionIntersectRightEdge 1', 99, 25, [0, 128, 0, 255]], |
| 76 ['PixelTestRegionIntersectRightEdge 2', 99, 50, [0, 128, 0, 255]], |
| 77 ['PixelTestRegionIntersectRightEdge 3', 99, 75, [0, 128, 0, 255]], |
| 78 ['PixelTestRegionIntersectTopEdge 1', 25, 0, [0, 128, 0, 255]], |
| 79 ['PixelTestRegionIntersectTopEdge 2', 50, 0, [0, 128, 0, 255]], |
| 80 ['PixelTestRegionIntersectTopEdge 3', 75, 0, [0, 128, 0, 255]], |
| 81 ['PixelTestRegionIntersectBototmEdge 1', 25, 99, [0, 128, 0, 255]], |
| 82 ['PixelTestRegionIntersectBototmEdge 2', 50, 99, [0, 128, 0, 255]], |
| 83 ['PixelTestRegionIntersectBototmEdge 3', 75, 99, [0, 128, 0, 255]], |
| 84 // 28 - 51 |
| 85 ['PixelTestDirtyRegionIntersectLeftEdge 1', 0, 25, [0, 128, 0, 255]], |
| 86 ['PixelTestDirtyRegionIntersectLeftEdge 2', 0, 50, [0, 128, 0, 255]], |
| 87 ['PixelTestDirtyRegionIntersectLeftEdge 3', 0, 75, [0, 128, 0, 255]], |
| 88 ['PixelTestDirtyRegionIntersectLeftEdge 4', 10, 25, [0, 128, 0, 255]], |
| 89 ['PixelTestDirtyRegionIntersectLeftEdge 5', 10, 50, [0, 128, 0, 255]], |
| 90 ['PixelTestDirtyRegionIntersectLeftEdge 6', 10, 75, [0, 128, 0, 255]], |
| 91 ['PixelTestDirtyRegionIntersectRightEdge 1', 99, 25, [0, 128, 0, 255]], |
| 92 ['PixelTestDirtyRegionIntersectRightEdge 2', 99, 50, [0, 128, 0, 255]], |
| 93 ['PixelTestDirtyRegionIntersectRightEdge 3', 99, 75, [0, 128, 0, 255]], |
| 94 ['PixelTestDirtyRegionIntersectRightEdge 4', 89, 25, [0, 128, 0, 255]], |
| 95 ['PixelTestDirtyRegionIntersectRightEdge 5', 89, 50, [0, 128, 0, 255]], |
| 96 ['PixelTestDirtyRegionIntersectRightEdge 6', 89, 75, [0, 128, 0, 255]], |
| 97 ['PixelTestDirtyRegionIntersectTopEdge 1', 25, 0, [0, 128, 0, 255]], |
| 98 ['PixelTestDirtyRegionIntersectTopEdge 2', 50, 0, [0, 128, 0, 255]], |
| 99 ['PixelTestDirtyRegionIntersectTopEdge 3', 75, 0, [0, 128, 0, 255]], |
| 100 ['PixelTestDirtyRegionIntersectTopEdge 4', 25, 10, [0, 128, 0, 255]], |
| 101 ['PixelTestDirtyRegionIntersectTopEdge 5', 50, 10, [0, 128, 0, 255]], |
| 102 ['PixelTestDirtyRegionIntersectTopEdge 6', 75, 10, [0, 128, 0, 255]], |
| 103 ['PixelTestDirtyRegionIntersectBottomEdge 1', 25, 99, [0, 128, 0, 255]], |
| 104 ['PixelTestDirtyRegionIntersectBottomEdge 2', 50, 99, [0, 128, 0, 255]], |
| 105 ['PixelTestDirtyRegionIntersectBottomEdge 3', 75, 99, [0, 128, 0, 255]], |
| 106 ['PixelTestDirtyRegionIntersectBottomEdge 4', 25, 89, [0, 128, 0, 255]], |
| 107 ['PixelTestDirtyRegionIntersectBottomEdge 5', 50, 89, [0, 128, 0, 255]], |
| 108 ['PixelTestDirtyRegionIntersectBottomEdge 6', 75, 89, [0, 128, 0, 255]], |
| 109 ]; |
| 110 |
| 111 // Test basic drawing |
| 112 context.putImageData(buffer, 0, 0); |
| 113 generate_tests(pixelShouldBe, pixelTests.slice(0,7)); |
| 114 |
| 115 // Test positioned drawing -- make bottom right green |
| 116 context.putImageData(buffer, 0, 50); |
| 117 generate_tests(pixelShouldBe, pixelTests.slice(7, 12)); |
| 118 |
| 119 // Test translation doesn't effect putImageData |
| 120 context.translate(50, -50); |
| 121 context.putImageData(buffer, 50, 50); |
| 122 generate_tests(pixelShouldBe, pixelTests.slice(12, 16)); |
| 123 context.translate(-50, 50); |
| 124 |
| 125 test(function(t) { |
| 126 buffer = context.createImageData(50, 50); |
| 127 fillRect(buffer, 0, 0, 50, 50, 0, 128, 0, 255); |
| 128 context.putImageData(buffer, 50, 0); |
| 129 fillRect(buffer, 0, 0, 50, 50, 255, 0, 0, 255); |
| 130 context.putImageData(buffer, 50, 0, 15, 15, 20, 20); |
| 131 context.fillStyle="rgb(0, 128, 0)"; |
| 132 context.fillRect(65, 15, 20, 20); |
| 133 var points = [0, 5, 15, 25, 35, 45]; |
| 134 var testPassed = true; |
| 135 outerLoop: |
| 136 for (var x = 0; x < points.length; x++) |
| 137 for (var y = 0; y < points.length; y++) |
| 138 if (!checkPixelColor(points[x] + 50, points[y], [0, 128, 0, 255])) { |
| 139 testPassed = false; |
| 140 break outerLoop; |
| 141 } |
| 142 assert_true(testPassed); |
| 143 }, "Test dirty rect handling."); |
| 144 |
| 145 test(function(t){ |
| 146 fillRect(buffer, 0, 0, 50, 50, 255, 0, 0, 255); |
| 147 context.putImageData(buffer, -50, 0); |
| 148 pixelShouldBe(0, 25, [0, 128, 0, 255]); |
| 149 context.putImageData(buffer, 100, 0); |
| 150 pixelShouldBe(99, 25, [0, 128, 0, 255]); |
| 151 context.putImageData(buffer, 0, -50); |
| 152 pixelShouldBe(25, 0, [0, 128, 0, 255]); |
| 153 context.putImageData(buffer, 0, 100); |
| 154 pixelShouldBe(25, 99, [0, 128, 0, 255]); |
| 155 }, "Test drawing outside the canvas border."); |
| 156 |
| 157 test(function(t){ |
| 158 context.putImageData(buffer, 50, 0, 50, 0, 100, 100); |
| 159 context.putImageData(buffer, 50, 0, -50, 0, 50, 100); |
| 160 context.putImageData(buffer, 50, 0, 0, 50, 100, 100); |
| 161 context.putImageData(buffer, 50, 0, 50, -50, 100, 100); |
| 162 var points = [0, 5, 15, 25, 35, 45]; |
| 163 var testPassed = true; |
| 164 outerLoop: |
| 165 for (var x = 0; x < points.length; x++) |
| 166 for (var y = 0; y < points.length; y++) |
| 167 if (!checkPixelColor(points[x] + 50, points[y], [0, 128, 0, 255])) { |
| 168 testPassed = false; |
| 169 break outerLoop; |
| 170 } |
| 171 assert_true(testPassed); |
| 172 }, "test drawing with non-intersecting dirty rect."); |
| 173 |
| 174 test(function(t) { |
| 175 buffer = context.createImageData(100, 100); |
| 176 fillRect(buffer, 0, 0, 100, 100, 0, 128, 0, 255); |
| 177 fillRect(buffer, 10, 10, 80, 80, 255, 0, 0, 255); |
| 178 //left edge |
| 179 context.putImageData(buffer, -90, 0); |
| 180 generate_tests(pixelShouldBe, pixelTests.slice(16, 19)); |
| 181 //right edge |
| 182 context.putImageData(buffer, 90, 0); |
| 183 generate_tests(pixelShouldBe, pixelTests.slice(19, 22)); |
| 184 //top edge |
| 185 context.putImageData(buffer, 0, -90); |
| 186 generate_tests(pixelShouldBe, pixelTests.slice(22, 25)); |
| 187 //bottom edge |
| 188 context.putImageData(buffer, 0, 90); |
| 189 generate_tests(pixelShouldBe, pixelTests.slice(25, 28)); |
| 190 }, "Test drawing to region intersect edge of canvas."); |
| 191 |
| 192 test(function(t) { |
| 193 // left edge |
| 194 context.putImageData(buffer, 0, 0, -90, 0, 100, 100); |
| 195 generate_tests(pixelShouldBe, pixelTests.slice(28, 34)); |
| 196 //right edge |
| 197 context.putImageData(buffer, 0, 0, 90, 0, 100, 100); |
| 198 generate_tests(pixelShouldBe, pixelTests.slice(34, 40)); |
| 199 // top edge |
| 200 context.putImageData(buffer, 0, 0, 0, -90, 100, 100); |
| 201 generate_tests(pixelShouldBe, pixelTests.slice(40, 46)); |
| 202 //bottom edge |
| 203 context.putImageData(buffer, 0, 0, 0, 90, 100, 100); |
| 204 generate_tests(pixelShouldBe, pixelTests.slice(46)); |
| 205 }, "Test drawing with only part of the dirty region intersecting the window."); |
| 206 |
| 207 test(function(t) { |
| 208 var smallbuffer = context.createImageData(10, 10); |
| 209 fillRect(smallbuffer, 0, 0, 10, 10, 255, 0, 0, 255); |
| 210 context.putImageData(smallbuffer, 1.5, 1); |
| 211 pixelShouldBe(11, 11, [0, 128, 0, 255]); |
| 212 fillRect(smallbuffer, 0, 0, 10, 10, 0, 128, 0, 255); |
| 213 context.putImageData(smallbuffer, 1.5, 1); |
| 214 pixelShouldBe(1, 1, [0, 128, 0, 255]); |
| 215 }, "Test clamping of dx/dy."); |
| 216 |
| 217 test(function(t) { |
| 218 var smallbuffer = context.createImageData(10, 10); |
| 219 fillRect(smallbuffer, 0, 0, 10, 10, 0, 128, 0, 255); |
| 220 context.fillStyle = "red"; |
| 221 context.fillRect(1, 1, 9, 9); |
| 222 context.putImageData(smallbuffer, 1, 1, 0.5, 0.5, 8.5, 8.5); |
| 223 pixelShouldBe(1, 1, [0, 128, 0, 255]); |
| 224 pixelShouldBe(10, 10, [0, 128, 0, 255]); |
| 225 context.fillRect(1, 1, 9, 9); |
| 226 context.putImageData(smallbuffer, 1, 1, 0.25, 0.25, 9, 9); |
| 227 pixelShouldBe(1, 1, [0, 128, 0, 255]); |
| 228 pixelShouldBe(10, 10, [0, 128, 0, 255]); |
| 229 context.fillRect(1, 1, 8, 8); |
| 230 context.putImageData(smallbuffer, 1, 1, 0.0, 0.0, 8.5, 8.5); |
| 231 pixelShouldBe(1, 1, [0, 128, 0, 255]); |
| 232 pixelShouldBe(9, 9, [0, 128, 0, 255]); |
| 233 context.fillRect(1, 1, 8, 8); |
| 234 context.putImageData(smallbuffer, 1, 1, 0.0, 0.0, 8.25, 8.25); |
| 235 pixelShouldBe(1, 1, [0, 128, 0, 255]); |
| 236 pixelShouldBe(9, 9, [0, 128, 0, 255]); |
| 237 context.fillRect(1, 1, 7, 7); |
| 238 context.putImageData(smallbuffer, 1, 1, 0.5, 0.5, 7.9, 7.9); |
| 239 pixelShouldBe(1, 1, [0, 128, 0, 255]); |
| 240 pixelShouldBe(9, 9, [0, 128, 0, 255]); |
| 241 }, "Test clamping of dirtyX/Y/Width/Height."); |
| 242 |
| 243 var testInvalidParams = [ |
| 244 ['TestInvalidParam 1', null, function() {context.putImageData({}, 0, 0);}], |
| 245 ['TestInvalidParam 2', null, function() {context.putImageData(buffer, NaN, 0
, 0, 0, 0, 0);}], |
| 246 ['TestInvalidParam 3', null, function() {context.putImageData(buffer, 0, NaN
, 0, 0, 0, 0);}], |
| 247 ['TestInvalidParam 4', null, function() {context.putImageData(buffer, 0, 0,
NaN, 0, 0, 0);}], |
| 248 ['TestInvalidParam 5', null, function() {context.putImageData(buffer, 0, 0,
0, NaN, 0, 0);}], |
| 249 ['TestInvalidParam 6', null, function() {context.putImageData(buffer, 0, 0,
0, 0, NaN, 0);}], |
| 250 ['TestInvalidParam 7', null, function() {context.putImageData(buffer, 0, 0,
0, 0, 0, NaN);}], |
| 251 ['TestInvalidParam 8', null, function() {context.putImageData(buffer, Infini
ty, 0, 0, 0, 0, 0);}], |
| 252 ['TestInvalidParam 9', null, function() {context.putImageData(buffer, 0, Inf
inity, 0, 0, 0, 0);}], |
| 253 ['TestInvalidParam 10', null, function() {context.putImageData(buffer, 0, 0,
Infinity, 0, 0, 0);}], |
| 254 ['TestInvalidParam 11', null, function() {context.putImageData(buffer, 0, 0,
0, Infinity, 0, 0);}], |
| 255 ['TestInvalidParam 12', null, function() {context.putImageData(buffer, 0, 0,
0, 0, Infinity, 0);}], |
| 256 ['TestInvalidParam 13', null, function() {context.putImageData(buffer, 0, 0,
0, 0, 0, Infinity);}], |
| 257 ['TestInvalidParam 14', null, function() {context.putImageData(buffer, undef
ined, 0, 0, 0, 0, 0);}], |
| 258 ['TestInvalidParam 15', null, function() {context.putImageData(buffer, 0, un
defined, 0, 0, 0, 0);}], |
| 259 ['TestInvalidParam 16', null, function() {context.putImageData(buffer, 0, 0,
undefined, 0, 0, 0);}], |
| 260 ['TestInvalidParam 17', null, function() {context.putImageData(buffer, 0, 0,
0, undefined, 0, 0);}], |
| 261 ['TestInvalidParam 18', null, function() {context.putImageData(buffer, 0, 0,
0, 0, undefined, 0);}], |
| 262 ['TestInvalidParam 19', null, function() {context.putImageData(buffer, 0, 0,
0, 0, 0, undefined);}], |
| 263 ['TestInvalidParam 20', null, function() {context.putImageData(null, 0, 0, 0
, 0, 0, 0);}], |
| 264 ['TestInvalidParam 21', null, function() {context.putImageData(undefined, 0,
0, 0, 0, 0, 0);}], |
| 265 |
| 266 ]; |
| 267 |
| 268 generate_tests(assert_throws, testInvalidParams); |
| 269 |
| 270 test(function(t) { |
| 271 var smallbuffer = context.createImageData(10, 10); |
| 272 var rectcanvas = document.createElement("canvas"); |
| 273 rectcanvas.width = 20; |
| 274 rectcanvas.height = 10; |
| 275 var rectbuffer = rectcanvas.getContext("2d"); |
| 276 rectbuffer.putImageData(smallbuffer, 10, 0); |
| 277 |
| 278 var rectcanvas = document.createElement("canvas"); |
| 279 rectcanvas.width = 10; |
| 280 rectcanvas.height = 20; |
| 281 var rectbuffer = rectcanvas.getContext("2d"); |
| 282 rectbuffer.putImageData(smallbuffer, 0, 10); |
| 283 }, "Ensure we don't mess up bounds clipping checks."); |
| 284 </script> |
| OLD | NEW |