|
| 1 | +package com.juul.kable |
| 2 | + |
| 3 | +import android.bluetooth.BluetoothDevice |
| 4 | +import android.bluetooth.BluetoothGattCallback |
| 5 | +import android.content.Context |
| 6 | +import android.os.Build |
| 7 | +import android.os.Handler |
| 8 | +import com.juul.kable.logs.Logging |
| 9 | +import io.mockk.Runs |
| 10 | +import io.mockk.every |
| 11 | +import io.mockk.just |
| 12 | +import io.mockk.mockk |
| 13 | +import io.mockk.verify |
| 14 | +import kotlinx.coroutines.flow.MutableSharedFlow |
| 15 | +import kotlinx.coroutines.flow.MutableStateFlow |
| 16 | +import org.junit.runner.RunWith |
| 17 | +import org.robolectric.RobolectricTestRunner |
| 18 | +import org.robolectric.RuntimeEnvironment |
| 19 | +import org.robolectric.annotation.Config |
| 20 | +import kotlin.coroutines.EmptyCoroutineContext |
| 21 | +import kotlin.test.Test |
| 22 | +import kotlin.test.assertEquals |
| 23 | +import kotlin.test.assertFailsWith |
| 24 | +import kotlin.test.assertSame |
| 25 | +import kotlin.time.Duration.Companion.seconds |
| 26 | + |
| 27 | +@RunWith(RobolectricTestRunner::class) |
| 28 | +@Config(sdk = [Build.VERSION_CODES.S]) |
| 29 | +class BluetoothDeviceTests { |
| 30 | + |
| 31 | + @Test |
| 32 | + fun connect_securityException_wrapsInIllegalStateExceptionAndReleasesThreading() { |
| 33 | + val securityException = SecurityException("Missing BLUETOOTH_CONNECT permission") |
| 34 | + val device = mockk<BluetoothDevice> { |
| 35 | + every { address } returns "00:11:22:AA:BB:CC" |
| 36 | + every { |
| 37 | + connectGatt( |
| 38 | + any<Context>(), |
| 39 | + false, |
| 40 | + any<BluetoothGattCallback>(), |
| 41 | + any<Int>(), |
| 42 | + any<Int>(), |
| 43 | + any<Handler>(), |
| 44 | + ) |
| 45 | + } throws securityException |
| 46 | + } |
| 47 | + val threading = mockk<Threading.Handler> { |
| 48 | + every { handler } returns mockk() |
| 49 | + } |
| 50 | + val threadingStrategy = mockk<ThreadingStrategy> { |
| 51 | + every { acquire() } returns threading |
| 52 | + every { release(threading) } just Runs |
| 53 | + } |
| 54 | + every { threading.strategy } returns threadingStrategy |
| 55 | + |
| 56 | + val exception = assertFailsWith<IllegalStateException> { |
| 57 | + device.connect( |
| 58 | + coroutineContext = EmptyCoroutineContext, |
| 59 | + context = RuntimeEnvironment.getApplication(), |
| 60 | + autoConnect = false, |
| 61 | + transport = Transport.Auto, |
| 62 | + phy = Phy.Le1M, |
| 63 | + state = MutableStateFlow(State.Disconnected()), |
| 64 | + services = MutableStateFlow<List<PlatformDiscoveredService>?>(null), |
| 65 | + mtu = MutableStateFlow<Int?>(null), |
| 66 | + onCharacteristicChanged = MutableSharedFlow<ObservationEvent<ByteArray>>(), |
| 67 | + logging = Logging(), |
| 68 | + threadingStrategy = threadingStrategy, |
| 69 | + disconnectTimeout = 1.seconds, |
| 70 | + ) |
| 71 | + } |
| 72 | + |
| 73 | + assertEquals(securityException.message, exception.message) |
| 74 | + assertSame(securityException, exception.cause) |
| 75 | + verify(exactly = 1) { threadingStrategy.release(threading) } |
| 76 | + } |
| 77 | +} |
0 commit comments