duplicate_key_test.dart 1.41 KB
Newer Older
Adam Barth's avatar
Adam Barth committed
1
import 'package:sky/src/fn3.dart';
Hixie's avatar
Hixie committed
2
import 'package:test/test.dart';
Adam Barth's avatar
Adam Barth committed
3 4

import '../fn3/widget_tester.dart';
Hixie's avatar
Hixie committed
5 6 7 8 9 10 11 12 13 14

class Item {
  GlobalKey key1 = new GlobalKey();
  GlobalKey key2 = new GlobalKey();
  String toString() => "Item($key1, $key2)";
}
List<Item> items = [new Item(), new Item()];

class StatefulLeaf extends StatefulComponent {
  StatefulLeaf({ GlobalKey key }) : super(key: key);
Adam Barth's avatar
Adam Barth committed
15 16 17 18
  StatefulLeafState createState() => new StatefulLeafState();
}

class StatefulLeafState extends State<StatefulLeaf> {
Hixie's avatar
Hixie committed
19
  void test() { setState(() { }); }
Adam Barth's avatar
Adam Barth committed
20
  Widget build(BuildContext context) => new Text('leaf');
Hixie's avatar
Hixie committed
21 22
}

Adam Barth's avatar
Adam Barth committed
23
class KeyedWrapper extends StatelessComponent {
Hixie's avatar
Hixie committed
24 25
  KeyedWrapper(this.key1, this.key2);
  Key key1, key2;
Adam Barth's avatar
Adam Barth committed
26
  Widget build(BuildContext context) {
Hixie's avatar
Hixie committed
27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45
    return new Container(
      key: key1,
      child: new StatefulLeaf(
        key: key2
      )
    );
  }
}

Widget builder() {
  return new Column([
    new KeyedWrapper(items[1].key1, items[1].key2),
    new KeyedWrapper(items[0].key1, items[0].key2)
  ]);
}

void main() {
  test('duplicate key smoke test', () {
    WidgetTester tester = new WidgetTester();
Adam Barth's avatar
Adam Barth committed
46 47
    tester.pumpFrame(builder());
    StatefulLeafState leaf = tester.findStateOfType(StatefulLeafState);
48
    leaf.test();
Hixie's avatar
Hixie committed
49 50 51 52
    tester.pumpFrameWithoutChange();
    Item lastItem = items[1];
    items.remove(lastItem);
    items.insert(0, lastItem);
Adam Barth's avatar
Adam Barth committed
53
    tester.pumpFrame(builder()); // this marks the app dirty and rebuilds it
Hixie's avatar
Hixie committed
54 55
  });
}