Files
OpenParcelBox/mobile-app/app/test/widget_test.dart
T
2026-07-16 17:36:28 +02:00

122 lines
3.6 KiB
Dart

import 'dart:typed_data';
import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:app/backup_service.dart';
import 'package:app/main.dart';
import 'package:app/models.dart';
import 'package:app/secure_box_store.dart';
class _MemoryStore extends SecureBoxStore {
_MemoryStore({this.language = 'en'});
final String? language;
@override
Future<List<SavedBox>> loadBoxes() async => <SavedBox>[];
@override
Future<String?> loadLanguage() async => language;
@override
Future<void> saveBoxes(List<SavedBox> boxes) async {}
}
class _FakeBackupService extends BackupService {
bool fileWasPicked = false;
@override
Future<Uint8List?> pickBackupFile() async {
fileWasPicked = true;
return Uint8List(0);
}
@override
Future<List<SavedBox>> importBoxes(Uint8List bytes, String password) async =>
<SavedBox>[];
}
void main() {
testWidgets('empty home only exposes registration and restore actions', (
WidgetTester tester,
) async {
await tester.pumpWidget(MyApp(store: _MemoryStore()));
await tester.pumpAndSettle();
expect(find.text('Add a new OpenParcelBox'), findsOneWidget);
expect(find.text('Restore a backup'), findsOneWidget);
expect(find.byIcon(Icons.lock_open), findsNothing);
expect(find.text('Permanent codes'), findsNothing);
});
testWidgets('registration opens the styled setup modal', (
WidgetTester tester,
) async {
await tester.pumpWidget(MyApp(store: _MemoryStore()));
await tester.pumpAndSettle();
await tester.tap(find.text('Add a new OpenParcelBox'));
await tester.pumpAndSettle();
expect(find.text('Box identifier'), findsOneWidget);
expect(find.text('Scan a guest QR code'), findsOneWidget);
expect(find.text('Detected OpenParcelBox devices'), findsOneWidget);
expect(find.byIcon(Icons.close), findsOneWidget);
});
testWidgets('registration text field works with the French locale', (
WidgetTester tester,
) async {
await tester.pumpWidget(MyApp(store: _MemoryStore(language: 'fr')));
await tester.pumpAndSettle();
await tester.tap(find.text('Ajouter une nouvelle OpenParcelBox'));
await tester.pumpAndSettle();
expect(find.byType(TextFormField), findsOneWidget);
expect(tester.takeException(), isNull);
});
testWidgets(
'closing registration after editing does not dispose input early',
(WidgetTester tester) async {
await tester.pumpWidget(MyApp(store: _MemoryStore()));
await tester.pumpAndSettle();
await tester.tap(find.text('Add a new OpenParcelBox'));
await tester.pumpAndSettle();
await tester.enterText(find.byType(TextFormField), 'Front gate');
await tester.tap(find.byIcon(Icons.close));
await tester.pumpAndSettle();
expect(find.text('Add a new OpenParcelBox'), findsOneWidget);
expect(tester.takeException(), isNull);
},
);
testWidgets('restore picks a file before requesting its password', (
WidgetTester tester,
) async {
final backupService = _FakeBackupService();
await tester.pumpWidget(
MyApp(store: _MemoryStore(), backupService: backupService),
);
await tester.pumpAndSettle();
await tester.tap(find.text('Restore a backup'));
await tester.pumpAndSettle();
expect(backupService.fileWasPicked, isTrue);
expect(find.text('Password'), findsNWidgets(2));
await tester.enterText(find.byType(TextField), 'secret');
await tester.tap(find.text('Confirm'));
await tester.pump();
await tester.pump(const Duration(milliseconds: 500));
expect(find.text('Backup restored.'), findsOneWidget);
expect(tester.takeException(), isNull);
});
}