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
// 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/widgets.dart';
class TestWidget extends StatefulWidget {
const TestWidget({ this.child, this.persistentState, this.syncedState });
final Widget child;
final int persistentState;
final int syncedState;
@override
TestWidgetState createState() => TestWidgetState();
}
class TestWidgetState extends State<TestWidget> {
int persistentState;
int syncedState;
int updates = 0;
@override
void initState() {
super.initState();
persistentState = widget.persistentState;
syncedState = widget.syncedState;
}
@override
void didUpdateWidget(TestWidget oldWidget) {
super.didUpdateWidget(oldWidget);
syncedState = widget.syncedState;
// we explicitly do NOT sync the persistentState from the new instance
// because we're using that to track whether we got recreated
updates += 1;
}
@override
Widget build(BuildContext context) => widget.child;
}
void main() {
testWidgets('no change', (WidgetTester tester) async {
await tester.pumpWidget(
Container(
child: Container(
child: TestWidget(
persistentState: 1,
child: Container()
)
)
)
);
final TestWidgetState state = tester.state(find.byType(TestWidget));
expect(state.persistentState, equals(1));
expect(state.updates, equals(0));
await tester.pumpWidget(
Container(
child: Container(
child: TestWidget(
persistentState: 2,
child: Container()
)
)
)
);
expect(state.persistentState, equals(1));
expect(state.updates, equals(1));
await tester.pumpWidget(Container());
});
testWidgets('remove one', (WidgetTester tester) async {
await tester.pumpWidget(
Container(
child: Container(
child: TestWidget(
persistentState: 10,
child: Container()
)
)
)
);
TestWidgetState state = tester.state(find.byType(TestWidget));
expect(state.persistentState, equals(10));
expect(state.updates, equals(0));
await tester.pumpWidget(
Container(
child: TestWidget(
persistentState: 11,
child: Container()
)
)
);
state = tester.state(find.byType(TestWidget));
expect(state.persistentState, equals(11));
expect(state.updates, equals(0));
await tester.pumpWidget(Container());
});
testWidgets('swap instances around', (WidgetTester tester) async {
const Widget a = TestWidget(persistentState: 0x61, syncedState: 0x41, child: Text('apple', textDirection: TextDirection.ltr));
const Widget b = TestWidget(persistentState: 0x62, syncedState: 0x42, child: Text('banana', textDirection: TextDirection.ltr));
await tester.pumpWidget(Column());
final GlobalKey keyA = GlobalKey();
final GlobalKey keyB = GlobalKey();
await tester.pumpWidget(
Column(
children: <Widget>[
Container(
key: keyA,
child: a
),
Container(
key: keyB,
child: b
)
]
)
);
TestWidgetState first, second;
first = tester.state(find.byWidget(a));
second = tester.state(find.byWidget(b));
expect(first.widget, equals(a));
expect(first.persistentState, equals(0x61));
expect(first.syncedState, equals(0x41));
expect(second.widget, equals(b));
expect(second.persistentState, equals(0x62));
expect(second.syncedState, equals(0x42));
await tester.pumpWidget(
Column(
children: <Widget>[
Container(
key: keyA,
child: a
),
Container(
key: keyB,
child: b
)
]
)
);
first = tester.state(find.byWidget(a));
second = tester.state(find.byWidget(b));
// same as before
expect(first.widget, equals(a));
expect(first.persistentState, equals(0x61));
expect(first.syncedState, equals(0x41));
expect(second.widget, equals(b));
expect(second.persistentState, equals(0x62));
expect(second.syncedState, equals(0x42));
// now we swap the nodes over
// since they are both "old" nodes, they shouldn't sync with each other even though they look alike
await tester.pumpWidget(
Column(
children: <Widget>[
Container(
key: keyA,
child: b
),
Container(
key: keyB,
child: a
)
]
)
);
first = tester.state(find.byWidget(b));
second = tester.state(find.byWidget(a));
expect(first.widget, equals(b));
expect(first.persistentState, equals(0x61));
expect(first.syncedState, equals(0x42));
expect(second.widget, equals(a));
expect(second.persistentState, equals(0x62));
expect(second.syncedState, equals(0x41));
});
}