Files
ttstd_family_care/test/app/app_dialogs_test.dart
T

151 lines
4.7 KiB
Dart

import 'package:cupertino_ui/cupertino_ui.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:ttstd_family_care/core/widgets/app_dialogs.dart';
void main() {
Future<BuildContext> mount(WidgetTester tester) async {
late BuildContext context;
await tester.pumpWidget(
CupertinoApp(
home: Builder(
builder: (value) {
context = value;
return const CupertinoPageScaffold(child: Text('Home'));
},
),
),
);
return context;
}
testWidgets(
'default blur retains the framework barrier and confirmation result',
(tester) async {
final context = await mount(tester);
final result = AppDialogs.confirm(
context,
message: 'Confirm?',
confirmText: 'Yes',
cancelText: 'No',
);
await tester.pumpAndSettle();
final barrier = find.byType(ModalBarrier).last;
expect(tester.widget<ModalBarrier>(barrier).dismissible, isFalse);
expect(
find.ancestor(of: barrier, matching: find.byType(BackdropFilter)),
findsOneWidget,
);
await tester.tapAt(const Offset(5, 5));
await tester.pumpAndSettle();
expect(find.text('Confirm?'), findsOneWidget);
await tester.tap(find.text('Yes'));
await tester.pumpAndSettle();
expect(await result, isTrue);
},
);
testWidgets(
'custom barrier preserves tap dismissal, route settings and result',
(tester) async {
final context = await mount(tester);
RouteBarrierDetails? details;
late BuildContext dialogContext;
final result = AppDialogs.show<int>(
context: context,
barrierDismissible: true,
barrierColor: const Color(0x44000000),
routeSettings: const RouteSettings(name: 'custom'),
barrierBuilder: (context, value, barrier) {
details = value;
return KeyedSubtree(key: const Key('custom-barrier'), child: barrier);
},
builder: (context) {
dialogContext = context;
return const Center(child: Text('Dialog'));
},
);
await tester.pumpAndSettle();
expect(find.byKey(const Key('custom-barrier')), findsOneWidget);
expect(details!.barrierDismissible, isTrue);
expect(details!.barrierColor, const Color(0x44000000));
expect(ModalRoute.of(dialogContext)!.settings.name, 'custom');
await tester.tapAt(const Offset(5, 5));
await tester.pumpAndSettle();
expect(await result, isNull);
expect(find.text('Dialog'), findsNothing);
},
);
testWidgets(
'PopScope still blocks system back and explicit close returns typed data',
(tester) async {
final context = await mount(tester);
final result = AppDialogs.show<int>(
context: context,
blurSigma: 0,
builder: (context) => PopScope(
canPop: false,
child: CupertinoAlertDialog(
title: const Text('Working'),
actions: [
CupertinoDialogAction(
onPressed: () => Navigator.pop(context, 7),
child: const Text('Done'),
),
],
),
),
);
await tester.pumpAndSettle();
final barrier = find.byType(ModalBarrier).last;
expect(
find.ancestor(of: barrier, matching: find.byType(BackdropFilter)),
findsNothing,
);
await tester.binding.handlePopRoute();
await tester.pumpAndSettle();
expect(find.text('Working'), findsOneWidget);
await tester.tap(find.text('Done'));
await tester.pumpAndSettle();
expect(await result, 7);
},
);
testWidgets('local navigator selection remains local', (tester) async {
final nested = GlobalKey<NavigatorState>();
late BuildContext context;
await tester.pumpWidget(
CupertinoApp(
home: Navigator(
key: nested,
onGenerateRoute: (_) => CupertinoPageRoute<void>(
builder: (value) {
context = value;
return const CupertinoPageScaffold(child: Text('Nested'));
},
),
),
),
);
final result = AppDialogs.show<bool>(
context: context,
useRootNavigator: false,
builder: (context) => CupertinoAlertDialog(
actions: [
CupertinoDialogAction(
onPressed: () => Navigator.pop(context, false),
child: const Text('Close'),
),
],
),
);
await tester.pumpAndSettle();
expect(nested.currentState!.canPop(), isTrue);
expect(Navigator.of(context, rootNavigator: true).canPop(), isFalse);
await tester.tap(find.text('Close'));
await tester.pumpAndSettle();
expect(await result, isFalse);
expect(nested.currentState!.canPop(), isFalse);
});
}