From 0568d8a3ac89353b89ead6fdcd066c523bfc8bb0 Mon Sep 17 00:00:00 2001 From: Bogdan Userman Kikacheishvili Date: Thu, 9 Feb 2023 19:51:02 +0200 Subject: [PATCH] Unit testing implemented. Native page rout platform detection moved to defaultTargetPlatform. --- lib/native_page_route.dart | 5 +- test/native_page_route_test.dart | 177 ++++++++++++++++++++++++++++++- 2 files changed, 178 insertions(+), 4 deletions(-) diff --git a/lib/native_page_route.dart b/lib/native_page_route.dart index 0bb1655..fdcfd93 100644 --- a/lib/native_page_route.dart +++ b/lib/native_page_route.dart @@ -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. @@ -23,7 +22,7 @@ PageRoute nativePageRoute({ bool? fullscreenDialog, String? iosTitle, }) { - if (Platform.isIOS) { + if (defaultTargetPlatform == TargetPlatform.iOS) { return CupertinoPageRoute( builder: builder, settings: settings, diff --git a/test/native_page_route_test.dart b/test/native_page_route_test.dart index 3c85b0e..cfe067d 100644 --- a/test/native_page_route_test.dart +++ b/test/native_page_route_test.dart @@ -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? 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 navigationKey() { + return GlobalKey(); +} + +class MyApp extends StatelessWidget { + final GlobalKey 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 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, 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, 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, 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, true); + debugDefaultTargetPlatformOverride = null; + }, + ); }