| OLD | NEW |
| 1 library CanvasTest; | 1 library CanvasTest; |
| 2 import '../../pkg/unittest/lib/unittest.dart'; | 2 import '../../pkg/unittest/lib/unittest.dart'; |
| 3 import '../../pkg/unittest/lib/html_config.dart'; | 3 import '../../pkg/unittest/lib/html_config.dart'; |
| 4 import 'dart:html'; | 4 import 'dart:html'; |
| 5 | 5 |
| 6 // We have aliased the legacy type CanvasPixelArray with the new type | 6 // We have aliased the legacy type CanvasPixelArray with the new type |
| 7 // Uint8ClampedArray by mapping the CanvasPixelArray type tag to | 7 // Uint8ClampedArray by mapping the CanvasPixelArray type tag to |
| 8 // Uint8ClampedArray. It is not a perfect match since CanvasPixelArray is | 8 // Uint8ClampedArray. It is not a perfect match since CanvasPixelArray is |
| 9 // missing the ArrayBufferView members. These should appear to be null. | 9 // missing the ArrayBufferView members. These should appear to be null. |
| 10 | 10 |
| 11 Object confuseType(x) => [1, x, [x], 's'] [1]; | 11 Object confuseType(x) => [1, x, [x], 's'] [1]; |
| 12 | 12 |
| 13 main() { | 13 main() { |
| 14 CanvasElement canvas; | 14 CanvasElement canvas; |
| 15 CanvasRenderingContext2D context; | 15 CanvasRenderingContext2D context; |
| 16 int width = 100; | 16 int width = 100; |
| 17 int height = 100; | 17 int height = 100; |
| 18 | 18 |
| 19 canvas = new Element.tag('canvas'); | 19 canvas = new Element.tag('canvas'); |
| 20 canvas.width = width; | 20 canvas.width = width; |
| 21 canvas.height = height; | 21 canvas.height = height; |
| 22 document.body.append(canvas); | 22 document.body.append(canvas); |
| 23 | 23 |
| 24 context = canvas.getContext('2d'); | 24 context = canvas.getContext('2d'); |
| 25 | 25 |
| 26 useHtmlConfiguration(); | 26 useHtmlConfiguration(); |
| 27 | 27 |
| 28 test('CreateImageData', () { | 28 test('CreateImageData', () { |
| 29 ImageData image = context.createImageData(canvas.width, | 29 ImageData image = context.createImageData(canvas.width.toDouble(), |
| 30 canvas.height); | 30 canvas.height.toDouble()); |
| 31 List<int> data = image.data; | 31 List<int> data = image.data; |
| 32 // It is legal for the dart2js compiler to believe the type of the native | 32 // It is legal for the dart2js compiler to believe the type of the native |
| 33 // ImageData.data and elides the check, so check the type explicitly: | 33 // ImageData.data and elides the check, so check the type explicitly: |
| 34 expect(confuseType(data) is List<int>, isTrue, | 34 expect(confuseType(data) is List<int>, isTrue, |
| 35 reason: 'canvas array type'); | 35 reason: 'canvas array type'); |
| 36 | 36 |
| 37 expect(data, hasLength(40000)); | 37 expect(data, hasLength(40000)); |
| 38 checkPixel(data, 0, [0, 0, 0, 0]); | 38 checkPixel(data, 0, [0, 0, 0, 0]); |
| 39 checkPixel(data, width * height - 1, [0, 0, 0, 0]); | 39 checkPixel(data, width * height - 1, [0, 0, 0, 0]); |
| 40 | 40 |
| 41 data[100] = 200; | 41 data[100] = 200; |
| 42 expect(data[100], equals(200)); | 42 expect(data[100], equals(200)); |
| 43 }); | 43 }); |
| 44 } | 44 } |
| 45 | 45 |
| 46 void checkPixel(List<int> data, int offset, List<int> rgba) | 46 void checkPixel(List<int> data, int offset, List<int> rgba) |
| 47 { | 47 { |
| 48 offset *= 4; | 48 offset *= 4; |
| 49 for (var i = 0; i < 4; ++i) { | 49 for (var i = 0; i < 4; ++i) { |
| 50 expect(rgba[i], equals(data[offset + i])); | 50 expect(rgba[i], equals(data[offset + i])); |
| 51 } | 51 } |
| 52 } | 52 } |
| OLD | NEW |