Skip to content

Commit e797a06

Browse files
srujzscommit-bot@chromium.org
authored andcommitted
Separate canvasrenderingcontext2d into multiple tests
Separate each group into its own test file and have them refer to a shared util file. Change-Id: I3f3534b22cb6921da19eb9534af8c4ebd8a2d1b5 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/137727 Commit-Queue: Srujan Gaddam <[email protected]> Reviewed-by: Sigmund Cherem <[email protected]>
1 parent d2bd969 commit e797a06

File tree

8 files changed

+802
-749
lines changed

8 files changed

+802
-749
lines changed
Lines changed: 103 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,103 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
library canvas_rendering_context_2d_test;
6+
7+
import 'dart:html';
8+
import 'dart:math';
9+
10+
import 'canvas_rendering_util.dart';
11+
import 'package:unittest/unittest.dart';
12+
13+
main() {
14+
setUp(setupFunc);
15+
tearDown(tearDownFunc);
16+
17+
test('default arc should be clockwise', () {
18+
context.beginPath();
19+
final r = 10;
20+
21+
// Center of arc.
22+
final cx = 20;
23+
final cy = 20;
24+
// Arc centered at (20, 20) with radius 10 will go clockwise
25+
// from (20 + r, 20) to (20, 20 + r), which is 1/4 of a circle.
26+
context.arc(cx, cy, r, 0, pi / 2);
27+
28+
context.strokeStyle = 'green';
29+
context.lineWidth = 2;
30+
context.stroke();
31+
32+
// Center should not be filled.
33+
expectPixelUnfilled(cx, cy);
34+
35+
// (cx + r, cy) should be filled.
36+
expectPixelFilled(cx + r, cy, true);
37+
// (cx, cy + r) should be filled.
38+
expectPixelFilled(cx, cy + r, true);
39+
// (cx - r, cy) should be empty.
40+
expectPixelFilled(cx - r, cy, false);
41+
// (cx, cy - r) should be empty.
42+
expectPixelFilled(cx, cy - r, false);
43+
44+
// (cx + r/SQRT2, cy + r/SQRT2) should be filled.
45+
expectPixelFilled((cx + r / sqrt2).toInt(), (cy + r / sqrt2).toInt(), true);
46+
47+
// (cx - r/SQRT2, cy - r/SQRT2) should be empty.
48+
expectPixelFilled(
49+
(cx - r / sqrt2).toInt(), (cy + r / sqrt2).toInt(), false);
50+
51+
// (cx + r/SQRT2, cy + r/SQRT2) should be empty.
52+
expectPixelFilled(
53+
(cx - r / sqrt2).toInt(), (cy - r / sqrt2).toInt(), false);
54+
55+
// (cx - r/SQRT2, cy - r/SQRT2) should be empty.
56+
expectPixelFilled(
57+
(cx + r / sqrt2).toInt(), (cy - r / sqrt2).toInt(), false);
58+
});
59+
60+
test('arc anticlockwise', () {
61+
context.beginPath();
62+
final r = 10;
63+
64+
// Center of arc.
65+
final cx = 20;
66+
final cy = 20;
67+
// Arc centered at (20, 20) with radius 10 will go anticlockwise
68+
// from (20 + r, 20) to (20, 20 + r), which is 3/4 of a circle.
69+
// Because of the way arc work, when going anti-clockwise, the end points
70+
// are not included, so small values are added to radius to make a little
71+
// more than a 3/4 circle.
72+
context.arc(cx, cy, r, .1, pi / 2 - .1, true);
73+
74+
context.strokeStyle = 'green';
75+
context.lineWidth = 2;
76+
context.stroke();
77+
78+
// Center should not be filled.
79+
expectPixelUnfilled(cx, cy);
80+
81+
// (cx + r, cy) should be filled.
82+
expectPixelFilled(cx + r, cy, true);
83+
// (cx, cy + r) should be filled.
84+
expectPixelFilled(cx, cy + r, true);
85+
// (cx - r, cy) should be filled.
86+
expectPixelFilled(cx - r, cy, true);
87+
// (cx, cy - r) should be filled.
88+
expectPixelFilled(cx, cy - r, true);
89+
90+
// (cx + r/SQRT2, cy + r/SQRT2) should be empty.
91+
expectPixelFilled(
92+
(cx + r / sqrt2).toInt(), (cy + r / sqrt2).toInt(), false);
93+
94+
// (cx - r/SQRT2, cy - r/SQRT2) should be filled.
95+
expectPixelFilled((cx - r / sqrt2).toInt(), (cy + r / sqrt2).toInt(), true);
96+
97+
// (cx + r/SQRT2, cy + r/SQRT2) should be filled.
98+
expectPixelFilled((cx - r / sqrt2).toInt(), (cy - r / sqrt2).toInt(), true);
99+
100+
// (cx - r/SQRT2, cy - r/SQRT2) should be filled.
101+
expectPixelFilled((cx + r / sqrt2).toInt(), (cy - r / sqrt2).toInt(), true);
102+
});
103+
}
Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
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
3+
// BSD-style license that can be found in the LICENSE file.
4+
5+
library canvas_rendering_context_2d_test;
6+
7+
import 'dart:html';
8+
import 'dart:math';
9+
10+
import 'package:unittest/unittest.dart';
11+
12+
// Some rounding errors in the browsers.
13+
checkPixel(List<int> pixel, List<int> expected) {
14+
expect(pixel[0], closeTo(expected[0], 2));
15+
expect(pixel[1], closeTo(expected[1], 2));
16+
expect(pixel[2], closeTo(expected[2], 2));
17+
expect(pixel[3], closeTo(expected[3], 2));
18+
}
19+
20+
var canvas;
21+
var context;
22+
var otherCanvas;
23+
var otherContext;
24+
var video;
25+
26+
void createCanvas() {
27+
canvas = new CanvasElement();
28+
canvas.width = 100;
29+
canvas.height = 100;
30+
31+
context = canvas.context2D;
32+
}
33+
34+
void createOtherCanvas() {
35+
otherCanvas = new CanvasElement();
36+
otherCanvas.width = 10;
37+
otherCanvas.height = 10;
38+
otherContext = otherCanvas.context2D;
39+
otherContext.fillStyle = "red";
40+
otherContext.fillRect(0, 0, otherCanvas.width, otherCanvas.height);
41+
}
42+
43+
void setupFunc() {
44+
createCanvas();
45+
createOtherCanvas();
46+
video = new VideoElement();
47+
}
48+
49+
void tearDownFunc() {
50+
canvas = null;
51+
context = null;
52+
otherCanvas = null;
53+
otherContext = null;
54+
video = null;
55+
}
56+
57+
List<int> readPixel(int x, int y) {
58+
var imageData = context.getImageData(x, y, 1, 1);
59+
return imageData.data;
60+
}
61+
62+
/// Returns true if the pixel has some data in it, false otherwise.
63+
bool isPixelFilled(int x, int y) => readPixel(x, y).any((p) => p != 0);
64+
65+
String pixelDataToString(List<int> data, int x, int y) {
66+
return '[${data.join(", ")}]';
67+
}
68+
69+
String _filled(bool v) => v ? "filled" : "unfilled";
70+
71+
void expectPixelFilled(int x, int y, [bool filled = true]) {
72+
expect(isPixelFilled(x, y), filled,
73+
reason: 'Pixel at ($x, $y) was expected to'
74+
' be: <${_filled(filled)}> but was: <${_filled(!filled)}> with data: '
75+
'${pixelDataToString(readPixel(x, y), x, y)}');
76+
}
77+
78+
void expectPixelUnfilled(int x, int y) {
79+
expectPixelFilled(x, y, false);
80+
}
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
// Copyright (c) 2020, the Dart project authors. Please see the AUTHORS file
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.
4+
5+
library canvas_rendering_context_2d_test;
6+
7+
import 'dart:html';
8+
import 'dart:math';
9+
10+
import 'canvas_rendering_util.dart';
11+
import 'package:unittest/unittest.dart';
12+
13+
main() {
14+
setUp(setupFunc);
15+
tearDown(tearDownFunc);
16+
17+
test('with 3 params', () {
18+
// Draw an image to the canvas from a canvas element.
19+
context.drawImage(otherCanvas, 50, 50);
20+
21+
expectPixelFilled(50, 50);
22+
expectPixelFilled(55, 55);
23+
expectPixelFilled(59, 59);
24+
expectPixelUnfilled(60, 60);
25+
expectPixelUnfilled(0, 0);
26+
expectPixelUnfilled(70, 70);
27+
});
28+
test('with 5 params', () {
29+
// Draw an image to the canvas from a canvas element.
30+
context.drawImageToRect(otherCanvas, new Rectangle(50, 50, 20, 20));
31+
32+
expectPixelFilled(50, 50);
33+
expectPixelFilled(55, 55);
34+
expectPixelFilled(59, 59);
35+
expectPixelFilled(60, 60);
36+
expectPixelFilled(69, 69);
37+
expectPixelUnfilled(70, 70);
38+
expectPixelUnfilled(0, 0);
39+
expectPixelUnfilled(80, 80);
40+
});
41+
test('with 9 params', () {
42+
// Draw an image to the canvas from a canvas element.
43+
otherContext.fillStyle = "blue";
44+
otherContext.fillRect(5, 5, 5, 5);
45+
context.drawImageToRect(otherCanvas, new Rectangle(50, 50, 20, 20),
46+
sourceRect: new Rectangle(2, 2, 6, 6));
47+
48+
checkPixel(readPixel(50, 50), [255, 0, 0, 255]);
49+
checkPixel(readPixel(55, 55), [255, 0, 0, 255]);
50+
checkPixel(readPixel(60, 50), [255, 0, 0, 255]);
51+
checkPixel(readPixel(65, 65), [0, 0, 255, 255]);
52+
checkPixel(readPixel(69, 69), [0, 0, 255, 255]);
53+
expectPixelFilled(50, 50);
54+
expectPixelFilled(55, 55);
55+
expectPixelFilled(59, 59);
56+
expectPixelFilled(60, 60);
57+
expectPixelFilled(69, 69);
58+
expectPixelUnfilled(70, 70);
59+
expectPixelUnfilled(0, 0);
60+
expectPixelUnfilled(80, 80);
61+
});
62+
63+
test('createImageData', () {
64+
var imageData = context.createImageData(15, 15);
65+
expect(imageData.width, 15);
66+
expect(imageData.height, 15);
67+
68+
var other = context.createImageDataFromImageData(imageData);
69+
expect(other.width, 15);
70+
expect(other.height, 15);
71+
});
72+
73+
test('createPattern', () {
74+
var pattern = context.createPattern(new CanvasElement(), '');
75+
//var pattern2 = context.createPatternFromImage(new ImageElement(), '');
76+
});
77+
}

0 commit comments

Comments
 (0)