1 description("This test covers the behaviour of pattern use and construction");
3 function dataToArray(data
) {
4 var result
= new Array(data
.length
)
5 for (var i
= 0; i
< data
.length
; i
++)
10 function getPixel(x
, y
) {
11 var data
= context
.getImageData(x
,y
,1,1);
12 if (!data
) // getImageData failed, which should never happen
14 return dataToArray(data
.data
);
17 function pixelShouldBe(x
, y
, colour
) {
18 shouldBe("getPixel(" + [x
, y
] +")", "["+colour
+"]");
21 function createCanvasImage(width
, height
, colour
) {
22 var c
= document
.createElement("canvas");
25 var context
= c
.getContext("2d");
26 context
.fillStyle
= colour
;
27 context
.fillRect(0,0,width
,height
);
32 var green1x1
= createCanvasImage(1, 1, "green");
33 var green100x50
= createCanvasImage(100, 50, "green");
35 var canvas
= document
.createElement("canvas");
38 var context
= canvas
.getContext("2d");
43 var pattern
= context
.createPattern(green1x1
, null);
46 testFailed("context.createPattern(green1x1, null) threw exception "+e
)
49 testPassed("context.createPattern(green1x1, null) did not throw an exception");
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]);
59 shouldThrow("context.createPattern(green1x1, 'null')");
62 shouldThrow("context.createPattern(green1x1, undefined)");
65 shouldThrow("context.createPattern(green1x1, 'undefined')");
68 shouldThrow("context.createPattern(green1x1, {toString:function(){ return null;}})");
71 shouldThrow("context.createPattern(null, '')");
74 shouldThrow("context.createPattern(undefined, '')");
77 shouldThrow("context.createPattern({}, '')");
80 shouldThrow("context.createPattern([], '')");
85 var pattern
= context
.createPattern(green1x1
, '');
88 testFailed("context.createPattern(green1x1, '') threw exception "+e
)
91 testPassed("context.createPattern(green1x1, '') did not throw an exception");
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]);
101 var didThrow
= false;
103 var pattern
= context
.createPattern(green1x1
, {toString:function(){ return 'repeat';}});
106 testFailed("context.createPattern(green1x1, {toString:function(){ return 'repeat';}}) threw exception "+e
)
109 testPassed("context.createPattern(green1x1, {toString:function(){ return 'repeat';}}) did not throw an exception");
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]);
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]);
131 for (var i
= 0; i
< tests
.length
; i
++) {
132 context
.fillStyle
="red";
133 context
.fillRect(0,0,100,50);