Skip to content

Commit

Permalink
setup ble mock with demo responses
Browse files Browse the repository at this point in the history
  • Loading branch information
Codel1417 committed Jun 5, 2024
1 parent 3bb1b3a commit fcd9720
Showing 1 changed file with 25 additions and 2 deletions.
27 changes: 25 additions & 2 deletions test/testing_utils/bluetooth_test_utils.dart
Original file line number Diff line number Diff line change
@@ -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<FlutterBluePlusMockable>()])
@GenerateNiceMocks([MockSpec<FlutterBluePlusMockable>(), MockSpec<BluetoothEvents>()])
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))]));
}

0 comments on commit fcd9720

Please sign in to comment.