RobolectricBleBonding

Reusable Robolectric helpers for driving Android Bluetooth bonding logic in androidHostTest source sets.

These let a unit test exercise the real AndroidBluetoothRepository.bond() (and any future BLE bonding code) without an emulator and without a production seam. They rely on two Robolectric behaviors (verified against Robolectric 4.16.x):

Isolation note: because the device cache is static and survives across tests in the same JVM, give each test a distinct MAC so bond-state cannot bleed between tests.

Properties

Link copied to clipboard
const val TEST_BLE_MAC: String

A syntactically valid BLE MAC for tests that don't care about the specific address.

Functions

Link copied to clipboard
fun deviceShadow(mac: String = TEST_BLE_MAC): ShadowBluetoothDevice

The (address-cached) ShadowBluetoothDevice for mac — the same instance production's getRemoteDevice sees.

Link copied to clipboard

Grant the runtime permissions ShadowBluetoothDevice.createBond checks, so it returns instead of throwing.

Link copied to clipboard
fun primeBond(mac: String = TEST_BLE_MAC, bondState: Int = BluetoothDevice.BOND_NONE, createBondReturns: Boolean = false): ShadowBluetoothDevice

Configure the cached device for mac: its bondState and what createBond() returns.

Link copied to clipboard
fun sendBondStateChanged(mac: String, newState: Int, previousState: Int)

Broadcast ACTION_BOND_STATE_CHANGED for mac and pump the main looper so a runtime-registered android.content.BroadcastReceiver (e.g. the one bond() registers) is delivered synchronously.