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

8 9
Key firstKey = new Key('first');
Key secondKey = new Key('second');
10 11
Key thirdKey = new Key('third');

12 13
final Map<String, WidgetBuilder> routes = <String, WidgetBuilder>{
  '/': (BuildContext context) => new Material(
14
    child: new Block(children: <Widget>[
15 16 17
      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),
18
      new FlatButton(child: new Text('two'), onPressed: () => Navigator.pushNamed(context, '/two')),
19 20
    ])
  ),
21
  '/two': (BuildContext context) => new Material(
22
    child: new Block(children: <Widget>[
23 24 25
      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),
26
      new FlatButton(child: new Text('three'), onPressed: () => Navigator.push(context, new ThreeRoute())),
27 28
    ])
  ),
29
};
30

31
class ThreeRoute extends MaterialPageRoute<Null> {
32 33
  ThreeRoute() : super(builder: (BuildContext context) {
    return new Material(
34
      child: new Block(children: <Widget>[
35 36 37 38 39 40 41 42
        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),
      ])
    );
  });
}

43
void main() {
44
  testWidgets('Heroes animate', (WidgetTester tester) async {
45

46
    await tester.pumpWidget(new MaterialApp(routes: routes));
47

48
    // the initial setup.
49

50 51 52
    expect(find.byKey(firstKey), isOnStage);
    expect(find.byKey(firstKey), isInCard);
    expect(find.byKey(secondKey), findsNothing);
53

54 55
    await tester.tap(find.text('two'));
    await tester.pump(); // begin navigation
56

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

60 61 62 63
    expect(find.byKey(firstKey), isOnStage);
    expect(find.byKey(firstKey), isInCard);
    expect(find.byKey(secondKey), isOffStage);
    expect(find.byKey(secondKey), isInCard);
64

65
    await tester.pump();
66

67 68
    // 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.
69

70 71 72
    expect(find.byKey(firstKey), findsNothing);
    expect(find.byKey(secondKey), isOnStage);
    expect(find.byKey(secondKey), isNotInCard);
73

74
    await tester.pump();
75

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

78 79 80
    expect(find.byKey(firstKey), findsNothing);
    expect(find.byKey(secondKey), isOnStage);
    expect(find.byKey(secondKey), isNotInCard);
81

82
    await tester.pump(new Duration(seconds: 1));
83

84 85
    // 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.
86

87 88 89
    expect(find.byKey(firstKey), findsNothing);
    expect(find.byKey(secondKey), isOnStage);
    expect(find.byKey(secondKey), isInCard);
90

91
    await tester.pump();
92

93
    // Should not change anything.
94

95 96 97
    expect(find.byKey(firstKey), findsNothing);
    expect(find.byKey(secondKey), isOnStage);
    expect(find.byKey(secondKey), isInCard);
98

99
    // Now move on to view 3
100

101 102
    await tester.tap(find.text('three'));
    await tester.pump(); // begin navigation
103

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

107 108 109 110
    expect(find.byKey(secondKey), isOnStage);
    expect(find.byKey(secondKey), isInCard);
    expect(find.byKey(thirdKey), isOffStage);
    expect(find.byKey(thirdKey), isInCard);
111

112
    await tester.pump();
113

114 115
    // 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.
116

117 118 119
    expect(find.byKey(secondKey), findsNothing);
    expect(find.byKey(thirdKey), isOnStage);
    expect(find.byKey(thirdKey), isNotInCard);
120

121
    await tester.pump();
122

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

125 126 127
    expect(find.byKey(secondKey), findsNothing);
    expect(find.byKey(thirdKey), isOnStage);
    expect(find.byKey(thirdKey), isNotInCard);
128

129
    await tester.pump(new Duration(seconds: 1));
130

131 132
    // 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.
133

134 135 136
    expect(find.byKey(secondKey), findsNothing);
    expect(find.byKey(thirdKey), isOnStage);
    expect(find.byKey(thirdKey), isInCard);
137

138
    await tester.pump();
139

140
    // Should not change anything.
141

142 143 144
    expect(find.byKey(secondKey), findsNothing);
    expect(find.byKey(thirdKey), isOnStage);
    expect(find.byKey(thirdKey), isInCard);
145 146
  });
}