Files
rippr/test/hud_layout_controller_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

72 lines
2.8 KiB
Dart

import 'package:flutter_test/flutter_test.dart';
import 'package:rippr/src/config/config.dart';
import 'package:rippr/src/hud/hud_layout_controller.dart';
import 'package:rippr/src/hud/hud_metric.dart';
import 'package:shared_preferences/shared_preferences.dart';
void main() {
Future<Config> freshConfig() async {
SharedPreferences.setMockInitialValues({});
return Config.load();
}
group('HudLayoutController', () {
test('updatePosition updates only the given metric, clamped', () async {
final controller = HudLayoutController(await freshConfig());
final before = controller.state[HudMetric.distance]!;
controller.updatePosition(HudMetric.speed, 5.0, 5.0);
expect(controller.state[HudMetric.speed]!.x, lessThanOrEqualTo(1.0));
expect(controller.state[HudMetric.distance]!.x, before.x,
reason: 'moving one metric must not disturb another');
});
test('updateSize clamps to the legibility floor/ceiling', () async {
final controller = HudLayoutController(await freshConfig());
controller.updateSize(HudMetric.speed, 0.0, 0.0);
expect(controller.state[HudMetric.speed]!.width, greaterThan(0.0));
expect(controller.state[HudMetric.speed]!.height, greaterThan(0.0));
});
test('setVisible(false) then setVisible(true) restores the last saved position, '
'not a fresh default', () async {
final controller = HudLayoutController(await freshConfig());
controller.updatePosition(HudMetric.avgSpeed, 0.6, 0.6);
final movedX = controller.state[HudMetric.avgSpeed]!.x;
controller.setVisible(HudMetric.avgSpeed, false);
expect(controller.state[HudMetric.avgSpeed]!.visible, isFalse);
controller.setVisible(HudMetric.avgSpeed, true);
expect(controller.state[HudMetric.avgSpeed]!.visible, isTrue);
expect(controller.state[HudMetric.avgSpeed]!.x, movedX,
reason: 'a metric with a real saved position keeps it when re-enabled');
});
test('persist writes the current state to Config', () async {
final config = await freshConfig();
final controller = HudLayoutController(config);
controller.updatePosition(HudMetric.speed, 0.4, 0.4);
await controller.persist();
final reloaded = config.hudLayout;
expect(reloaded[HudMetric.speed]!.x, 0.4);
});
test('edits before persist are not visible to a fresh read of Config', () async {
final config = await freshConfig();
final controller = HudLayoutController(config);
controller.updatePosition(HudMetric.speed, 0.4, 0.4);
// No persist() call yet.
final reloaded = config.hudLayout;
expect(reloaded[HudMetric.speed]!.x, isNot(0.4),
reason: 'a drag in progress must only touch in-memory state -- '
'persisting every frame is the ticket\'s own named risk');
});
});
}