| OLD | NEW |
| 1 <script src="../../resources/testharness.js"></script> | 1 <script src="../../resources/testharness.js"></script> |
| 2 <script src="../../resources/testharnessreport.js"></script> | 2 <script src="../../resources/testharnessreport.js"></script> |
| 3 <body> | 3 <body> |
| 4 <script> | 4 <script> |
| 5 // Create auxiliary canvas to draw to and create an image from. | 5 // Create auxiliary canvas to draw to and create an image from. |
| 6 // This is done instead of simply loading an image from the file system | 6 // This is done instead of simply loading an image from the file system |
| 7 // because that would throw a SECURITY_ERR DOM Exception. | 7 // because that would throw a SECURITY_ERR DOM Exception. |
| 8 var aCanvas = document.createElement('canvas'); | 8 var aCanvas = document.createElement('canvas'); |
| 9 aCanvas.setAttribute('width', '200'); | 9 aCanvas.setAttribute('width', '200'); |
| 10 aCanvas.setAttribute('height', '200'); | 10 aCanvas.setAttribute('height', '200'); |
| 11 var aCtx = aCanvas.getContext('2d'); | 11 var aCtx = aCanvas.getContext('2d'); |
| 12 | 12 |
| 13 // Draw a circle on the same canvas. | 13 // Draw a circle on the same canvas. |
| 14 aCtx.beginPath(); | 14 aCtx.beginPath(); |
| 15 aCtx.fillStyle = 'green'; | 15 aCtx.fillStyle = 'green'; |
| 16 aCtx.arc(100, 100, 150, 0, -Math.PI/2, false); | 16 aCtx.arc(100, 100, 150, 0, -Math.PI/2, false); |
| 17 aCtx.fill(); | 17 aCtx.fill(); |
| 18 | 18 |
| 19 // Create the image object to be drawn on the master canvas. | 19 // Create the image object to be drawn on the master canvas. |
| 20 var img = new Image(); | 20 var img = new Image(); |
| 21 img.onload = drawImageToCanvasAndCheckPixels; | |
| 22 img.src = aCanvas.toDataURL(); // set a data URI of the base64 enconded image as
the source | 21 img.src = aCanvas.toDataURL(); // set a data URI of the base64 enconded image as
the source |
| 23 | 22 |
| 24 // Create master canvas. | 23 // Create master canvas. |
| 25 var canvas = document.createElement('canvas'); | 24 var canvas = document.createElement('canvas'); |
| 26 document.body.appendChild(canvas); | 25 document.body.appendChild(canvas); |
| 27 canvas.setAttribute('width', '600'); | 26 canvas.setAttribute('width', '600'); |
| 28 canvas.setAttribute('height', '600'); | 27 canvas.setAttribute('height', '600'); |
| 29 var ctx = canvas.getContext('2d'); | 28 var ctx = canvas.getContext('2d'); |
| 30 | 29 |
| 30 var testScenarios = [ |
| 31 ['Test solid shadow 1', 260, 300, [0, 0, 0, 0]], |
| 32 ['Test solid shadow 2', 350, 100, [240, 50, 50, 255]], |
| 33 ['Test solid shadow 3', 400, 200, [240, 50, 50, 255]], |
| 34 ['Test solid shadow 4', 490, 65, [0, 0, 0, 0]], |
| 35 ['Test solid shadow 5', 485, 65, [0, 0, 0, 0]], |
| 36 |
| 37 ['Test blurry shadow 1', 260, 400, [0, 0, 0, 0]], |
| 38 ['Test blurry shadow 2', 350, 300, [0, 0, 255, 'neq', 255]], |
| 39 ['Test blurry shadow 3', 300, 400, [0, 0, 255, 'neq', 255]], |
| 40 ['Test blurry shadow 4', 300, 500, [0, 0, 255, 'neq', 255]], |
| 41 ['Test blurry shadow 5', 400, 500, [0, 0, 255, 'neq', 255]], |
| 42 ['Test blurry shadow 6', 400, 400, [0, 0, 255]], |
| 43 ['Test blurry shadow 7', 490, 315, [0, 0, 0, 0]], |
| 44 ['Test blurry shadow 8', 485, 320, [0, 0, 0, 0]], |
| 45 ]; |
| 46 |
| 47 function runTestScenario(x, y, expectedColor) |
| 48 { |
| 49 imageData = ctx.getImageData(x, y, 1, 1).data; |
| 50 if (expectedColor.length == 5) { |
| 51 assert_array_equals(imageData.slice(0,3), expectedColor.slice(0,3)); |
| 52 assert_not_equals(imageData[3], expectedColor[4]); |
| 53 } else { |
| 54 assert_array_equals(imageData.slice(0, expectedColor.length), expectedCo
lor); |
| 55 } |
| 56 } |
| 57 |
| 31 function drawImageToCanvasAndCheckPixels() { | 58 function drawImageToCanvasAndCheckPixels() { |
| 32 ctx.shadowOffsetX = 250; | 59 ctx.shadowOffsetX = 250; |
| 33 ctx.shadowColor = 'rgba(240, 50, 50, 1.0)'; | 60 ctx.shadowColor = 'rgba(240, 50, 50, 1.0)'; |
| 34 ctx.drawImage(img, 50, 50); | 61 ctx.drawImage(img, 50, 50); |
| 35 | 62 |
| 36 ctx.shadowOffsetX = 250; | 63 ctx.shadowOffsetX = 250; |
| 37 ctx.shadowBlur = 6; | 64 ctx.shadowBlur = 6; |
| 38 ctx.shadowColor = 'rgba(50, 50, 200, 0.9)'; | 65 ctx.shadowColor = 'rgba(50, 50, 200, 0.9)'; |
| 39 ctx.shadowColor = 'rgba(0, 0, 255, 1.0)'; | 66 ctx.shadowColor = 'rgba(0, 0, 255, 1.0)'; |
| 40 ctx.drawImage(img, 50, 300); | 67 ctx.drawImage(img, 50, 300); |
| 41 | 68 |
| 42 checkPixels(); | 69 for (var i = 0; i < testScenarios.length; i++) |
| 43 } | 70 runTestScenario(testScenarios[i][1], |
| 71 testScenarios[i][2], |
| 72 testScenarios[i][3]);} |
| 44 | 73 |
| 45 function checkPixels() { | 74 async_test(t => { |
| 46 test(function(t) { | 75 img.onload = function() { |
| 47 var imageData, data; | 76 t.step(drawImageToCanvasAndCheckPixels); |
| 48 | 77 t.done(); |
| 49 // Verify solid shadow. | 78 } |
| 50 imageData = ctx.getImageData(260, 300, 1, 1); | 79 }, "Ensure correct behavior of canvas with image shadow. A square with a cut-out
top-right corner should be displayed with solid shadow (top) and blur shadow (b
ottom)."); |
| 51 d = imageData.data; | |
| 52 assert_equals(d[0], 0); | |
| 53 assert_equals(d[1], 0); | |
| 54 assert_equals(d[2], 0); | |
| 55 assert_equals(d[3], 0); | |
| 56 | |
| 57 imageData = ctx.getImageData(350, 100, 1, 1); | |
| 58 d = imageData.data; | |
| 59 assert_equals(d[0], 240); | |
| 60 assert_equals(d[1], 50); | |
| 61 assert_equals(d[2], 50); | |
| 62 assert_equals(d[3], 255); | |
| 63 | |
| 64 imageData = ctx.getImageData(400, 200, 1, 1); | |
| 65 d = imageData.data; | |
| 66 assert_equals(d[0], 240); | |
| 67 assert_equals(d[1], 50); | |
| 68 assert_equals(d[2], 50); | |
| 69 assert_equals(d[3], 255); | |
| 70 | |
| 71 imageData = ctx.getImageData(490, 65, 1, 1); | |
| 72 d = imageData.data; | |
| 73 assert_equals(d[0], 0); | |
| 74 assert_equals(d[1], 0); | |
| 75 assert_equals(d[2], 0); | |
| 76 assert_equals(d[3], 0); | |
| 77 | |
| 78 imageData = ctx.getImageData(485, 65, 1, 1); | |
| 79 d = imageData.data; | |
| 80 assert_equals(d[0], 0); | |
| 81 assert_equals(d[1], 0); | |
| 82 assert_equals(d[2], 0); | |
| 83 assert_equals(d[3], 0); | |
| 84 | |
| 85 // Verify blurry shadow. | |
| 86 imageData = ctx.getImageData(260, 400, 1, 1); | |
| 87 d = imageData.data; | |
| 88 assert_equals(d[0], 0); | |
| 89 assert_equals(d[1], 0); | |
| 90 assert_equals(d[2], 0); | |
| 91 assert_equals(d[3], 0); | |
| 92 | |
| 93 imageData = ctx.getImageData(350, 300, 1, 1); | |
| 94 d = imageData.data; | |
| 95 assert_equals(d[0], 0); | |
| 96 assert_equals(d[1], 0); | |
| 97 assert_equals(d[2], 255); | |
| 98 assert_not_equals(d[3], 255); | |
| 99 | |
| 100 imageData = ctx.getImageData(300, 400, 1, 1); | |
| 101 d = imageData.data; | |
| 102 assert_equals(d[0], 0); | |
| 103 assert_equals(d[1], 0); | |
| 104 assert_equals(d[2], 255); | |
| 105 assert_not_equals(d[3], 255); | |
| 106 | |
| 107 imageData = ctx.getImageData(300, 500, 1, 1); | |
| 108 d = imageData.data; | |
| 109 assert_equals(d[0], 0); | |
| 110 assert_equals(d[1], 0); | |
| 111 assert_equals(d[2], 255); | |
| 112 assert_not_equals(d[3], 255); | |
| 113 | |
| 114 imageData = ctx.getImageData(400, 500, 1, 1); | |
| 115 d = imageData.data; | |
| 116 assert_equals(d[0], 0); | |
| 117 assert_equals(d[1], 0); | |
| 118 assert_equals(d[2], 255); | |
| 119 assert_not_equals(d[3], 255); | |
| 120 | |
| 121 imageData = ctx.getImageData(400, 400, 1, 1); | |
| 122 d = imageData.data; | |
| 123 assert_equals(d[0], 0); | |
| 124 assert_equals(d[1], 0); | |
| 125 assert_equals(d[2], 255); | |
| 126 | |
| 127 imageData = ctx.getImageData(490, 315, 1, 1); | |
| 128 d = imageData.data; | |
| 129 assert_equals(d[0], 0); | |
| 130 assert_equals(d[1], 0); | |
| 131 assert_equals(d[2], 0); | |
| 132 assert_equals(d[3], 0); | |
| 133 | |
| 134 imageData = ctx.getImageData(485, 320, 1, 1); | |
| 135 d = imageData.data; | |
| 136 assert_equals(d[0], 0); | |
| 137 assert_equals(d[1], 0); | |
| 138 assert_equals(d[2], 0); | |
| 139 assert_equals(d[3], 0); | |
| 140 | |
| 141 }, "Ensure correct behavior of canvas with image shadow. A square with a cut
-out top-right corner should be displayed with solid shadow (top) and blur shado
w (bottom)."); | |
| 142 } | |
| 143 </script> | 80 </script> |
| 144 </body> | 81 </body> |
| OLD | NEW |