page_forward_transitions_test.dart 8.01 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
Hixie's avatar
Hixie committed
2 3 4
// 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';
Hixie's avatar
Hixie committed
6
import 'package:flutter/material.dart';
7
import 'package:flutter/rendering.dart';
Hixie's avatar
Hixie committed
8

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

  final Widget childFirstHalf;
  final Widget childSecondHalf;

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

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

32
  final Widget child;
33 34

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

  @override
38
  final Color barrierColor;
39

40 41 42
  @override
  String get barrierLabel => null;

43 44 45
  @override
  bool get maintainState => false;

46
  @override
47
  Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
48 49
    return child;
  }
50 51
}

Hixie's avatar
Hixie committed
52
void main() {
53 54
  const Duration kTwoTenthsOfTheTransitionDuration = Duration(milliseconds: 30);
  const Duration kFourTenthsOfTheTransitionDuration = Duration(milliseconds: 60);
Hixie's avatar
Hixie committed
55

56
  testWidgets('Check onstage/offstage handling around transitions', (WidgetTester tester) async {
57

58
    final GlobalKey insideKey = GlobalKey();
59

60
    String state({ bool skipOffstage = true }) {
61
      String result = '';
62
      if (tester.any(find.text('A', skipOffstage: skipOffstage)))
63
        result += 'A';
64
      if (tester.any(find.text('B', skipOffstage: skipOffstage)))
65
        result += 'B';
66
      if (tester.any(find.text('C', skipOffstage: skipOffstage)))
67
        result += 'C';
68
      if (tester.any(find.text('D', skipOffstage: skipOffstage)))
69
        result += 'D';
70
      if (tester.any(find.text('E', skipOffstage: skipOffstage)))
71
        result += 'E';
72
      if (tester.any(find.text('F', skipOffstage: skipOffstage)))
73
        result += 'F';
74
      if (tester.any(find.text('G', skipOffstage: skipOffstage)))
75 76 77 78
        result += 'G';
      return result;
    }

79
    await tester.pumpWidget(
80
      MaterialApp(
81 82 83
        onGenerateRoute: (RouteSettings settings) {
          switch (settings.name) {
            case '/':
84
              return TestRoute<void>(
85
                settings: settings,
86
                child: Builder(
87 88
                  key: insideKey,
                  builder: (BuildContext context) {
89
                    final PageRoute<void> route = ModalRoute.of(context) as PageRoute<void>;
90
                    return Column(
91
                      children: <Widget>[
92
                        TestTransition(
93 94
                          childFirstHalf: const Text('A'),
                          childSecondHalf: const Text('B'),
95
                          animation: route.animation,
96
                        ),
97
                        TestTransition(
98 99
                          childFirstHalf: const Text('C'),
                          childSecondHalf: const Text('D'),
100
                          animation: route.secondaryAnimation,
101
                        ),
102
                      ],
103
                    );
104 105
                  },
                ),
106
              );
107 108 109
            case '/2': return TestRoute<void>(settings: settings, child: const Text('E'));
            case '/3': return TestRoute<void>(settings: settings, child: const Text('F'));
            case '/4': return TestRoute<void>(settings: settings, child: const Text('G'));
Hixie's avatar
Hixie committed
110
          }
111
          return null;
112 113 114
        }
      )
    );
Hixie's avatar
Hixie committed
115

116
    final NavigatorState navigator = insideKey.currentContext.findAncestorStateOfType<NavigatorState>();
Hixie's avatar
Hixie committed
117

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

120
    navigator.pushNamed('/2');
121
    expect(state(), equals('BC')); // transition 1->2 is not yet built
122
    await tester.pump();
123 124
    expect(state(), equals('BC')); // transition 1->2 is at 0.0
    expect(state(skipOffstage: false), equals('BCE')); // E is offstage
Hixie's avatar
Hixie committed
125

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

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

132
    await tester.pump(kFourTenthsOfTheTransitionDuration);
133
    expect(state(), equals('E')); // transition 1->2 is at 1.0
134
    expect(state(skipOffstage: false), equals('E')); // B and C are gone, the route is inactive with maintainState=false
Hixie's avatar
Hixie committed
135

136
    navigator.pop();
137
    expect(state(), equals('E')); // transition 1<-2 is at 1.0, just reversed
138
    await tester.pump();
Hans Muller's avatar
Hans Muller committed
139 140
    await tester.pump();

141
    expect(state(), equals('BDE')); // transition 1<-2 is at 1.0
Hixie's avatar
Hixie committed
142

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

146
    navigator.pushNamed('/3');
147
    expect(state(), equals('BDE')); // transition 1<-2 is at 0.6
148
    await tester.pump();
149 150
    expect(state(), equals('BDE')); // transition 1<-2 is at 0.6, 1->3 is at 0.0
    expect(state(skipOffstage: false), equals('BDEF')); // F is offstage since we're at 0.0
Hixie's avatar
Hixie committed
151

152
    await tester.pump(kFourTenthsOfTheTransitionDuration);
153
    expect(state(), equals('BCEF')); // transition 1<-2 is at 0.2, 1->3 is at 0.4
154
    expect(state(skipOffstage: false), equals('BCEF')); // nothing secret going on here
Hixie's avatar
Hixie committed
155

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

159
    navigator.pop();
160
    expect(state(), equals('BDF')); // transition 1<-3 is at 0.8, just reversed
161
    await tester.pump();
162
    expect(state(), equals('BDF')); // transition 1<-3 is at 0.8
Hixie's avatar
Hixie committed
163

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

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

170
    navigator.pushNamed('/4');
171
    expect(state(), equals('BCF')); // transition 1<-3 is at 0.2, 1->4 is not yet built
172
    await tester.pump();
173 174
    expect(state(), equals('BCF')); // transition 1<-3 is at 0.2, 1->4 is at 0.0
    expect(state(skipOffstage: false), equals('BCFG')); // G is offstage
Hixie's avatar
Hixie committed
175

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

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

182
    await tester.pump(kFourTenthsOfTheTransitionDuration);
183
    expect(state(), equals('G')); // transition 1->4 is done
184
    expect(state(skipOffstage: false), equals('G')); // route 1 is not around any more
Hixie's avatar
Hixie committed
185 186

  });
187 188 189

  testWidgets('Check onstage/offstage handling of barriers around transitions', (WidgetTester tester) async {
    await tester.pumpWidget(
190
      MaterialApp(
191 192
        onGenerateRoute: (RouteSettings settings) {
          switch (settings.name) {
193 194
            case '/': return TestRoute<void>(settings: settings, child: const Text('A'));
            case '/1': return TestRoute<void>(settings: settings, barrierColor: const Color(0xFFFFFF00), child: const Text('B'));
195
          }
196
          return null;
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214
        }
      )
    );
    expect(find.byType(ModalBarrier), findsOneWidget);

    tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/1');
    expect(find.byType(ModalBarrier), findsOneWidget);

    await tester.pump();
    expect(find.byType(ModalBarrier), findsNWidgets(2));
    expect(tester.widget<ModalBarrier>(find.byType(ModalBarrier).first).color, isNull);
    expect(tester.widget<ModalBarrier>(find.byType(ModalBarrier).last).color, isNull);

    await tester.pump(const Duration(seconds: 1));
    expect(find.byType(ModalBarrier), findsOneWidget);
    expect(tester.widget<ModalBarrier>(find.byType(ModalBarrier)).color, const Color(0xFFFFFF00));

  });
Hixie's avatar
Hixie committed
215
}