Files
sdk/tests/lib/html/canvasrendering/canvas_rendering_util.dart
T
Srujan Gaddam a925f4770f [dart:html] Remove 2.7 annotation from lib/html
Strips out the temporary annotation so all the tests without
NNBD related changes can run in strong mode.

Change-Id: I88f18916f0c5a45a80eddb73941db19984f0bfcf
Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/140652
Commit-Queue: Srujan Gaddam <srujzs@google.com>
Reviewed-by: Bob Nystrom <rnystrom@google.com>
2020-04-06 20:41:14 +00:00

81 lines
2.0 KiB
Dart

// Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file
// for details. All rights reserved. Use of this source code is governed by a
// BSD-style license that can be found in the LICENSE file.
library canvas_rendering_context_2d_test;
import 'dart:html';
import 'dart:math';
import 'package:expect/minitest.dart';
// Some rounding errors in the browsers.
checkPixel(List<int> pixel, List<int> expected) {
expect(pixel[0], closeTo(expected[0], 2));
expect(pixel[1], closeTo(expected[1], 2));
expect(pixel[2], closeTo(expected[2], 2));
expect(pixel[3], closeTo(expected[3], 2));
}
var canvas;
var context;
var otherCanvas;
var otherContext;
var video;
void createCanvas() {
canvas = new CanvasElement();
canvas.width = 100;
canvas.height = 100;
context = canvas.context2D;
}
void createOtherCanvas() {
otherCanvas = new CanvasElement();
otherCanvas.width = 10;
otherCanvas.height = 10;
otherContext = otherCanvas.context2D;
otherContext.fillStyle = "red";
otherContext.fillRect(0, 0, otherCanvas.width, otherCanvas.height);
}
void setupFunc() {
createCanvas();
createOtherCanvas();
video = new VideoElement();
}
void tearDownFunc() {
canvas = null;
context = null;
otherCanvas = null;
otherContext = null;
video = null;
}
List<int> readPixel(int x, int y) {
var imageData = context.getImageData(x, y, 1, 1);
return imageData.data;
}
/// Returns true if the pixel has some data in it, false otherwise.
bool isPixelFilled(int x, int y) => readPixel(x, y).any((p) => p != 0);
String pixelDataToString(List<int> data, int x, int y) {
return '[${data.join(", ")}]';
}
String _filled(bool v) => v ? "filled" : "unfilled";
void expectPixelFilled(int x, int y, [bool filled = true]) {
expect(isPixelFilled(x, y), filled,
reason: 'Pixel at ($x, $y) was expected to'
' be: <${_filled(filled)}> but was: <${_filled(!filled)}> with data: '
'${pixelDataToString(readPixel(x, y), x, y)}');
}
void expectPixelUnfilled(int x, int y) {
expectPixelFilled(x, y, false);
}