| 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'; | |
| 10 | 9 |
| 11 // Some rounding errors in the browsers. | 10 // Some rounding errors in the browsers. |
| 12 checkPixel(List<int> pixel, List<int> expected) { | 11 checkPixel(List<int> pixel, List<int> expected) { |
| 13 expect(pixel[0], closeTo(expected[0], 2)); | 12 expect(pixel[0], closeTo(expected[0], 2)); |
| 14 expect(pixel[1], closeTo(expected[1], 2)); | 13 expect(pixel[1], closeTo(expected[1], 2)); |
| 15 expect(pixel[2], closeTo(expected[2], 2)); | 14 expect(pixel[2], closeTo(expected[2], 2)); |
| 16 expect(pixel[3], closeTo(expected[3], 2)); | 15 expect(pixel[3], closeTo(expected[3], 2)); |
| 17 } | 16 } |
| 18 | 17 |
| 19 main() { | 18 main() { |
| 20 useHtmlConfiguration(); | 19 useHtmlConfiguration(); |
| 20 var canvas = new CanvasElement(); |
| 21 canvas.width = 100; |
| 22 canvas.height = 100; |
| 21 | 23 |
| 22 group('canvasRenderingContext2d', () { | 24 var context = canvas.context2d; |
| 23 var canvas; | |
| 24 var context; | |
| 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 context.fillRect(0, 0, canvas.width, canvas.height); |
| 34 expect(readPixel(), [255, 0, 255, 255]); |
| 35 }); |
| 33 | 36 |
| 34 tearDown(() { | 37 test('setFillColorHsl hue', () { |
| 35 canvas = null; | 38 context.setFillColorHsl(0, 100, 50); |
| 36 context = null; | 39 context.fillRect(0, 0, canvas.width, canvas.height); |
| 37 }); | 40 checkPixel(readPixel(), [255, 0, 0, 255]); |
| 41 }); |
| 38 | 42 |
| 39 List<int> readPixel(int x, int y) { | 43 test('setFillColorHsl hue 2', () { |
| 40 var imageData = context.getImageData(x, y, 1, 1); | 44 context.setFillColorHsl(240, 100, 50); |
| 41 return imageData.data; | 45 context.fillRect(0, 0, canvas.width, canvas.height); |
| 42 } | 46 checkPixel(readPixel(), [0, 0, 255, 255]); |
| 47 }); |
| 43 | 48 |
| 44 /// Returns true if the pixel has some data in it, false otherwise. | 49 test('setFillColorHsl sat', () { |
| 45 bool isPixelFilled(int x, int y) => readPixel(x,y).any((p) => p != 0); | 50 context.setFillColorHsl(0, 0, 50); |
| 51 context.fillRect(0, 0, canvas.width, canvas.height); |
| 52 checkPixel(readPixel(), [127, 127, 127, 255]); |
| 53 }); |
| 46 | 54 |
| 47 String pixelDataToString(int x, int y) { | 55 test('setStrokeColorRgb', () { |
| 48 var data = readPixel(x, y); | 56 context.setStrokeColorRgb(255, 0, 255, 1); |
| 57 context.lineWidth = 10; |
| 58 context.strokeRect(0, 0, canvas.width, canvas.height); |
| 59 expect(readPixel(), [255, 0, 255, 255]); |
| 60 }); |
| 49 | 61 |
| 50 return '[${data.join(", ")}]'; | 62 test('setStrokeColorHsl hue', () { |
| 51 } | 63 context.setStrokeColorHsl(0, 100, 50); |
| 64 context.lineWidth = 10; |
| 65 context.strokeRect(0, 0, canvas.width, canvas.height); |
| 66 expect(readPixel(), [255, 0, 0, 255]); |
| 67 }); |
| 52 | 68 |
| 53 String _filled(bool v) => v ? "filled" : "unfilled"; | 69 test('setStrokeColorHsl hue 2', () { |
| 70 context.setStrokeColorHsl(240, 100, 50); |
| 71 context.lineWidth = 10; |
| 72 context.strokeRect(0, 0, canvas.width, canvas.height); |
| 73 expect(readPixel(), [0, 0, 255, 255]); |
| 74 }); |
| 54 | 75 |
| 55 void expectPixelFilled(int x, int y, [bool filled = true]) { | 76 test('setStrokeColorHsl sat', () { |
| 56 expect(isPixelFilled(x, y), filled, reason: | 77 context.setStrokeColorHsl(0, 0, 50); |
| 57 'Pixel at ($x, $y) was expected to' | 78 context.lineWidth = 10; |
| 58 ' be: <${_filled(filled)}> but was: <${_filled(!filled)}> with data: ' | 79 context.strokeRect(0, 0, canvas.width, canvas.height); |
| 59 '${pixelDataToString(x,y)}'); | 80 checkPixel(readPixel(), [127, 127, 127, 255]); |
| 60 } | 81 }); |
| 61 | 82 |
| 62 void expectPixelUnfilled(int x, int y) { | 83 test('fillStyle', () { |
| 63 expectPixelFilled(x, y, false); | 84 context.fillStyle = "red"; |
| 64 } | 85 context.fillRect(0, 0, canvas.width, canvas.height); |
| 86 checkPixel(readPixel(), [255, 0, 0, 255]); |
| 87 }); |
| 65 | 88 |
| 89 test('strokeStyle', () { |
| 90 context.strokeStyle = "blue"; |
| 91 context.lineWidth = 10; |
| 92 context.strokeRect(0, 0, canvas.width, canvas.height); |
| 93 expect(readPixel(), [0, 0, 255, 255]); |
| 94 }); |
| 66 | 95 |
| 67 test('setFillColorRgb', () { | 96 test('fillStyle linearGradient', () { |
| 68 context.setFillColorRgb(255, 0, 255, 1); | 97 var gradient = context.createLinearGradient(0,0,20,20); |
| 69 context.fillRect(0, 0, canvas.width, canvas.height); | 98 gradient.addColorStop(0,'red'); |
| 70 expect(readPixel(2, 2), [255, 0, 255, 255]); | 99 gradient.addColorStop(1,'blue'); |
| 71 }); | 100 context.fillStyle = gradient; |
| 101 context.fillRect(0, 0, canvas.width, canvas.height); |
| 102 expect(context.fillStyle is CanvasGradient, isTrue); |
| 103 }); |
| 72 | 104 |
| 73 test('setFillColorHsl hue', () { | 105 test('putImageData', () { |
| 74 context.setFillColorHsl(0, 100, 50); | 106 ImageData expectedData = context.getImageData(0, 0, 10, 10); |
| 75 context.fillRect(0, 0, canvas.width, canvas.height); | 107 expectedData.data[0] = 25; |
| 76 checkPixel(readPixel(2, 2), [255, 0, 0, 255]); | 108 expectedData.data[2] = 255; |
| 77 }); | 109 context.fillStyle = 'green'; |
| 110 context.fillRect(0, 0, canvas.width, canvas.height); |
| 78 | 111 |
| 79 test('setFillColorHsl hue 2', () { | 112 context.putImageData(expectedData, 0, 0); |
| 80 context.setFillColorHsl(240, 100, 50); | |
| 81 context.fillRect(0, 0, canvas.width, canvas.height); | |
| 82 checkPixel(readPixel(2, 2), [0, 0, 255, 255]); | |
| 83 }); | |
| 84 | 113 |
| 85 test('setFillColorHsl sat', () { | 114 var resultingData = context.getImageData(0, 0, 10, 10); |
| 86 context.setFillColorHsl(0, 0, 50); | 115 // Make sure that we read back what we wrote. |
| 87 context.fillRect(0, 0, canvas.width, canvas.height); | 116 expect(resultingData.data, expectedData.data); |
| 88 checkPixel(readPixel(2, 2), [127, 127, 127, 255]); | |
| 89 }); | |
| 90 | |
| 91 test('setStrokeColorRgb', () { | |
| 92 context.setStrokeColorRgb(255, 0, 255, 1); | |
| 93 context.lineWidth = 10; | |
| 94 context.strokeRect(0, 0, canvas.width, canvas.height); | |
| 95 expect(readPixel(2, 2), [255, 0, 255, 255]); | |
| 96 }); | |
| 97 | |
| 98 test('setStrokeColorHsl hue', () { | |
| 99 context.setStrokeColorHsl(0, 100, 50); | |
| 100 context.lineWidth = 10; | |
| 101 context.strokeRect(0, 0, canvas.width, canvas.height); | |
| 102 expect(readPixel(2, 2), [255, 0, 0, 255]); | |
| 103 }); | |
| 104 | |
| 105 test('setStrokeColorHsl hue 2', () { | |
| 106 context.setStrokeColorHsl(240, 100, 50); | |
| 107 context.lineWidth = 10; | |
| 108 context.strokeRect(0, 0, canvas.width, canvas.height); | |
| 109 expect(readPixel(2, 2), [0, 0, 255, 255]); | |
| 110 }); | |
| 111 | |
| 112 test('setStrokeColorHsl sat', () { | |
| 113 context.setStrokeColorHsl(0, 0, 50); | |
| 114 context.lineWidth = 10; | |
| 115 context.strokeRect(0, 0, canvas.width, canvas.height); | |
| 116 checkPixel(readPixel(2, 2), [127, 127, 127, 255]); | |
| 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, cy + r/SQRT2, true); | |
| 187 | |
| 188 // (cx - r/SQRT2, cy - r/SQRT2) should be empty. | |
| 189 expectPixelFilled(cx - r/SQRT2, cy + r/SQRT2, false); | |
| 190 | |
| 191 // (cx + r/SQRT2, cy + r/SQRT2) should be empty. | |
| 192 expectPixelFilled(cx - r/SQRT2, cy - r/SQRT2, false); | |
| 193 | |
| 194 // (cx - r/SQRT2, cy - r/SQRT2) should be empty. | |
| 195 expectPixelFilled(cx + r/SQRT2, cy - r/SQRT2, 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, cy + r/SQRT2, false); | |
| 230 | |
| 231 // (cx - r/SQRT2, cy - r/SQRT2) should be filled. | |
| 232 expectPixelFilled(cx - r/SQRT2, cy + r/SQRT2, true); | |
| 233 | |
| 234 // (cx + r/SQRT2, cy + r/SQRT2) should be filled. | |
| 235 expectPixelFilled(cx - r/SQRT2, cy - r/SQRT2, true); | |
| 236 | |
| 237 // (cx - r/SQRT2, cy - r/SQRT2) should be filled. | |
| 238 expectPixelFilled(cx + r/SQRT2, cy - r/SQRT2, true); | |
| 239 }); | |
| 240 }); | 117 }); |
| 241 } | 118 } |
| OLD | NEW |