-
Notifications
You must be signed in to change notification settings - Fork 3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
13 changed files
with
1,024 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
114 changes: 114 additions & 0 deletions
114
packages/go_router/test/custom_transition_page_test.dart
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,114 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
|
||
void main() { | ||
testWidgets('CustomTransitionPage builds its child using transitionsBuilder', | ||
(WidgetTester tester) async { | ||
const HomeScreen child = HomeScreen(); | ||
final CustomTransitionPage<void> transition = CustomTransitionPage<void>( | ||
transitionsBuilder: expectAsync4((_, __, ___, Widget child) => child), | ||
child: child, | ||
); | ||
final GoRouter router = GoRouter( | ||
routes: <GoRoute>[ | ||
GoRoute( | ||
path: '/', | ||
pageBuilder: (_, __) => transition, | ||
), | ||
], | ||
); | ||
await tester.pumpWidget( | ||
MaterialApp.router( | ||
routeInformationParser: router.routeInformationParser, | ||
routerDelegate: router.routerDelegate, | ||
title: 'GoRouter Example', | ||
), | ||
); | ||
expect(find.byWidget(child), findsOneWidget); | ||
}); | ||
|
||
testWidgets('NoTransitionPage does not apply any transition', | ||
(WidgetTester tester) async { | ||
final ValueNotifier<bool> showHomeValueNotifier = | ||
ValueNotifier<bool>(false); | ||
await tester.pumpWidget( | ||
MaterialApp( | ||
home: ValueListenableBuilder<bool>( | ||
valueListenable: showHomeValueNotifier, | ||
builder: (_, bool showHome, __) { | ||
return Navigator( | ||
pages: <Page<void>>[ | ||
const NoTransitionPage<void>( | ||
child: LoginScreen(), | ||
), | ||
if (showHome) | ||
const NoTransitionPage<void>( | ||
child: HomeScreen(), | ||
), | ||
], | ||
onPopPage: (Route<dynamic> route, dynamic result) { | ||
return route.didPop(result); | ||
}, | ||
); | ||
}, | ||
), | ||
), | ||
); | ||
|
||
final Finder homeScreenFinder = find.byType(HomeScreen); | ||
|
||
showHomeValueNotifier.value = true; | ||
await tester.pump(); | ||
final Offset homeScreenPositionInTheMiddleOfAddition = | ||
tester.getTopLeft(homeScreenFinder); | ||
await tester.pumpAndSettle(); | ||
final Offset homeScreenPositionAfterAddition = | ||
tester.getTopLeft(homeScreenFinder); | ||
|
||
showHomeValueNotifier.value = false; | ||
await tester.pump(); | ||
final Offset homeScreenPositionInTheMiddleOfRemoval = | ||
tester.getTopLeft(homeScreenFinder); | ||
await tester.pumpAndSettle(); | ||
|
||
expect( | ||
homeScreenPositionInTheMiddleOfAddition, | ||
homeScreenPositionAfterAddition, | ||
); | ||
expect( | ||
homeScreenPositionAfterAddition, | ||
homeScreenPositionInTheMiddleOfRemoval, | ||
); | ||
}); | ||
} | ||
|
||
class HomeScreen extends StatelessWidget { | ||
const HomeScreen({Key? key}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return const Scaffold( | ||
body: Center( | ||
child: Text('HomeScreen'), | ||
), | ||
); | ||
} | ||
} | ||
|
||
class LoginScreen extends StatelessWidget { | ||
const LoginScreen({Key? key}) : super(key: key); | ||
|
||
@override | ||
Widget build(BuildContext context) { | ||
return const Scaffold( | ||
body: Center( | ||
child: Text('LoginScreen'), | ||
), | ||
); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,66 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter/cupertino.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
|
||
import 'go_router_test.dart'; | ||
|
||
WidgetTesterCallback testPageNotFound({required Widget widget}) { | ||
return (WidgetTester tester) async { | ||
await tester.pumpWidget(widget); | ||
expect(find.text('page not found'), findsOneWidget); | ||
}; | ||
} | ||
|
||
WidgetTesterCallback testPageShowsExceptionMessage({ | ||
required Exception exception, | ||
required Widget widget, | ||
}) { | ||
return (WidgetTester tester) async { | ||
await tester.pumpWidget(widget); | ||
expect(find.text('$exception'), findsOneWidget); | ||
}; | ||
} | ||
|
||
WidgetTesterCallback testClickingTheButtonRedirectsToRoot({ | ||
required Finder buttonFinder, | ||
required Widget widget, | ||
Widget Function(GoRouter router) appRouterBuilder = materialAppRouterBuilder, | ||
}) { | ||
return (WidgetTester tester) async { | ||
final GoRouter router = GoRouter( | ||
initialLocation: '/error', | ||
routes: <GoRoute>[ | ||
GoRoute(path: '/', builder: (_, __) => const DummyStatefulWidget()), | ||
GoRoute( | ||
path: '/error', | ||
builder: (_, __) => widget, | ||
), | ||
], | ||
); | ||
await tester.pumpWidget(appRouterBuilder(router)); | ||
await tester.tap(buttonFinder); | ||
await tester.pumpAndSettle(); | ||
expect(find.byType(DummyStatefulWidget), findsOneWidget); | ||
}; | ||
} | ||
|
||
Widget materialAppRouterBuilder(GoRouter router) { | ||
return MaterialApp.router( | ||
routeInformationParser: router.routeInformationParser, | ||
routerDelegate: router.routerDelegate, | ||
title: 'GoRouter Example', | ||
); | ||
} | ||
|
||
Widget cupertinoAppRouterBuilder(GoRouter router) { | ||
return CupertinoApp.router( | ||
routeInformationParser: router.routeInformationParser, | ||
routerDelegate: router.routerDelegate, | ||
title: 'GoRouter Example', | ||
); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,12 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:go_router/go_router.dart'; | ||
|
||
void main() { | ||
test('throws when a builder is not set', () { | ||
expect(() => GoRoute(path: '/'), throwsException); | ||
}); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,105 @@ | ||
// Copyright 2013 The Flutter Authors. All rights reserved. | ||
// Use of this source code is governed by a BSD-style license that can be | ||
// found in the LICENSE file. | ||
|
||
import 'package:flutter/cupertino.dart'; | ||
import 'package:flutter/material.dart'; | ||
import 'package:flutter_test/flutter_test.dart'; | ||
import 'package:go_router/src/go_router_cupertino.dart'; | ||
|
||
import 'error_screen_helpers.dart'; | ||
|
||
void main() { | ||
group('isCupertinoApp', () { | ||
testWidgets('returns [true] when CupertinoApp is present', | ||
(WidgetTester tester) async { | ||
final GlobalKey<_DummyStatefulWidgetState> key = | ||
GlobalKey<_DummyStatefulWidgetState>(); | ||
await tester.pumpWidget( | ||
CupertinoApp( | ||
home: DummyStatefulWidget(key: key), | ||
), | ||
); | ||
final bool isCupertino = isCupertinoApp(key.currentContext! as Element); | ||
expect(isCupertino, true); | ||
}); | ||
|
||
testWidgets('returns [false] when MaterialApp is present', | ||
(WidgetTester tester) async { | ||
final GlobalKey<_DummyStatefulWidgetState> key = | ||
GlobalKey<_DummyStatefulWidgetState>(); | ||
await tester.pumpWidget( | ||
MaterialApp( | ||
home: DummyStatefulWidget(key: key), | ||
), | ||
); | ||
final bool isCupertino = isCupertinoApp(key.currentContext! as Element); | ||
expect(isCupertino, false); | ||
}); | ||
}); | ||
|
||
test('pageBuilderForCupertinoApp creates a [CupertinoPage] accordingly', () { | ||
final UniqueKey key = UniqueKey(); | ||
const String name = 'name'; | ||
const String arguments = 'arguments'; | ||
const String restorationId = 'restorationId'; | ||
const DummyStatefulWidget child = DummyStatefulWidget(); | ||
final CupertinoPage<void> page = pageBuilderForCupertinoApp( | ||
key: key, | ||
name: name, | ||
arguments: arguments, | ||
restorationId: restorationId, | ||
child: child, | ||
); | ||
expect(page.key, key); | ||
expect(page.name, name); | ||
expect(page.arguments, arguments); | ||
expect(page.restorationId, restorationId); | ||
expect(page.child, child); | ||
}); | ||
|
||
group('GoRouterCupertinoErrorScreen', () { | ||
testWidgets( | ||
'shows "page not found" by default', | ||
testPageNotFound( | ||
widget: const CupertinoApp( | ||
home: GoRouterCupertinoErrorScreen(null), | ||
), | ||
), | ||
); | ||
|
||
final Exception exception = Exception('Something went wrong!'); | ||
testWidgets( | ||
'shows the exception message when provided', | ||
testPageShowsExceptionMessage( | ||
exception: exception, | ||
widget: CupertinoApp( | ||
home: GoRouterCupertinoErrorScreen(exception), | ||
), | ||
), | ||
); | ||
|
||
testWidgets( | ||
'clicking the CupertinoButton should redirect to /', | ||
testClickingTheButtonRedirectsToRoot( | ||
buttonFinder: find.byType(CupertinoButton), | ||
appRouterBuilder: cupertinoAppRouterBuilder, | ||
widget: const CupertinoApp( | ||
home: GoRouterCupertinoErrorScreen(null), | ||
), | ||
), | ||
); | ||
}); | ||
} | ||
|
||
class DummyStatefulWidget extends StatefulWidget { | ||
const DummyStatefulWidget({Key? key}) : super(key: key); | ||
|
||
@override | ||
State<DummyStatefulWidget> createState() => _DummyStatefulWidgetState(); | ||
} | ||
|
||
class _DummyStatefulWidgetState extends State<DummyStatefulWidget> { | ||
@override | ||
Widget build(BuildContext context) => Container(); | ||
} |
Oops, something went wrong.