OLD | NEW |
1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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 CanvasTest; | 5 library CanvasTest; |
| 6 |
6 import 'package:unittest/unittest.dart'; | 7 import 'package:unittest/unittest.dart'; |
7 import 'package:unittest/html_individual_config.dart'; | 8 import 'package:unittest/html_individual_config.dart'; |
8 import 'dart:html'; | 9 import 'dart:html'; |
9 import 'dart:typed_data'; | 10 import 'dart:typed_data'; |
10 | 11 |
11 // We have aliased the legacy type CanvasPixelArray with the new type | 12 // We have aliased the legacy type CanvasPixelArray with the new type |
12 // Uint8ClampedArray by mapping the CanvasPixelArray type tag to | 13 // Uint8ClampedArray by mapping the CanvasPixelArray type tag to |
13 // Uint8ClampedArray. It is not a perfect match since CanvasPixelArray is | 14 // Uint8ClampedArray. It is not a perfect match since CanvasPixelArray is |
14 // missing the ArrayBufferView members. These should appear to be null. | 15 // missing the ArrayBufferView members. These should appear to be null. |
15 | 16 |
16 var inscrutable; | 17 var inscrutable; |
17 | 18 |
18 main() { | 19 main() { |
19 | |
20 useHtmlIndividualConfiguration(); | 20 useHtmlIndividualConfiguration(); |
21 | 21 |
22 inscrutable = (x) => x; | 22 inscrutable = (x) => x; |
23 | 23 |
24 int width = 100; | 24 int width = 100; |
25 int height = 100; | 25 int height = 100; |
26 | 26 |
27 CanvasElement canvas = new CanvasElement(width: width, height: height); | 27 CanvasElement canvas = new CanvasElement(width: width, height: height); |
28 document.body.append(canvas); | 28 document.body.append(canvas); |
29 | 29 |
30 CanvasRenderingContext2D context = canvas.context2D; | 30 CanvasRenderingContext2D context = canvas.context2D; |
31 | 31 |
32 group('basic', () { | 32 group('basic', () { |
33 test('CreateImageData', () { | 33 test('CreateImageData', () { |
34 ImageData image = context.createImageData(canvas.width, | 34 ImageData image = context.createImageData(canvas.width, canvas.height); |
35 canvas.height); | |
36 List<int> data = image.data; | 35 List<int> data = image.data; |
37 // It is legal for the dart2js compiler to believe the type of the native | 36 // It is legal for the dart2js compiler to believe the type of the native |
38 // ImageData.data and elides the check, so check the type explicitly: | 37 // ImageData.data and elides the check, so check the type explicitly: |
39 expect(inscrutable(data) is List<int>, isTrue, | 38 expect(inscrutable(data) is List<int>, isTrue, |
40 reason: 'canvas array type'); | 39 reason: 'canvas array type'); |
41 | 40 |
42 expect(data, hasLength(40000)); | 41 expect(data, hasLength(40000)); |
43 checkPixel(data, 0, [0, 0, 0, 0]); | 42 checkPixel(data, 0, [0, 0, 0, 0]); |
44 checkPixel(data, width * height - 1, [0, 0, 0, 0]); | 43 checkPixel(data, width * height - 1, [0, 0, 0, 0]); |
45 | 44 |
(...skipping 23 matching lines...) Expand all Loading... |
69 test('isUint8ClampedList', () { | 68 test('isUint8ClampedList', () { |
70 var data = context.createImageData(canvas.width, canvas.height).data; | 69 var data = context.createImageData(canvas.width, canvas.height).data; |
71 expect(inscrutable(data) is Uint8ClampedList, true); | 70 expect(inscrutable(data) is Uint8ClampedList, true); |
72 }); | 71 }); |
73 | 72 |
74 test('consistent_isUint8ClampedList', () { | 73 test('consistent_isUint8ClampedList', () { |
75 var data = context.createImageData(canvas.width, canvas.height).data; | 74 var data = context.createImageData(canvas.width, canvas.height).data; |
76 // Static and dynamic values consistent? Type inference should be able to | 75 // Static and dynamic values consistent? Type inference should be able to |
77 // constant-fold 'data is Uint8ClampedList' to 'true'. | 76 // constant-fold 'data is Uint8ClampedList' to 'true'. |
78 expect(inscrutable(data) is Uint8ClampedList == data is Uint8ClampedList, | 77 expect(inscrutable(data) is Uint8ClampedList == data is Uint8ClampedList, |
79 isTrue); | 78 isTrue); |
80 }); | 79 }); |
81 | 80 |
82 // TODO(sra): Why does this fail on Dartium? There are two types with the | 81 // TODO(sra): Why does this fail on Dartium? There are two types with the |
83 // same print string: | 82 // same print string: |
84 // | 83 // |
85 // Expected: ?:<Uint8ClampedList> Actual: ?:<Uint8ClampedList> | 84 // Expected: ?:<Uint8ClampedList> Actual: ?:<Uint8ClampedList> |
86 /* | 85 /* |
87 test('runtimeType', () { | 86 test('runtimeType', () { |
88 var data = context.createImageData(canvas.width, canvas.height).data; | 87 var data = context.createImageData(canvas.width, canvas.height).data; |
89 expect(inscrutable(data).runtimeType, Uint8ClampedList); | 88 expect(inscrutable(data).runtimeType, Uint8ClampedList); |
(...skipping 20 matching lines...) Expand all Loading... |
110 }); | 109 }); |
111 }); | 110 }); |
112 } | 111 } |
113 | 112 |
114 void checkPixel(List<int> data, int offset, List<int> rgba) { | 113 void checkPixel(List<int> data, int offset, List<int> rgba) { |
115 offset *= 4; | 114 offset *= 4; |
116 for (var i = 0; i < 4; ++i) { | 115 for (var i = 0; i < 4; ++i) { |
117 expect(rgba[i], equals(data[offset + i])); | 116 expect(rgba[i], equals(data[offset + i])); |
118 } | 117 } |
119 } | 118 } |
OLD | NEW |