| OLD | NEW |
| (Empty) |
| 1 description("This test covers the behaviour of pattern use and construction"); | |
| 2 | |
| 3 function dataToArray(data) { | |
| 4 var result = new Array(data.length) | |
| 5 for (var i = 0; i < data.length; i++) | |
| 6 result[i] = data[i]; | |
| 7 return result; | |
| 8 } | |
| 9 | |
| 10 function getPixel(x, y) { | |
| 11 var data = context.getImageData(x,y,1,1); | |
| 12 if (!data) // getImageData failed, which should never happen | |
| 13 return [-1,-1,-1,-1]; | |
| 14 return dataToArray(data.data); | |
| 15 } | |
| 16 | |
| 17 function pixelShouldBe(x, y, colour) { | |
| 18 shouldBe("getPixel(" + [x, y] +")", "["+colour+"]"); | |
| 19 } | |
| 20 | |
| 21 function createCanvasImage(width, height, colour) { | |
| 22 var c = document.createElement("canvas"); | |
| 23 c.width = width; | |
| 24 c.height = height; | |
| 25 var context = c.getContext("2d"); | |
| 26 context.fillStyle = colour; | |
| 27 context.fillRect(0,0,width,height); | |
| 28 return c; | |
| 29 } | |
| 30 | |
| 31 | |
| 32 var green1x1 = createCanvasImage(1, 1, "green"); | |
| 33 var green100x50 = createCanvasImage(100, 50, "green"); | |
| 34 | |
| 35 var canvas = document.createElement("canvas"); | |
| 36 canvas.width = 100; | |
| 37 canvas.height = 50; | |
| 38 var context = canvas.getContext("2d"); | |
| 39 var tests = [ | |
| 40 function () { | |
| 41 var didThrow = false; | |
| 42 try { | |
| 43 var pattern = context.createPattern(green1x1, null); | |
| 44 } catch (e) { | |
| 45 didThrow = true; | |
| 46 testFailed("context.createPattern(green1x1, null) threw exception "+
e) | |
| 47 } | |
| 48 if (!didThrow) | |
| 49 testPassed("context.createPattern(green1x1, null) did not throw an e
xception"); | |
| 50 | |
| 51 context.fillStyle = pattern; | |
| 52 context.fillRect(0, 0, 100, 50); | |
| 53 pixelShouldBe(1, 1, [0,128,0,255]); | |
| 54 pixelShouldBe(98, 1, [0,128,0,255]); | |
| 55 pixelShouldBe(1, 48, [0,128,0,255]); | |
| 56 pixelShouldBe(98, 48, [0,128,0,255]); | |
| 57 }, | |
| 58 function () { | |
| 59 shouldThrow("context.createPattern(green1x1, 'null')"); | |
| 60 }, | |
| 61 function () { | |
| 62 shouldThrow("context.createPattern(green1x1, undefined)"); | |
| 63 }, | |
| 64 function () { | |
| 65 shouldThrow("context.createPattern(green1x1, 'undefined')"); | |
| 66 }, | |
| 67 function () { | |
| 68 shouldThrow("context.createPattern(green1x1, {toString:function(){ retur
n null;}})"); | |
| 69 }, | |
| 70 function () { | |
| 71 shouldThrow("context.createPattern(null, '')"); | |
| 72 }, | |
| 73 function () { | |
| 74 shouldThrow("context.createPattern(undefined, '')"); | |
| 75 }, | |
| 76 function () { | |
| 77 shouldThrow("context.createPattern({}, '')"); | |
| 78 }, | |
| 79 function () { | |
| 80 shouldThrow("context.createPattern([], '')"); | |
| 81 }, | |
| 82 function () { | |
| 83 var didThrow = false; | |
| 84 try { | |
| 85 var pattern = context.createPattern(green1x1, ''); | |
| 86 } catch (e) { | |
| 87 didThrow = true; | |
| 88 testFailed("context.createPattern(green1x1, '') threw exception "+e) | |
| 89 } | |
| 90 if (!didThrow) | |
| 91 testPassed("context.createPattern(green1x1, '') did not throw an exc
eption"); | |
| 92 | |
| 93 context.fillStyle = pattern; | |
| 94 context.fillRect(0, 0, 100, 50); | |
| 95 pixelShouldBe(1, 1, [0,128,0,255]); | |
| 96 pixelShouldBe(98, 1, [0,128,0,255]); | |
| 97 pixelShouldBe(1, 48, [0,128,0,255]); | |
| 98 pixelShouldBe(98, 48, [0,128,0,255]); | |
| 99 }, | |
| 100 function () { | |
| 101 var didThrow = false; | |
| 102 try { | |
| 103 var pattern = context.createPattern(green1x1, {toString:function(){
return 'repeat';}}); | |
| 104 } catch (e) { | |
| 105 didThrow = true; | |
| 106 testFailed("context.createPattern(green1x1, {toString:function(){ re
turn 'repeat';}}) threw exception "+e) | |
| 107 } | |
| 108 if (!didThrow) | |
| 109 testPassed("context.createPattern(green1x1, {toString:function(){ re
turn 'repeat';}}) did not throw an exception"); | |
| 110 | |
| 111 context.fillStyle = pattern; | |
| 112 context.fillRect(0, 0, 100, 50); | |
| 113 pixelShouldBe(1, 1, [0,128,0,255]); | |
| 114 pixelShouldBe(98, 1, [0,128,0,255]); | |
| 115 pixelShouldBe(1, 48, [0,128,0,255]); | |
| 116 pixelShouldBe(98, 48, [0,128,0,255]); | |
| 117 }, | |
| 118 function () { | |
| 119 context.fillStyle = "green"; | |
| 120 context.fillRect(0, 0, 50, 50); | |
| 121 var pattern = context.createPattern(green100x50, "no-repeat"); | |
| 122 context.fillStyle = pattern; | |
| 123 context.translate(50, 0); | |
| 124 context.fillRect(-50, 0, 100, 50); | |
| 125 pixelShouldBe(1, 1, [0,128,0,255]); | |
| 126 pixelShouldBe(98, 1, [0,128,0,255]); | |
| 127 pixelShouldBe(1, 48, [0,128,0,255]); | |
| 128 pixelShouldBe(98, 48, [0,128,0,255]); | |
| 129 }, | |
| 130 ]; | |
| 131 for (var i = 0; i < tests.length; i++) { | |
| 132 context.fillStyle="red"; | |
| 133 context.fillRect(0,0,100,50); | |
| 134 tests[i](); | |
| 135 } | |
| OLD | NEW |