Skip to content
This repository was archived by the owner on Aug 30, 2022. It is now read-only.

Commit ad1d9a7

Browse files
authored
Add requestMtuOrThrow extension function (#61)
1 parent 25f2fe6 commit ad1d9a7

File tree

3 files changed

+60
-22
lines changed

3 files changed

+60
-22
lines changed

core/src/test/java/gatt/CoroutinesGattTest.kt

Lines changed: 15 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -325,9 +325,7 @@ class CoroutinesGattTest {
325325
createDispatcher().use { dispatcher ->
326326
val bluetoothGatt = mockk<BluetoothGatt> {
327327
every { readCharacteristic(any()) } returns false
328-
every<BluetoothDevice?> { device } returns mockk {
329-
every { this@mockk.toString() } returns "00:11:22:33:FF:EE"
330-
}
328+
every<BluetoothDevice?> { device } returns createBluetoothDevice()
331329
}
332330
val callback = GattCallback(dispatcher)
333331
val gatt = CoroutinesGatt(bluetoothGatt, dispatcher, callback)
@@ -373,9 +371,7 @@ class CoroutinesGattTest {
373371
val callback = GattCallback(dispatcher)
374372
val bluetoothGatt = mockk<BluetoothGatt> {
375373
every { close() } returns Unit
376-
every { device } returns mockk {
377-
every { this@mockk.toString() } returns "00:11:22:33:FF:EE"
378-
}
374+
every { device } returns createBluetoothDevice()
379375
every { disconnect() } answers {
380376
callback.onConnectionStateChange(this@mockk, GATT_SUCCESS, STATE_DISCONNECTING)
381377
callback.onConnectionStateChange(this@mockk, GATT_SUCCESS, STATE_DISCONNECTED)
@@ -397,9 +393,7 @@ class CoroutinesGattTest {
397393
val callback = GattCallback(dispatcher)
398394
val bluetoothGatt = mockk<BluetoothGatt> {
399395
every { close() } returns Unit
400-
every { device } returns mockk {
401-
every { this@mockk.toString() } returns "00:11:22:33:FF:EE"
402-
}
396+
every { device } returns createBluetoothDevice()
403397
every { disconnect() } answers {
404398
callback.onConnectionStateChange(this@mockk, GATT_SUCCESS, STATE_DISCONNECTING)
405399
}
@@ -425,11 +419,12 @@ class CoroutinesGattTest {
425419
val callback = GattCallback(dispatcher)
426420
val bluetoothGatt = mockk<BluetoothGatt> {
427421
every { close() } returns Unit
428-
every { device } returns mockk {
429-
every { this@mockk.toString() } returns "00:11:22:33:FF:EE"
430-
}
422+
every { device } returns createBluetoothDevice()
431423
}
432-
val characteristic = FakeCharacteristic(testUuid, value = byteArrayOf(0xF, 0x0, 0x0, 0xD))
424+
val characteristic = FakeCharacteristic(
425+
testUuid,
426+
value = byteArrayOf(0xF, 0x0, 0x0, 0xD)
427+
)
433428
val gatt = CoroutinesGatt(bluetoothGatt, dispatcher, callback)
434429

435430
val events = runBlocking {
@@ -463,9 +458,7 @@ class CoroutinesGattTest {
463458
val callback = GattCallback(dispatcher)
464459
val bluetoothGatt = mockk<BluetoothGatt> {
465460
every { close() } returns Unit
466-
every { device } returns mockk {
467-
every { this@mockk.toString() } returns "00:11:22:33:FF:EE"
468-
}
461+
every { device } returns createBluetoothDevice()
469462
}
470463
val gatt = CoroutinesGatt(bluetoothGatt, dispatcher, callback)
471464

@@ -488,9 +481,7 @@ class CoroutinesGattTest {
488481
val callback = GattCallback(dispatcher)
489482
val bluetoothGatt = mockk<BluetoothGatt> {
490483
every { close() } returns Unit
491-
every { device } returns mockk {
492-
every { this@mockk.toString() } returns "00:11:22:33:FF:EE"
493-
}
484+
every { device } returns createBluetoothDevice()
494485
every { readCharacteristic(any()) } answers {
495486
callback.onConnectionStateChange(this@mockk, GATT_SUCCESS, STATE_DISCONNECTED)
496487
true
@@ -520,9 +511,7 @@ class CoroutinesGattTest {
520511
val callback = GattCallback(dispatcher)
521512
val bluetoothGatt = mockk<BluetoothGatt> {
522513
every { close() } returns Unit
523-
every { device } returns mockk {
524-
every { this@mockk.toString() } returns "00:11:22:33:FF:EE"
525-
}
514+
every { device } returns createBluetoothDevice()
526515
every { readCharacteristic(any()) } answers {
527516
didReadCharacteristic.offer(Unit)
528517
true
@@ -554,6 +543,10 @@ private val dispatcherNumber = AtomicInteger()
554543
private fun createDispatcher() =
555544
newSingleThreadContext("MockGatt${dispatcherNumber.incrementAndGet()}")
556545

546+
private fun createBluetoothDevice(): BluetoothDevice = mockk {
547+
every { this@mockk.toString() } returns "00:11:22:33:FF:EE"
548+
}
549+
557550
private fun createCharacteristic(
558551
uuid: UUID = testUuid,
559552
data: ByteArray = byteArrayOf()

throw/src/main/java/GattOrThrow.kt

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,3 +89,15 @@ suspend fun GattIo.writeDescriptorOrThrow(
8989
check(status == GATT_SUCCESS) { "Descriptor write failed with status $status." }
9090
}
9191
}
92+
93+
/**
94+
* @throws [IllegalStateException] if [GattIo.requestMtu] call does not return [GATT_SUCCESS].
95+
*/
96+
suspend fun GattIo.requestMtuOrThrow(
97+
mtu: Int
98+
): Int {
99+
return requestMtu(mtu)
100+
.also { (_, status) ->
101+
check(status == GATT_SUCCESS) { "Request MTU of $mtu failed with status $status." }
102+
}.mtu
103+
}

throw/src/test/java/GattTest.kt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,12 @@ import com.juul.able.gatt.Gatt
1414
import com.juul.able.gatt.OnCharacteristicRead
1515
import com.juul.able.gatt.OnCharacteristicWrite
1616
import com.juul.able.gatt.OnDescriptorWrite
17+
import com.juul.able.gatt.OnMtuChanged
1718
import com.juul.able.gatt.OnReadRemoteRssi
1819
import com.juul.able.throwable.discoverServicesOrThrow
1920
import com.juul.able.throwable.readCharacteristicOrThrow
2021
import com.juul.able.throwable.readRemoteRssiOrThrow
22+
import com.juul.able.throwable.requestMtuOrThrow
2123
import com.juul.able.throwable.setCharacteristicNotificationOrThrow
2224
import com.juul.able.throwable.writeCharacteristicOrThrow
2325
import com.juul.able.throwable.writeDescriptorOrThrow
@@ -171,4 +173,35 @@ class GattTest {
171173
}
172174
}
173175
}
176+
177+
@Test
178+
fun `requestMtuOrThrow throws IllegalStateException for non-GATT_SUCCESS response`() {
179+
val gatt = mockk<Gatt> {
180+
coEvery { requestMtu(any()) } returns OnMtuChanged(
181+
mtu = 23,
182+
status = GATT_FAILURE
183+
)
184+
}
185+
186+
assertFailsWith<IllegalStateException> {
187+
runBlocking {
188+
gatt.requestMtuOrThrow(1024)
189+
}
190+
}
191+
}
192+
193+
@Test
194+
fun `requestMtuOrThrow returns MTU as Int for GATT_SUCCESS response`() {
195+
val gatt = mockk<Gatt> {
196+
coEvery { requestMtu(any()) } returns OnMtuChanged(
197+
mtu = 128,
198+
status = GATT_SUCCESS
199+
)
200+
}
201+
202+
assertEquals(
203+
expected = 128,
204+
actual = runBlocking { gatt.requestMtuOrThrow(128) }
205+
)
206+
}
174207
}

0 commit comments

Comments
 (0)