oliver@apple.com | 9ec5ede | 2008-03-09 02:39:28 +0000 | [diff] [blame] | 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 | |
oliver@apple.com | d0524ef | 2008-03-09 06:48:41 +0000 | [diff] [blame] | 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"); |
oliver@apple.com | 9ec5ede | 2008-03-09 02:39:28 +0000 | [diff] [blame] | 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 exception"); |
| 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(){ return null;}})"); |
| 69 | }, |
| 70 | function () { |
| 71 | var didThrow = false; |
| 72 | try { |
| 73 | var pattern = context.createPattern(green1x1, ''); |
| 74 | } catch (e) { |
| 75 | didThrow = true; |
| 76 | testFailed("context.createPattern(green1x1, '') threw exception "+e) |
| 77 | } |
| 78 | if (!didThrow) |
| 79 | testPassed("context.createPattern(green1x1, '') did not throw an exception"); |
| 80 | |
| 81 | context.fillStyle = pattern; |
| 82 | context.fillRect(0, 0, 100, 50); |
| 83 | pixelShouldBe(1, 1, [0,128,0,255]); |
| 84 | pixelShouldBe(98, 1, [0,128,0,255]); |
| 85 | pixelShouldBe(1, 48, [0,128,0,255]); |
| 86 | pixelShouldBe(98, 48, [0,128,0,255]); |
| 87 | }, |
| 88 | function () { |
| 89 | var didThrow = false; |
| 90 | try { |
| 91 | var pattern = context.createPattern(green1x1, {toString:function(){ return 'repeat';}}); |
| 92 | } catch (e) { |
| 93 | didThrow = true; |
| 94 | testFailed("context.createPattern(green1x1, {toString:function(){ return 'repeat';}}) threw exception "+e) |
| 95 | } |
| 96 | if (!didThrow) |
| 97 | testPassed("context.createPattern(green1x1, {toString:function(){ return 'repeat';}}) did not throw an exception"); |
| 98 | |
| 99 | context.fillStyle = pattern; |
| 100 | context.fillRect(0, 0, 100, 50); |
| 101 | pixelShouldBe(1, 1, [0,128,0,255]); |
| 102 | pixelShouldBe(98, 1, [0,128,0,255]); |
| 103 | pixelShouldBe(1, 48, [0,128,0,255]); |
| 104 | pixelShouldBe(98, 48, [0,128,0,255]); |
| 105 | }, |
oliver@apple.com | d0524ef | 2008-03-09 06:48:41 +0000 | [diff] [blame] | 106 | function () { |
| 107 | context.fillStyle = "green"; |
| 108 | context.fillRect(0, 0, 50, 50); |
| 109 | var pattern = context.createPattern(green100x50, "no-repeat"); |
| 110 | context.fillStyle = pattern; |
| 111 | context.translate(50, 0); |
| 112 | context.fillRect(-50, 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 | }, |
oliver@apple.com | 9ec5ede | 2008-03-09 02:39:28 +0000 | [diff] [blame] | 118 | ]; |
| 119 | for (var i = 0; i < tests.length; i++) { |
| 120 | context.fillStyle="red"; |
| 121 | context.fillRect(0,0,100,50); |
| 122 | tests[i](); |
| 123 | } |