OLD | NEW |
1 library CanvasTest; | 1 library CanvasTest; |
| 2 |
2 import 'package:unittest/unittest.dart'; | 3 import 'package:unittest/unittest.dart'; |
3 import 'package:unittest/html_config.dart'; | 4 import 'package:unittest/html_config.dart'; |
4 import 'dart:html'; | 5 import 'dart:html'; |
5 | 6 |
6 main() { | 7 main() { |
7 CanvasElement canvas; | 8 CanvasElement canvas; |
8 CanvasRenderingContext2D context; | 9 CanvasRenderingContext2D context; |
9 int width = 100; | 10 int width = 100; |
10 int height = 100; | 11 int height = 100; |
11 | 12 |
12 canvas = new CanvasElement(width:width, height:height); | 13 canvas = new CanvasElement(width: width, height: height); |
13 document.body.append(canvas); | 14 document.body.append(canvas); |
14 | 15 |
15 context = canvas.context2D; | 16 context = canvas.context2D; |
16 | 17 |
17 useHtmlConfiguration(); | 18 useHtmlConfiguration(); |
18 test('CreateImageData', () { | 19 test('CreateImageData', () { |
19 ImageData image = context.createImageData(canvas.width, | 20 ImageData image = context.createImageData(canvas.width, canvas.height); |
20 canvas.height); | |
21 List<int> data = image.data; | 21 List<int> data = image.data; |
22 | 22 |
23 expect(data, hasLength(40000)); | 23 expect(data, hasLength(40000)); |
24 checkPixel(data, 0, [0, 0, 0, 0]); | 24 checkPixel(data, 0, [0, 0, 0, 0]); |
25 checkPixel(data, width * height - 1, [0, 0, 0, 0]); | 25 checkPixel(data, width * height - 1, [0, 0, 0, 0]); |
26 | 26 |
27 data[100] = 200; | 27 data[100] = 200; |
28 expect(data[100], equals(200)); | 28 expect(data[100], equals(200)); |
29 }); | 29 }); |
30 | 30 |
31 test('toDataUrl', () { | 31 test('toDataUrl', () { |
32 var canvas = new CanvasElement(width: 100, height: 100); | 32 var canvas = new CanvasElement(width: 100, height: 100); |
33 var context = canvas.context2D; | 33 var context = canvas.context2D; |
34 context.fillStyle = 'red'; | 34 context.fillStyle = 'red'; |
35 context.fill(); | 35 context.fill(); |
36 | 36 |
37 var url = canvas.toDataUrl(); | 37 var url = canvas.toDataUrl(); |
38 | 38 |
39 var img = new ImageElement(); | 39 var img = new ImageElement(); |
40 img.onLoad.listen(expectAsync((_) { | 40 img.onLoad.listen(expectAsync((_) { |
41 expect(img.complete, true); | 41 expect(img.complete, true); |
42 })); | 42 })); |
43 img.onError.listen((_) { | 43 img.onError.listen((_) { |
44 fail('URL failed to load.'); | 44 fail('URL failed to load.'); |
45 }); | 45 }); |
46 img.src = url; | 46 img.src = url; |
47 }); | 47 }); |
48 } | 48 } |
49 | 49 |
50 void checkPixel(List<int> data, int offset, List<int> rgba) | 50 void checkPixel(List<int> data, int offset, List<int> rgba) { |
51 { | |
52 offset *= 4; | 51 offset *= 4; |
53 for (var i = 0; i < 4; ++i) { | 52 for (var i = 0; i < 4; ++i) { |
54 expect(data[offset + i], equals(rgba[i])); | 53 expect(data[offset + i], equals(rgba[i])); |
55 } | 54 } |
56 } | 55 } |
OLD | NEW |