Restore server-scoped Settings category after relaunch
This commit is contained in:
@@ -0,0 +1,132 @@
|
||||
import 'dart:async';
|
||||
|
||||
import 'package:flutter/cupertino.dart';
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:pipelines_app/core/servers/server_scope.dart';
|
||||
import 'package:pipelines_app/features/settings/settings_tabs.dart';
|
||||
import 'package:pipelines_app/features/settings/settings_topic_store.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
import 'settings_topic_test_helper.dart';
|
||||
|
||||
Widget host({
|
||||
bool cupertino = false,
|
||||
String? scope,
|
||||
SettingsTopicStore? store,
|
||||
}) {
|
||||
final tabs = SettingsTopicTabs(
|
||||
navigationScope: scope,
|
||||
selectionStore: store,
|
||||
pages: {
|
||||
for (final topic in SettingsTopic.values)
|
||||
topic: (_) => Text('${topic.name} page'),
|
||||
},
|
||||
);
|
||||
return cupertino
|
||||
? CupertinoApp(home: CupertinoPageScaffold(child: tabs))
|
||||
: MaterialApp(home: Scaffold(body: tabs));
|
||||
}
|
||||
|
||||
class DelayedStore implements SettingsTopicStore {
|
||||
final read = Completer<String?>();
|
||||
final saves = <String>[];
|
||||
final pending = <Completer<void>>[];
|
||||
bool blockWrites = false;
|
||||
@override
|
||||
Future<String?> load() => read.future;
|
||||
@override
|
||||
Future<void> save(String topic) async {
|
||||
saves.add(topic);
|
||||
if (blockWrites) {
|
||||
final gate = Completer<void>();
|
||||
pending.add(gate);
|
||||
await gate.future;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void main() {
|
||||
for (final cupertino in [false, true]) {
|
||||
testWidgets('restores selected category after recreation ($cupertino)', (
|
||||
tester,
|
||||
) async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
await tester.pumpWidget(host(cupertino: cupertino, scope: 'server-a'));
|
||||
await tester.pumpAndSettle();
|
||||
await selectSettingsTopic(tester, 'Connection');
|
||||
expect(find.text('connection page'), findsOneWidget);
|
||||
await tester.pumpWidget(const SizedBox.shrink());
|
||||
await tester.pumpWidget(host(cupertino: cupertino, scope: 'server-a'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('connection page'), findsOneWidget);
|
||||
final preferences = await SharedPreferences.getInstance();
|
||||
expect(
|
||||
preferences.getString(scopedServerKey(settingsTopicKey, 'server-a')),
|
||||
'connection',
|
||||
);
|
||||
});
|
||||
}
|
||||
|
||||
testWidgets('server scopes and malformed selections are independent', (
|
||||
tester,
|
||||
) async {
|
||||
SharedPreferences.setMockInitialValues({
|
||||
scopedServerKey(settingsTopicKey, 'server-a'): 'connection',
|
||||
scopedServerKey(settingsTopicKey, 'server-b'): 'not-a-topic',
|
||||
});
|
||||
await tester.pumpWidget(host(scope: 'server-a'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('connection page'), findsOneWidget);
|
||||
await tester.pumpWidget(host(scope: 'server-b'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('appearance page'), findsOneWidget);
|
||||
await tester.pumpWidget(host(scope: 'server-a'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('connection page'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('standalone category tabs do not read or write preferences', (
|
||||
tester,
|
||||
) async {
|
||||
SharedPreferences.setMockInitialValues({settingsTopicKey: 'updates'});
|
||||
await tester.pumpWidget(host());
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('appearance page'), findsOneWidget);
|
||||
await selectSettingsTopic(tester, 'Connection');
|
||||
expect(
|
||||
(await SharedPreferences.getInstance()).getString(settingsTopicKey),
|
||||
'updates',
|
||||
);
|
||||
});
|
||||
|
||||
testWidgets('late restore cannot overwrite a newer user selection', (
|
||||
tester,
|
||||
) async {
|
||||
final store = DelayedStore();
|
||||
await tester.pumpWidget(host(store: store));
|
||||
await selectSettingsTopic(tester, 'Connection');
|
||||
store.read.complete('updates');
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('connection page'), findsOneWidget);
|
||||
expect(store.saves, ['connection']);
|
||||
});
|
||||
|
||||
testWidgets('writes are serialized so the latest selection wins last', (
|
||||
tester,
|
||||
) async {
|
||||
final store = DelayedStore()..blockWrites = true;
|
||||
store.read.complete(null);
|
||||
await tester.pumpWidget(host(store: store));
|
||||
await tester.pumpAndSettle();
|
||||
await selectSettingsTopic(tester, 'Connection');
|
||||
await selectSettingsTopic(tester, 'Notifications');
|
||||
expect(store.saves, ['connection']);
|
||||
store.pending.first.complete();
|
||||
await tester.pumpAndSettle();
|
||||
expect(store.saves, ['connection', 'notifications']);
|
||||
store.pending.last.complete();
|
||||
await tester.pumpAndSettle();
|
||||
expect(find.text('notifications page'), findsOneWidget);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user