V3-02 + V3-03: settings screen, and metric/imperial units
Built together because V3-02's settings screen needed something real for V3-03's units to control -- executed in reverse of the ticket numbering, but both tickets are independently complete. V3-03: UnitSystem (metric/imperial) lives in domain/models.dart alongside Activity, defaulting from Platform.localeName on first launch. Storage stays SI everywhere -- conversion happens only in ui/format.dart, at the last possible moment, which is now documented as the file's central invariant. Threaded through all three screens plus the speed histogram's bucket labels, which relabel for display without changing how speedHistogram itself bins. Export was deliberately left untouched: gpx()/geoJson() take no UnitSystem parameter at all, a stronger guarantee than validating one would be. V3-02: SettingsScreen reachable from the record screen. Map render toggle (nothing previously exposed mapEnabledProvider to the user, despite it existing since T15 -- there was nothing to "move off trip detail" as drafted), unit selector, upload endpoint with http(s) validation, a copyable device id, and a licences page via Flutter's built-in showLicensePage. Skipped package_info_plus (static version string instead) and a privacy-policy link (none published yet) as disproportionate to an S-sized ticket. Neither ticket needed the ConfigNotifier the implementation notes proposed: unitSystemProvider reuses the exact StateProvider-seeded-from-Config pattern mapEnabledProvider already established, since Config mutates its own backing SharedPreferences in place and re-assigning the same instance would never notify a watcher anyway. A real locale-dependent flake was caught, not just anticipated: a test asserting a fresh Config defaults to metric failed, because this machine's own locale resolves to a region in the imperial set. Fixed by seeding an explicit value before asserting, and config_test.dart's locale test was written from the start to only prove the fallback path doesn't throw, not to assert which value it returns. 221 tests (211 -> 221): 13 format_test, 9 config_test, 9 settings_screen_test, 1 confirming the record screen's settings button is genuinely wired. Analyze clean. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
164
test/settings_screen_test.dart
Normal file
164
test/settings_screen_test.dart
Normal file
@@ -0,0 +1,164 @@
|
||||
import 'package:flutter/material.dart';
|
||||
import 'package:flutter_riverpod/flutter_riverpod.dart';
|
||||
import 'package:flutter_test/flutter_test.dart';
|
||||
import 'package:rippr/src/app/providers.dart';
|
||||
import 'package:rippr/src/config/config.dart';
|
||||
import 'package:rippr/src/domain/models.dart';
|
||||
import 'package:rippr/src/ui/settings/settings_screen.dart';
|
||||
import 'package:rippr/src/ui/theme.dart';
|
||||
import 'package:shared_preferences/shared_preferences.dart';
|
||||
|
||||
/// V3-02: every `Config` value must be viewable and editable here, and every write must
|
||||
/// actually reach `Config` -- not just update local widget state that looks right until
|
||||
/// the next app launch.
|
||||
void main() {
|
||||
late Config config;
|
||||
|
||||
setUp(() async {
|
||||
SharedPreferences.setMockInitialValues({});
|
||||
config = await Config.load();
|
||||
});
|
||||
|
||||
Widget host() => ProviderScope(
|
||||
overrides: [configProvider.overrideWith((ref) => config)],
|
||||
child: MaterialApp(theme: ripprTheme(), home: const SettingsScreen()),
|
||||
);
|
||||
|
||||
testWidgets('the map switch reflects and writes through to Config', (tester) async {
|
||||
await tester.pumpWidget(host());
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final initial = tester
|
||||
.widget<SwitchListTile>(find.byKey(const Key('map-enabled-switch')))
|
||||
.value;
|
||||
expect(initial, isTrue, reason: 'Config.mapEnabled defaults to true');
|
||||
|
||||
await tester.tap(find.byKey(const Key('map-enabled-switch')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(config.mapEnabled, isFalse,
|
||||
reason: 'the switch must write through to Config, not just local state');
|
||||
});
|
||||
|
||||
testWidgets('changing the map switch is visible to other providers immediately',
|
||||
(tester) async {
|
||||
// V3-02's acceptance criterion: "changing the map toggle takes effect without an
|
||||
// app restart". mapEnabledProvider is what trip detail actually reads.
|
||||
await tester.pumpWidget(host());
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
final container = ProviderScope.containerOf(
|
||||
tester.element(find.byType(SettingsScreen)),
|
||||
);
|
||||
expect(container.read(mapEnabledProvider), isTrue);
|
||||
|
||||
await tester.tap(find.byKey(const Key('map-enabled-switch')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(container.read(mapEnabledProvider), isFalse);
|
||||
});
|
||||
|
||||
testWidgets('the unit selector writes through to Config', (tester) async {
|
||||
// The default depends on the test runner's own locale (see config_test.dart), so
|
||||
// start from an explicit, known value rather than assuming metric.
|
||||
await config.setUnitSystem(UnitSystem.metric);
|
||||
await tester.pumpWidget(host());
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.tap(find.text('Imperial'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(config.unitSystem, UnitSystem.imperial);
|
||||
|
||||
await tester.tap(find.text('Metric'));
|
||||
await tester.pumpAndSettle();
|
||||
expect(config.unitSystem, UnitSystem.metric);
|
||||
});
|
||||
|
||||
group('upload endpoint', () {
|
||||
testWidgets('a valid https URL is saved', (tester) async {
|
||||
await tester.pumpWidget(host());
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.enterText(
|
||||
find.byKey(const Key('endpoint-field')),
|
||||
'https://example.test/ingest',
|
||||
);
|
||||
await tester.tap(find.byKey(const Key('save-endpoint')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(config.uploadEndpoint, 'https://example.test/ingest');
|
||||
expect(find.text('Endpoint saved'), findsOneWidget);
|
||||
});
|
||||
|
||||
testWidgets('an invalid endpoint is rejected with a readable message, not '
|
||||
'silently stored', (tester) async {
|
||||
await tester.pumpWidget(host());
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.enterText(
|
||||
find.byKey(const Key('endpoint-field')),
|
||||
'not a url at all',
|
||||
);
|
||||
await tester.tap(find.byKey(const Key('save-endpoint')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(config.uploadEndpoint, '',
|
||||
reason: 'garbage input must never reach storage');
|
||||
expect(find.textContaining('http'), findsWidgets);
|
||||
});
|
||||
|
||||
testWidgets('a non-http scheme is rejected', (tester) async {
|
||||
await tester.pumpWidget(host());
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.enterText(
|
||||
find.byKey(const Key('endpoint-field')),
|
||||
'ftp://example.test/ingest',
|
||||
);
|
||||
await tester.tap(find.byKey(const Key('save-endpoint')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(config.uploadEndpoint, '');
|
||||
});
|
||||
|
||||
testWidgets('clearing the endpoint is a valid way to disable upload',
|
||||
(tester) async {
|
||||
await config.setUploadEndpoint('https://example.test/ingest');
|
||||
await tester.pumpWidget(host());
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
await tester.enterText(find.byKey(const Key('endpoint-field')), '');
|
||||
await tester.tap(find.byKey(const Key('save-endpoint')));
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(config.uploadEndpoint, '');
|
||||
expect(find.text('Upload disabled'), findsOneWidget);
|
||||
});
|
||||
});
|
||||
|
||||
testWidgets('the device id is shown and a copy control exists', (tester) async {
|
||||
await tester.pumpWidget(host());
|
||||
await tester.pumpAndSettle();
|
||||
|
||||
expect(find.text(config.deviceId), findsOneWidget);
|
||||
expect(find.byKey(const Key('copy-device-id')), findsOneWidget);
|
||||
|
||||
// Tapping must not throw even though no real clipboard exists in the test harness.
|
||||
await tester.tap(find.byKey(const Key('copy-device-id')));
|
||||
await tester.pumpAndSettle();
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
|
||||
testWidgets('shows a spinner rather than crashing while Config is still loading',
|
||||
(tester) async {
|
||||
await tester.pumpWidget(
|
||||
const ProviderScope(
|
||||
child: MaterialApp(home: SettingsScreen()),
|
||||
),
|
||||
);
|
||||
await tester.pump();
|
||||
|
||||
expect(find.byType(CircularProgressIndicator), findsOneWidget);
|
||||
expect(tester.takeException(), isNull);
|
||||
});
|
||||
}
|
||||
Reference in New Issue
Block a user