This repository was archived by the owner on Feb 25, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 6k
Expand file tree
/
Copy pathpath_test.dart
More file actions
196 lines (159 loc) · 7.13 KB
/
Copy pathpath_test.dart
File metadata and controls
196 lines (159 loc) · 7.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
// Copyright 2013 The Flutter Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
import 'package:test/bootstrap/browser.dart';
import 'package:test/test.dart';
import 'package:ui/src/engine.dart';
import 'package:ui/ui.dart' as ui;
import 'common.dart';
void main() {
internalBootstrapBrowserTest(() => testMain);
}
void testMain() {
group('CkPath', () {
setUpCanvasKitTest();
test('Using CanvasKit', () {
expect(renderer is CanvasKitRenderer, isTrue);
});
test(CkPathMetrics, () {
final ui.Path path = ui.Path();
expect(path, isA<CkPath>());
expect(path.computeMetrics().length, 0);
path.addRect(const ui.Rect.fromLTRB(0, 0, 10, 10));
final ui.PathMetric metric = path.computeMetrics().single;
expect(metric.contourIndex, 0);
expect(metric.extractPath(0, 0.5).computeMetrics().length, 1);
final ui.Tangent tangent1 = metric.getTangentForOffset(5)!;
expect(tangent1.position, const ui.Offset(5, 0));
expect(tangent1.vector, const ui.Offset(1, 0));
final ui.Tangent tangent2 = metric.getTangentForOffset(15)!;
expect(tangent2.position, const ui.Offset(10, 5));
expect(tangent2.vector, const ui.Offset(0, 1));
expect(metric.isClosed, isTrue);
path.addOval(const ui.Rect.fromLTRB(10, 10, 100, 100));
expect(path.computeMetrics().length, 2);
// Path metrics can be iterated over multiple times.
final ui.PathMetrics metrics = path.computeMetrics();
expect(metrics.toList().length, 2);
expect(metrics.toList().length, 2);
expect(metrics.toList().length, 2);
// Can simultaneously iterate over multiple metrics from the same path.
final ui.PathMetrics metrics1 = path.computeMetrics();
final ui.PathMetrics metrics2 = path.computeMetrics();
final Iterator<ui.PathMetric> iter1 = metrics1.iterator;
final Iterator<ui.PathMetric> iter2 = metrics2.iterator;
expect(iter1.moveNext(), isTrue);
expect(iter2.moveNext(), isTrue);
expect(iter1.current, isNotNull);
expect(iter2.current, isNotNull);
expect(iter1.moveNext(), isTrue);
expect(iter2.moveNext(), isTrue);
expect(iter1.current, isNotNull);
expect(iter2.current, isNotNull);
expect(iter1.moveNext(), isFalse);
expect(iter2.moveNext(), isFalse);
expect(() => iter1.current, throwsRangeError);
expect(() => iter2.current, throwsRangeError);
// TODO(hterkelsen): https://github.com/flutter/flutter/issues/115327
}, skip: true);
test('CkPath.reset', () {
final ui.Path path = ui.Path();
expect(path, isA<CkPath>());
path.addRect(const ui.Rect.fromLTRB(0, 0, 10, 10));
expect(path.contains(const ui.Offset(5, 5)), isTrue);
expect(path.fillType, ui.PathFillType.nonZero);
path.fillType = ui.PathFillType.evenOdd;
expect(path.fillType, ui.PathFillType.evenOdd);
path.reset();
expect(path.fillType, ui.PathFillType.nonZero);
expect(path.contains(const ui.Offset(5, 5)), isFalse);
});
test('CkPath.shift creates a shifted copy of the path', () {
const ui.Rect testRect = ui.Rect.fromLTRB(0, 0, 10, 10);
final CkPath path = CkPath();
path.addRect(testRect);
expect(path.getBounds(), testRect);
expect(
path.shift(const ui.Offset(20, 20)).getBounds(),
testRect.shift(const ui.Offset(20, 20)),
);
// Make sure the original path wasn't mutated.
expect(path.getBounds(), testRect);
});
test('CkPath resurrection', () {
const ui.Rect rect = ui.Rect.fromLTRB(0, 0, 10, 10);
final CkPath path = CkPath();
path.addRect(rect);
path.delete();
final SkPath resurrectedCopy = path.resurrect();
expect(fromSkRect(resurrectedCopy.getBounds()), rect);
});
test('Resurrect CkContourMeasure in the middle of iteration', () {
browserSupportsFinalizationRegistry = false;
final ui.Path path = ui.Path();
expect(path, isA<CkPath>());
path.addRect(const ui.Rect.fromLTRB(0, 0, 10, 10));
path.addRect(const ui.Rect.fromLTRB(20, 20, 30, 30));
path.addRect(const ui.Rect.fromLTRB(40, 40, 50, 50));
final ui.PathMetrics metrics = path.computeMetrics();
final CkContourMeasureIter iterator = metrics.iterator as CkContourMeasureIter;
expect(iterator.moveNext(), isTrue);
expect(iterator.current.contourIndex, 0);
expect(iterator.moveNext(), isTrue);
expect(iterator.current.contourIndex, 1);
// Delete iterator in the middle of iteration
iterator.delete();
iterator.rawSkiaObject = null;
// Check that the iterator can continue from the last position.
expect(iterator.moveNext(), isTrue);
expect(iterator.current.contourIndex, 2);
expect(iterator.moveNext(), isFalse);
});
test('Resurrect CkContourMeasure', () {
browserSupportsFinalizationRegistry = false;
final ui.Path path = ui.Path();
expect(path, isA<CkPath>());
path.addRect(const ui.Rect.fromLTRB(0, 0, 10, 10));
path.addRect(const ui.Rect.fromLTRB(20, 20, 30, 30));
path.addRect(const ui.Rect.fromLTRB(40, 40, 50, 50));
final ui.PathMetrics metrics = path.computeMetrics();
final CkContourMeasureIter iterator = metrics.iterator as CkContourMeasureIter;
expect(iterator.moveNext(), isTrue);
final CkContourMeasure measure0 = iterator.current as CkContourMeasure;
expect(measure0.contourIndex, 0);
expect(measure0.extractPath(0, 15).getBounds(), const ui.Rect.fromLTRB(0, 0, 10, 5));
expect(iterator.moveNext(), isTrue);
final CkContourMeasure measure1 = iterator.current as CkContourMeasure;
expect(measure1.contourIndex, 1);
expect(measure1.extractPath(0, 15).getBounds(), const ui.Rect.fromLTRB(20, 20, 30, 25));
// Delete iterator and the measure in the middle of iteration
iterator.delete();
iterator.rawSkiaObject = null;
measure0.delete();
measure0.rawSkiaObject = null;
measure1.delete();
measure1.rawSkiaObject = null;
// Check that the measure is still value after resurrection.
expect(measure0.contourIndex, 0);
expect(measure0.extractPath(0, 15).getBounds(), const ui.Rect.fromLTRB(0, 0, 10, 5));
expect(measure1.contourIndex, 1);
expect(measure1.extractPath(0, 15).getBounds(), const ui.Rect.fromLTRB(20, 20, 30, 25));
// TODO(hterkelsen): https://github.com/flutter/flutter/issues/115327
}, skip: true);
test('Path.from', () {
const ui.Rect rect1 = ui.Rect.fromLTRB(0, 0, 10, 10);
const ui.Rect rect2 = ui.Rect.fromLTRB(10, 10, 20, 20);
final ui.Path original = ui.Path();
original.addRect(rect1);
expect(original, isA<CkPath>());
expect(original.getBounds(), rect1);
final ui.Path copy = ui.Path.from(original);
expect(copy, isA<CkPath>());
expect(copy.getBounds(), rect1);
// Test that when copy is mutated, the original is not affected
copy.addRect(rect2);
expect(original.getBounds(), rect1);
expect(copy.getBounds(), rect1.expandToInclude(rect2));
});
});
}