OLD | NEW |
---|---|
1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file |
2 // for details. All rights reserved. Use of this source code is governed by a | 2 // for details. All rights reserved. Use of this source code is governed by a |
3 // BSD-style license that can be found in the LICENSE file. | 3 // BSD-style license that can be found in the LICENSE file. |
4 | 4 |
5 library url_test; | 5 library url_test; |
6 import '../../pkg/unittest/lib/unittest.dart'; | 6 import '../../pkg/unittest/lib/unittest.dart'; |
7 import '../../pkg/unittest/lib/html_config.dart'; | 7 import '../../pkg/unittest/lib/html_config.dart'; |
8 import 'dart:html'; | 8 import 'dart:html'; |
9 import 'dart:math'; | |
9 | 10 |
10 // Some rounding errors in the browsers. | 11 // Some rounding errors in the browsers. |
11 checkPixel(List<int> pixel, List<int> expected) { | 12 checkPixel(List<int> pixel, List<int> expected, {ignoreAlpha: false}) { |
blois
2013/03/08 01:54:50
I don't see ignoreAlpha being used anywhere.
Andrei Mouravski
2013/03/08 08:11:25
Done.
| |
12 expect(pixel[0], closeTo(expected[0], 2)); | 13 expect(pixel[0], closeTo(expected[0], 2)); |
13 expect(pixel[1], closeTo(expected[1], 2)); | 14 expect(pixel[1], closeTo(expected[1], 2)); |
14 expect(pixel[2], closeTo(expected[2], 2)); | 15 expect(pixel[2], closeTo(expected[2], 2)); |
15 expect(pixel[3], closeTo(expected[3], 2)); | 16 // We might not care about the exact alpha value (because of aliasing.) |
blois
2013/03/08 01:54:50
Alpha will impact the other channels as well. We s
Andrei Mouravski
2013/03/08 08:11:25
Done.
| |
17 if (!ignoreAlpha) { | |
18 expect(pixel[3], closeTo(expected[3], 2)); | |
19 } | |
16 } | 20 } |
17 | 21 |
18 main() { | 22 main() { |
19 useHtmlConfiguration(); | 23 useHtmlConfiguration(); |
20 var canvas = new CanvasElement(); | 24 |
21 canvas.width = 100; | 25 group('canvasRenderingContext2d', () { |
22 canvas.height = 100; | 26 var canvas; |
23 | 27 var context; |
24 var context = canvas.context2d; | 28 |
25 | 29 setUp(() { |
26 List<int> readPixel() { | 30 canvas = new CanvasElement(); |
27 var imageData = context.getImageData(2, 2, 1, 1); | 31 canvas.width = 100; |
28 return imageData.data; | 32 canvas.height = 100; |
29 } | 33 |
30 | 34 context = canvas.context2d; |
31 test('setFillColorRgb', () { | 35 }); |
32 context.setFillColorRgb(255, 0, 255, 1); | 36 |
33 context.fillRect(0, 0, canvas.width, canvas.height); | 37 tearDown(() { |
34 expect(readPixel(), [255, 0, 255, 255]); | 38 canvas = null; |
35 }); | 39 context = null; |
36 | 40 }); |
37 test('setFillColorHsl hue', () { | 41 |
38 context.setFillColorHsl(0, 100, 50); | 42 List<int> readPixel(int x, int y) { |
39 context.fillRect(0, 0, canvas.width, canvas.height); | 43 var imageData = context.getImageData(x, y, 1, 1); |
40 checkPixel(readPixel(), [255, 0, 0, 255]); | 44 return imageData.data; |
41 }); | 45 } |
42 | 46 |
43 test('setFillColorHsl hue 2', () { | 47 /// Returns true if the pixel has some data in it, false otherwise. |
44 context.setFillColorHsl(240, 100, 50); | 48 bool isPixelFilled(int x, int y) { |
45 context.fillRect(0, 0, canvas.width, canvas.height); | 49 var data = readPixel(x, y); |
46 checkPixel(readPixel(), [0, 0, 255, 255]); | 50 for (int d in data) { |
47 }); | 51 if (d != 0) { |
48 | 52 return true; |
49 test('setFillColorHsl sat', () { | 53 } |
50 context.setFillColorHsl(0, 0, 50); | 54 } |
51 context.fillRect(0, 0, canvas.width, canvas.height); | 55 return false; |
52 checkPixel(readPixel(), [127, 127, 127, 255]); | 56 } |
53 }); | 57 |
54 | 58 String pixelDataToString(int x, int y) { |
55 test('setStrokeColorRgb', () { | 59 var data = readPixel(x, y); |
56 context.setStrokeColorRgb(255, 0, 255, 1); | 60 |
57 context.lineWidth = 10; | 61 StringBuffer sb = new StringBuffer('['); |
58 context.strokeRect(0, 0, canvas.width, canvas.height); | 62 for (int i = 0; i < data.length; i++) { |
blois
2013/03/08 01:54:50
data.join(', ')?
Andrei Mouravski
2013/03/08 08:11:25
Done.
| |
59 expect(readPixel(), [255, 0, 255, 255]); | 63 sb.write(data[i]); |
60 }); | 64 if (i != data.length - 1) sb.write(', '); |
61 | 65 } |
62 test('setStrokeColorHsl hue', () { | 66 sb.write(']'); |
63 context.setStrokeColorHsl(0, 100, 50); | 67 return sb.toString(); |
64 context.lineWidth = 10; | 68 } |
65 context.strokeRect(0, 0, canvas.width, canvas.height); | 69 |
66 expect(readPixel(), [255, 0, 0, 255]); | 70 String _filled(bool v) => v ? "filled" : "unfilled"; |
67 }); | 71 |
68 | 72 void expectPixelFilled(int x, int y, [bool filled = true]) { |
69 test('setStrokeColorHsl hue 2', () { | 73 expect(isPixelFilled(x, y), filled, reason: |
70 context.setStrokeColorHsl(240, 100, 50); | 74 'Pixel at ($x, $y) was expected to' |
71 context.lineWidth = 10; | 75 ' be: <${_filled(filled)}> but was: <${_filled(!filled)}> with data: ' |
72 context.strokeRect(0, 0, canvas.width, canvas.height); | 76 '${pixelDataToString(x,y)}'); |
73 expect(readPixel(), [0, 0, 255, 255]); | 77 } |
74 }); | 78 |
75 | 79 void expectPixelUnfilled(int x, int y) { |
76 test('setStrokeColorHsl sat', () { | 80 expectPixelFilled(x, y, false); |
77 context.setStrokeColorHsl(0, 0, 50); | 81 } |
78 context.lineWidth = 10; | 82 |
79 context.strokeRect(0, 0, canvas.width, canvas.height); | 83 |
80 checkPixel(readPixel(), [127, 127, 127, 255]); | 84 test('setFillColorRgb', () { |
81 }); | 85 context.setFillColorRgb(255, 0, 255, 1); |
82 | 86 context.fillRect(0, 0, canvas.width, canvas.height); |
83 test('fillStyle', () { | 87 expect(readPixel(2, 2), [255, 0, 255, 255]); |
84 context.fillStyle = "red"; | 88 }); |
85 context.fillRect(0, 0, canvas.width, canvas.height); | 89 |
86 checkPixel(readPixel(), [255, 0, 0, 255]); | 90 test('setFillColorHsl hue', () { |
87 }); | 91 context.setFillColorHsl(0, 100, 50); |
88 | 92 context.fillRect(0, 0, canvas.width, canvas.height); |
89 test('strokeStyle', () { | 93 checkPixel(readPixel(2, 2), [255, 0, 0, 255]); |
90 context.strokeStyle = "blue"; | 94 }); |
91 context.lineWidth = 10; | 95 |
92 context.strokeRect(0, 0, canvas.width, canvas.height); | 96 test('setFillColorHsl hue 2', () { |
93 expect(readPixel(), [0, 0, 255, 255]); | 97 context.setFillColorHsl(240, 100, 50); |
94 }); | 98 context.fillRect(0, 0, canvas.width, canvas.height); |
95 | 99 checkPixel(readPixel(2, 2), [0, 0, 255, 255]); |
96 test('fillStyle linearGradient', () { | 100 }); |
97 var gradient = context.createLinearGradient(0,0,20,20); | 101 |
98 gradient.addColorStop(0,'red'); | 102 test('setFillColorHsl sat', () { |
99 gradient.addColorStop(1,'blue'); | 103 context.setFillColorHsl(0, 0, 50); |
100 context.fillStyle = gradient; | 104 context.fillRect(0, 0, canvas.width, canvas.height); |
101 context.fillRect(0, 0, canvas.width, canvas.height); | 105 checkPixel(readPixel(2, 2), [127, 127, 127, 255]); |
102 expect(context.fillStyle is CanvasGradient, isTrue); | 106 }); |
103 }); | 107 |
104 | 108 test('setStrokeColorRgb', () { |
105 test('putImageData', () { | 109 context.setStrokeColorRgb(255, 0, 255, 1); |
106 ImageData expectedData = context.getImageData(0, 0, 10, 10); | 110 context.lineWidth = 10; |
107 expectedData.data[0] = 25; | 111 context.strokeRect(0, 0, canvas.width, canvas.height); |
108 expectedData.data[2] = 255; | 112 expect(readPixel(2, 2), [255, 0, 255, 255]); |
109 context.fillStyle = 'green'; | 113 }); |
110 context.fillRect(0, 0, canvas.width, canvas.height); | 114 |
111 | 115 test('setStrokeColorHsl hue', () { |
112 context.putImageData(expectedData, 0, 0); | 116 context.setStrokeColorHsl(0, 100, 50); |
113 | 117 context.lineWidth = 10; |
114 var resultingData = context.getImageData(0, 0, 10, 10); | 118 context.strokeRect(0, 0, canvas.width, canvas.height); |
115 // Make sure that we read back what we wrote. | 119 expect(readPixel(2, 2), [255, 0, 0, 255]); |
116 expect(resultingData.data, expectedData.data); | 120 }); |
121 | |
122 test('setStrokeColorHsl hue 2', () { | |
123 context.setStrokeColorHsl(240, 100, 50); | |
124 context.lineWidth = 10; | |
125 context.strokeRect(0, 0, canvas.width, canvas.height); | |
126 expect(readPixel(2, 2), [0, 0, 255, 255]); | |
127 }); | |
128 | |
129 test('setStrokeColorHsl sat', () { | |
130 context.setStrokeColorHsl(0, 0, 50); | |
131 context.lineWidth = 10; | |
132 context.strokeRect(0, 0, canvas.width, canvas.height); | |
133 checkPixel(readPixel(2, 2), [127, 127, 127, 255]); | |
134 }); | |
135 | |
136 test('fillStyle', () { | |
137 context.fillStyle = "red"; | |
138 context.fillRect(0, 0, canvas.width, canvas.height); | |
139 checkPixel(readPixel(2, 2), [255, 0, 0, 255]); | |
140 }); | |
141 | |
142 test('strokeStyle', () { | |
143 context.strokeStyle = "blue"; | |
144 context.lineWidth = 10; | |
145 context.strokeRect(0, 0, canvas.width, canvas.height); | |
146 expect(readPixel(2, 2), [0, 0, 255, 255]); | |
147 }); | |
148 | |
149 test('fillStyle linearGradient', () { | |
150 var gradient = context.createLinearGradient(0,0,20,20); | |
151 gradient.addColorStop(0,'red'); | |
152 gradient.addColorStop(1,'blue'); | |
153 context.fillStyle = gradient; | |
154 context.fillRect(0, 0, canvas.width, canvas.height); | |
155 expect(context.fillStyle is CanvasGradient, isTrue); | |
156 }); | |
157 | |
158 test('putImageData', () { | |
159 ImageData expectedData = context.getImageData(0, 0, 10, 10); | |
160 expectedData.data[0] = 25; | |
161 expectedData.data[1] = 65; | |
162 expectedData.data[2] = 255; | |
163 // Set alpha to 255 to make the pixels show up. | |
164 expectedData.data[3] = 255; | |
165 context.fillStyle = 'green'; | |
166 context.fillRect(0, 0, canvas.width, canvas.height); | |
167 | |
168 context.putImageData(expectedData, 0, 0); | |
169 | |
170 var resultingData = context.getImageData(0, 0, 10, 10); | |
171 // Make sure that we read back what we wrote. | |
172 expect(resultingData.data, expectedData.data); | |
173 }); | |
174 | |
175 test('default arc should be clockwise', () { | |
176 context.beginPath(); | |
177 final r = 10; | |
178 | |
179 // Center of arc. | |
180 final cx = 20; | |
181 final cy = 20; | |
182 // Arc centered at (20, 20) with radius 10 will go clockwise | |
183 // from (20 + r, 20) to (20, 20 + r), which is 1/4 of a circle. | |
184 context.arc(cx, cy, r, 0, PI/2); | |
185 | |
186 context.strokeStyle = 'green'; | |
187 context.lineWidth = 2; | |
188 context.stroke(); | |
189 | |
190 // Center should not be filled. | |
191 expectPixelUnfilled(cx, cy); | |
192 | |
193 // (cx + r, cy) should be filled. | |
194 expectPixelFilled(cx + r, cy, true); | |
195 // (cx, cy + r) should be filled. | |
196 expectPixelFilled(cx, cy + r, true); | |
197 // (cx - r, cy) should be empty. | |
198 expectPixelFilled(cx - r, cy, false); | |
199 // (cx, cy - r) should be empty. | |
200 expectPixelFilled(cx, cy - r, false); | |
201 | |
202 // (cx + r/SQRT2, cy + r/SQRT2) should be filled. | |
203 expectPixelFilled(cx + r/SQRT2, cy + r/SQRT2, true); | |
204 | |
205 // (cx - r/SQRT2, cy - r/SQRT2) should be empty. | |
206 expectPixelFilled(cx - r/SQRT2, cy + r/SQRT2, false); | |
207 | |
208 // (cx + r/SQRT2, cy + r/SQRT2) should be empty. | |
209 expectPixelFilled(cx - r/SQRT2, cy - r/SQRT2, false); | |
210 | |
211 // (cx - r/SQRT2, cy - r/SQRT2) should be empty. | |
212 expectPixelFilled(cx + r/SQRT2, cy - r/SQRT2, false); | |
213 }); | |
214 | |
215 test('arc anticlockwise', () { | |
216 context.beginPath(); | |
217 final r = 10; | |
218 | |
219 // Center of arc. | |
220 final cx = 20; | |
221 final cy = 20; | |
222 // Arc centered at (20, 20) with radius 10 will go anticlockwise | |
223 // from (20 + r, 20) to (20, 20 + r), which is 3/4 of a circle. | |
224 // Because of the way arc work, when going anti-clockwise, the end points | |
225 // are not included, so small values are added to radius to make a little | |
226 // more than a 3/4 circle. | |
227 context.arc(cx, cy, r, .1, PI/2 - .1, true); | |
228 | |
229 context.strokeStyle = 'green'; | |
230 context.lineWidth = 2; | |
231 context.stroke(); | |
232 | |
233 // Center should not be filled. | |
234 expectPixelUnfilled(cx, cy); | |
235 | |
236 // (cx + r, cy) should be filled. | |
237 expectPixelFilled(cx + r, cy, true); | |
238 // (cx, cy + r) should be filled. | |
239 expectPixelFilled(cx, cy + r, true); | |
240 // (cx - r, cy) should be filled. | |
241 expectPixelFilled(cx - r, cy, true); | |
242 // (cx, cy - r) should be filled. | |
243 expectPixelFilled(cx, cy - r, true); | |
244 | |
245 // (cx + r/SQRT2, cy + r/SQRT2) should be empty. | |
246 expectPixelFilled(cx + r/SQRT2, cy + r/SQRT2, false); | |
247 | |
248 // (cx - r/SQRT2, cy - r/SQRT2) should be filled. | |
249 expectPixelFilled(cx - r/SQRT2, cy + r/SQRT2, true); | |
250 | |
251 // (cx + r/SQRT2, cy + r/SQRT2) should be filled. | |
252 expectPixelFilled(cx - r/SQRT2, cy - r/SQRT2, true); | |
253 | |
254 // (cx - r/SQRT2, cy - r/SQRT2) should be filled. | |
255 expectPixelFilled(cx + r/SQRT2, cy - r/SQRT2, true); | |
256 }); | |
117 }); | 257 }); |
118 } | 258 } |
OLD | NEW |