Files
rippr/test/hud_layout_controller_test.dart
uhryniuk 7b452b7b81 FB-09: size default HUD widgets by title, not a fixed uniform cell
Adds minColSpanForLabel as a character-count proxy for how many grid
columns a metric's title needs, reworks HudWidgetLayout.defaultFor into
a left-to-right row-packing layout using it, enforces the same minimum
as a floor on manual resize, and splits _HudMetricValue's FittedBox so
the title renders at a fixed reference size while only the value
shrinks/grows to fill the remaining space.

Confirms a real, documented interaction with FB-08's _reflowRow: its
even column split has no awareness of a member's title-driven minimum
and can push one below it. Left unfixed per this ticket's own
Out-of-scope section -- _reflowRow's algorithm is FB-08's, not this
ticket's, to change.

Test count 426 -> 434, all green. flutter analyze unchanged (same 4
pre-existing, unrelated info-level issues).
2026-08-25 00:28:40 -05:00

234 lines
10 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:rippr/src/hud/hud_widget_layout.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 to the grid', () async {
final controller = HudLayoutController(await freshConfig());
final before = controller.state[HudMetric.distance]!;
// Row 4 is unused by any default layout, so this can't collide.
controller.updatePosition(HudMetric.speed, 0, 4);
expect(controller.state[HudMetric.speed]!.row, 4);
expect(controller.state[HudMetric.distance]!.col, before.col,
reason: 'moving one metric must not disturb another');
expect(controller.state[HudMetric.distance]!.row, before.row);
});
test('updatePosition clamps a candidate that lands outside the grid', () async {
final controller = HudLayoutController(await freshConfig());
controller.updatePosition(HudMetric.speed, 99, 99);
final speed = controller.state[HudMetric.speed]!;
expect(speed.col, lessThanOrEqualTo(hudGridColumns - speed.colSpan));
expect(speed.row, lessThanOrEqualTo(hudGridRows - speed.rowSpan));
});
test('updateSize clamps to the legibility floor/ceiling', () async {
final controller = HudLayoutController(await freshConfig());
controller.updateSize(HudMetric.speed, 0, 0);
expect(controller.state[HudMetric.speed]!.colSpan, hudMinSpan);
expect(controller.state[HudMetric.speed]!.rowSpan, hudMinSpan);
});
test('updatePosition onto a cell occupied by another visible metric is a no-op', () async {
final controller = HudLayoutController(await freshConfig());
final before = controller.state[HudMetric.speed]!;
final avgSpeed = controller.state[HudMetric.avgSpeed]!;
// avgSpeed is visible by default -- try to drag speed directly on top of it.
controller.updatePosition(HudMetric.speed, avgSpeed.col, avgSpeed.row);
expect(controller.state[HudMetric.speed]!.col, before.col);
expect(controller.state[HudMetric.speed]!.row, before.row);
});
test('updateSize that would overlap a sibling is a no-op', () async {
final controller = HudLayoutController(await freshConfig());
final before = controller.state[HudMetric.speed]!;
// Growing speed (default col 0, row 0) to span all 4 columns would swallow
// avgSpeed/distance/elapsedTime's row-0 slots.
controller.updateSize(HudMetric.speed, hudGridColumns, before.rowSpan);
expect(controller.state[HudMetric.speed]!.colSpan, before.colSpan);
expect(controller.state[HudMetric.speed]!.rowSpan, before.rowSpan);
});
test('setVisible(false) then setVisible(true) restores the last saved position, '
'when that position is still free', () async {
final controller = HudLayoutController(await freshConfig());
// Row 4 is unused by any default layout, so this can't collide with anything.
controller.updatePosition(HudMetric.avgSpeed, 0, 4);
final moved = controller.state[HudMetric.avgSpeed]!;
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]!.col, moved.col,
reason: 'a metric with a real saved position keeps it when re-enabled');
expect(controller.state[HudMetric.avgSpeed]!.row, moved.row);
});
test('setVisible(true) when the metric\'s last position now collides with '
'another visible widget places it somewhere else instead', () async {
final controller = HudLayoutController(await freshConfig());
final maxSpeedDefault = controller.state[HudMetric.maxSpeed]!;
// maxSpeed starts hidden at its default slot; move a currently-visible metric
// onto that exact slot so it's occupied by the time maxSpeed is re-enabled.
controller.updatePosition(
HudMetric.speed,
maxSpeedDefault.col,
maxSpeedDefault.row,
);
controller.setVisible(HudMetric.maxSpeed, true);
final maxSpeed = controller.state[HudMetric.maxSpeed]!;
expect(maxSpeed.visible, isTrue);
final others = controller.state.values.where(
(l) => l.visible && l.metric != HudMetric.maxSpeed,
);
for (final other in others) {
expect(hudRectsOverlap(maxSpeed, other), isFalse,
reason: 'the re-enabled metric must not stack on any other visible widget');
}
});
test('hiding one of four same-row metrics reflows the remaining three to fill '
'the row with no gap', () async {
final controller = HudLayoutController(await freshConfig());
// FB-09's title-driven defaultFor no longer puts all four default-visible
// metrics in row 0 with uniform colSpan 1 (avgSpeed and elapsedTime need 2
// columns for their longer titles). Force that uniform row-0 arrangement
// explicitly here so this test exercises _reflowRow's own algorithm in
// isolation, independent of exactly where defaultFor happens to place things.
controller.updateSize(HudMetric.elapsedTime, 1, 2);
controller.updateSize(HudMetric.avgSpeed, 1, 2);
controller.updatePosition(HudMetric.elapsedTime, 2, 0);
expect(
[HudMetric.speed, HudMetric.avgSpeed, HudMetric.elapsedTime, HudMetric.distance]
.map((m) => controller.state[m]!.row)
.toSet(),
{0},
reason: 'test setup assumes all four land in row 0',
);
controller.setVisible(HudMetric.avgSpeed, false);
final remaining = [HudMetric.speed, HudMetric.distance, HudMetric.elapsedTime]
.map((m) => controller.state[m]!)
.toList()
..sort((a, b) => a.col.compareTo(b.col));
final totalSpan = remaining.fold<int>(0, (sum, l) => sum + l.colSpan);
expect(totalSpan, hudGridColumns);
// baseSpan = 4 ~/ 3 = 1, extra = 1 -- the first (leftmost) member absorbs it.
expect(remaining[0].colSpan, 2);
expect(remaining[1].colSpan, 1);
expect(remaining[2].colSpan, 1);
expect(controller.state[HudMetric.avgSpeed]!.visible, isFalse);
});
test('hiding a metric whose row has no other visible member leaves every '
'other metric untouched', () async {
final controller = HudLayoutController(await freshConfig());
// Row 4 is unused by any default layout -- move speed there alone.
controller.updatePosition(HudMetric.speed, 0, 4);
final others = {
for (final m in HudMetric.values.where((m) => m != HudMetric.speed))
m: controller.state[m]!,
};
controller.setVisible(HudMetric.speed, false);
for (final entry in others.entries) {
final after = controller.state[entry.key]!;
expect(after.col, entry.value.col, reason: '${entry.key} col changed');
expect(after.row, entry.value.row, reason: '${entry.key} row changed');
expect(after.colSpan, entry.value.colSpan,
reason: '${entry.key} colSpan changed');
}
});
test('showing a previously-hidden metric back into a row with existing '
'members reflows that row to fit it', () async {
final controller = HudLayoutController(await freshConfig());
// FB-09's title-driven defaultFor no longer necessarily lands maxSpeed and
// movingTime in the same row (their default rows depend on every metric's
// individual title width). Move both hidden metrics into the same row
// explicitly, before enabling either, so this test is independent of exactly
// where defaultFor happens to place them.
controller.updatePosition(HudMetric.maxSpeed, 0, 6);
controller.updatePosition(HudMetric.movingTime, 1, 6);
controller.setVisible(HudMetric.maxSpeed, true);
controller.setVisible(HudMetric.movingTime, true);
final maxSpeed = controller.state[HudMetric.maxSpeed]!;
final movingTime = controller.state[HudMetric.movingTime]!;
expect(maxSpeed.row, movingTime.row,
reason: 'test setup assumes both land in the same row');
expect(maxSpeed.colSpan + movingTime.colSpan, hudGridColumns);
});
test('toggling a metric in one row leaves a different row\'s members '
'untouched', () async {
final controller = HudLayoutController(await freshConfig());
final row2Before = {
for (final m in [HudMetric.maxSpeed, HudMetric.movingTime, HudMetric.elevationGain])
m: controller.state[m]!,
};
controller.setVisible(HudMetric.avgSpeed, false);
for (final entry in row2Before.entries) {
final after = controller.state[entry.key]!;
expect(after.col, entry.value.col, reason: '${entry.key} col changed');
expect(after.colSpan, entry.value.colSpan,
reason: '${entry.key} colSpan changed');
}
});
test('persist writes the current state to Config', () async {
final config = await freshConfig();
final controller = HudLayoutController(config);
controller.updatePosition(HudMetric.speed, 0, 4);
await controller.persist();
final reloaded = config.hudLayout;
expect(reloaded[HudMetric.speed]!.row, 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);
// No persist() call yet.
final reloaded = config.hudLayout;
expect(reloaded[HudMetric.speed]!.row, isNot(4),
reason: 'a drag in progress must only touch in-memory state -- '
'persisting every frame is the ticket\'s own named risk');
});
});
}