boost_page_route_test.dart 5.75 KB
Newer Older
yangwu.jia's avatar
yangwu.jia committed
1 2 3
import 'package:flutter_boost/container/boost_page_route.dart';
import 'package:flutter_test/flutter_test.dart';

yangwu.jia's avatar
yangwu.jia committed
4 5 6 7
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

yangwu.jia's avatar
yangwu.jia committed
8
void main() {
yangwu.jia's avatar
yangwu.jia committed
9
  TestWidgetsFlutterBinding.ensureInitialized();
yangwu.jia's avatar
yangwu.jia committed
10

yangwu.jia's avatar
yangwu.jia committed
11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26
  testWidgets('test iOS edge swipe then drop back at starting point works',
      (WidgetTester tester) async {
    await tester.pumpWidget(
      MaterialApp(
        theme: ThemeData(platform: TargetPlatform.iOS),
        onGenerateRoute: (RouteSettings settings) {
          return BoostPageRoute<void>(
            settings: settings,
            builder: (BuildContext context) {
              final String pageNumber = settings.name == '/' ? '1' : '2';
              return Center(child: Text('Page $pageNumber'));
            },
          );
        },
      ),
    );
yangwu.jia's avatar
yangwu.jia committed
27

yangwu.jia's avatar
yangwu.jia committed
28 29 30 31
    tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/next');

    await tester.pump();
    await tester.pump(const Duration(seconds: 1));
yangwu.jia's avatar
yangwu.jia committed
32

yangwu.jia's avatar
yangwu.jia committed
33 34
    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 2'), isOnstage);
yangwu.jia's avatar
yangwu.jia committed
35

yangwu.jia's avatar
yangwu.jia committed
36 37 38 39 40 41 42 43 44 45 46
    final TestGesture gesture = await tester.startGesture(const Offset(5, 200));
    await gesture.moveBy(const Offset(300, 0));
    await tester.pump();
    // Bring it exactly back such that there's nothing to animate when releasing.
    await gesture.moveBy(const Offset(-300, 0));
    await gesture.up();
    await tester.pump();

    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 2'), isOnstage);
  });
Vadaski's avatar
Vadaski committed
47 48 49

  group('Try to get the BoostPageRoute in the ancestor node', () {
    testWidgets(
justin's avatar
justin committed
50
        'obtain BoostPageRoute through the BoostPageRoute.of(context) method',
Vadaski's avatar
Vadaski committed
51
        (WidgetTester tester) async {
justin's avatar
justin committed
52 53
      dynamic boostPageRoute;
      dynamic boostPageRouteFindByOfMethod;
Vadaski's avatar
Vadaski committed
54 55 56 57 58 59 60 61 62 63

      await tester.pumpWidget(
        MaterialApp(
          onGenerateRoute: (RouteSettings settings) {
            boostPageRoute = BoostPageRoute<void>(
              settings: settings,
              builder: (BuildContext context) => Builder(
                builder: (context) {
                  return FloatingActionButton(
                    onPressed: () {
justin's avatar
justin committed
64
                      boostPageRouteFindByOfMethod = BoostPageRoute.of<dynamic>(context);
Vadaski's avatar
Vadaski committed
65 66 67 68 69 70 71 72 73 74 75 76
                    },
                  );
                },
              ),
            );
            return boostPageRoute;
          },
        ),
      );

      await tester.tap(find.byType(FloatingActionButton));

justin's avatar
justin committed
77 78
      await tester.pump(Duration(seconds: 1));

Vadaski's avatar
Vadaski committed
79 80 81 82 83 84
      // The route obtained from the ancestor node through the `of` method should be the same BoostPageRoute
      // as the originally created BoostPageRoute
      expect(boostPageRoute, boostPageRouteFindByOfMethod);
    });

    testWidgets(
justin's avatar
justin committed
85
        'try to find BoostPageRoute through the BoostPageRoute.of(context) method, '
Vadaski's avatar
Vadaski committed
86 87
        'but it doesn\'t exist, the method should throw an Exception',
        (WidgetTester tester) async {
justin's avatar
justin committed
88
      dynamic contextCache;
Vadaski's avatar
Vadaski committed
89 90 91 92

      await tester.pumpWidget(
        MaterialApp(
          onGenerateRoute: (RouteSettings settings) {
justin's avatar
justin committed
93
            return MaterialPageRoute<dynamic>(
Vadaski's avatar
Vadaski committed
94 95 96 97 98 99 100 101 102 103 104 105 106
              settings: settings,
              builder: (context) => Builder(
                builder: (context) => FloatingActionButton(
                  onPressed: () {
                    contextCache = context;
                  },
                ),
              ),
            );
          },
        ),
      );
      await tester.tap(find.byType(FloatingActionButton));
justin's avatar
justin committed
107
      await tester.pump(Duration(seconds: 1));
Vadaski's avatar
Vadaski committed
108

justin's avatar
justin committed
109
      expect(() => BoostPageRoute.of<dynamic>(contextCache), throwsException);
Vadaski's avatar
Vadaski committed
110 111 112
    });

    testWidgets(
justin's avatar
justin committed
113
        'obtain BoostPageRoute through the BoostPageRoute.tryOf(context) method',
Vadaski's avatar
Vadaski committed
114
        (WidgetTester tester) async {
justin's avatar
justin committed
115 116
      dynamic boostPageRoute;
      dynamic boostPageRouteFindByOfMethod;
Vadaski's avatar
Vadaski committed
117 118 119 120 121 122 123 124 125 126 127

      await tester.pumpWidget(
        MaterialApp(
          onGenerateRoute: (RouteSettings settings) {
            boostPageRoute = BoostPageRoute<void>(
              settings: settings,
              builder: (BuildContext context) => Builder(
                builder: (context) {
                  return FloatingActionButton(
                    onPressed: () {
                      boostPageRouteFindByOfMethod =
justin's avatar
justin committed
128
                          BoostPageRoute.tryOf<dynamic>(context);
Vadaski's avatar
Vadaski committed
129 130 131 132 133 134 135 136 137 138 139
                    },
                  );
                },
              ),
            );
            return boostPageRoute;
          },
        ),
      );

      await tester.tap(find.byType(FloatingActionButton));
justin's avatar
justin committed
140
      await tester.pump(Duration(seconds: 1));
Vadaski's avatar
Vadaski committed
141 142 143 144 145 146 147 148

      // The route obtained from the ancestor node through the `tryOf` method should be the same BoostPageRoute
      // as the originally created BoostPageRoute
      expect(boostPageRoute, boostPageRouteFindByOfMethod);
    });
  });

  testWidgets(
justin's avatar
justin committed
149
      'try to find BoostPageRoute through the BoostPageRoute.tryOf(context) method, '
Vadaski's avatar
Vadaski committed
150 151
      'but it doesn\'t exist, the method should return null',
      (WidgetTester tester) async {
justin's avatar
justin committed
152
        dynamic boostPageRouteFindByOfMethod;
Vadaski's avatar
Vadaski committed
153 154 155 156

    await tester.pumpWidget(
      MaterialApp(
        onGenerateRoute: (RouteSettings settings) {
justin's avatar
justin committed
157
          return MaterialPageRoute<dynamic>(
Vadaski's avatar
Vadaski committed
158 159 160 161 162 163
            settings: settings,
            builder: (BuildContext context) => Builder(
              builder: (context) {
                return FloatingActionButton(
                  onPressed: () {
                    boostPageRouteFindByOfMethod =
justin's avatar
justin committed
164
                        BoostPageRoute.tryOf<dynamic>(context);
Vadaski's avatar
Vadaski committed
165 166 167 168 169 170 171 172 173 174
                  },
                );
              },
            ),
          );
        },
      ),
    );

    await tester.tap(find.byType(FloatingActionButton));
justin's avatar
justin committed
175
    await tester.pump(Duration(seconds: 1));
Vadaski's avatar
Vadaski committed
176 177 178

    expect(boostPageRouteFindByOfMethod, null);
  });
yangwu.jia's avatar
yangwu.jia committed
179
}