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
// Copyright 2015 The Chromium 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_test/flutter_test.dart';
import 'package:flutter/rendering.dart';
import 'package:flutter/widgets.dart';
final BoxDecoration kBoxDecorationA = new BoxDecoration(); // ignore: prefer_const_constructor
final BoxDecoration kBoxDecorationB = new BoxDecoration(); // ignore: prefer_const_constructor
final BoxDecoration kBoxDecorationC = new BoxDecoration(); // ignore: prefer_const_constructor
class TestWidget extends StatelessWidget {
const TestWidget({ this.child });
final Widget child;
@override
Widget build(BuildContext context) => child;
}
class TestOrientedBox extends SingleChildRenderObjectWidget {
TestOrientedBox({ Key key, Widget child }) : super(key: key, child: child);
Decoration _getDecoration(BuildContext context) {
Orientation orientation = MediaQuery.of(context).orientation;
switch (orientation) {
case Orientation.landscape:
return const BoxDecoration(backgroundColor: const Color(0xFF00FF00));
case Orientation.portrait:
return const BoxDecoration(backgroundColor: const Color(0xFF0000FF));
}
assert(orientation != null);
return null;
}
@override
RenderDecoratedBox createRenderObject(BuildContext context) => new RenderDecoratedBox(decoration: _getDecoration(context));
@override
void updateRenderObject(BuildContext context, RenderDecoratedBox renderObject) {
renderObject.decoration = _getDecoration(context);
}
}
void main() {
testWidgets('RenderObjectWidget smoke test', (WidgetTester tester) async {
await tester.pumpWidget(new DecoratedBox(decoration: kBoxDecorationA));
SingleChildRenderObjectElement element =
tester.element(find.byElementType(SingleChildRenderObjectElement));
expect(element, isNotNull);
expect(element.renderObject is RenderDecoratedBox, isTrue);
RenderDecoratedBox renderObject = element.renderObject;
expect(renderObject.decoration, equals(kBoxDecorationA));
expect(renderObject.position, equals(DecorationPosition.background));
await tester.pumpWidget(new DecoratedBox(decoration: kBoxDecorationB));
element = tester.element(find.byElementType(SingleChildRenderObjectElement));
expect(element, isNotNull);
expect(element.renderObject is RenderDecoratedBox, isTrue);
renderObject = element.renderObject;
expect(renderObject.decoration, equals(kBoxDecorationB));
expect(renderObject.position, equals(DecorationPosition.background));
});
testWidgets('RenderObjectWidget can add and remove children', (WidgetTester tester) async {
void checkFullTree() {
SingleChildRenderObjectElement element =
tester.firstElement(find.byElementType(SingleChildRenderObjectElement));
expect(element, isNotNull);
expect(element.renderObject is RenderDecoratedBox, isTrue);
RenderDecoratedBox renderObject = element.renderObject;
expect(renderObject.decoration, equals(kBoxDecorationA));
expect(renderObject.position, equals(DecorationPosition.background));
expect(renderObject.child, isNotNull);
expect(renderObject.child is RenderDecoratedBox, isTrue);
RenderDecoratedBox child = renderObject.child;
expect(child.decoration, equals(kBoxDecorationB));
expect(child.position, equals(DecorationPosition.background));
expect(child.child, isNull);
}
void childBareTree() {
SingleChildRenderObjectElement element =
tester.element(find.byElementType(SingleChildRenderObjectElement));
expect(element, isNotNull);
expect(element.renderObject is RenderDecoratedBox, isTrue);
RenderDecoratedBox renderObject = element.renderObject;
expect(renderObject.decoration, equals(kBoxDecorationA));
expect(renderObject.position, equals(DecorationPosition.background));
expect(renderObject.child, isNull);
}
await tester.pumpWidget(new DecoratedBox(
decoration: kBoxDecorationA,
child: new DecoratedBox(
decoration: kBoxDecorationB
)
));
checkFullTree();
await tester.pumpWidget(new DecoratedBox(
decoration: kBoxDecorationA,
child: new TestWidget(
child: new DecoratedBox(
decoration: kBoxDecorationB
)
)
));
checkFullTree();
await tester.pumpWidget(new DecoratedBox(
decoration: kBoxDecorationA,
child: new DecoratedBox(
decoration: kBoxDecorationB
)
));
checkFullTree();
await tester.pumpWidget(new DecoratedBox(
decoration: kBoxDecorationA
));
childBareTree();
await tester.pumpWidget(new DecoratedBox(
decoration: kBoxDecorationA,
child: new TestWidget(
child: new TestWidget(
child: new DecoratedBox(
decoration: kBoxDecorationB
)
)
)
));
checkFullTree();
await tester.pumpWidget(new DecoratedBox(
decoration: kBoxDecorationA
));
childBareTree();
});
testWidgets('Detached render tree is intact', (WidgetTester tester) async {
await tester.pumpWidget(new DecoratedBox(
decoration: kBoxDecorationA,
child: new DecoratedBox(
decoration: kBoxDecorationB,
child: new DecoratedBox(
decoration: kBoxDecorationC
)
)
));
SingleChildRenderObjectElement element =
tester.firstElement(find.byElementType(SingleChildRenderObjectElement));
expect(element.renderObject is RenderDecoratedBox, isTrue);
RenderDecoratedBox parent = element.renderObject;
expect(parent.child is RenderDecoratedBox, isTrue);
RenderDecoratedBox child = parent.child;
expect(child.decoration, equals(kBoxDecorationB));
expect(child.child is RenderDecoratedBox, isTrue);
RenderDecoratedBox grandChild = child.child;
expect(grandChild.decoration, equals(kBoxDecorationC));
expect(grandChild.child, isNull);
await tester.pumpWidget(new DecoratedBox(
decoration: kBoxDecorationA
));
element =
tester.element(find.byElementType(SingleChildRenderObjectElement));
expect(element.renderObject is RenderDecoratedBox, isTrue);
expect(element.renderObject, equals(parent));
expect(parent.child, isNull);
expect(child.parent, isNull);
expect(child.decoration, equals(kBoxDecorationB));
expect(child.child, equals(grandChild));
expect(grandChild.parent, equals(child));
expect(grandChild.decoration, equals(kBoxDecorationC));
expect(grandChild.child, isNull);
});
testWidgets('Can watch inherited widgets', (WidgetTester tester) async {
Key boxKey = new UniqueKey();
TestOrientedBox box = new TestOrientedBox(key: boxKey);
await tester.pumpWidget(new MediaQuery(
data: const MediaQueryData(size: const Size(400.0, 300.0)),
child: box
));
RenderDecoratedBox renderBox = tester.renderObject(find.byKey(boxKey));
BoxDecoration decoration = renderBox.decoration;
expect(decoration.backgroundColor, equals(const Color(0xFF00FF00)));
await tester.pumpWidget(new MediaQuery(
data: const MediaQueryData(size: const Size(300.0, 400.0)),
child: box
));
decoration = renderBox.decoration;
expect(decoration.backgroundColor, equals(const Color(0xFF0000FF)));
});
}