page_forward_transitions_test.dart 6.47 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4 5 6
// 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.

import 'package:flutter_test/flutter_test.dart';
import 'package:flutter/material.dart';
Ian Hickson's avatar
Ian Hickson committed
7
import 'package:test/test.dart' hide TypeMatcher;
Hixie's avatar
Hixie committed
8

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

  final Widget childFirstHalf;
  final Widget childSecondHalf;

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

31
class TestRoute<T> extends PageRoute<T> {
32
  TestRoute({ this.child, RouteSettings settings}) : super(settings: settings);
33

34
  final Widget child;
35 36

  @override
37
  Duration get transitionDuration => kMaterialPageRouteTransitionDuration;
38 39

  @override
40
  Color get barrierColor => null;
41 42

  @override
43
  Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> forwardAnimation) {
44 45
    return child;
  }
46 47
}

Hixie's avatar
Hixie committed
48 49 50 51 52 53 54 55 56 57 58
void main() {
  final Duration kTwoTenthsOfTheTransitionDuration = kMaterialPageRouteTransitionDuration * 0.2;
  final Duration kFourTenthsOfTheTransitionDuration = kMaterialPageRouteTransitionDuration * 0.4;

  test('Check onstage/offstage handling around transitions', () {
    testWidgets((WidgetTester tester) {

      GlobalKey insideKey = new GlobalKey();

      String state() {
        String result = '';
59
        if (tester.exists(find.text('A')))
Hixie's avatar
Hixie committed
60
          result += 'A';
61
        if (tester.exists(find.text('B')))
Hixie's avatar
Hixie committed
62
          result += 'B';
63
        if (tester.exists(find.text('C')))
Hixie's avatar
Hixie committed
64
          result += 'C';
65
        if (tester.exists(find.text('D')))
Hixie's avatar
Hixie committed
66
          result += 'D';
67
        if (tester.exists(find.text('E')))
Hixie's avatar
Hixie committed
68
          result += 'E';
69
        if (tester.exists(find.text('F')))
Hixie's avatar
Hixie committed
70
          result += 'F';
71
        if (tester.exists(find.text('G')))
Hixie's avatar
Hixie committed
72 73 74 75 76 77
          result += 'G';
        return result;
      }

      tester.pumpWidget(
        new MaterialApp(
78
          onGenerateRoute: (RouteSettings settings) {
79 80
            switch (settings.name) {
              case '/':
81
                return new TestRoute<Null>(
82 83
                  settings: settings,
                  child: new Builder(
84 85
                    key: insideKey,
                    builder: (BuildContext context) {
86
                      PageRoute<Null> route = ModalRoute.of(context);
87 88 89 90 91
                      return new Column(
                        children: <Widget>[
                          new TestTransition(
                            childFirstHalf: new Text('A'),
                            childSecondHalf: new Text('B'),
92
                            animation: route.animation
93 94 95 96
                          ),
                          new TestTransition(
                            childFirstHalf: new Text('C'),
                            childSecondHalf: new Text('D'),
97
                            animation: route.forwardAnimation
98 99 100
                          ),
                        ]
                      );
101 102 103
                    }
                  )
                );
104 105 106
              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'));
107
            }
Hixie's avatar
Hixie committed
108 109 110 111
          }
        )
      );

Ian Hickson's avatar
Ian Hickson committed
112
      NavigatorState navigator = insideKey.currentContext.ancestorStateOfType(const TypeMatcher<NavigatorState>());
Hixie's avatar
Hixie committed
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 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177

      expect(state(), equals('BC')); // transition ->1 is at 1.0

      navigator.openTransaction((NavigatorTransaction transaction) => transaction.pushNamed('/2'));
      expect(state(), equals('BC')); // transition 1->2 is not yet built
      tester.pump();
      expect(state(), equals('BCE')); // transition 1->2 is at 0.0

      tester.pump(kFourTenthsOfTheTransitionDuration);
      expect(state(), equals('BCE')); // transition 1->2 is at 0.4

      tester.pump(kFourTenthsOfTheTransitionDuration);
      expect(state(), equals('BDE')); // transition 1->2 is at 0.8

      tester.pump(kFourTenthsOfTheTransitionDuration);
      expect(state(), equals('E')); // transition 1->2 is at 1.0


      navigator.openTransaction((NavigatorTransaction transaction) => transaction.pop());
      expect(state(), equals('E')); // transition 1<-2 is at 1.0, just reversed
      tester.pump();
      expect(state(), equals('BDE')); // transition 1<-2 is at 1.0

      tester.pump(kFourTenthsOfTheTransitionDuration);
      expect(state(), equals('BDE')); // transition 1<-2 is at 0.6

      navigator.openTransaction((NavigatorTransaction transaction) => transaction.pushNamed('/3'));
      expect(state(), equals('BDE')); // transition 1<-2 is at 0.6
      tester.pump();
      expect(state(), equals('BDEF')); // transition 1<-2 is at 0.6, 1->3 is at 0.0

      tester.pump(kFourTenthsOfTheTransitionDuration);
      expect(state(), equals('BCEF')); // transition 1<-2 is at 0.2, 1->3 is at 0.4

      tester.pump(kFourTenthsOfTheTransitionDuration);
      expect(state(), equals('BDF')); // transition 1<-2 is done, 1->3 is at 0.8

      navigator.openTransaction((NavigatorTransaction transaction) => transaction.pop());
      expect(state(), equals('BDF')); // transition 1<-3 is at 0.8, just reversed
      tester.pump();
      expect(state(), equals('BDF')); // transition 1<-3 is at 0.8

      tester.pump(kTwoTenthsOfTheTransitionDuration); // notice that dT=0.2 here, not 0.4
      expect(state(), equals('BDF')); // transition 1<-3 is at 0.6

      tester.pump(kFourTenthsOfTheTransitionDuration);
      expect(state(), equals('BCF')); // transition 1<-3 is at 0.2

      navigator.openTransaction((NavigatorTransaction transaction) => transaction.pushNamed('/4'));
      expect(state(), equals('BCF')); // transition 1<-3 is at 0.2, 1->4 is not yet built
      tester.pump();
      expect(state(), equals('BCFG')); // transition 1<-3 is at 0.2, 1->4 is at 0.0

      tester.pump(kFourTenthsOfTheTransitionDuration);
      expect(state(), equals('BCG')); // transition 1<-3 is done, 1->4 is at 0.4

      tester.pump(kFourTenthsOfTheTransitionDuration);
      expect(state(), equals('BDG')); // transition 1->4 is at 0.8

      tester.pump(kFourTenthsOfTheTransitionDuration);
      expect(state(), equals('G')); // transition 1->4 is done

    });
  });
}