From fcd9720477155058969a8b3d17b183bc85ce4c87 Mon Sep 17 00:00:00 2001 From: Codel1417 Date: Wed, 5 Jun 2024 14:18:13 -0400 Subject: [PATCH] setup ble mock with demo responses --- test/testing_utils/bluetooth_test_utils.dart | 27 ++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/test/testing_utils/bluetooth_test_utils.dart b/test/testing_utils/bluetooth_test_utils.dart index 29fd8cc7..e0a3531c 100644 --- a/test/testing_utils/bluetooth_test_utils.dart +++ b/test/testing_utils/bluetooth_test_utils.dart @@ -1,11 +1,34 @@ +import 'package:flutter_blue_plus/flutter_blue_plus.dart'; import 'package:mockito/annotations.dart'; +import 'package:mockito/mockito.dart'; import 'package:tail_app/Backend/Bluetooth/bluetooth_manager_plus.dart'; import 'package:tail_app/Backend/Bluetooth/bluetooth_utils.dart'; // Annotation which generates the cat.mocks.dart library and the MockCat class. -@GenerateNiceMocks([MockSpec()]) +@GenerateNiceMocks([MockSpec(), MockSpec()]) import 'bluetooth_test_utils.mocks.dart'; -void setupBTMock() { +void setupBTMock(String btName, String btMac) { flutterBluePlus = MockFlutterBluePlusMockable(); + + when(flutterBluePlus.isSupported).thenAnswer((_) async => true); + when(flutterBluePlus.adapterState).thenAnswer((_) => Stream.fromIterable([BluetoothAdapterState.on])); + when(flutterBluePlus.isScanning).thenAnswer((_) => Stream.fromIterable([false])); + when(flutterBluePlus.isScanningNow).thenAnswer((_) => true); + when(flutterBluePlus.isSupported).thenAnswer((_) async => true); + when(flutterBluePlus.isSupported).thenAnswer((_) async => true); + when(flutterBluePlus.connectedDevices).thenAnswer((_) => [BluetoothDevice(remoteId: DeviceIdentifier(btMac))]); + when(flutterBluePlus.scanResults).thenAnswer((_) => + Stream.value([ScanResult(rssi: 50, advertisementData: AdvertisementData(advName: btName, txPowerLevel: null, appearance: null, connectable: true, manufacturerData: {}, serviceData: {}, serviceUuids: []), device: BluetoothDevice(remoteId: DeviceIdentifier(btMac)), timeStamp: DateTime.now())])); + when(flutterBluePlus.onScanResults).thenAnswer((_) => + Stream.value([ScanResult(rssi: 50, advertisementData: AdvertisementData(advName: btName, txPowerLevel: null, appearance: null, connectable: true, manufacturerData: {}, serviceData: {}, serviceUuids: []), device: BluetoothDevice(remoteId: DeviceIdentifier(btMac)), timeStamp: DateTime.now())])); + when(flutterBluePlus.setLogLevel(LogLevel.warning, color: true)).thenReturn(Future(() {})); + + BluetoothEvents bluetoothEvents = MockBluetoothEvents(); + when(flutterBluePlus.events).thenAnswer((_) => bluetoothEvents); + when(bluetoothEvents.onMtuChanged).thenAnswer((_) => Stream.fromIterable([OnMtuChangedEvent(BmMtuChangedResponse(mtu: 50, success: true, remoteId: DeviceIdentifier(btMac)))])); + when(bluetoothEvents.onReadRssi).thenAnswer((_) => Stream.fromIterable([OnReadRssiEvent(BmReadRssiResult(rssi: 50, success: true, remoteId: DeviceIdentifier(btMac), errorCode: 0, errorString: ''))])); + when(bluetoothEvents.onServicesReset).thenAnswer((_) => Stream.fromIterable([OnServicesResetEvent(BmBluetoothDevice(remoteId: DeviceIdentifier(btMac), platformName: btName))])); + when(bluetoothEvents.onDiscoveredServices).thenAnswer((_) => Stream.fromIterable([OnDiscoveredServicesEvent(BmDiscoverServicesResult(remoteId: DeviceIdentifier(btMac), services: [], success: true, errorCode: 0, errorString: ''))])); + when(bluetoothEvents.onConnectionStateChanged).thenAnswer((_) => Stream.fromIterable([OnConnectionStateChangedEvent(BmConnectionStateResponse(remoteId: DeviceIdentifier(btMac), connectionState: BmConnectionStateEnum.connected, disconnectReasonCode: null, disconnectReasonString: null))])); }