Files
swiftcontrol/test/zwift_clickv2_unlock_method_test.dart
Jonas Bark 6778c1e45b feat: optimize Zwift Click V2 left/right experience
- Make the split left/right controllers available to everyone: replace the
  isPro gate in the device factory with a "Use new unlock method" setting
  (default on); off falls back to the legacy unified ZwiftClickV2.
- Add the "Use new unlock method" toggle to all three Click V2 settings panels;
  flipping it reconnects the Click as the new/legacy representation.
- Fix the "Unlock again" button placement: on narrow side-by-side cards the
  action now drops below the status text instead of squeezing it.
- Add "Use the right side only" to the right side when a left side is connected:
  ignores the left side (reversible via Ignored Devices) and remaps the active
  keymap so + shifts up and B shifts down.
- Localize new strings (de/es/fr/it/pl; en already present) and add
  factory/settings unit tests.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
2026-06-17 19:50:08 +02:00

86 lines
3.1 KiB
Dart

import 'dart:typed_data';
import 'package:bike_control/bluetooth/devices/bluetooth_device.dart';
import 'package:bike_control/bluetooth/devices/zwift/constants.dart';
import 'package:bike_control/bluetooth/devices/zwift/zwift_clickv2.dart';
import 'package:bike_control/bluetooth/devices/zwift/zwift_clickv2_left_side.dart';
import 'package:bike_control/bluetooth/devices/zwift/zwift_clickv2_right_side.dart';
import 'package:bike_control/utils/actions/base_actions.dart';
import 'package:bike_control/utils/core.dart';
import 'package:bike_control/utils/settings/settings.dart';
import 'package:flutter_test/flutter_test.dart';
import 'package:shared_preferences/shared_preferences.dart';
import 'package:universal_ble/universal_ble.dart';
BleDevice _clickV2(int sideCode) => BleDevice(
deviceId: 'click-$sideCode',
name: 'Zwift Click',
manufacturerDataList: [
ManufacturerData(ZwiftConstants.ZWIFT_MANUFACTURER_ID, Uint8List.fromList([sideCode])),
],
services: [ZwiftConstants.ZWIFT_CUSTOM_SERVICE_UUID.toLowerCase()],
);
void main() {
TestWidgetsFlutterBinding.ensureInitialized();
group('getUseNewUnlockMethod persistence', () {
late Settings settings;
setUp(() async {
SharedPreferences.setMockInitialValues({});
settings = Settings();
settings.prefs = await SharedPreferences.getInstance();
});
test('defaults to on', () {
expect(settings.getUseNewUnlockMethod(), isTrue);
});
test('round-trips', () async {
await settings.setUseNewUnlockMethod(false);
expect(settings.getUseNewUnlockMethod(), isFalse);
await settings.setUseNewUnlockMethod(true);
expect(settings.getUseNewUnlockMethod(), isTrue);
});
test('returns the default when prefs are not initialised', () {
// A Settings whose prefs were never assigned (e.g. device detection in a
// pure unit test) must not throw and should fall back to the default.
expect(Settings().getUseNewUnlockMethod(), isTrue);
});
});
group('Click V2 factory honours the unlock method toggle', () {
setUp(() async {
SharedPreferences.setMockInitialValues({});
core.actionHandler = StubActions();
core.settings.prefs = await SharedPreferences.getInstance();
});
test('new method on: left/right connect as separate controllers (no Pro gate)', () async {
await core.settings.setUseNewUnlockMethod(true);
expect(
BluetoothDevice.fromScanResult(_clickV2(ZwiftConstants.CLICK_V2_LEFT_SIDE)),
isA<ZwiftClickV2LeftSide>(),
);
expect(
BluetoothDevice.fromScanResult(_clickV2(ZwiftConstants.CLICK_V2_RIGHT_SIDE)),
isA<ZwiftClickV2RightSide>(),
);
});
test('new method off: falls back to the legacy single controller', () async {
await core.settings.setUseNewUnlockMethod(false);
final left = BluetoothDevice.fromScanResult(_clickV2(ZwiftConstants.CLICK_V2_LEFT_SIDE));
expect(left, isA<ZwiftClickV2>());
expect(left, isNot(isA<ZwiftClickV2LeftSide>()));
expect(BluetoothDevice.fromScanResult(_clickV2(ZwiftConstants.CLICK_V2_RIGHT_SIDE)), isNull);
});
});
}