boost_page_route_test.dart 5.58 KB
Newer Older
justin's avatar
justin committed
1 2 3 4 5 6 7 8 9 10 11 12 13 14 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 41 42 43 44 45 46 47 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 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
import 'package:flutter_boost/container/boost_page_route.dart';
import 'package:flutter_test/flutter_test.dart';

import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';

void main() {
  TestWidgetsFlutterBinding.ensureInitialized();

  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'));
            },
          );
        },
      ),
    );

    tester.state<NavigatorState>(find.byType(Navigator)).pushNamed('/next');

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

    expect(find.text('Page 1'), findsNothing);
    expect(find.text('Page 2'), isOnstage);

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

  group('Try to get the BoostPageRoute in the ancestor node', () {
    testWidgets(
        'obtain BoostPageRoute through the `BoostPageRoute.of(context)` method',
        (WidgetTester tester) async {
      dynamic boostPageRoute;
      dynamic boostPageRouteFindByOfMethod;

      await tester.pumpWidget(
        MaterialApp(
          onGenerateRoute: (RouteSettings settings) {
            boostPageRoute = BoostPageRoute<void>(
              settings: settings,
              builder: (BuildContext context) => Builder(
                builder: (context) {
                  return FloatingActionButton(
                    onPressed: () {
                      boostPageRouteFindByOfMethod = BoostPageRoute.of<dynamic>(context);
                    },
                  );
                },
              ),
            );
            return boostPageRoute;
          },
        ),
      );

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

      // 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(
        'try to find BoostPageRoute through the `BoostPageRoute.of(context)` method, '
        'but it doesn\'t exist, the method should throw an Exception',
        (WidgetTester tester) async {
      dynamic contextCache;

      await tester.pumpWidget(
        MaterialApp(
          onGenerateRoute: (RouteSettings settings) {
            return MaterialPageRoute<dynamic>(
              settings: settings,
              builder: (context) => Builder(
                builder: (context) => FloatingActionButton(
                  onPressed: () {
                    contextCache = context;
                  },
                ),
              ),
            );
          },
        ),
      );
      await tester.tap(find.byType(FloatingActionButton));

      expect(() => BoostPageRoute.of<dynamic>(contextCache), throwsException);
    });

    testWidgets(
        'obtain BoostPageRoute through the `BoostPageRoute.tryOf(context)` method',
        (WidgetTester tester) async {
      dynamic boostPageRoute;
      dynamic boostPageRouteFindByOfMethod;

      await tester.pumpWidget(
        MaterialApp(
          onGenerateRoute: (RouteSettings settings) {
            boostPageRoute = BoostPageRoute<void>(
              settings: settings,
              builder: (BuildContext context) => Builder(
                builder: (context) {
                  return FloatingActionButton(
                    onPressed: () {
                      boostPageRouteFindByOfMethod =
                          BoostPageRoute.tryOf<dynamic>(context);
                    },
                  );
                },
              ),
            );
            return boostPageRoute;
          },
        ),
      );

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

      // 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(
      'try to find BoostPageRoute through the `BoostPageRoute.tryOf(context)` method, '
      'but it doesn\'t exist, the method should return null',
      (WidgetTester tester) async {
        dynamic boostPageRouteFindByOfMethod;

    await tester.pumpWidget(
      MaterialApp(
        onGenerateRoute: (RouteSettings settings) {
          return MaterialPageRoute<dynamic>(
            settings: settings,
            builder: (BuildContext context) => Builder(
              builder: (context) {
                return FloatingActionButton(
                  onPressed: () {
                    boostPageRouteFindByOfMethod =
                        BoostPageRoute.tryOf<dynamic>(context);
                  },
                );
              },
            ),
          );
        },
      ),
    );

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

    expect(boostPageRouteFindByOfMethod, null);
  });
}