|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +import 'package:flutter/cupertino.dart'; |
| 6 | +import 'package:flutter/material.dart'; |
| 7 | +import 'package:flutter_test/flutter_test.dart'; |
| 8 | +import 'package:go_router/src/go_router_cupertino.dart'; |
| 9 | + |
| 10 | +import 'error_screen_helpers.dart'; |
| 11 | + |
| 12 | +void main() { |
| 13 | + group('isCupertinoApp', () { |
| 14 | + testWidgets('returns [true] when CupertinoApp is present', |
| 15 | + (WidgetTester tester) async { |
| 16 | + final GlobalKey<_DummyStatefulWidgetState> key = |
| 17 | + GlobalKey<_DummyStatefulWidgetState>(); |
| 18 | + await tester.pumpWidget( |
| 19 | + CupertinoApp( |
| 20 | + home: DummyStatefulWidget(key: key), |
| 21 | + ), |
| 22 | + ); |
| 23 | + final bool isCupertino = isCupertinoApp(key.currentContext! as Element); |
| 24 | + expect(isCupertino, true); |
| 25 | + }); |
| 26 | + |
| 27 | + testWidgets('returns [false] when MaterialApp is present', |
| 28 | + (WidgetTester tester) async { |
| 29 | + final GlobalKey<_DummyStatefulWidgetState> key = |
| 30 | + GlobalKey<_DummyStatefulWidgetState>(); |
| 31 | + await tester.pumpWidget( |
| 32 | + MaterialApp( |
| 33 | + home: DummyStatefulWidget(key: key), |
| 34 | + ), |
| 35 | + ); |
| 36 | + final bool isCupertino = isCupertinoApp(key.currentContext! as Element); |
| 37 | + expect(isCupertino, false); |
| 38 | + }); |
| 39 | + }); |
| 40 | + |
| 41 | + test('pageBuilderForCupertinoApp creates a [CupertinoPage] accordingly', () { |
| 42 | + final UniqueKey key = UniqueKey(); |
| 43 | + const String name = 'name'; |
| 44 | + const String arguments = 'arguments'; |
| 45 | + const String restorationId = 'restorationId'; |
| 46 | + const DummyStatefulWidget child = DummyStatefulWidget(); |
| 47 | + final CupertinoPage<void> page = pageBuilderForCupertinoApp( |
| 48 | + key: key, |
| 49 | + name: name, |
| 50 | + arguments: arguments, |
| 51 | + restorationId: restorationId, |
| 52 | + child: child, |
| 53 | + ); |
| 54 | + expect(page.key, key); |
| 55 | + expect(page.name, name); |
| 56 | + expect(page.arguments, arguments); |
| 57 | + expect(page.restorationId, restorationId); |
| 58 | + expect(page.child, child); |
| 59 | + }); |
| 60 | + |
| 61 | + group('GoRouterCupertinoErrorScreen', () { |
| 62 | + testWidgets( |
| 63 | + 'shows "page not found" by default', |
| 64 | + testPageNotFound( |
| 65 | + widget: const CupertinoApp( |
| 66 | + home: GoRouterCupertinoErrorScreen(null), |
| 67 | + ), |
| 68 | + ), |
| 69 | + ); |
| 70 | + |
| 71 | + final Exception exception = Exception('Something went wrong!'); |
| 72 | + testWidgets( |
| 73 | + 'shows the exception message when provided', |
| 74 | + testPageShowsExceptionMessage( |
| 75 | + exception: exception, |
| 76 | + widget: CupertinoApp( |
| 77 | + home: GoRouterCupertinoErrorScreen(exception), |
| 78 | + ), |
| 79 | + ), |
| 80 | + ); |
| 81 | + |
| 82 | + testWidgets( |
| 83 | + 'clicking the CupertinoButton should redirect to /', |
| 84 | + testClickingTheButtonRedirectsToRoot( |
| 85 | + buttonFinder: find.byType(CupertinoButton), |
| 86 | + appRouterBuilder: cupertinoAppRouterBuilder, |
| 87 | + widget: const CupertinoApp( |
| 88 | + home: GoRouterCupertinoErrorScreen(null), |
| 89 | + ), |
| 90 | + ), |
| 91 | + ); |
| 92 | + }); |
| 93 | +} |
| 94 | + |
| 95 | +class DummyStatefulWidget extends StatefulWidget { |
| 96 | + const DummyStatefulWidget({Key? key}) : super(key: key); |
| 97 | + |
| 98 | + @override |
| 99 | + State<DummyStatefulWidget> createState() => _DummyStatefulWidgetState(); |
| 100 | +} |
| 101 | + |
| 102 | +class _DummyStatefulWidgetState extends State<DummyStatefulWidget> { |
| 103 | + @override |
| 104 | + Widget build(BuildContext context) => Container(); |
| 105 | +} |
0 commit comments