Skip to content

Commit

Permalink
Unit testing implemented. Native page rout platform detection moved t…
Browse files Browse the repository at this point in the history
…o defaultTargetPlatform.
  • Loading branch information
Bogdan Userman Kikacheishvili committed Feb 9, 2023
1 parent 5f601b4 commit 0568d8a
Show file tree
Hide file tree
Showing 2 changed files with 178 additions and 4 deletions.
5 changes: 2 additions & 3 deletions lib/native_page_route.dart
Original file line number Diff line number Diff line change
@@ -1,8 +1,7 @@
library native_page_route;

import 'dart:io';

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

/// Creates a page route for use in an native style.
Expand All @@ -23,7 +22,7 @@ PageRoute<T> nativePageRoute<T>({
bool? fullscreenDialog,
String? iosTitle,
}) {
if (Platform.isIOS) {
if (defaultTargetPlatform == TargetPlatform.iOS) {
return CupertinoPageRoute<T>(
builder: builder,
settings: settings,
Expand Down
177 changes: 176 additions & 1 deletion test/native_page_route_test.dart
Original file line number Diff line number Diff line change
@@ -1,3 +1,178 @@
import 'dart:io';
import 'package:flutter/cupertino.dart';
import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:native_page_route/native_page_route.dart';

abstract class RouteKeys {
static const String repeatable = 'repeatable';
static const String nonRepeatable = 'nonRepeatable';
}

// ignore: non_constant_identifier_names, body_might_complete_normally_nullable
Route<dynamic>? AppRouter(RouteSettings settings, BuildContext? context) {
switch (settings.name) {
case RouteKeys.repeatable:
return nativePageRoute(
builder: (_) => Container(),
settings: settings,
);
case RouteKeys.nonRepeatable:
return notRepeatableNativePageRoute(
builder: (_) => Container(),
context: context!,
settings: settings,
);
}
}

GlobalKey<NavigatorState> navigationKey() {
return GlobalKey<NavigatorState>();
}

class MyApp extends StatelessWidget {
final GlobalKey<NavigatorState> navKey;
final String initial;

const MyApp({super.key, required this.navKey, required this.initial});

// This widget is the root of your application.
@override
Widget build(BuildContext context) {
return MaterialApp(
title: 'Native route example',
theme: ThemeData(
primarySwatch: Colors.blue,
),
navigatorKey: navKey,
initialRoute: initial,
onGenerateRoute: (settings) => AppRouter(settings, navKey.currentContext),
);
}
}

class PreparedApp {
Widget app;
GlobalKey<NavigatorState> key;

PreparedApp(this.app, this.key);
}

PreparedApp preparedApp(String initial) {
final key = navigationKey();
final app = MyApp(navKey: key, initial: initial);

runApp(app);

return PreparedApp(app, key);
}

void main() {
//TODO: Write tests
testWidgets('Context exists', (tester) async {
final prepared = preparedApp(RouteKeys.repeatable);
await tester.pumpWidget(prepared.app);
final contextExists = prepared.key.currentContext != null;

expect(contextExists, true);
});

testWidgets(
'If you try create notRepeatableNativePageRoute twice in row should return null',
(tester) async {
final prepared = preparedApp(RouteKeys.nonRepeatable);
await tester.pumpWidget(prepared.app);
final route = notRepeatableNativePageRoute(
builder: (_) => Container(),
settings: RouteSettings(name: RouteKeys.nonRepeatable),
context: prepared.key.currentContext!,
);

expect(route, null);
},
);

testWidgets(
'If you try create nativePageRoute twice in a row it should return route',
(tester) async {
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
final prepared = preparedApp(RouteKeys.repeatable);
await tester.pumpWidget(prepared.app);

final route = nativePageRoute(
builder: (_) => Container(),
settings: RouteSettings(name: RouteKeys.repeatable),
);

expect(route is CupertinoPageRoute, true);
debugDefaultTargetPlatformOverride = null;
},
);

testWidgets(
'If you try create nativePageRoute on iOS platform it should return CupertinoRoute',
(tester) async {
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;

final route = nativePageRoute(
builder: (_) => Container(),
settings: RouteSettings(name: RouteKeys.nonRepeatable),
);

expect(route is CupertinoPageRoute<dynamic>, true);
debugDefaultTargetPlatformOverride = null;
},
);

testWidgets(
'If you try create nativePageRoute on Android platform it should return MaterialRoute',
(tester) async {
debugDefaultTargetPlatformOverride = TargetPlatform.android;

final route = nativePageRoute(
builder: (_) => Container(),
settings: RouteSettings(name: RouteKeys.nonRepeatable),
);

expect(route is MaterialPageRoute<dynamic>, true);
debugDefaultTargetPlatformOverride = null;
},
);

testWidgets(
'If you try create notRepeatableNativePageRoute on iOS platform it should return CupertinoRoute',
(tester) async {
debugDefaultTargetPlatformOverride = TargetPlatform.iOS;
final prepared = preparedApp(RouteKeys.nonRepeatable);
await tester.pumpWidget(prepared.app);

final route = notRepeatableNativePageRoute(
builder: (_) => Container(),
settings: RouteSettings(name: RouteKeys.repeatable),
context: prepared.key.currentContext!,
);

expect(route is CupertinoPageRoute<dynamic>, true);
debugDefaultTargetPlatformOverride = null;
},
);

testWidgets(
'If you try create notRepeatableNativePageRoute on Android platform it should return MaterialRoute',
(tester) async {
debugDefaultTargetPlatformOverride = TargetPlatform.android;

final prepared = preparedApp(RouteKeys.nonRepeatable);
await tester.pumpWidget(prepared.app);

final route = notRepeatableNativePageRoute(
builder: (_) => Container(),
settings: RouteSettings(name: RouteKeys.repeatable),
context: prepared.key.currentContext!,
);

expect(route is MaterialPageRoute<dynamic>, true);
debugDefaultTargetPlatformOverride = null;
},
);
}

0 comments on commit 0568d8a

Please sign in to comment.