page_forward_transitions_test.dart 6.42 KB
Newer Older
Hixie's avatar
Hixie committed
1 2 3 4 5 6 7
// 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/animation.dart';
import 'package:flutter/material.dart';
Ian Hickson's avatar
Ian Hickson committed
8
import 'package:test/test.dart' hide TypeMatcher;
Hixie's avatar
Hixie committed
9

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

  final Widget childFirstHalf;
  final Widget childSecondHalf;

  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 35
  final Widget child;
  Duration get transitionDuration => kMaterialPageRouteTransitionDuration;
  Color get barrierColor => null;
36
  Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> forwardAnimation) {
37 38
    return child;
  }
39 40
}

Hixie's avatar
Hixie committed
41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70
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 = '';
        if (tester.findText('A') != null)
          result += 'A';
        if (tester.findText('B') != null)
          result += 'B';
        if (tester.findText('C') != null)
          result += 'C';
        if (tester.findText('D') != null)
          result += 'D';
        if (tester.findText('E') != null)
          result += 'E';
        if (tester.findText('F') != null)
          result += 'F';
        if (tester.findText('G') != null)
          result += 'G';
        return result;
      }

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

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

      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

    });
  });
}