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';
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 12 13
class Item {
  GlobalKey key1 = new GlobalKey();
  GlobalKey key2 = new GlobalKey();
  String toString() => "Item($key1, $key2)";
}
14
List<Item> items = <Item>[new Item(), new Item()];
Hixie's avatar
Hixie committed
15 16 17

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

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

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

Widget builder() {
40 41 42 43 44 45
  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
46 47 48 49
}

void main() {
  test('duplicate key smoke test', () {
50 51 52 53 54 55 56 57 58 59
    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
60 61
  });
}