duplicate_key_test.dart 1.41 KB
Newer Older
1
import 'package:flutter/widgets.dart';
Hixie's avatar
Hixie committed
2
import 'package:test/test.dart';
Adam Barth's avatar
Adam Barth committed
3

Adam Barth's avatar
Adam Barth committed
4
import '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
    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', () {
45 46 47 48 49 50 51 52 53 54
    testWidgets((WidgetTester tester) {
      tester.pumpWidget(builder());
      StatefulLeafState leaf = tester.findStateOfType(StatefulLeafState);
      leaf.test();
      tester.pump();
      Item lastItem = items[1];
      items.remove(lastItem);
      items.insert(0, lastItem);
      tester.pumpWidget(builder()); // this marks the app dirty and rebuilds it
    });
Hixie's avatar
Hixie committed
55 56
  });
}