navigator_replacement_test.dart 2.88 KB
Newer Older
1 2 3 4 5 6 7 8
// Copyright 2016 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/material.dart';

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

    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);
  });
41 42 43

  testWidgets('pushAndRemoveUntil', (WidgetTester tester) async {
    await tester.pumpWidget(new MaterialApp(
Ian Hickson's avatar
Ian Hickson committed
44
      home: const Material(child: const Text('home')),
45
      routes: <String, WidgetBuilder> {
Ian Hickson's avatar
Ian Hickson committed
46 47
        '/a': (BuildContext context) => const Material(child: const Text('a')),
        '/b': (BuildContext context) => const Material(child: const Text('b')),
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79
      },
    ));

    final NavigatorState navigator = tester.state(find.byType(Navigator));
    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);

    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);

    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);

    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);
  });
80
}