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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
// Copyright 2014 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:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
import '../flutter_test_alternative.dart';
void main() {
group('$AnnotatedRegion find', () {
test('finds the first value in a OffsetLayer when sized', () {
final ContainerLayer containerLayer = ContainerLayer();
final List<OffsetLayer> layers = <OffsetLayer>[
OffsetLayer(offset: Offset.zero),
OffsetLayer(offset: const Offset(0.0, 100.0)),
OffsetLayer(offset: const Offset(0.0, 200.0)),
];
int i = 0;
for (final OffsetLayer layer in layers) {
layer.append(AnnotatedRegionLayer<int>(i, size: const Size(200.0, 100.0)));
containerLayer.append(layer);
i += 1;
}
expect(containerLayer.find<int>(const Offset(0.0, 1.0)), 0);
expect(containerLayer.find<int>(const Offset(0.0, 101.0)), 1);
expect(containerLayer.find<int>(const Offset(0.0, 201.0)), 2);
});
test('finds a value within the clip in a ClipRectLayer', () {
final ContainerLayer containerLayer = ContainerLayer();
final List<ClipRectLayer> layers = <ClipRectLayer>[
ClipRectLayer(clipRect: const Rect.fromLTRB(0.0, 0.0, 100.0, 100.0)),
ClipRectLayer(clipRect: const Rect.fromLTRB(0.0, 100.0, 100.0, 200.0)),
ClipRectLayer(clipRect: const Rect.fromLTRB(0.0, 200.0, 100.0, 300.0)),
];
int i = 0;
for (final ClipRectLayer layer in layers) {
layer.append(AnnotatedRegionLayer<int>(i));
containerLayer.append(layer);
i += 1;
}
expect(containerLayer.find<int>(const Offset(0.0, 1.0)), 0);
expect(containerLayer.find<int>(const Offset(0.0, 101.0)), 1);
expect(containerLayer.find<int>(const Offset(0.0, 201.0)), 2);
});
test('finds a value within the clip in a ClipRRectLayer', () {
final ContainerLayer containerLayer = ContainerLayer();
final List<ClipRRectLayer> layers = <ClipRRectLayer>[
ClipRRectLayer(clipRRect: RRect.fromLTRBR(0.0, 0.0, 100.0, 100.0, const Radius.circular(4.0))),
ClipRRectLayer(clipRRect: RRect.fromLTRBR(0.0, 100.0, 100.0, 200.0, const Radius.circular(4.0))),
ClipRRectLayer(clipRRect: RRect.fromLTRBR(0.0, 200.0, 100.0, 300.0, const Radius.circular(4.0))),
];
int i = 0;
for (final ClipRRectLayer layer in layers) {
layer.append(AnnotatedRegionLayer<int>(i));
containerLayer.append(layer);
i += 1;
}
expect(containerLayer.find<int>(const Offset(5.0, 5.0)), 0);
expect(containerLayer.find<int>(const Offset(5.0, 105.0)), 1);
expect(containerLayer.find<int>(const Offset(5.0, 205.0)), 2);
});
test('finds a value under a TransformLayer', () {
final Matrix4 transform = Matrix4(
2.625, 0.0, 0.0, 0.0,
0.0, 2.625, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
);
final TransformLayer transformLayer = TransformLayer(transform: transform);
final List<OffsetLayer> layers = <OffsetLayer>[
OffsetLayer(),
OffsetLayer(offset: const Offset(0.0, 100.0)),
OffsetLayer(offset: const Offset(0.0, 200.0)),
];
int i = 0;
for (final OffsetLayer layer in layers) {
final AnnotatedRegionLayer<int> annotatedRegionLayer = AnnotatedRegionLayer<int>(i, size: const Size(100.0, 100.0));
layer.append(annotatedRegionLayer);
transformLayer.append(layer);
i += 1;
}
expect(transformLayer.find<int>(const Offset(0.0, 100.0)), 0);
expect(transformLayer.find<int>(const Offset(0.0, 200.0)), 0);
expect(transformLayer.find<int>(const Offset(0.0, 270.0)), 1);
expect(transformLayer.find<int>(const Offset(0.0, 400.0)), 1);
expect(transformLayer.find<int>(const Offset(0.0, 530.0)), 2);
});
test('looks for child AnnotatedRegions before parents', () {
final AnnotatedRegionLayer<int> parent = AnnotatedRegionLayer<int>(1);
final AnnotatedRegionLayer<int> child = AnnotatedRegionLayer<int>(2);
final ContainerLayer layer = ContainerLayer();
parent.append(child);
layer.append(parent);
expect(parent.find<int>(Offset.zero), 2);
});
test('looks for correct type', () {
final AnnotatedRegionLayer<int> child1 = AnnotatedRegionLayer<int>(1);
final AnnotatedRegionLayer<String> child2 = AnnotatedRegionLayer<String>('hello');
final ContainerLayer layer = ContainerLayer();
layer.append(child2);
layer.append(child1);
expect(layer.find<String>(Offset.zero), 'hello');
});
test('does not clip Layer.find on an AnnotatedRegion with an unrelated type', () {
final AnnotatedRegionLayer<int> child = AnnotatedRegionLayer<int>(1);
final AnnotatedRegionLayer<String> parent = AnnotatedRegionLayer<String>('hello', size: const Size(10.0, 10.0));
final ContainerLayer layer = ContainerLayer();
parent.append(child);
layer.append(parent);
expect(layer.find<int>(const Offset(100.0, 100.0)), 1);
});
test('handles non-invertable transforms', () {
final AnnotatedRegionLayer<int> child = AnnotatedRegionLayer<int>(1);
final TransformLayer parent = TransformLayer(transform: Matrix4.diagonal3Values(0.0, 1.0, 1.0));
parent.append(child);
expect(parent.find<int>(const Offset(0.0, 0.0)), null);
parent.transform = Matrix4.diagonal3Values(1.0, 1.0, 1.0);
expect(parent.find<int>(const Offset(0.0, 0.0)), 1);
});
});
group('$AnnotatedRegion findAllAnnotations', () {
test('finds the first value in a OffsetLayer when sized', () {
final ContainerLayer containerLayer = ContainerLayer();
final List<OffsetLayer> layers = <OffsetLayer>[
OffsetLayer(offset: Offset.zero),
OffsetLayer(offset: const Offset(0.0, 100.0)),
OffsetLayer(offset: const Offset(0.0, 200.0)),
];
int i = 0;
for (final OffsetLayer layer in layers) {
layer.append(AnnotatedRegionLayer<int>(i, size: const Size(200.0, 100.0)));
containerLayer.append(layer);
i += 1;
}
expect(containerLayer.findAllAnnotations<int>(const Offset(0.0, 1.0)).annotations.toList(), equals(<int>[0]));
expect(containerLayer.findAllAnnotations<int>(const Offset(0.0, 101.0)).annotations.toList(), equals(<int>[1]));
expect(containerLayer.findAllAnnotations<int>(const Offset(0.0, 201.0)).annotations.toList(), equals(<int>[2]));
});
test('finds a value within the clip in a ClipRectLayer', () {
final ContainerLayer containerLayer = ContainerLayer();
final List<ClipRectLayer> layers = <ClipRectLayer>[
ClipRectLayer(clipRect: const Rect.fromLTRB(0.0, 0.0, 100.0, 100.0)),
ClipRectLayer(clipRect: const Rect.fromLTRB(0.0, 100.0, 100.0, 200.0)),
ClipRectLayer(clipRect: const Rect.fromLTRB(0.0, 200.0, 100.0, 300.0)),
];
int i = 0;
for (final ClipRectLayer layer in layers) {
layer.append(AnnotatedRegionLayer<int>(i));
containerLayer.append(layer);
i += 1;
}
expect(containerLayer.findAllAnnotations<int>(const Offset(0.0, 1.0)).annotations.toList(), equals(<int>[0]));
expect(containerLayer.findAllAnnotations<int>(const Offset(0.0, 101.0)).annotations.toList(), equals(<int>[1]));
expect(containerLayer.findAllAnnotations<int>(const Offset(0.0, 201.0)).annotations.toList(), equals(<int>[2]));
});
test('finds a value within the clip in a ClipRRectLayer', () {
final ContainerLayer containerLayer = ContainerLayer();
final List<ClipRRectLayer> layers = <ClipRRectLayer>[
ClipRRectLayer(clipRRect: RRect.fromLTRBR(0.0, 0.0, 100.0, 100.0, const Radius.circular(4.0))),
ClipRRectLayer(clipRRect: RRect.fromLTRBR(0.0, 100.0, 100.0, 200.0, const Radius.circular(4.0))),
ClipRRectLayer(clipRRect: RRect.fromLTRBR(0.0, 200.0, 100.0, 300.0, const Radius.circular(4.0))),
];
int i = 0;
for (final ClipRRectLayer layer in layers) {
layer.append(AnnotatedRegionLayer<int>(i));
containerLayer.append(layer);
i += 1;
}
expect(containerLayer.findAllAnnotations<int>(const Offset(5.0, 5.0)).annotations.toList(), equals(<int>[0]));
expect(containerLayer.findAllAnnotations<int>(const Offset(5.0, 105.0)).annotations.toList(), equals(<int>[1]));
expect(containerLayer.findAllAnnotations<int>(const Offset(5.0, 205.0)).annotations.toList(), equals(<int>[2]));
});
test('finds a value under a TransformLayer', () {
final Matrix4 transform = Matrix4(
2.625, 0.0, 0.0, 0.0,
0.0, 2.625, 0.0, 0.0,
0.0, 0.0, 1.0, 0.0,
0.0, 0.0, 0.0, 1.0,
);
final TransformLayer transformLayer = TransformLayer(transform: transform);
final List<OffsetLayer> layers = <OffsetLayer>[
OffsetLayer(),
OffsetLayer(offset: const Offset(0.0, 100.0)),
OffsetLayer(offset: const Offset(0.0, 200.0)),
];
int i = 0;
for (final OffsetLayer layer in layers) {
final AnnotatedRegionLayer<int> annotatedRegionLayer = AnnotatedRegionLayer<int>(i, size: const Size(100.0, 100.0));
layer.append(annotatedRegionLayer);
transformLayer.append(layer);
i += 1;
}
expect(transformLayer.findAllAnnotations<int>(const Offset(0.0, 100.0)).annotations.toList(), equals(<int>[0]));
expect(transformLayer.findAllAnnotations<int>(const Offset(0.0, 200.0)).annotations.toList(), equals(<int>[0]));
expect(transformLayer.findAllAnnotations<int>(const Offset(0.0, 270.0)).annotations.toList(), equals(<int>[1]));
expect(transformLayer.findAllAnnotations<int>(const Offset(0.0, 400.0)).annotations.toList(), equals(<int>[1]));
expect(transformLayer.findAllAnnotations<int>(const Offset(0.0, 530.0)).annotations.toList(), equals(<int>[2]));
});
test('finds multiple nested, overlapping regions', () {
final ContainerLayer parent = ContainerLayer();
int index = 0;
final List<AnnotatedRegionLayer<int>> layers = <AnnotatedRegionLayer<int>>[
AnnotatedRegionLayer<int>(index++, size: const Size(100.0, 100.0)),
AnnotatedRegionLayer<int>(index++, size: const Size(100.0, 100.0)),
];
for (final ContainerLayer layer in layers) {
final AnnotatedRegionLayer<int> annotatedRegionLayer = AnnotatedRegionLayer<int>(index++, size: const Size(100.0, 100.0));
layer.append(annotatedRegionLayer);
parent.append(layer);
}
expect(parent.findAllAnnotations<int>(const Offset(0.0, 0.0)).annotations.toList(), equals(<int>[3, 1, 2, 0,]));
});
test('looks for child AnnotatedRegions before parents', () {
final AnnotatedRegionLayer<int> parent = AnnotatedRegionLayer<int>(1);
final AnnotatedRegionLayer<int> child1 = AnnotatedRegionLayer<int>(2);
final AnnotatedRegionLayer<int> child2 = AnnotatedRegionLayer<int>(3);
final AnnotatedRegionLayer<int> child3 = AnnotatedRegionLayer<int>(4);
final ContainerLayer layer = ContainerLayer();
parent.append(child1);
parent.append(child2);
parent.append(child3);
layer.append(parent);
expect(parent.findAllAnnotations<int>(Offset.zero).annotations.toList(), equals(<int>[4, 3, 2, 1]));
});
test('looks for correct type', () {
final AnnotatedRegionLayer<int> child1 = AnnotatedRegionLayer<int>(1);
final AnnotatedRegionLayer<String> child2 = AnnotatedRegionLayer<String>('hello');
final ContainerLayer layer = ContainerLayer();
layer.append(child2);
layer.append(child1);
expect(layer.findAllAnnotations<String>(Offset.zero).annotations.toList(), equals(<String>['hello']));
});
test('does not clip Layer.find on an AnnotatedRegion with an unrelated type', () {
final AnnotatedRegionLayer<int> child = AnnotatedRegionLayer<int>(1);
final AnnotatedRegionLayer<String> parent = AnnotatedRegionLayer<String>('hello', size: const Size(10.0, 10.0));
final ContainerLayer layer = ContainerLayer();
parent.append(child);
layer.append(parent);
expect(layer.findAllAnnotations<int>(const Offset(100.0, 100.0)).annotations.toList(), equals(<int>[1]));
});
test('handles non-invertable transforms', () {
final AnnotatedRegionLayer<int> child = AnnotatedRegionLayer<int>(1);
final TransformLayer parent = TransformLayer(transform: Matrix4.diagonal3Values(0.0, 1.0, 1.0));
parent.append(child);
expect(parent.findAllAnnotations<int>(const Offset(0.0, 0.0)).annotations.toList(), equals(<int>[]));
parent.transform = Matrix4.diagonal3Values(1.0, 1.0, 1.0);
expect(parent.findAllAnnotations<int>(const Offset(0.0, 0.0)).annotations.toList(), equals(<int>[1]));
});
});
}