page_forward_transitions_test.dart 6.25 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.

5
import 'package:flutter_test/flutter_test.dart' hide TypeMatcher;
Hixie's avatar
Hixie committed
6 7
import 'package:flutter/material.dart';

8
class TestTransition extends AnimatedWidget {
Hixie's avatar
Hixie committed
9 10 11 12
  TestTransition({
    Key key,
    this.childFirstHalf,
    this.childSecondHalf,
13
    Animation<double> animation
14
  }) : super(key: key, animation: animation);
Hixie's avatar
Hixie committed
15 16 17 18

  final Widget childFirstHalf;
  final Widget childSecondHalf;

19
  @override
Hixie's avatar
Hixie committed
20
  Widget build(BuildContext context) {
21
    final Animation<double> animation = this.animation;
22
    if (animation.value >= 0.5)
Hixie's avatar
Hixie committed
23 24 25 26 27
      return childSecondHalf;
    return childFirstHalf;
  }
}

28
class TestRoute<T> extends PageRoute<T> {
29
  TestRoute({ this.child, RouteSettings settings}) : super(settings: settings);
30

31
  final Widget child;
32 33

  @override
34
  Duration get transitionDuration => const Duration(milliseconds: 150);
35 36

  @override
37
  Color get barrierColor => null;
38 39

  @override
40
  Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> forwardAnimation) {
41 42
    return child;
  }
43 44
}

Hixie's avatar
Hixie committed
45
void main() {
46 47
  final Duration kTwoTenthsOfTheTransitionDuration = const Duration(milliseconds: 30);
  final Duration kFourTenthsOfTheTransitionDuration = const Duration(milliseconds: 60);
Hixie's avatar
Hixie committed
48

49
  testWidgets('Check onstage/offstage handling around transitions', (WidgetTester tester) async {
50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71

    GlobalKey insideKey = new GlobalKey();

    String state() {
      String result = '';
      if (tester.any(find.text('A')))
        result += 'A';
      if (tester.any(find.text('B')))
        result += 'B';
      if (tester.any(find.text('C')))
        result += 'C';
      if (tester.any(find.text('D')))
        result += 'D';
      if (tester.any(find.text('E')))
        result += 'E';
      if (tester.any(find.text('F')))
        result += 'F';
      if (tester.any(find.text('G')))
        result += 'G';
      return result;
    }

72
    await tester.pumpWidget(
73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
      new MaterialApp(
        onGenerateRoute: (RouteSettings settings) {
          switch (settings.name) {
            case '/':
              return new TestRoute<Null>(
                settings: settings,
                child: new Builder(
                  key: insideKey,
                  builder: (BuildContext context) {
                    PageRoute<Null> route = ModalRoute.of(context);
                    return new Column(
                      children: <Widget>[
                        new TestTransition(
                          childFirstHalf: new Text('A'),
                          childSecondHalf: new Text('B'),
                          animation: route.animation
                        ),
                        new TestTransition(
                          childFirstHalf: new Text('C'),
                          childSecondHalf: new Text('D'),
                          animation: route.forwardAnimation
                        ),
                      ]
                    );
                  }
                )
              );
            case '/2': return new TestRoute<Null>(settings: settings, child: new Text('E'));
            case '/3': return new TestRoute<Null> (settings: settings, child: new Text('F'));
            case '/4': return new TestRoute<Null> (settings: settings, child: new Text('G'));
Hixie's avatar
Hixie committed
103
          }
104 105 106
        }
      )
    );
Hixie's avatar
Hixie committed
107

108
    NavigatorState navigator = insideKey.currentContext.ancestorStateOfType(const TypeMatcher<NavigatorState>());
Hixie's avatar
Hixie committed
109

110
    expect(state(), equals('BC')); // transition ->1 is at 1.0
Hixie's avatar
Hixie committed
111

112 113
    navigator.openTransaction((NavigatorTransaction transaction) => transaction.pushNamed('/2'));
    expect(state(), equals('BC')); // transition 1->2 is not yet built
114
    await tester.pump();
115
    expect(state(), equals('BCE')); // transition 1->2 is at 0.0
Hixie's avatar
Hixie committed
116

117
    await tester.pump(kFourTenthsOfTheTransitionDuration);
118
    expect(state(), equals('BCE')); // transition 1->2 is at 0.4
Hixie's avatar
Hixie committed
119

120
    await tester.pump(kFourTenthsOfTheTransitionDuration);
121
    expect(state(), equals('BDE')); // transition 1->2 is at 0.8
Hixie's avatar
Hixie committed
122

123
    await tester.pump(kFourTenthsOfTheTransitionDuration);
124
    expect(state(), equals('E')); // transition 1->2 is at 1.0
Hixie's avatar
Hixie committed
125 126


127 128
    navigator.openTransaction((NavigatorTransaction transaction) => transaction.pop());
    expect(state(), equals('E')); // transition 1<-2 is at 1.0, just reversed
129
    await tester.pump();
130
    expect(state(), equals('BDE')); // transition 1<-2 is at 1.0
Hixie's avatar
Hixie committed
131

132
    await tester.pump(kFourTenthsOfTheTransitionDuration);
133
    expect(state(), equals('BDE')); // transition 1<-2 is at 0.6
Hixie's avatar
Hixie committed
134

135 136
    navigator.openTransaction((NavigatorTransaction transaction) => transaction.pushNamed('/3'));
    expect(state(), equals('BDE')); // transition 1<-2 is at 0.6
137
    await tester.pump();
138
    expect(state(), equals('BDEF')); // transition 1<-2 is at 0.6, 1->3 is at 0.0
Hixie's avatar
Hixie committed
139

140
    await tester.pump(kFourTenthsOfTheTransitionDuration);
141
    expect(state(), equals('BCEF')); // transition 1<-2 is at 0.2, 1->3 is at 0.4
Hixie's avatar
Hixie committed
142

143
    await tester.pump(kFourTenthsOfTheTransitionDuration);
144
    expect(state(), equals('BDF')); // transition 1<-2 is done, 1->3 is at 0.8
Hixie's avatar
Hixie committed
145

146 147
    navigator.openTransaction((NavigatorTransaction transaction) => transaction.pop());
    expect(state(), equals('BDF')); // transition 1<-3 is at 0.8, just reversed
148
    await tester.pump();
149
    expect(state(), equals('BDF')); // transition 1<-3 is at 0.8
Hixie's avatar
Hixie committed
150

151
    await tester.pump(kTwoTenthsOfTheTransitionDuration); // notice that dT=0.2 here, not 0.4
152
    expect(state(), equals('BDF')); // transition 1<-3 is at 0.6
Hixie's avatar
Hixie committed
153

154
    await tester.pump(kFourTenthsOfTheTransitionDuration);
155
    expect(state(), equals('BCF')); // transition 1<-3 is at 0.2
Hixie's avatar
Hixie committed
156

157 158
    navigator.openTransaction((NavigatorTransaction transaction) => transaction.pushNamed('/4'));
    expect(state(), equals('BCF')); // transition 1<-3 is at 0.2, 1->4 is not yet built
159
    await tester.pump();
160
    expect(state(), equals('BCFG')); // transition 1<-3 is at 0.2, 1->4 is at 0.0
Hixie's avatar
Hixie committed
161

162
    await tester.pump(kFourTenthsOfTheTransitionDuration);
163
    expect(state(), equals('BCG')); // transition 1<-3 is done, 1->4 is at 0.4
Hixie's avatar
Hixie committed
164

165
    await tester.pump(kFourTenthsOfTheTransitionDuration);
166
    expect(state(), equals('BDG')); // transition 1->4 is at 0.8
Hixie's avatar
Hixie committed
167

168
    await tester.pump(kFourTenthsOfTheTransitionDuration);
169
    expect(state(), equals('G')); // transition 1->4 is done
Hixie's avatar
Hixie committed
170 171 172

  });
}