syncing_test.dart 5.28 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
import 'package:test/test.dart';

9
class TestWidget extends StatefulWidget {
10 11 12 13 14 15
  TestWidget({ this.child, this.persistentState, this.syncedState });

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

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

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

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

32
  @override
33 34
  void didUpdateConfig(TestWidget oldConfig) {
    syncedState = config.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) => config.child;
42 43 44 45 46
}

void main() {

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

59
      TestWidgetState state = tester.findStateOfType(TestWidgetState);
60

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

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

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

78 79
      tester.pumpWidget(new Container());
    });
80 81
  });

82
  test('remove one', () {
83 84 85 86 87 88 89 90
    testWidgets((WidgetTester tester) {
      tester.pumpWidget(
        new Container(
          child: new Container(
            child: new TestWidget(
              persistentState: 10,
              child: new Container()
            )
91 92
          )
        )
93
      );
94

95
      TestWidgetState state = tester.findStateOfType(TestWidgetState);
96

97 98
      expect(state.persistentState, equals(10));
      expect(state.updates, equals(0));
99

100 101 102 103 104 105
      tester.pumpWidget(
        new Container(
          child: new TestWidget(
            persistentState: 11,
            child: new Container()
          )
106
        )
107
      );
108

109
      state = tester.findStateOfType(TestWidgetState);
110

111 112
      expect(state.persistentState, equals(11));
      expect(state.updates, equals(0));
113

114 115
      tester.pumpWidget(new Container());
    });
116
  });
117

118
  test('swap instances around', () {
119 120 121
    testWidgets((WidgetTester tester) {
      Widget a = new TestWidget(persistentState: 0x61, syncedState: 0x41, child: new Text('apple'));
      Widget b = new TestWidget(persistentState: 0x62, syncedState: 0x42, child: new Text('banana'));
122
      tester.pumpWidget(new Column());
123 124 125 126 127

      GlobalKey keyA = new GlobalKey();
      GlobalKey keyB = new GlobalKey();

      tester.pumpWidget(
128 129 130 131 132 133 134 135 136 137 138 139
        new Column(
          children: <Widget>[
            new Container(
              key: keyA,
              child: a
            ),
            new Container(
              key: keyB,
              child: b
            )
          ]
        )
140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
      );

      TestWidgetState first, second;

      first = tester.findStateByConfig(a);
      second = tester.findStateByConfig(b);

      expect(first.config, equals(a));
      expect(first.persistentState, equals(0x61));
      expect(first.syncedState, equals(0x41));
      expect(second.config, equals(b));
      expect(second.persistentState, equals(0x62));
      expect(second.syncedState, equals(0x42));

      tester.pumpWidget(
155 156 157 158 159 160 161 162 163 164 165 166
        new Column(
          children: <Widget>[
            new Container(
              key: keyA,
              child: a
            ),
            new Container(
              key: keyB,
              child: b
            )
          ]
        )
167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
      );

      first = tester.findStateByConfig(a);
      second = tester.findStateByConfig(b);

      // same as before
      expect(first.config, equals(a));
      expect(first.persistentState, equals(0x61));
      expect(first.syncedState, equals(0x41));
      expect(second.config, 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

      tester.pumpWidget(
184 185 186 187 188 189 190 191 192 193 194 195
        new Column(
          children: <Widget>[
            new Container(
              key: keyA,
              child: b
            ),
            new Container(
              key: keyB,
              child: a
            )
          ]
        )
196 197 198 199 200 201 202 203 204 205 206 207
      );

      first = tester.findStateByConfig(b);
      second = tester.findStateByConfig(a);

      expect(first.config, equals(b));
      expect(first.persistentState, equals(0x61));
      expect(first.syncedState, equals(0x42));
      expect(second.config, equals(a));
      expect(second.persistentState, equals(0x62));
      expect(second.syncedState, equals(0x41));
    });
208
  });
209
}