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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
// 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.
@Tags(<String>['reduced-test-set'])
import 'dart:math' as math show pi;
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import '../widgets/semantics_tester.dart';
class MockCanvas extends Fake implements Canvas {
late Path capturedPath;
late Paint capturedPaint;
@override
void drawPath(Path path, Paint paint) {
capturedPath = path;
capturedPaint = paint;
}
late double capturedSx;
late double capturedSy;
@override
void scale(double sx, [double? sy]) {
capturedSx = sx;
capturedSy = sy!;
invocations.add(RecordedScale(sx, sy));
}
final List<RecordedCanvasCall> invocations = <RecordedCanvasCall>[];
@override
void rotate(double radians) {
invocations.add(RecordedRotate(radians));
}
@override
void translate(double dx, double dy) {
invocations.add(RecordedTranslate(dx, dy));
}
}
@immutable
abstract class RecordedCanvasCall {
const RecordedCanvasCall();
}
class RecordedRotate extends RecordedCanvasCall {
const RecordedRotate(this.radians);
final double radians;
@override
bool operator ==(Object other) {
return other is RecordedRotate && other.radians == radians;
}
@override
int get hashCode => radians.hashCode;
}
class RecordedTranslate extends RecordedCanvasCall {
const RecordedTranslate(this.dx, this.dy);
final double dx;
final double dy;
@override
bool operator ==(Object other) {
return other is RecordedTranslate && other.dx == dx && other.dy == dy;
}
@override
int get hashCode => Object.hash(dx, dy);
}
class RecordedScale extends RecordedCanvasCall {
const RecordedScale(this.sx, this.sy);
final double sx;
final double sy;
@override
bool operator ==(Object other) {
return other is RecordedScale && other.sx == sx && other.sy == sy;
}
@override
int get hashCode => Object.hash(sx, sy);
}
void main() {
testWidgets('IconTheme color', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: IconTheme(
data: IconThemeData(
color: Color(0xFF666666),
),
child: AnimatedIcon(
progress: AlwaysStoppedAnimation<double>(0.0),
icon: AnimatedIcons.arrow_menu,
),
),
),
);
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas();
customPaint.painter!.paint(canvas, const Size(48.0, 48.0));
expect(canvas.capturedPaint, hasColor(0xFF666666));
});
testWidgets('IconTheme opacity', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: IconTheme(
data: IconThemeData(
color: Color(0xFF666666),
opacity: 0.5,
),
child: AnimatedIcon(
progress: AlwaysStoppedAnimation<double>(0.0),
icon: AnimatedIcons.arrow_menu,
),
),
),
);
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas();
customPaint.painter!.paint(canvas, const Size(48.0, 48.0));
expect(canvas.capturedPaint, hasColor(0x80666666));
});
testWidgets('color overrides IconTheme color', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: IconTheme(
data: IconThemeData(
color: Color(0xFF666666),
),
child: AnimatedIcon(
progress: AlwaysStoppedAnimation<double>(0.0),
icon: AnimatedIcons.arrow_menu,
color: Color(0xFF0000FF),
),
),
),
);
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas();
customPaint.painter!.paint(canvas, const Size(48.0, 48.0));
expect(canvas.capturedPaint, hasColor(0xFF0000FF));
});
testWidgets('IconTheme size', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: IconTheme(
data: IconThemeData(
color: Color(0xFF666666),
size: 12.0,
),
child: AnimatedIcon(
progress: AlwaysStoppedAnimation<double>(0.0),
icon: AnimatedIcons.arrow_menu,
),
),
),
);
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas();
customPaint.painter!.paint(canvas, const Size(12.0, 12.0));
// arrow_menu default size is 48x48 so we expect it to be scaled by 0.25.
expect(canvas.capturedSx, 0.25);
expect(canvas.capturedSy, 0.25);
});
testWidgets('size overridesIconTheme size', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: IconTheme(
data: IconThemeData(
color: Color(0xFF666666),
size: 12.0,
),
child: AnimatedIcon(
progress: AlwaysStoppedAnimation<double>(0.0),
icon: AnimatedIcons.arrow_menu,
size: 96.0,
),
),
),
);
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas();
customPaint.painter!.paint(canvas, const Size(12.0, 12.0));
// arrow_menu default size is 48x48 so we expect it to be scaled by 2.
expect(canvas.capturedSx, 2);
expect(canvas.capturedSy, 2);
});
testWidgets('Semantic label', (WidgetTester tester) async {
final SemanticsTester semantics = SemanticsTester(tester);
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: AnimatedIcon(
progress: AlwaysStoppedAnimation<double>(0.0),
icon: AnimatedIcons.arrow_menu,
size: 96.0,
semanticLabel: 'a label',
),
),
);
expect(semantics, includesNodeWith(label: 'a label'));
semantics.dispose();
});
testWidgets('Inherited text direction rtl', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.rtl,
child: IconTheme(
data: IconThemeData(
color: Color(0xFF666666),
),
child: RepaintBoundary(
child: AnimatedIcon(
progress: AlwaysStoppedAnimation<double>(0.0),
icon: AnimatedIcons.arrow_menu,
),
),
),
),
);
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas();
customPaint.painter!.paint(canvas, const Size(48.0, 48.0));
expect(canvas.invocations, const <RecordedCanvasCall>[
RecordedRotate(math.pi),
RecordedTranslate(-48, -48),
RecordedScale(0.5, 0.5),
]);
await expectLater(find.byType(AnimatedIcon),
matchesGoldenFile('animated_icons_test.icon.rtl.png'));
});
testWidgets('Inherited text direction ltr', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: IconTheme(
data: IconThemeData(
color: Color(0xFF666666),
),
child: RepaintBoundary(
child: AnimatedIcon(
progress: AlwaysStoppedAnimation<double>(0.0),
icon: AnimatedIcons.arrow_menu,
),
),
),
),
);
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas();
customPaint.painter!.paint(canvas, const Size(48.0, 48.0));
expect(canvas.invocations, const <RecordedCanvasCall>[
RecordedScale(0.5, 0.5),
]);
await expectLater(find.byType(AnimatedIcon),
matchesGoldenFile('animated_icons_test.icon.ltr.png'));
});
testWidgets('Inherited text direction overridden', (WidgetTester tester) async {
await tester.pumpWidget(
const Directionality(
textDirection: TextDirection.ltr,
child: IconTheme(
data: IconThemeData(
color: Color(0xFF666666),
),
child: AnimatedIcon(
progress: AlwaysStoppedAnimation<double>(0.0),
icon: AnimatedIcons.arrow_menu,
textDirection: TextDirection.rtl,
),
),
),
);
final CustomPaint customPaint = tester.widget(find.byType(CustomPaint));
final MockCanvas canvas = MockCanvas();
customPaint.painter!.paint(canvas, const Size(48.0, 48.0));
expect(canvas.invocations, const <RecordedCanvasCall>[
RecordedRotate(math.pi),
RecordedTranslate(-48, -48),
RecordedScale(0.5, 0.5),
]);
});
testWidgets('Direction has no effect on position of widget', (WidgetTester tester) async {
const AnimatedIcon icon = AnimatedIcon(
progress: AlwaysStoppedAnimation<double>(0.0),
icon: AnimatedIcons.arrow_menu,
);
await tester.pumpWidget(
const Directionality(textDirection: TextDirection.rtl, child: icon),
);
final Rect rtlRect = tester.getRect(find.byType(AnimatedIcon));
await tester.pumpWidget(
const Directionality(textDirection: TextDirection.ltr, child: icon),
);
final Rect ltrRect = tester.getRect(find.byType(AnimatedIcon));
expect(rtlRect, ltrRect);
});
}
PaintColorMatcher hasColor(int color) {
return PaintColorMatcher(color);
}
class PaintColorMatcher extends Matcher {
const PaintColorMatcher(this.expectedColor);
final int expectedColor;
@override
Description describe(Description description) =>
description.add('color was not $expectedColor');
@override
bool matches(dynamic item, Map<dynamic, dynamic> matchState) {
final Paint actualPaint = item as Paint;
return actualPaint.color == Color(expectedColor);
}
}