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
// 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 'package:flutter_test/flutter_test.dart';
class TestCustomPainter extends CustomPainter {
TestCustomPainter({ required this.log, this.name });
final List<String?> log;
final String? name;
@override
void paint(Canvas canvas, Size size) {
log.add(name);
}
@override
bool shouldRepaint(TestCustomPainter oldPainter) => true;
}
class MockCanvas extends Fake implements Canvas {
int saveCount = 0;
int saveCountDelta = 1;
@override
int getSaveCount() {
return saveCount += saveCountDelta;
}
@override
void save() { }
}
class MockPaintingContext extends Fake implements PaintingContext {
@override
final MockCanvas canvas = MockCanvas();
}
void main() {
testWidgets('Control test for custom painting', (WidgetTester tester) async {
final List<String?> log = <String?>[];
await tester.pumpWidget(CustomPaint(
painter: TestCustomPainter(
log: log,
name: 'background',
),
foregroundPainter: TestCustomPainter(
log: log,
name: 'foreground',
),
child: CustomPaint(
painter: TestCustomPainter(
log: log,
name: 'child',
),
),
));
expect(log, equals(<String>['background', 'child', 'foreground']));
});
testWidgets('Throws FlutterError on custom painter incorrect restore/save calls', (WidgetTester tester) async {
final GlobalKey target = GlobalKey();
final List<String?> log = <String?>[];
await tester.pumpWidget(CustomPaint(
key: target,
isComplex: true,
painter: TestCustomPainter(log: log),
));
final RenderCustomPaint renderCustom = target.currentContext!.findRenderObject()! as RenderCustomPaint;
final MockPaintingContext paintingContext = MockPaintingContext();
final MockCanvas canvas = paintingContext.canvas;
FlutterError getError() {
late FlutterError error;
try {
renderCustom.paint(paintingContext, Offset.zero);
} on FlutterError catch (e) {
error = e;
}
return error;
}
FlutterError error = getError();
expect(error.toStringDeep(), equalsIgnoringHashCodes(
'FlutterError\n'
' The TestCustomPainter#00000() custom painter called canvas.save()\n'
' or canvas.saveLayer() at least 1 more time than it called\n'
' canvas.restore().\n'
' This leaves the canvas in an inconsistent state and will probably\n'
' result in a broken display.\n'
' You must pair each call to save()/saveLayer() with a later\n'
' matching call to restore().\n',
));
canvas.saveCountDelta = -1;
error = getError();
expect(error.toStringDeep(), equalsIgnoringHashCodes(
'FlutterError\n'
' The TestCustomPainter#00000() custom painter called\n'
' canvas.restore() 1 more time than it called canvas.save() or\n'
' canvas.saveLayer().\n'
' This leaves the canvas in an inconsistent state and will result\n'
' in a broken display.\n'
' You should only call restore() if you first called save() or\n'
' saveLayer().\n',
));
canvas.saveCountDelta = 2;
error = getError();
expect(error.toStringDeep(), contains('2 more times'));
canvas.saveCountDelta = -2;
error = getError();
expect(error.toStringDeep(), contains('2 more times'));
});
testWidgets('CustomPaint sizing', (WidgetTester tester) async {
final GlobalKey target = GlobalKey();
await tester.pumpWidget(Center(
child: CustomPaint(key: target),
));
expect(target.currentContext!.size, Size.zero);
await tester.pumpWidget(Center(
child: CustomPaint(key: target, child: Container()),
));
expect(target.currentContext!.size, const Size(800.0, 600.0));
await tester.pumpWidget(Center(
child: CustomPaint(key: target, size: const Size(20.0, 20.0)),
));
expect(target.currentContext!.size, const Size(20.0, 20.0));
await tester.pumpWidget(Center(
child: CustomPaint(key: target, size: const Size(2000.0, 100.0)),
));
expect(target.currentContext!.size, const Size(800.0, 100.0));
await tester.pumpWidget(Center(
child: CustomPaint(key: target, child: Container()),
));
expect(target.currentContext!.size, const Size(800.0, 600.0));
await tester.pumpWidget(Center(
child: CustomPaint(key: target, child: const SizedBox.shrink()),
));
expect(target.currentContext!.size, Size.zero);
});
testWidgets('Raster cache hints', (WidgetTester tester) async {
final GlobalKey target = GlobalKey();
final List<String?> log = <String?>[];
await tester.pumpWidget(CustomPaint(
key: target,
isComplex: true,
painter: TestCustomPainter(log: log),
));
RenderCustomPaint renderCustom = target.currentContext!.findRenderObject()! as RenderCustomPaint;
expect(renderCustom.isComplex, true);
expect(renderCustom.willChange, false);
await tester.pumpWidget(CustomPaint(
key: target,
willChange: true,
foregroundPainter: TestCustomPainter(log: log),
));
renderCustom = target.currentContext!.findRenderObject()! as RenderCustomPaint;
expect(renderCustom.isComplex, false);
expect(renderCustom.willChange, true);
});
test('Raster cache hints cannot be set with null painters', () {
expect(() => CustomPaint(isComplex: true), throwsAssertionError);
expect(() => CustomPaint(willChange: true), throwsAssertionError);
});
test('RenderCustomPaint consults preferred size for intrinsics when it has no child', () {
final RenderCustomPaint inner = RenderCustomPaint(preferredSize: const Size(20, 30));
expect(inner.getMinIntrinsicWidth(double.infinity), 20);
expect(inner.getMaxIntrinsicWidth(double.infinity), 20);
expect(inner.getMinIntrinsicHeight(double.infinity), 30);
expect(inner.getMaxIntrinsicHeight(double.infinity), 30);
});
test('RenderCustomPaint does not return infinity for its intrinsics', () {
final RenderCustomPaint inner = RenderCustomPaint(preferredSize: Size.infinite);
expect(inner.getMinIntrinsicWidth(double.infinity), 0);
expect(inner.getMaxIntrinsicWidth(double.infinity), 0);
expect(inner.getMinIntrinsicHeight(double.infinity), 0);
expect(inner.getMaxIntrinsicHeight(double.infinity), 0);
});
}