blob: 5d296ad8cc87cdae5f950e4d115f3570d51b9a70 [file] [log] [blame]
// 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.
// ignore_for_file: cascade_invocations, diagnostic_describe_all_properties
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:go_router/go_router.dart';
Future<GoRouter> createGoRouter(WidgetTester tester) async {
final GoRouter goRouter = GoRouter(
initialLocation: '/',
routes: <GoRoute>[
GoRoute(path: '/', builder: (_, __) => const DummyStatefulWidget()),
GoRoute(
path: '/error',
builder: (_, __) => TestErrorScreen(TestFailure('Exception')),
),
],
);
await tester.pumpWidget(MaterialApp.router(
routerConfig: goRouter,
));
return goRouter;
}
Widget fakeNavigationBuilder(
BuildContext context,
GoRouterState state,
Widget child,
) =>
child;
class GoRouterNamedLocationSpy extends GoRouter {
GoRouterNamedLocationSpy({required super.routes});
String? name;
Map<String, String>? params;
Map<String, dynamic>? queryParams;
@override
String namedLocation(
String name, {
Map<String, String> params = const <String, String>{},
Map<String, dynamic> queryParams = const <String, dynamic>{},
}) {
this.name = name;
this.params = params;
this.queryParams = queryParams;
return '';
}
}
class GoRouterGoSpy extends GoRouter {
GoRouterGoSpy({required super.routes});
String? myLocation;
Object? extra;
@override
void go(String location, {Object? extra}) {
myLocation = location;
this.extra = extra;
}
}
class GoRouterGoNamedSpy extends GoRouter {
GoRouterGoNamedSpy({required super.routes});
String? name;
Map<String, String>? params;
Map<String, dynamic>? queryParams;
Object? extra;
@override
void goNamed(
String name, {
Map<String, String> params = const <String, String>{},
Map<String, dynamic> queryParams = const <String, dynamic>{},
Object? extra,
}) {
this.name = name;
this.params = params;
this.queryParams = queryParams;
this.extra = extra;
}
}
class GoRouterPushSpy extends GoRouter {
GoRouterPushSpy({required super.routes});
String? myLocation;
Object? extra;
@override
void push(String location, {Object? extra}) {
myLocation = location;
this.extra = extra;
}
}
class GoRouterPushNamedSpy extends GoRouter {
GoRouterPushNamedSpy({required super.routes});
String? name;
Map<String, String>? params;
Map<String, dynamic>? queryParams;
Object? extra;
@override
void pushNamed(
String name, {
Map<String, String> params = const <String, String>{},
Map<String, dynamic> queryParams = const <String, dynamic>{},
Object? extra,
}) {
this.name = name;
this.params = params;
this.queryParams = queryParams;
this.extra = extra;
}
}
class GoRouterPopSpy extends GoRouter {
GoRouterPopSpy({required super.routes});
bool popped = false;
@override
void pop<T extends Object?>([T? result]) {
popped = true;
}
}
Future<GoRouter> createRouter(
List<RouteBase> routes,
WidgetTester tester, {
GoRouterRedirect? redirect,
String initialLocation = '/',
int redirectLimit = 5,
GlobalKey<NavigatorState>? navigatorKey,
GoRouterWidgetBuilder? errorBuilder,
}) async {
final GoRouter goRouter = GoRouter(
routes: routes,
redirect: redirect,
initialLocation: initialLocation,
redirectLimit: redirectLimit,
errorBuilder: errorBuilder ??
(BuildContext context, GoRouterState state) =>
TestErrorScreen(state.error!),
navigatorKey: navigatorKey,
);
await tester.pumpWidget(
MaterialApp.router(
routerConfig: goRouter,
),
);
return goRouter;
}
class TestErrorScreen extends DummyScreen {
const TestErrorScreen(this.ex, {super.key});
final Exception ex;
}
class HomeScreen extends DummyScreen {
const HomeScreen({super.key});
}
class Page1Screen extends DummyScreen {
const Page1Screen({super.key});
}
class Page2Screen extends DummyScreen {
const Page2Screen({super.key});
}
class LoginScreen extends DummyScreen {
const LoginScreen({super.key});
}
class FamilyScreen extends DummyScreen {
const FamilyScreen(this.fid, {super.key});
final String fid;
}
class FamiliesScreen extends DummyScreen {
const FamiliesScreen({required this.selectedFid, super.key});
final String selectedFid;
}
class PersonScreen extends DummyScreen {
const PersonScreen(this.fid, this.pid, {super.key});
final String fid;
final String pid;
}
class DummyScreen extends StatelessWidget {
const DummyScreen({
this.queryParametersAll = const <String, dynamic>{},
super.key,
});
final Map<String, dynamic> queryParametersAll;
@override
Widget build(BuildContext context) => const Placeholder();
}
Widget dummy(BuildContext context, GoRouterState state) => const DummyScreen();
final GlobalKey<NavigatorState> navigatorKey = GlobalKey<NavigatorState>();
class DummyStatefulWidget extends StatefulWidget {
const DummyStatefulWidget({super.key});
@override
State<DummyStatefulWidget> createState() => DummyStatefulWidgetState();
}
class DummyStatefulWidgetState extends State<DummyStatefulWidget> {
@override
Widget build(BuildContext context) => Container();
}
Future<void> simulateAndroidBackButton(WidgetTester tester) async {
final ByteData message =
const JSONMethodCodec().encodeMethodCall(const MethodCall('popRoute'));
await tester.binding.defaultBinaryMessenger
.handlePlatformMessage('flutter/navigation', message, (_) {});
}