Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / third_party / WebKit / LayoutTests / fast / canvas / canvas-pattern-behaviour.js
blob8d6f3a5bb18d6d6b8d95beadd1bb9b8316e75340
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++)
6 result[i] = data[i];
7 return result;
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);
17 function pixelShouldBe(x, y, colour) {
18 shouldBe("getPixel(" + [x, y] +")", "["+colour+"]");
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;
32 var green1x1 = createCanvasImage(1, 1, "green");
33 var green100x50 = createCanvasImage(100, 50, "green");
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)
48 if (!didThrow)
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]);
58 function () {
59 shouldThrow("context.createPattern(green1x1, 'null')");
61 function () {
62 shouldThrow("context.createPattern(green1x1, undefined)");
64 function () {
65 shouldThrow("context.createPattern(green1x1, 'undefined')");
67 function () {
68 shouldThrow("context.createPattern(green1x1, {toString:function(){ return null;}})");
70 function () {
71 shouldThrow("context.createPattern(null, '')");
73 function () {
74 shouldThrow("context.createPattern(undefined, '')");
76 function () {
77 shouldThrow("context.createPattern({}, '')");
79 function () {
80 shouldThrow("context.createPattern([], '')");
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)
90 if (!didThrow)
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]);
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(){ return 'repeat';}}) threw exception "+e)
108 if (!didThrow)
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]);
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]);
131 for (var i = 0; i < tests.length; i++) {
132 context.fillStyle="red";
133 context.fillRect(0,0,100,50);
134 tests[i]();