page_forward_transitions_test.dart 8.04 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 5
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

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

8
class TestTransition extends AnimatedWidget {
9
  const TestTransition({
10 11 12 13
    Key? key,
    required this.childFirstHalf,
    required this.childSecondHalf,
    required Animation<double> animation,
14
  }) : super(key: key, listenable: 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 = listenable as Animation<double>;
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 30 31 32 33
  TestRoute({
    required this.child,
    required RouteSettings settings,
    this.barrierColor,
  }) : super(settings: settings);
34

35
  final Widget child;
36 37

  @override
38
  Duration get transitionDuration => const Duration(milliseconds: 150);
39 40

  @override
41
  final Color? barrierColor;
42

43
  @override
44
  String? get barrierLabel => null;
45

46 47 48
  @override
  bool get maintainState => false;

49
  @override
50
  Widget buildPage(BuildContext context, Animation<double> animation, Animation<double> secondaryAnimation) {
51 52
    return child;
  }
53 54
}

Hixie's avatar
Hixie committed
55
void main() {
56 57
  const Duration kTwoTenthsOfTheTransitionDuration = Duration(milliseconds: 30);
  const Duration kFourTenthsOfTheTransitionDuration = Duration(milliseconds: 60);
Hixie's avatar
Hixie committed
58

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

61
    final GlobalKey insideKey = GlobalKey();
62

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

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

119
    final NavigatorState navigator = insideKey.currentContext!.findAncestorStateOfType<NavigatorState>()!;
Hixie's avatar
Hixie committed
120

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

123
    navigator.pushNamed('/2');
124
    expect(state(), equals('BC')); // transition 1->2 is not yet built
125
    await tester.pump();
126 127
    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
128

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

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

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

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

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

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

149
    navigator.pushNamed('/3');
150
    expect(state(), equals('BDE')); // transition 1<-2 is at 0.6
151
    await tester.pump();
152 153
    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
154

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

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

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

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

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

173
    navigator.pushNamed('/4');
174
    expect(state(), equals('BCF')); // transition 1<-3 is at 0.2, 1->4 is not yet built
175
    await tester.pump();
176 177
    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
178

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

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

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

  });
190 191 192

  testWidgets('Check onstage/offstage handling of barriers around transitions', (WidgetTester tester) async {
    await tester.pumpWidget(
193
      MaterialApp(
194 195
        onGenerateRoute: (RouteSettings settings) {
          switch (settings.name) {
196 197
            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'));
198
          }
199
          return null;
200 201
        },
      ),
202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217
    );
    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
218
}