heroes_test.dart 5.34 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 7 8
import 'package:flutter/material.dart';
import 'package:test/test.dart';

9
import 'test_matchers.dart';
10

11 12
Key firstKey = new Key('first');
Key secondKey = new Key('second');
13 14
Key thirdKey = new Key('third');

15
final Map<String, RouteBuilder> routes = <String, RouteBuilder>{
16
  '/': (RouteArguments args) => new Material(
17
    child: new Block(children: <Widget>[
18 19 20
      new Container(height: 100.0, width: 100.0),
      new Card(child: new Hero(tag: 'a', child: new Container(height: 100.0, width: 100.0, key: firstKey))),
      new Container(height: 100.0, width: 100.0),
21
      new FlatButton(child: new Text('two'), onPressed: () => Navigator.pushNamed(args.context, '/two')),
22 23 24
    ])
  ),
  '/two': (RouteArguments args) => new Material(
25
    child: new Block(children: <Widget>[
26 27 28
      new Container(height: 150.0, width: 150.0),
      new Card(child: new Hero(tag: 'a', child: new Container(height: 150.0, width: 150.0, key: secondKey))),
      new Container(height: 150.0, width: 150.0),
29
      new FlatButton(child: new Text('three'), onPressed: () => Navigator.push(args.context, new ThreeRoute())),
30 31
    ])
  ),
32
};
33

34 35 36
class ThreeRoute extends MaterialPageRoute {
  ThreeRoute() : super(builder: (BuildContext context) {
    return new Material(
37
      child: new Block(children: <Widget>[
38 39 40 41 42 43 44 45
        new Container(height: 200.0, width: 200.0),
        new Card(child: new Hero(tag: 'a', child: new Container(height: 200.0, width: 200.0, key: thirdKey))),
        new Container(height: 200.0, width: 200.0),
      ])
    );
  });
}

46
void main() {
47
  test('Heroes animate', () {
48 49 50 51
    testWidgets((WidgetTester tester) {

      tester.pumpWidget(new MaterialApp(routes: routes));

52
      // the initial setup.
53

54 55 56
      expect(tester.findElementByKey(firstKey), isOnStage);
      expect(tester.findElementByKey(firstKey), isInCard);
      expect(tester.findElementByKey(secondKey), isNull);
57

58
      tester.tap(tester.findText('two'));
59 60 61 62 63 64 65 66 67
      tester.pump(); // begin navigation

      // at this stage, the second route is off-stage, so that we can form the
      // hero party.

      expect(tester.findElementByKey(firstKey), isOnStage);
      expect(tester.findElementByKey(firstKey), isInCard);
      expect(tester.findElementByKey(secondKey), isOffStage);
      expect(tester.findElementByKey(secondKey), isInCard);
68 69 70

      tester.pump();

71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
      // at this stage, the heroes have just gone on their journey, we are
      // seeing them at t=16ms. The original page no longer contains the hero.

      expect(tester.findElementByKey(firstKey), isNull);
      expect(tester.findElementByKey(secondKey), isOnStage);
      expect(tester.findElementByKey(secondKey), isNotInCard);

      tester.pump();

      // t=32ms for the journey. Surely they are still at it.

      expect(tester.findElementByKey(firstKey), isNull);
      expect(tester.findElementByKey(secondKey), isOnStage);
      expect(tester.findElementByKey(secondKey), isNotInCard);

      tester.pump(new Duration(seconds: 1));

      // t=1.032s for the journey. The journey has ended (it ends this frame, in
      // fact). The hero should now be in the new page, on-stage.

      expect(tester.findElementByKey(firstKey), isNull);
      expect(tester.findElementByKey(secondKey), isOnStage);
      expect(tester.findElementByKey(secondKey), isInCard);

      tester.pump();
96

97
      // Should not change anything.
98

99 100 101
      expect(tester.findElementByKey(firstKey), isNull);
      expect(tester.findElementByKey(secondKey), isOnStage);
      expect(tester.findElementByKey(secondKey), isInCard);
102

103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
      // Now move on to view 3

      tester.tap(tester.findText('three'));
      tester.pump(); // begin navigation

      // at this stage, the second route is off-stage, so that we can form the
      // hero party.

      expect(tester.findElementByKey(secondKey), isOnStage);
      expect(tester.findElementByKey(secondKey), isInCard);
      expect(tester.findElementByKey(thirdKey), isOffStage);
      expect(tester.findElementByKey(thirdKey), isInCard);

      tester.pump();

      // at this stage, the heroes have just gone on their journey, we are
      // seeing them at t=16ms. The original page no longer contains the hero.

      expect(tester.findElementByKey(secondKey), isNull);
      expect(tester.findElementByKey(thirdKey), isOnStage);
      expect(tester.findElementByKey(thirdKey), isNotInCard);

      tester.pump();

      // t=32ms for the journey. Surely they are still at it.

      expect(tester.findElementByKey(secondKey), isNull);
      expect(tester.findElementByKey(thirdKey), isOnStage);
      expect(tester.findElementByKey(thirdKey), isNotInCard);

      tester.pump(new Duration(seconds: 1));

      // t=1.032s for the journey. The journey has ended (it ends this frame, in
      // fact). The hero should now be in the new page, on-stage.

      expect(tester.findElementByKey(secondKey), isNull);
      expect(tester.findElementByKey(thirdKey), isOnStage);
      expect(tester.findElementByKey(thirdKey), isInCard);

      tester.pump();

      // Should not change anything.

      expect(tester.findElementByKey(secondKey), isNull);
      expect(tester.findElementByKey(thirdKey), isOnStage);
      expect(tester.findElementByKey(thirdKey), isInCard);
149 150 151
    });
  });
}