Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(352)

Side by Side Diff: tests/html/canvasrenderingcontext2d_test.dart

Issue 12719002: Update context2d.arc to take optional anticlockwise parameter (Closed) Base URL: https://dart.googlecode.com/svn/branches/bleeding_edge/dart
Patch Set: Created 7 years, 9 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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) {
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 expect(pixel[3], closeTo(expected[3], 2));
16 } 17 }
17 18
18 main() { 19 main() {
19 useHtmlConfiguration(); 20 useHtmlConfiguration();
20 var canvas = new CanvasElement(); 21
21 canvas.width = 100; 22 group('canvasRenderingContext2d', () {
22 canvas.height = 100; 23 var canvas;
23 24 var context;
24 var context = canvas.context2d; 25
25 26 setUp(() {
26 List<int> readPixel() { 27 canvas = new CanvasElement();
27 var imageData = context.getImageData(2, 2, 1, 1); 28 canvas.width = 100;
28 return imageData.data; 29 canvas.height = 100;
29 } 30
30 31 context = canvas.context2d;
31 test('setFillColorRgb', () { 32 });
32 context.setFillColorRgb(255, 0, 255, 1); 33
33 context.fillRect(0, 0, canvas.width, canvas.height); 34 tearDown(() {
34 expect(readPixel(), [255, 0, 255, 255]); 35 canvas = null;
35 }); 36 context = null;
36 37 });
37 test('setFillColorHsl hue', () { 38
38 context.setFillColorHsl(0, 100, 50); 39 List<int> readPixel(int x, int y) {
39 context.fillRect(0, 0, canvas.width, canvas.height); 40 var imageData = context.getImageData(x, y, 1, 1);
40 checkPixel(readPixel(), [255, 0, 0, 255]); 41 return imageData.data;
41 }); 42 }
42 43
43 test('setFillColorHsl hue 2', () { 44 /// Returns true if the pixel has some data in it, false otherwise.
44 context.setFillColorHsl(240, 100, 50); 45 bool isPixelFilled(int x, int y) => readPixel(x,y).any((p) => p != 0);
45 context.fillRect(0, 0, canvas.width, canvas.height); 46
46 checkPixel(readPixel(), [0, 0, 255, 255]); 47 String pixelDataToString(int x, int y) {
47 }); 48 var data = readPixel(x, y);
48 49
49 test('setFillColorHsl sat', () { 50 return '[${data.join(", ")}]';
50 context.setFillColorHsl(0, 0, 50); 51 }
51 context.fillRect(0, 0, canvas.width, canvas.height); 52
52 checkPixel(readPixel(), [127, 127, 127, 255]); 53 String _filled(bool v) => v ? "filled" : "unfilled";
53 }); 54
54 55 void expectPixelFilled(int x, int y, [bool filled = true]) {
55 test('setStrokeColorRgb', () { 56 expect(isPixelFilled(x, y), filled, reason:
56 context.setStrokeColorRgb(255, 0, 255, 1); 57 'Pixel at ($x, $y) was expected to'
57 context.lineWidth = 10; 58 ' be: <${_filled(filled)}> but was: <${_filled(!filled)}> with data: '
58 context.strokeRect(0, 0, canvas.width, canvas.height); 59 '${pixelDataToString(x,y)}');
59 expect(readPixel(), [255, 0, 255, 255]); 60 }
60 }); 61
61 62 void expectPixelUnfilled(int x, int y) {
62 test('setStrokeColorHsl hue', () { 63 expectPixelFilled(x, y, false);
63 context.setStrokeColorHsl(0, 100, 50); 64 }
64 context.lineWidth = 10; 65
65 context.strokeRect(0, 0, canvas.width, canvas.height); 66
66 expect(readPixel(), [255, 0, 0, 255]); 67 test('setFillColorRgb', () {
67 }); 68 context.setFillColorRgb(255, 0, 255, 1);
68 69 context.fillRect(0, 0, canvas.width, canvas.height);
69 test('setStrokeColorHsl hue 2', () { 70 expect(readPixel(2, 2), [255, 0, 255, 255]);
70 context.setStrokeColorHsl(240, 100, 50); 71 });
71 context.lineWidth = 10; 72
72 context.strokeRect(0, 0, canvas.width, canvas.height); 73 test('setFillColorHsl hue', () {
73 expect(readPixel(), [0, 0, 255, 255]); 74 context.setFillColorHsl(0, 100, 50);
74 }); 75 context.fillRect(0, 0, canvas.width, canvas.height);
75 76 checkPixel(readPixel(2, 2), [255, 0, 0, 255]);
76 test('setStrokeColorHsl sat', () { 77 });
77 context.setStrokeColorHsl(0, 0, 50); 78
78 context.lineWidth = 10; 79 test('setFillColorHsl hue 2', () {
79 context.strokeRect(0, 0, canvas.width, canvas.height); 80 context.setFillColorHsl(240, 100, 50);
80 checkPixel(readPixel(), [127, 127, 127, 255]); 81 context.fillRect(0, 0, canvas.width, canvas.height);
81 }); 82 checkPixel(readPixel(2, 2), [0, 0, 255, 255]);
82 83 });
83 test('fillStyle', () { 84
84 context.fillStyle = "red"; 85 test('setFillColorHsl sat', () {
85 context.fillRect(0, 0, canvas.width, canvas.height); 86 context.setFillColorHsl(0, 0, 50);
86 checkPixel(readPixel(), [255, 0, 0, 255]); 87 context.fillRect(0, 0, canvas.width, canvas.height);
87 }); 88 checkPixel(readPixel(2, 2), [127, 127, 127, 255]);
88 89 });
89 test('strokeStyle', () { 90
90 context.strokeStyle = "blue"; 91 test('setStrokeColorRgb', () {
91 context.lineWidth = 10; 92 context.setStrokeColorRgb(255, 0, 255, 1);
92 context.strokeRect(0, 0, canvas.width, canvas.height); 93 context.lineWidth = 10;
93 expect(readPixel(), [0, 0, 255, 255]); 94 context.strokeRect(0, 0, canvas.width, canvas.height);
94 }); 95 expect(readPixel(2, 2), [255, 0, 255, 255]);
95 96 });
96 test('fillStyle linearGradient', () { 97
97 var gradient = context.createLinearGradient(0,0,20,20); 98 test('setStrokeColorHsl hue', () {
98 gradient.addColorStop(0,'red'); 99 context.setStrokeColorHsl(0, 100, 50);
99 gradient.addColorStop(1,'blue'); 100 context.lineWidth = 10;
100 context.fillStyle = gradient; 101 context.strokeRect(0, 0, canvas.width, canvas.height);
101 context.fillRect(0, 0, canvas.width, canvas.height); 102 expect(readPixel(2, 2), [255, 0, 0, 255]);
102 expect(context.fillStyle is CanvasGradient, isTrue); 103 });
103 }); 104
104 105 test('setStrokeColorHsl hue 2', () {
105 test('putImageData', () { 106 context.setStrokeColorHsl(240, 100, 50);
106 ImageData expectedData = context.getImageData(0, 0, 10, 10); 107 context.lineWidth = 10;
107 expectedData.data[0] = 25; 108 context.strokeRect(0, 0, canvas.width, canvas.height);
108 expectedData.data[2] = 255; 109 expect(readPixel(2, 2), [0, 0, 255, 255]);
109 context.fillStyle = 'green'; 110 });
110 context.fillRect(0, 0, canvas.width, canvas.height); 111
111 112 test('setStrokeColorHsl sat', () {
112 context.putImageData(expectedData, 0, 0); 113 context.setStrokeColorHsl(0, 0, 50);
113 114 context.lineWidth = 10;
114 var resultingData = context.getImageData(0, 0, 10, 10); 115 context.strokeRect(0, 0, canvas.width, canvas.height);
115 // Make sure that we read back what we wrote. 116 checkPixel(readPixel(2, 2), [127, 127, 127, 255]);
116 expect(resultingData.data, expectedData.data); 117 });
118
119 test('fillStyle', () {
120 context.fillStyle = "red";
121 context.fillRect(0, 0, canvas.width, canvas.height);
122 checkPixel(readPixel(2, 2), [255, 0, 0, 255]);
123 });
124
125 test('strokeStyle', () {
126 context.strokeStyle = "blue";
127 context.lineWidth = 10;
128 context.strokeRect(0, 0, canvas.width, canvas.height);
129 expect(readPixel(2, 2), [0, 0, 255, 255]);
130 });
131
132 test('fillStyle linearGradient', () {
133 var gradient = context.createLinearGradient(0,0,20,20);
134 gradient.addColorStop(0,'red');
135 gradient.addColorStop(1,'blue');
136 context.fillStyle = gradient;
137 context.fillRect(0, 0, canvas.width, canvas.height);
138 expect(context.fillStyle is CanvasGradient, isTrue);
139 });
140
141 test('putImageData', () {
142 ImageData expectedData = context.getImageData(0, 0, 10, 10);
143 expectedData.data[0] = 25;
144 expectedData.data[1] = 65;
145 expectedData.data[2] = 255;
146 // Set alpha to 255 to make the pixels show up.
147 expectedData.data[3] = 255;
148 context.fillStyle = 'green';
149 context.fillRect(0, 0, canvas.width, canvas.height);
150
151 context.putImageData(expectedData, 0, 0);
152
153 var resultingData = context.getImageData(0, 0, 10, 10);
154 // Make sure that we read back what we wrote.
155 expect(resultingData.data, expectedData.data);
156 });
157
158 test('default arc should be clockwise', () {
159 context.beginPath();
160 final r = 10;
161
162 // Center of arc.
163 final cx = 20;
164 final cy = 20;
165 // Arc centered at (20, 20) with radius 10 will go clockwise
166 // from (20 + r, 20) to (20, 20 + r), which is 1/4 of a circle.
167 context.arc(cx, cy, r, 0, PI/2);
168
169 context.strokeStyle = 'green';
170 context.lineWidth = 2;
171 context.stroke();
172
173 // Center should not be filled.
174 expectPixelUnfilled(cx, cy);
175
176 // (cx + r, cy) should be filled.
177 expectPixelFilled(cx + r, cy, true);
178 // (cx, cy + r) should be filled.
179 expectPixelFilled(cx, cy + r, true);
180 // (cx - r, cy) should be empty.
181 expectPixelFilled(cx - r, cy, false);
182 // (cx, cy - r) should be empty.
183 expectPixelFilled(cx, cy - r, false);
184
185 // (cx + r/SQRT2, cy + r/SQRT2) should be filled.
186 expectPixelFilled((cx + r/SQRT2).toInt(), (cy + r/SQRT2).toInt(), true);
187
188 // (cx - r/SQRT2, cy - r/SQRT2) should be empty.
189 expectPixelFilled((cx - r/SQRT2).toInt(), (cy + r/SQRT2).toInt(), false);
190
191 // (cx + r/SQRT2, cy + r/SQRT2) should be empty.
192 expectPixelFilled((cx - r/SQRT2).toInt(), (cy - r/SQRT2).toInt(), false);
193
194 // (cx - r/SQRT2, cy - r/SQRT2) should be empty.
195 expectPixelFilled((cx + r/SQRT2).toInt(), (cy - r/SQRT2).toInt(), false);
196 });
197
198 test('arc anticlockwise', () {
199 context.beginPath();
200 final r = 10;
201
202 // Center of arc.
203 final cx = 20;
204 final cy = 20;
205 // Arc centered at (20, 20) with radius 10 will go anticlockwise
206 // from (20 + r, 20) to (20, 20 + r), which is 3/4 of a circle.
207 // Because of the way arc work, when going anti-clockwise, the end points
208 // are not included, so small values are added to radius to make a little
209 // more than a 3/4 circle.
210 context.arc(cx, cy, r, .1, PI/2 - .1, true);
211
212 context.strokeStyle = 'green';
213 context.lineWidth = 2;
214 context.stroke();
215
216 // Center should not be filled.
217 expectPixelUnfilled(cx, cy);
218
219 // (cx + r, cy) should be filled.
220 expectPixelFilled(cx + r, cy, true);
221 // (cx, cy + r) should be filled.
222 expectPixelFilled(cx, cy + r, true);
223 // (cx - r, cy) should be filled.
224 expectPixelFilled(cx - r, cy, true);
225 // (cx, cy - r) should be filled.
226 expectPixelFilled(cx, cy - r, true);
227
228 // (cx + r/SQRT2, cy + r/SQRT2) should be empty.
229 expectPixelFilled((cx + r/SQRT2).toInt(), (cy + r/SQRT2).toInt(), false);
230
231 // (cx - r/SQRT2, cy - r/SQRT2) should be filled.
232 expectPixelFilled((cx - r/SQRT2).toInt(), (cy + r/SQRT2).toInt(), true);
233
234 // (cx + r/SQRT2, cy + r/SQRT2) should be filled.
235 expectPixelFilled((cx - r/SQRT2).toInt(), (cy - r/SQRT2).toInt(), true);
236
237 // (cx - r/SQRT2, cy - r/SQRT2) should be filled.
238 expectPixelFilled((cx + r/SQRT2).toInt(), (cy - r/SQRT2).toInt(), true);
239 });
117 }); 240 });
118 } 241 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698