105 lines
3.9 KiB
Dart
105 lines
3.9 KiB
Dart
import 'package:cutdex/errors/app_failure.dart';
|
|
import 'package:cutdex/l10n/app_localizations.dart';
|
|
import 'package:cutdex/shared/widgets/error_notification_host.dart';
|
|
import 'package:flutter/gestures.dart';
|
|
import 'package:flutter/material.dart';
|
|
import 'package:flutter/services.dart';
|
|
import 'package:flutter_test/flutter_test.dart';
|
|
|
|
void main() {
|
|
Future<ErrorNotificationHostState> mount(WidgetTester tester) async {
|
|
await tester.pumpWidget(
|
|
MaterialApp(
|
|
localizationsDelegates: AppLocalizations.localizationsDelegates,
|
|
supportedLocales: AppLocalizations.supportedLocales,
|
|
builder: (context, child) => ErrorNotificationHost(child: child!),
|
|
home: const Scaffold(body: Text('Workspace')),
|
|
),
|
|
);
|
|
return tester.state<ErrorNotificationHostState>(
|
|
find.byType(ErrorNotificationHost),
|
|
);
|
|
}
|
|
|
|
testWidgets('top error copies complete details and closes', (tester) async {
|
|
String? copied;
|
|
tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
|
SystemChannels.platform,
|
|
(call) async {
|
|
if (call.method == 'Clipboard.setData') {
|
|
copied = (call.arguments as Map)['text'] as String;
|
|
}
|
|
return null;
|
|
},
|
|
);
|
|
addTearDown(
|
|
() => tester.binding.defaultBinaryMessenger.setMockMethodCallHandler(
|
|
SystemChannels.platform,
|
|
null,
|
|
),
|
|
);
|
|
final host = await mount(tester);
|
|
host.showFailure(
|
|
const AppFailure(
|
|
AppErrorCode.resourceDownloadHttpFailed,
|
|
httpStatusCode: 503,
|
|
diagnostic: 'service unavailable',
|
|
),
|
|
'Download failed',
|
|
);
|
|
await tester.pump();
|
|
final notice = find.byKey(const Key('global-error-notification'));
|
|
expect(tester.getRect(notice).top, 48);
|
|
expect(find.byType(SnackBar), findsNothing);
|
|
await tester.tap(find.byKey(const Key('copy-error-button')));
|
|
await tester.pump();
|
|
expect(copied, contains('Download failed'));
|
|
expect(copied, contains('code: resourceDownloadHttpFailed'));
|
|
expect(copied, contains('HTTP: 503'));
|
|
expect(copied, contains('service unavailable'));
|
|
expect(find.byIcon(Icons.check), findsOneWidget);
|
|
await tester.tap(find.byKey(const Key('close-error-button')));
|
|
await tester.pump();
|
|
expect(notice, findsNothing);
|
|
});
|
|
|
|
testWidgets('errors queue and pause dismissal while hovered', (tester) async {
|
|
final host = await mount(tester);
|
|
host.show(const ErrorNotification(content: Text('First')));
|
|
host.show(const ErrorNotification(content: Text('Second')));
|
|
await tester.pump();
|
|
expect(find.text('First'), findsOneWidget);
|
|
expect(find.text('Second'), findsNothing);
|
|
final mouse = await tester.createGesture(kind: PointerDeviceKind.mouse);
|
|
await mouse.addPointer(location: Offset.zero);
|
|
await mouse.moveTo(
|
|
tester.getCenter(find.byKey(const Key('global-error-notification'))),
|
|
);
|
|
await tester.pump(const Duration(seconds: 9));
|
|
expect(find.text('First'), findsOneWidget);
|
|
await mouse.moveTo(const Offset(1, 500));
|
|
await tester.pump(const Duration(seconds: 8));
|
|
expect(find.text('Second'), findsOneWidget);
|
|
await tester.pump(const Duration(seconds: 8));
|
|
expect(find.byKey(const Key('global-error-notification')), findsNothing);
|
|
await mouse.removePointer();
|
|
});
|
|
|
|
testWidgets('error remains above an open dialog', (tester) async {
|
|
final host = await mount(tester);
|
|
final context = tester.element(find.text('Workspace'));
|
|
showDialog<void>(
|
|
context: context,
|
|
builder: (_) => const AlertDialog(content: Text('Dialog')),
|
|
);
|
|
await tester.pumpAndSettle();
|
|
host.show(const ErrorNotification(content: Text('Dialog error')));
|
|
await tester.pump();
|
|
expect(find.text('Dialog'), findsOneWidget);
|
|
await tester.tap(find.byKey(const Key('close-error-button')));
|
|
await tester.pump();
|
|
expect(find.text('Dialog error'), findsNothing);
|
|
expect(find.text('Dialog'), findsOneWidget);
|
|
});
|
|
}
|