setVisible now calls a new _reflowRow helper on the toggled metric's row, spreading the remaining (or newly added) visible widgets evenly across the 4-column grid instead of leaving a blank cell where a hidden metric used to be. Reflow is scoped to setVisible only; drag/resize behavior is unchanged.
211 lines
8.8 KiB
Dart
211 lines
8.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: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());
|
|
|
|
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());
|
|
|
|
// maxSpeed and movingTime both default to row 2, hidden, at adjacent columns.
|
|
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');
|
|
});
|
|
});
|
|
}
|