global_keys_moving_test.dart 1.75 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
Ian Hickson's avatar
Ian Hickson committed
13
  String toString() => 'Item($key1, $key2)';
Hixie's avatar
Hixie committed
14
}
15
List<Item> items = <Item>[new Item(), new Item()];
Hixie's avatar
Hixie committed
16

17
class StatefulLeaf extends StatefulWidget {
18
  const 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> {
25
  void markNeedsBuild() { setState(() { }); }
26 27

  @override
Ian Hickson's avatar
Ian Hickson committed
28
  Widget build(BuildContext context) => const Text('leaf', textDirection: TextDirection.ltr);
Hixie's avatar
Hixie committed
29 30
}

31
class KeyedWrapper extends StatelessWidget {
32
  const KeyedWrapper(this.key1, this.key2);
33

34 35
  final Key key1;
  final Key key2;
36 37

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

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

void main() {
58
  testWidgets('moving subtrees with global keys - smoketest', (WidgetTester tester) async {
59
    await tester.pumpWidget(builder());
60
    final StatefulLeafState leaf = tester.firstState(find.byType(StatefulLeaf));
61
    leaf.markNeedsBuild();
62
    await tester.pump();
63
    final Item lastItem = items[1];
64 65
    items.remove(lastItem);
    items.insert(0, lastItem);
66
    await tester.pumpWidget(builder()); // this marks the app dirty and rebuilds it
Hixie's avatar
Hixie committed
67 68
  });
}