syncing_test.dart 5.11 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.

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

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

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

20
  @override
21
  TestWidgetState createState() => TestWidgetState();
22 23 24
}

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

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

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

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

void main() {

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

63
    final TestWidgetState state = tester.state(find.byType(TestWidget));
64

65 66
    expect(state.persistentState, equals(1));
    expect(state.updates, equals(0));
67

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

79 80
    expect(state.persistentState, equals(1));
    expect(state.updates, equals(1));
81

82
    await tester.pumpWidget(Container());
83 84
  });

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

97
    TestWidgetState state = tester.state(find.byType(TestWidget));
98

99 100
    expect(state.persistentState, equals(10));
    expect(state.updates, equals(0));
101

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

111
    state = tester.state(find.byType(TestWidget));
112

113 114
    expect(state.persistentState, equals(11));
    expect(state.updates, equals(0));
115

116
    await tester.pumpWidget(Container());
117
  });
118

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

124 125
    final GlobalKey keyA = GlobalKey();
    final GlobalKey keyB = GlobalKey();
126

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

    TestWidgetState first, second;

144 145
    first = tester.state(find.byWidget(a));
    second = tester.state(find.byWidget(b));
146

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

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

169 170
    first = tester.state(find.byWidget(a));
    second = tester.state(find.byWidget(b));
171 172

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

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

198 199
    first = tester.state(find.byWidget(b));
    second = tester.state(find.byWidget(a));
200

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