container_manager_test.dart 8.48 KB
Newer Older
yangwu.jia's avatar
yangwu.jia committed
1 2
import 'package:flutter_boost/container/container_manager.dart';
import 'package:flutter_test/flutter_test.dart';
yangwu.jia's avatar
yangwu.jia committed
3 4 5
import 'package:flutter/material.dart';
import 'package:flutter/widgets.dart';
import 'package:flutter_boost/flutter_boost.dart';
Vadaski's avatar
Vadaski committed
6
import 'package:flutter_boost/container/container_coordinator.dart';
yangwu.jia's avatar
yangwu.jia committed
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

final GlobalKey scaffoldKey = GlobalKey();

class FirstRouteWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('First Route'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              child: Text('First'),
              onPressed: () {
                print("open second page!");
                FlutterBoost.singleton.open("second").then((Map value) {
                  print(
                      "call me when page is finished. did recieve second route result $value");
                });
              },
            ),
          ],
        ),
      ),
    );
  }
}

class SecondRouteWidget extends StatelessWidget {
  @override
  Widget build(BuildContext context) {
    return Scaffold(
      appBar: AppBar(
        title: Text('Second Route'),
      ),
      body: Center(
        child: Column(
          mainAxisAlignment: MainAxisAlignment.center,
          children: <Widget>[
            RaisedButton(
              child: Text('Second'),
              onPressed: () {
                print("open second page!");
                FlutterBoost.singleton.open("second").then((Map value) {
                  print(
                      "call me when page is finished. did recieve second route result $value");
                });
              },
            ),
          ],
        ),
      ),
    );
  }
}

class MyApp extends StatefulWidget {
  @override
  _MyAppState createState() => _MyAppState();
}

class _MyAppState extends State<MyApp> {
  @override
  void initState() {
    super.initState();

    FlutterBoost.singleton.registerPageBuilders({
      'first': (pageName, params, _) => FirstRouteWidget(),
      'second': (pageName, params, _) => SecondRouteWidget(),
    });
  }

  @override
  Widget build(BuildContext context) {
    return MaterialApp(
        title: 'Flutter Boost example',
        key: scaffoldKey,
        builder: (BuildContext context, Widget child) {
          assert(child is Navigator, 'child must be Navigator, what is wrong?');

          final BoostContainerManager manager = BoostContainerManager(
            initNavigator: child,
          );

          return manager;
        },
        home: Container());
  }
}

yangwu.jia's avatar
yangwu.jia committed
100
void main() {
yangwu.jia's avatar
yangwu.jia committed
101
  TestWidgetsFlutterBinding.ensureInitialized();
yangwu.jia's avatar
yangwu.jia committed
102

yangwu.jia's avatar
yangwu.jia committed
103 104 105
  testWidgets('test iOS edge swipe then drop back at starting point works',
      (WidgetTester tester) async {
    await tester.pumpWidget(MyApp());
yangwu.jia's avatar
yangwu.jia committed
106

yangwu.jia's avatar
yangwu.jia committed
107
    expect(find.text('First'), findsNothing);
yangwu.jia's avatar
yangwu.jia committed
108
  });
Vadaski's avatar
Vadaski committed
109 110 111 112 113 114 115

  group(
    'Try to get the ContainerManagerState in the ancestor node',
    () {
      testWidgets(
        'through the `BoostContainerManager.of(context)` method',
        (WidgetTester tester) async {
Vadaski's avatar
Vadaski committed
116 117 118 119 120 121 122 123 124 125 126 127 128
          var builderContext;

          FlutterBoost.singleton.registerPageBuilders({
            'context': (pageName, params, _) => Builder(
                  builder: (context) {
                    return FloatingActionButton(
                      onPressed: () {
                        builderContext = context;
                      },
                    );
                  },
                ),
          });
Vadaski's avatar
Vadaski committed
129 130 131

          await tester.pumpWidget(
            MaterialApp(
Vadaski's avatar
Vadaski committed
132 133
              builder: FlutterBoost.init(),
              home: Container(),
Vadaski's avatar
Vadaski committed
134 135 136
            ),
          );

Vadaski's avatar
Vadaski committed
137 138 139 140 141 142 143 144 145
          //open context page
          ContainerCoordinator.singleton
              .nativeContainerDidShow("context", {}, "1000000");

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

          expect(find.byType(FloatingActionButton), findsOneWidget);

          //get the context of the Builder
Vadaski's avatar
Vadaski committed
146 147
          await tester.tap(find.byType(FloatingActionButton));

Vadaski's avatar
Vadaski committed
148 149 150 151 152
          final isFind = BoostContainerManager.of(builderContext) != null;

          expect(isFind, true,
              reason: '`BoostContainerManager.of` should be able to '
                  'find `ContainerManagerState` in `FlutterBoost.init()` based on the context of the `Builder`');
Vadaski's avatar
Vadaski committed
153 154 155
        },
      );

Vadaski's avatar
Vadaski committed
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
//      testWidgets(
//        'through the `BoostContainerManager.of(context)` method',
//        (WidgetTester tester) async {
//          var builderContext;
//
//          await tester.pumpWidget(
//            MaterialApp(
//              home: Builder(
//                builder: (context) {
//                  return FloatingActionButton(
//                    onPressed: () {
//                      builderContext = context;
//                    },
//                  );
//                },
//              ),
//            ),
//          );
//
//          expect(find.byType(FloatingActionButton), findsOneWidget);
//
//          //get the context of the Builder
//          await tester.tap(find.byType(FloatingActionButton));
//
//          expect(BoostContainerManager.of(builderContext), isAssertionError);
//        },
//      );

Vadaski's avatar
Vadaski committed
184 185 186
      testWidgets(
        'through the `BoostContainerManager.tryOf(context)` method',
        (WidgetTester tester) async {
Vadaski's avatar
Vadaski committed
187 188 189 190 191 192 193 194 195 196 197 198 199
          var builderContext;

          FlutterBoost.singleton.registerPageBuilders({
            'context': (pageName, params, _) => Builder(
                  builder: (context) {
                    return FloatingActionButton(
                      onPressed: () {
                        builderContext = context;
                      },
                    );
                  },
                ),
          });
Vadaski's avatar
Vadaski committed
200 201 202

          await tester.pumpWidget(
            MaterialApp(
Vadaski's avatar
Vadaski committed
203 204
              builder: FlutterBoost.init(),
              home: Container(),
Vadaski's avatar
Vadaski committed
205 206 207
            ),
          );

Vadaski's avatar
Vadaski committed
208 209 210 211 212 213 214 215 216
          //open context page
          ContainerCoordinator.singleton
              .nativeContainerDidShow("context", {}, "1000000");

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

          expect(find.byType(FloatingActionButton), findsOneWidget);

          //get the context of the Builder
Vadaski's avatar
Vadaski committed
217 218
          await tester.tap(find.byType(FloatingActionButton));

Vadaski's avatar
Vadaski committed
219 220 221 222 223
          final isFind = BoostContainerManager.tryOf(builderContext) != null;

          expect(isFind, true,
              reason: '`BoostContainerManager.tryOf` should be able to '
                  'find `ContainerManagerState` in `FlutterBoost.init()` based on the context of the `Builder`');
Vadaski's avatar
Vadaski committed
224 225 226 227
        },
      );
    },
  );
Vadaski's avatar
Vadaski committed
228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294

  group('ContainerManagerState', () {
    testWidgets(
      'containerCounts should change based on the number of pages',
      (WidgetTester tester) async {
        var builderContext;

        FlutterBoost.singleton.registerPageBuilders({
          'context': (pageName, params, _) => Builder(
                builder: (context) {
                  return FloatingActionButton(
                    onPressed: () {
                      builderContext = context;
                    },
                  );
                },
              ),
        });

        await tester.pumpWidget(
          MaterialApp(
            builder: FlutterBoost.init(),
            home: Container(),
          ),
        );

        //open first context page
        ContainerCoordinator.singleton
            .nativeContainerDidShow("context", {}, "1000000");

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

        //get the context of the Builder
        await tester.tap(find.byType(FloatingActionButton));

        final containerManagerState = BoostContainerManager.of(builderContext);

        expect(containerManagerState.containerCounts, 1,
            reason: '1 page shown');

        //open second context page
        ContainerCoordinator.singleton
            .nativeContainerDidShow("context", {}, "2000000");

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

        expect(containerManagerState.containerCounts, 2,
            reason: '2 page shown');

        //pop second context page
        containerManagerState.pop();

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

        expect(containerManagerState.containerCounts, 1,
            reason: 'second context page closed, Only one page left');

        //pop last context page
        containerManagerState.pop();

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

        expect(containerManagerState.containerCounts, 0,
            reason: 'last context page closed, no page left');
      },
    );
  });
yangwu.jia's avatar
yangwu.jia committed
295
}