syncing_test.dart 5.12 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Hixie's avatar
Hixie committed
2 3 4
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

5 6
// @dart = 2.8

Adam Barth's avatar
Adam Barth committed
7
import 'package:flutter_test/flutter_test.dart';
8
import 'package:flutter/widgets.dart';
9

10
class TestWidget extends StatefulWidget {
11 12 13 14 15 16
  const TestWidget({
    Key key,
    this.child,
    this.persistentState,
    this.syncedState,
  }) : super(key: key);
17 18 19 20 21

  final Widget child;
  final int persistentState;
  final int syncedState;

22
  @override
23
  TestWidgetState createState() => TestWidgetState();
24 25 26
}

class TestWidgetState extends State<TestWidget> {
27 28
  int persistentState;
  int syncedState;
29 30
  int updates = 0;

31
  @override
32 33
  void initState() {
    super.initState();
34 35
    persistentState = widget.persistentState;
    syncedState = widget.syncedState;
36 37
  }

38
  @override
39
  void didUpdateWidget(TestWidget oldWidget) {
40
    super.didUpdateWidget(oldWidget);
41
    syncedState = widget.syncedState;
42
    // we explicitly do NOT sync the persistentState from the new instance
43
    // because we're using that to track whether we got recreated
44
    updates += 1;
45
  }
46

47
  @override
48
  Widget build(BuildContext context) => widget.child;
49 50 51 52
}

void main() {

53 54
  testWidgets('no change', (WidgetTester tester) async {
    await tester.pumpWidget(
55 56 57
      Container(
        child: Container(
          child: TestWidget(
58
            persistentState: 1,
59 60 61
            child: Container(),
          ),
        ),
62
      ),
63
    );
64

65
    final TestWidgetState state = tester.state(find.byType(TestWidget));
66

67 68
    expect(state.persistentState, equals(1));
    expect(state.updates, equals(0));
69

70
    await tester.pumpWidget(
71 72 73
      Container(
        child: Container(
          child: TestWidget(
74
            persistentState: 2,
75 76 77
            child: Container(),
          ),
        ),
78
      ),
79
    );
80

81 82
    expect(state.persistentState, equals(1));
    expect(state.updates, equals(1));
83

84
    await tester.pumpWidget(Container());
85 86
  });

87 88
  testWidgets('remove one', (WidgetTester tester) async {
    await tester.pumpWidget(
89 90 91
      Container(
        child: Container(
          child: TestWidget(
92
            persistentState: 10,
93 94 95
            child: Container(),
          ),
        ),
96
      ),
97
    );
98

99
    TestWidgetState state = tester.state(find.byType(TestWidget));
100

101 102
    expect(state.persistentState, equals(10));
    expect(state.updates, equals(0));
103

104
    await tester.pumpWidget(
105 106
      Container(
        child: TestWidget(
107
          persistentState: 11,
108 109
          child: Container(),
        ),
110
      ),
111
    );
112

113
    state = tester.state(find.byType(TestWidget));
114

115 116
    expect(state.persistentState, equals(11));
    expect(state.updates, equals(0));
117

118
    await tester.pumpWidget(Container());
119
  });
120

121
  testWidgets('swap instances around', (WidgetTester tester) async {
122 123
    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));
124
    await tester.pumpWidget(Column());
125

126 127
    final GlobalKey keyA = GlobalKey();
    final GlobalKey keyB = GlobalKey();
128

129
    await tester.pumpWidget(
130
      Column(
131
        children: <Widget>[
132
          Container(
133
            key: keyA,
134
            child: a,
135
          ),
136
          Container(
137
            key: keyB,
138 139 140
            child: b,
          ),
        ],
141
      ),
142 143 144 145
    );

    TestWidgetState first, second;

146 147
    first = tester.state(find.byWidget(a));
    second = tester.state(find.byWidget(b));
148

149
    expect(first.widget, equals(a));
150 151
    expect(first.persistentState, equals(0x61));
    expect(first.syncedState, equals(0x41));
152
    expect(second.widget, equals(b));
153 154 155
    expect(second.persistentState, equals(0x62));
    expect(second.syncedState, equals(0x42));

156
    await tester.pumpWidget(
157
      Column(
158
        children: <Widget>[
159
          Container(
160
            key: keyA,
161
            child: a,
162
          ),
163
          Container(
164
            key: keyB,
165 166 167
            child: b,
          ),
        ],
168
      ),
169 170
    );

171 172
    first = tester.state(find.byWidget(a));
    second = tester.state(find.byWidget(b));
173 174

    // same as before
175
    expect(first.widget, equals(a));
176 177
    expect(first.persistentState, equals(0x61));
    expect(first.syncedState, equals(0x41));
178
    expect(second.widget, equals(b));
179 180 181 182 183 184
    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

185
    await tester.pumpWidget(
186
      Column(
187
        children: <Widget>[
188
          Container(
189
            key: keyA,
190
            child: b,
191
          ),
192
          Container(
193
            key: keyB,
194 195 196
            child: a,
          ),
        ],
197
      ),
198 199
    );

200 201
    first = tester.state(find.byWidget(b));
    second = tester.state(find.byWidget(a));
202

203
    expect(first.widget, equals(b));
204 205
    expect(first.persistentState, equals(0x61));
    expect(first.syncedState, equals(0x42));
206
    expect(second.widget, equals(a));
207 208
    expect(second.persistentState, equals(0x62));
    expect(second.syncedState, equals(0x41));
209
  });
210
}