syncing_test.dart 4.89 KB
Newer Older
1
import 'package:sky/widgets.dart';
2 3
import 'package:test/test.dart';

Adam Barth's avatar
Adam Barth committed
4
import 'widget_tester.dart';
5

6 7 8 9 10 11 12 13 14 15 16
class TestWidget extends StatefulComponent {
  TestWidget({ this.child, this.persistentState, this.syncedState });

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

  TestWidgetState createState() => new TestWidgetState();
}

class TestWidgetState extends State<TestWidget> {
17 18
  int persistentState;
  int syncedState;
19 20
  int updates = 0;

21 22
  void initState() {
    super.initState();
23 24 25 26 27 28
    persistentState = config.persistentState;
    syncedState = config.syncedState;
  }

  void didUpdateConfig(TestWidget oldConfig) {
    syncedState = config.syncedState;
29
    // we explicitly do NOT sync the persistentState from the new instance
30
    // because we're using that to track whether we got recreated
31
    updates += 1;
32
  }
33 34

  Widget build(BuildContext context) => config.child;
35 36 37 38 39
}

void main() {

  test('no change', () {
40 41 42 43 44 45 46 47
    testWidgets((WidgetTester tester) {
      tester.pumpWidget(
        new Container(
          child: new Container(
            child: new TestWidget(
              persistentState: 1,
              child: new Container()
            )
48 49
          )
        )
50
      );
51

52
      TestWidgetState state = tester.findStateOfType(TestWidgetState);
53

54 55
      expect(state.persistentState, equals(1));
      expect(state.updates, equals(0));
56

57 58 59 60 61 62 63
      tester.pumpWidget(
        new Container(
          child: new Container(
            child: new TestWidget(
              persistentState: 2,
              child: new Container()
            )
64 65
          )
        )
66
      );
67

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

71 72
      tester.pumpWidget(new Container());
    });
73 74
  });

75
  test('remove one', () {
76 77 78 79 80 81 82 83
    testWidgets((WidgetTester tester) {
      tester.pumpWidget(
        new Container(
          child: new Container(
            child: new TestWidget(
              persistentState: 10,
              child: new Container()
            )
84 85
          )
        )
86
      );
87

88
      TestWidgetState state = tester.findStateOfType(TestWidgetState);
89

90 91
      expect(state.persistentState, equals(10));
      expect(state.updates, equals(0));
92

93 94 95 96 97 98
      tester.pumpWidget(
        new Container(
          child: new TestWidget(
            persistentState: 11,
            child: new Container()
          )
99
        )
100
      );
101

102
      state = tester.findStateOfType(TestWidgetState);
103

104 105
      expect(state.persistentState, equals(11));
      expect(state.updates, equals(0));
106

107 108
      tester.pumpWidget(new Container());
    });
109
  });
110

111
  test('swap instances around', () {
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
    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'));
      tester.pumpWidget(new Column([]));

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

      tester.pumpWidget(
        new Column([
          new Container(
            key: keyA,
            child: a
          ),
          new Container(
            key: keyB,
            child: b
          )
        ])
      );

      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(
        new Column([
          new Container(
            key: keyA,
            child: a
          ),
          new Container(
            key: keyB,
            child: b
          )
        ])
      );

      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(
        new Column([
          new Container(
            key: keyA,
            child: b
          ),
          new Container(
            key: keyB,
            child: a
          )
        ])
      );

      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));
    });
195
  });
196
}