Files
swiftcontrol/test/utils/keymap/training_peaks_keymap_test.dart
Jonas Bark 8170cd97b9 fix(trainingpeaks): direction and face buttons now reach the app
The TPV profile mapped navigationUp and the coloured face buttons to a
keyboard key / touch position only. _handleDirectConnect forwards a KeyPair
solely when it carries an inGameAction, so those presses never left the
phone — and an actionless default is worse than no default at all, because
it shadows the action the button would otherwise inherit from
ControllerButton.action via Keymap.addNewButtons (which is exactly why
navigationDown, absent from the profile, worked).

Same gap hit the HUD-toggle and difficulty defaults. Steer Left/Right are
added to defaultObpSupportedButtons as well: TPV advertises them in its
live app-info frame, but without them the button editor hid steering
before a connection.

Reported by a rider on Android with TPV on an iPad, whose log showed
"Sent Down button press" next to "No connection method active".

Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
2026-08-18 12:54:00 +02:00

66 lines
3.0 KiB
Dart

import 'package:bike_control/bluetooth/devices/elite/elite_square.dart';
import 'package:bike_control/bluetooth/devices/zwift/constants.dart';
import 'package:bike_control/utils/keymap/apps/training_peaks.dart';
import 'package:bike_control/utils/keymap/buttons.dart';
import 'package:bike_control/utils/keymap/keymap.dart';
import 'package:dartx/dartx.dart';
import 'package:flutter_test/flutter_test.dart';
/// TrainingPeaks Virtual is reached over OpenBikeControl (BLE / DirCon), and
/// `BaseActions._handleDirectConnect` only forwards a [KeyPair] that carries an
/// [InGameAction]. A default that maps a button to a keyboard key or a touch
/// position *only* is therefore worse than shipping no default at all: it
/// shadows the action the button would otherwise inherit from
/// [ControllerButton.action] via [Keymap.addNewButtons], and the press can
/// never leave the phone.
void main() {
final app = TrainingPeaks();
final supportedActions = app.defaultObpSupportedButtons.mapNotNull((b) => b.action).toSet();
test('every default key pair carries an in-game action', () {
final actionless = app.keymap.keyPairs
.where((k) => k.inGameAction == null)
.expand((k) => k.buttons)
.map((b) => b.name)
.toList();
expect(actionless, isEmpty, reason: 'these buttons can never reach TPV over OBP: $actionless');
});
test('every default in-game action is one TPV accepts over OBP', () {
for (final keyPair in app.keymap.keyPairs) {
expect(
supportedActions,
contains(keyPair.inGameAction),
reason: '${keyPair.buttons.first.name} → ${keyPair.inGameAction} is not in defaultObpSupportedButtons',
);
}
});
test('navigation and face buttons keep their conventional actions', () {
InGameAction? actionFor(ControllerButton button) =>
app.keymap.getKeyPair(button, trigger: ButtonTrigger.singleClick)?.inGameAction;
expect(actionFor(ZwiftButtons.navigationUp), InGameAction.up);
expect(actionFor(ZwiftButtons.navigationLeft), InGameAction.steerLeft);
expect(actionFor(ZwiftButtons.navigationRight), InGameAction.steerRight);
expect(actionFor(ZwiftButtons.a), InGameAction.select);
expect(actionFor(ZwiftButtons.b), InGameAction.back);
expect(actionFor(ZwiftButtons.y), InGameAction.menu);
expect(actionFor(ZwiftButtons.z), InGameAction.pause);
// The Elite Square shares the face-button key pairs.
expect(actionFor(EliteSquareButtons.a), InGameAction.select);
expect(actionFor(EliteSquareButtons.y), InGameAction.menu);
});
test('navigation down inherits its default action instead of being shadowed', () {
// Not in the profile at all — [Keymap.addNewButtons] seeds it from the
// button itself when a controller connects. This is the behaviour the
// explicit defaults above have to match.
expect(app.keymap.getKeyPair(ZwiftButtons.navigationDown), isNull);
expect(ZwiftButtons.navigationDown.action, InGameAction.down);
expect(supportedActions, contains(InGameAction.down));
});
}