duplicate_key_test.dart 1.67 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';
Hixie's avatar
Hixie committed
7
import 'package:test/test.dart';
Adam Barth's avatar
Adam Barth committed
8

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

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

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

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

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

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

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

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

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

Widget builder() {
49 50 51 52 53 54
  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
55 56 57 58
}

void main() {
  test('duplicate key smoke test', () {
59 60 61 62 63 64 65 66 67 68
    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
69 70
  });
}