Files
rippr/test/config_test.dart
uhryniuk bcface3529 UI-04: customizable, persisted HUD telemetry widget layout
Adds the full drag/resize/visibility infrastructure for a rider-owned HUD
layout: HudMetric (the stable 8-metric set), HudWidgetLayout (fractional
x/y/width/height + visible, with clamping to a legibility floor/ceiling
and JSON round-trip that degrades to sane defaults rather than crashing),
Config.hudLayout persistence, and a HudLayoutController that stays
in-memory-authoritative during an edit session and only writes through on
persist() -- never per drag frame.

DraggableResizableHudWidget and HudEditOverlay assemble the interaction:
edit mode is entered by a long-press on empty HUD space (not a specific
widget) and exited via Done or a tap on empty space; only while editing
does a widget attach any drag/resize gesture at all, so a normal tap can
never move one mid-ride by construction, not by an internal flag.

Fixes a real gesture-arena bug found during testing: outside edit mode, a
long-press landing on a widget was free to bubble to the overlay's
background long-press handler and wrongly enter edit mode. An inner no-op
GestureDetector of the same gesture type now absorbs it.

Adds a "Live HUD stats" Settings section, one switch per metric, that
persists immediately (unlike drag frames). Placed at the end of the
Settings list rather than in the middle -- inserting mid-list pushed
every later section below several existing tests' viewport assumptions.

Verified end-to-end on a real emulator including a full process restart:
resized a widget, force-stopped the app, relaunched, and the resize held.
No consuming screen exists yet (UI-05's job) -- verified via a throwaway
preview entry point, deleted after use.

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_012Xki7YAcc2TiN2PRZJ2tXr
2026-08-23 20:45:02 -05:00

167 lines
5.6 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:rippr/src/config/config.dart';
import 'package:rippr/src/domain/models.dart';
import 'package:rippr/src/hud/hud_metric.dart';
import 'package:rippr/src/hud/hud_widget_layout.dart';
import 'package:shared_preferences/shared_preferences.dart';
/// `Config` had no dedicated tests before V3-03 — every existing widget test leaves
/// `configProvider` null, so this is its first direct coverage.
void main() {
Future<Config> freshConfig([Map<String, Object> initial = const {}]) async {
SharedPreferences.setMockInitialValues(initial);
return Config.load();
}
group('unitSystem', () {
test('an explicit choice always wins over the locale default', () async {
final config = await freshConfig();
await config.setUnitSystem(UnitSystem.imperial);
expect(config.unitSystem, UnitSystem.imperial);
await config.setUnitSystem(UnitSystem.metric);
expect(config.unitSystem, UnitSystem.metric);
});
test('with nothing stored, falls back to a locale default without throwing', () async {
// The exact value is environment-dependent (it reads the real platform locale),
// so this asserts the fallback path is safe to reach, not a specific answer.
final config = await freshConfig();
expect(config.unitSystem, isA<UnitSystem>());
});
});
group('mapEnabled', () {
test('defaults to true', () async {
final config = await freshConfig();
expect(config.mapEnabled, isTrue);
});
test('round-trips through set/get', () async {
final config = await freshConfig();
await config.setMapEnabled(false);
expect(config.mapEnabled, isFalse);
});
});
group('mountedMode', () {
test('defaults to false', () async {
final config = await freshConfig();
expect(config.mountedMode, isFalse);
});
test('round-trips through set/get', () async {
final config = await freshConfig();
await config.setMountedMode(true);
expect(config.mountedMode, isTrue);
});
});
group('crashReportingEnabled', () {
test('defaults to false', () async {
final config = await freshConfig();
expect(config.crashReportingEnabled, isFalse);
});
test('round-trips through set/get', () async {
final config = await freshConfig();
await config.setCrashReportingEnabled(true);
expect(config.crashReportingEnabled, isTrue);
});
});
group('uploadEndpoint', () {
test('defaults to empty, not null or a placeholder', () async {
final config = await freshConfig();
expect(config.uploadEndpoint, '');
});
test('trims whitespace on write', () async {
final config = await freshConfig();
await config.setUploadEndpoint(' https://example.test/ingest ');
expect(config.uploadEndpoint, 'https://example.test/ingest');
});
});
group('hudLayout (UI-04)', () {
test('a fresh install gets a default layout for every metric', () async {
final config = await freshConfig();
final layout = config.hudLayout;
expect(layout.keys.toSet(), HudMetric.values.toSet());
});
test('round-trips an edited layout through set/get', () async {
final config = await freshConfig();
final edited = {
...config.hudLayout,
HudMetric.speed: const HudWidgetLayout(
metric: HudMetric.speed,
x: 0.33,
y: 0.44,
width: 0.25,
height: 0.1,
visible: true,
),
};
await config.setHudLayout(edited);
final reloaded = config.hudLayout;
expect(reloaded[HudMetric.speed]!.x, 0.33);
expect(reloaded[HudMetric.speed]!.y, 0.44);
});
test('a metric missing from a saved layout (e.g. one added in a later app '
'version) falls back to its default rather than being omitted', () async {
SharedPreferences.setMockInitialValues({
'hud_layout': '{"speed": {"x": 0.1, "y": 0.1, "width": 0.2, "height": 0.1, '
'"visible": true}}',
});
final config = await Config.load();
final layout = config.hudLayout;
expect(layout.keys.toSet(), HudMetric.values.toSet());
final expectedDefault = HudWidgetLayout.defaultFor(HudMetric.distance);
expect(layout[HudMetric.distance]!.x, expectedDefault.x);
expect(layout[HudMetric.distance]!.visible, expectedDefault.visible);
});
test('corrupted stored JSON falls back to defaults rather than crashing',
() async {
SharedPreferences.setMockInitialValues({'hud_layout': 'not json at all'});
final config = await Config.load();
expect(config.hudLayout.keys.toSet(), HudMetric.values.toSet());
});
});
group('deviceId', () {
test('is generated once and then stable across reads', () async {
final config = await freshConfig();
final first = config.deviceId;
final second = config.deviceId;
expect(first, second);
expect(first, isNotEmpty);
});
test('is a v4 UUID', () async {
final config = await freshConfig();
// xxxxxxxx-xxxx-4xxx-{8,9,a,b}xxx-xxxxxxxxxxxx
final v4 = RegExp(
r'^[0-9a-f]{8}-[0-9a-f]{4}-4[0-9a-f]{3}-[89ab][0-9a-f]{3}-[0-9a-f]{12}$',
);
expect(v4.hasMatch(config.deviceId), isTrue,
reason: 'got ${config.deviceId}');
});
test('persists across a fresh Config over the same preferences', () async {
SharedPreferences.setMockInitialValues({});
final first = await Config.load();
final id = first.deviceId;
final second = await Config.load();
expect(second.deviceId, id,
reason: 'a server distinguishing riders needs this to be stable');
});
});
}