OLD | NEW |
| (Empty) |
1 #import('../../../../../dart/client/testing/unittest/unittest.dart'); | |
2 #import('dart:dom'); | |
3 | |
4 main() { | |
5 HTMLCanvasElement canvas; | |
6 CanvasRenderingContext2D context; | |
7 int width = 100; | |
8 int height = 100; | |
9 | |
10 // FIXME: once main is run on content loaded, this hack won't be necessary. | |
11 window.setTimeout(() { | |
12 canvas = document.createElement('canvas'); | |
13 canvas.setAttribute('width', '$width'); | |
14 canvas.setAttribute('height', '$height'); | |
15 document.body.appendChild(canvas); | |
16 | |
17 context = canvas.getContext('2d'); | |
18 }, 0); | |
19 | |
20 forLayoutTests(); | |
21 test('FillStyle', () { | |
22 context.fillStyle = "red"; | |
23 context.fillRect(10, 10, 20, 20); | |
24 | |
25 CanvasPixelArray data = context.getImageData(0, 0, width, height).data; | |
26 checkPixel(data, 0, [0, 0, 0, 0]); | |
27 checkPixel(data, 9 + width * 10, [0, 0, 0, 0]); | |
28 checkPixel(data, 10 + width * 10, [255, 0, 0, 255]); | |
29 checkPixel(data, 29 + width * 10, [255, 0, 0, 255]); | |
30 checkPixel(data, 30 + width * 10, [0, 0, 0, 0]); | |
31 }); | |
32 test('SetFillColor', () { | |
33 // With floats. | |
34 context.setFillColor(10, 10, 10, 10); | |
35 context.fillRect(10, 10, 20, 20); | |
36 | |
37 // With rationals. | |
38 context.setFillColor(10.0, 10.0, 10.0, 10.0); | |
39 context.fillRect(20, 20, 30, 30); | |
40 | |
41 // With ints. | |
42 context.setFillColor(10, 10, 10, 10); | |
43 context.fillRect(30, 30, 40, 40); | |
44 | |
45 // TODO(vsm): Verify the result once we have the ability to read pixels. | |
46 }); | |
47 test('StrokeStyle', () { | |
48 context.strokeStyle = "blue"; | |
49 context.strokeRect(30, 30, 10, 20); | |
50 | |
51 // TODO(vsm): Verify the result once we have the ability to read pixels. | |
52 }); | |
53 test('CreateImageData', () { | |
54 ImageData image = context.createImageData(canvas.width, | |
55 canvas.height); | |
56 CanvasPixelArray data = image.data; | |
57 | |
58 Expect.equals(40000, data.length); | |
59 checkPixel(data, 0, [0, 0, 0, 0]); | |
60 checkPixel(data, width * height - 1, [0, 0, 0, 0]); | |
61 | |
62 data[100] = 200; | |
63 Expect.equals(200, data[100]); | |
64 }); | |
65 test('PutImageData', () { | |
66 ImageData data = context.getImageData(0, 0, width, height); | |
67 data.data[0] = 25; | |
68 data.data[3] = 255; | |
69 context.putImageData(data, 0, 0); | |
70 | |
71 data = context.getImageData(0, 0, width, height); | |
72 Expect.equals(25, data.data[0]); | |
73 Expect.equals(255, data.data[3]); | |
74 }); | |
75 } | |
76 | |
77 void checkPixel(CanvasPixelArray data, int offset, List<int> rgba) | |
78 { | |
79 offset *= 4; | |
80 for (var i = 0; i < 4; ++i) { | |
81 Expect.equals(rgba[i], data[offset + i]); | |
82 } | |
83 } | |
OLD | NEW |