navigator_replacement_test.dart 8.2 KB
Newer Older
Ian Hickson's avatar
Ian Hickson committed
1
// Copyright 2014 The Flutter Authors. All rights reserved.
2 3 4 5 6 7
// 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';

8 9
import 'observer_tester.dart';

10
void main() {
11
  testWidgets('Back during pushReplacement', (WidgetTester tester) async {
12
    await tester.pumpWidget(MaterialApp(
13
      home: const Material(child: Text('home')),
14
      routes: <String, WidgetBuilder>{
15 16
        '/a': (BuildContext context) => const Material(child: Text('a')),
        '/b': (BuildContext context) => const Material(child: Text('b')),
17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
      },
    ));

    final NavigatorState navigator = tester.state(find.byType(Navigator));
    navigator.pushNamed('/a');
    await tester.pumpAndSettle();

    expect(find.text('a'), findsOneWidget);
    expect(find.text('home'), findsNothing);

    navigator.pushReplacementNamed('/b');
    await tester.pump();
    await tester.pump(const Duration(milliseconds: 10));

    expect(find.text('a'), findsOneWidget);
    expect(find.text('b'), findsOneWidget);
    expect(find.text('home'), findsNothing);

    navigator.pop();

    await tester.pumpAndSettle();

    expect(find.text('a'), findsNothing);
    expect(find.text('b'), findsNothing);
    expect(find.text('home'), findsOneWidget);
  });
43

44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60
  group('pushAndRemoveUntil', () {
    testWidgets('notifies appropriately', (WidgetTester tester) async {
      final TestObserver observer = TestObserver();
      final Widget myApp = MaterialApp(
        home: const Material(child: Text('home')),
        routes: <String, WidgetBuilder>{
          '/a': (BuildContext context) => const Material(child: Text('a')),
          '/b': (BuildContext context) => const Material(child: Text('b')),
        },
        navigatorObservers: <NavigatorObserver>[observer],
      );

      await tester.pumpWidget(myApp);

      final NavigatorState navigator = tester.state(find.byType(Navigator));
      final List<String> log = <String>[];
      observer
61 62
        ..onPushed = (Route<dynamic>? route, Route<dynamic>? previousRoute) {
          log.add('${route!.settings.name} pushed, previous route: ${previousRoute!.settings.name}');
63
        }
64 65
        ..onRemoved = (Route<dynamic>? route, Route<dynamic>? previousRoute) {
          log.add('${route!.settings.name} removed, previous route: ${previousRoute?.settings.name}');
66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 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 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189
        };


      navigator.pushNamed('/a');
      await tester.pumpAndSettle();

      expect(find.text('home', skipOffstage: false), findsOneWidget);
      expect(find.text('a', skipOffstage: false), findsOneWidget);
      expect(find.text('b', skipOffstage: false), findsNothing);

      // Remove all routes below
      navigator.pushNamedAndRemoveUntil('/b', (Route<dynamic> route) => false);
      await tester.pumpAndSettle();

      expect(find.text('home', skipOffstage: false), findsNothing);
      expect(find.text('a', skipOffstage: false), findsNothing);
      expect(find.text('b', skipOffstage: false), findsOneWidget);
      expect(log, equals(<String>[
        '/a pushed, previous route: /',
        '/b pushed, previous route: /a',
        '/a removed, previous route: null',
        '/ removed, previous route: null',
      ]));

      log.clear();

      navigator.pushNamed('/');
      await tester.pumpAndSettle();

      expect(find.text('home', skipOffstage: false), findsOneWidget);
      expect(find.text('a', skipOffstage: false), findsNothing);
      expect(find.text('b', skipOffstage: false), findsOneWidget);

      // Remove only some routes below
      navigator.pushNamedAndRemoveUntil('/a', ModalRoute.withName('/b'));
      await tester.pumpAndSettle();

      expect(find.text('home', skipOffstage: false), findsNothing);
      expect(find.text('a', skipOffstage: false), findsOneWidget);
      expect(find.text('b', skipOffstage: false), findsOneWidget);
      expect(log, equals(<String>[
        '/ pushed, previous route: /b',
        '/a pushed, previous route: /',
        '/ removed, previous route: /b',
      ]));
    });

    testWidgets('triggers page transition animation for pushed route', (WidgetTester tester) async {
      final Widget myApp = MaterialApp(
        home: const Material(child: Text('home')),
        routes: <String, WidgetBuilder>{
          '/a': (BuildContext context) => const Material(child: Text('a')),
          '/b': (BuildContext context) => const Material(child: Text('b')),
        },
      );

      await tester.pumpWidget(myApp);
      final NavigatorState navigator = tester.state(find.byType(Navigator));

      navigator.pushNamed('/a');
      await tester.pumpAndSettle();

      navigator.pushNamedAndRemoveUntil('/b', (Route<dynamic> route) => false);
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 100));

      // We are mid-transition, both pages are onstage
      expect(find.text('a'), findsOneWidget);
      expect(find.text('b'), findsOneWidget);

      // Complete transition
      await tester.pumpAndSettle();
      expect(find.text('a'), findsNothing);
      expect(find.text('b'), findsOneWidget);
    });

    testWidgets('Hero transition triggers when preceding route contains hero, and predicate route does not', (WidgetTester tester) async {
      const String kHeroTag = 'hero';
      final Widget myApp = MaterialApp(
        initialRoute: '/',
        routes: <String, WidgetBuilder>{
          '/': (BuildContext context) => const Material(child: Text('home')),
          '/a': (BuildContext context) => const Material(child: Hero(
            tag: kHeroTag,
            child: Text('a'),
          )),
          '/b': (BuildContext context) => const Material(child: Padding(
            padding: EdgeInsets.all(100.0),
            child: Hero(
              tag: kHeroTag,
              child: Text('b'),
            ),
          )),
        },
      );

      await tester.pumpWidget(myApp);
      final NavigatorState navigator = tester.state(find.byType(Navigator));

      navigator.pushNamed('/a');
      await tester.pumpAndSettle();

      navigator.pushNamedAndRemoveUntil('/b', ModalRoute.withName('/'));
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 16));

      expect(find.text('b'), isOnstage);

      // 'b' text is heroing to its new location
      final Offset bOffset = tester.getTopLeft(find.text('b'));
      expect(bOffset.dx, greaterThan(0.0));
      expect(bOffset.dx, lessThan(100.0));
      expect(bOffset.dy, greaterThan(0.0));
      expect(bOffset.dy, lessThan(100.0));

      await tester.pump(const Duration(seconds: 1));

      expect(find.text('a'), findsNothing);
      expect(find.text('b'), isOnstage);
    });

    testWidgets('Hero transition does not trigger when preceding route does not contain hero, but predicate route does', (WidgetTester tester) async {
      const String kHeroTag = 'hero';
      final Widget myApp = MaterialApp(
190 191 192 193 194 195 196
        theme: ThemeData(
          pageTransitionsTheme: const PageTransitionsTheme(
            builders: <TargetPlatform, PageTransitionsBuilder>{
              TargetPlatform.android: FadeUpwardsPageTransitionsBuilder(),
            },
          ),
        ),
197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230
        initialRoute: '/',
        routes: <String, WidgetBuilder>{
          '/': (BuildContext context) => const Material(child: Hero(
            tag:kHeroTag,
            child: Text('home'),
          )),
          '/a': (BuildContext context) => const Material(child: Text('a')),
          '/b': (BuildContext context) => const Material(child: Padding(
            padding: EdgeInsets.all(100.0),
            child: Hero(
              tag: kHeroTag,
              child: Text('b'),
            ),
          )),
        },
      );

      await tester.pumpWidget(myApp);
      final NavigatorState navigator = tester.state(find.byType(Navigator));

      navigator.pushNamed('/a');
      await tester.pumpAndSettle();

      navigator.pushNamedAndRemoveUntil('/b', ModalRoute.withName('/'));
      await tester.pump();
      await tester.pump(const Duration(milliseconds: 16));

      expect(find.text('b'), isOnstage);

      // 'b' text is sliding in from the right, no hero transition
      final Offset bOffset = tester.getTopLeft(find.text('b'));
      expect(bOffset.dx, 100.0);
      expect(bOffset.dy, greaterThan(100.0));
    });
231
  });
232
}