syncing_test.dart 5.18 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4
// 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.

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
  const TestWidget({ this.child, this.persistentState, this.syncedState });
10 11 12 13 14

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

15
  @override
16 17 18 19
  TestWidgetState createState() => new TestWidgetState();
}

class TestWidgetState extends State<TestWidget> {
20 21
  int persistentState;
  int syncedState;
22 23
  int updates = 0;

24
  @override
25 26
  void initState() {
    super.initState();
27 28
    persistentState = widget.persistentState;
    syncedState = widget.syncedState;
29 30
  }

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

40
  @override
41
  Widget build(BuildContext context) => widget.child;
42 43 44 45
}

void main() {

46 47
  testWidgets('no change', (WidgetTester tester) async {
    await tester.pumpWidget(
48 49 50 51 52
      new Container(
        child: new Container(
          child: new TestWidget(
            persistentState: 1,
            child: new Container()
53 54
          )
        )
55 56
      )
    );
57

58
    final TestWidgetState state = tester.state(find.byType(TestWidget));
59

60 61
    expect(state.persistentState, equals(1));
    expect(state.updates, equals(0));
62

63
    await tester.pumpWidget(
64 65 66 67 68
      new Container(
        child: new Container(
          child: new TestWidget(
            persistentState: 2,
            child: new Container()
69 70
          )
        )
71 72
      )
    );
73

74 75
    expect(state.persistentState, equals(1));
    expect(state.updates, equals(1));
76

77
    await tester.pumpWidget(new Container());
78 79
  });

80 81
  testWidgets('remove one', (WidgetTester tester) async {
    await tester.pumpWidget(
82 83 84 85 86
      new Container(
        child: new Container(
          child: new TestWidget(
            persistentState: 10,
            child: new Container()
87 88
          )
        )
89 90
      )
    );
91

92
    TestWidgetState state = tester.state(find.byType(TestWidget));
93

94 95
    expect(state.persistentState, equals(10));
    expect(state.updates, equals(0));
96

97
    await tester.pumpWidget(
98 99 100 101
      new Container(
        child: new TestWidget(
          persistentState: 11,
          child: new Container()
102
        )
103 104
      )
    );
105

106
    state = tester.state(find.byType(TestWidget));
107

108 109
    expect(state.persistentState, equals(11));
    expect(state.updates, equals(0));
110

111
    await tester.pumpWidget(new Container());
112
  });
113

114
  testWidgets('swap instances around', (WidgetTester tester) async {
115 116
    const Widget a = const TestWidget(persistentState: 0x61, syncedState: 0x41, child: const Text('apple', textDirection: TextDirection.ltr));
    const Widget b = const TestWidget(persistentState: 0x62, syncedState: 0x42, child: const Text('banana', textDirection: TextDirection.ltr));
117
    await tester.pumpWidget(new Column());
118

119 120
    final GlobalKey keyA = new GlobalKey();
    final GlobalKey keyB = new GlobalKey();
121

122
    await tester.pumpWidget(
123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138
      new Column(
        children: <Widget>[
          new Container(
            key: keyA,
            child: a
          ),
          new Container(
            key: keyB,
            child: b
          )
        ]
      )
    );

    TestWidgetState first, second;

139 140
    first = tester.state(find.byWidget(a));
    second = tester.state(find.byWidget(b));
141

142
    expect(first.widget, equals(a));
143 144
    expect(first.persistentState, equals(0x61));
    expect(first.syncedState, equals(0x41));
145
    expect(second.widget, equals(b));
146 147 148
    expect(second.persistentState, equals(0x62));
    expect(second.syncedState, equals(0x42));

149
    await tester.pumpWidget(
150 151 152 153 154 155 156 157 158 159 160 161 162 163
      new Column(
        children: <Widget>[
          new Container(
            key: keyA,
            child: a
          ),
          new Container(
            key: keyB,
            child: b
          )
        ]
      )
    );

164 165
    first = tester.state(find.byWidget(a));
    second = tester.state(find.byWidget(b));
166 167

    // same as before
168
    expect(first.widget, equals(a));
169 170
    expect(first.persistentState, equals(0x61));
    expect(first.syncedState, equals(0x41));
171
    expect(second.widget, equals(b));
172 173 174 175 176 177
    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

178
    await tester.pumpWidget(
179 180 181 182 183 184 185 186 187 188 189 190 191 192
      new Column(
        children: <Widget>[
          new Container(
            key: keyA,
            child: b
          ),
          new Container(
            key: keyB,
            child: a
          )
        ]
      )
    );

193 194
    first = tester.state(find.byWidget(b));
    second = tester.state(find.byWidget(a));
195

196
    expect(first.widget, equals(b));
197 198
    expect(first.persistentState, equals(0x61));
    expect(first.syncedState, equals(0x42));
199
    expect(second.widget, equals(a));
200 201
    expect(second.persistentState, equals(0x62));
    expect(second.syncedState, equals(0x41));
202
  });
203
}