Files
TPSchedule/mobile/test/widgets/recovery_test.dart
T

73 lines
2.6 KiB
Dart

import 'package:flutter/material.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:timezone/data/latest.dart' as tzdata;
import 'package:hepan_everyday/app/app_controller.dart';
import 'package:hepan_everyday/app/hepan_app.dart';
import 'package:hepan_everyday/domain/app_snapshot.dart';
import '../support/fakes.dart';
void main() {
setUpAll(tzdata.initializeTimeZones);
for (final badBackup in [false, true]) {
testWidgets(
'corrupt primary with ${badBackup ? "corrupt" : "missing"} backup has confirmed fresh recovery',
(tester) async {
final store = DamagedStore(badBackup);
final controller = AppController(
store,
FakeNotifications(),
FakeCalendar(),
DateTime.now,
);
await controller.initialize();
await controller.restoreBackup();
await tester.pumpWidget(HepanApp(controller: controller));
await tester.pumpAndSettle();
expect(controller.storageReady, isFalse);
expect(find.text('重新建立课表'), findsOneWidget);
await tester.tap(find.text('重新建立课表'));
await tester.pumpAndSettle();
await tester.tap(find.text('取消'));
await tester.pumpAndSettle();
expect(controller.storageReady, isFalse);
expect(store.freshStarts, 0);
await tester.tap(find.text('重新建立课表'));
await tester.pumpAndSettle();
await tester.tap(find.text('保留损坏文件并重新建立'));
await tester.pumpAndSettle();
expect(controller.storageReady, isTrue);
await tester.tap(find.bySemanticsLabel('我的'));
await tester.pumpAndSettle();
await tester.ensureVisible(find.text('课表设置'));
await tester.pumpAndSettle();
await tester.tap(find.text('课表设置'));
await tester.pumpAndSettle();
await tester.tap(find.text('单色'));
await tester.pumpAndSettle();
expect(store.value!.settings.colorMode.name, 'single');
expect(store.freshStarts, 1);
await tester.pumpWidget(const SizedBox());
controller.dispose();
},
);
}
}
class DamagedStore extends MemoryStore {
DamagedStore(this.badBackup);
final bool badBackup;
int freshStarts = 0;
@override
Future<AppSnapshot?> load() async =>
value ?? (throw const FormatException('damaged'));
@override
Future<AppSnapshot> restoreBackup() async =>
throw StateError(badBackup ? '备份损坏' : '没有备份');
@override
Future<AppSnapshot> startFresh() async {
freshStarts++;
return value = AppSnapshot();
}
}