duplicate_key_test.dart 1.63 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';
Adam Barth's avatar
Adam Barth committed
7

Hixie's avatar
Hixie committed
8 9 10
class Item {
  GlobalKey key1 = new GlobalKey();
  GlobalKey key2 = new GlobalKey();
11 12

  @override
Hixie's avatar
Hixie committed
13 14
  String toString() => "Item($key1, $key2)";
}
15
List<Item> items = <Item>[new Item(), new Item()];
Hixie's avatar
Hixie committed
16

17
class StatefulLeaf extends StatefulWidget {
Hixie's avatar
Hixie committed
18
  StatefulLeaf({ GlobalKey key }) : super(key: key);
19 20

  @override
Adam Barth's avatar
Adam Barth committed
21 22 23 24
  StatefulLeafState createState() => new StatefulLeafState();
}

class StatefulLeafState extends State<StatefulLeaf> {
Hixie's avatar
Hixie committed
25
  void test() { setState(() { }); }
26 27

  @override
Adam Barth's avatar
Adam Barth committed
28
  Widget build(BuildContext context) => new Text('leaf');
Hixie's avatar
Hixie committed
29 30
}

31
class KeyedWrapper extends StatelessWidget {
Hixie's avatar
Hixie committed
32
  KeyedWrapper(this.key1, this.key2);
33

Hixie's avatar
Hixie committed
34
  Key key1, key2;
35 36

  @override
Adam Barth's avatar
Adam Barth committed
37
  Widget build(BuildContext context) {
Hixie's avatar
Hixie committed
38 39 40 41 42 43 44 45 46 47
    return new Container(
      key: key1,
      child: new StatefulLeaf(
        key: key2
      )
    );
  }
}

Widget builder() {
48 49 50 51 52 53
  return new Column(
    children: <Widget>[
      new KeyedWrapper(items[1].key1, items[1].key2),
      new KeyedWrapper(items[0].key1, items[0].key2)
    ]
  );
Hixie's avatar
Hixie committed
54 55 56
}

void main() {
57 58
  testWidgets('duplicate key smoke test', (WidgetTester tester) async {
    await tester.pumpWidget(builder());
59 60
    StatefulLeafState leaf = tester.firstState(find.byType(StatefulLeaf));
    leaf.test();
61
    await tester.pump();
62 63 64
    Item lastItem = items[1];
    items.remove(lastItem);
    items.insert(0, lastItem);
65
    await tester.pumpWidget(builder()); // this marks the app dirty and rebuilds it
Hixie's avatar
Hixie committed
66 67
  });
}