page_forward_transitions_test.dart 8.07 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({
11 12 13 14
    Key? key,
    required this.childFirstHalf,
    required this.childSecondHalf,
    required 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 31 32 33 34
  TestRoute({
    required this.child,
    required RouteSettings settings,
    this.barrierColor,
  }) : super(settings: settings);
35

36
  final Widget child;
37 38

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

  @override
42
  final Color? barrierColor;
43

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

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

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

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

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

62
    final GlobalKey insideKey = GlobalKey();
63

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

  });
191 192 193

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