@@ -11,10 +11,10 @@ import android.bluetooth.BluetoothGattCallback
11
11
import android.bluetooth.BluetoothProfile.STATE_CONNECTED
12
12
import android.bluetooth.BluetoothProfile.STATE_CONNECTING
13
13
import android.bluetooth.BluetoothProfile.STATE_DISCONNECTED
14
+ import android.os.RemoteException
14
15
import com.juul.able.device.ConnectGattResult
15
16
import com.juul.able.device.ConnectGattResult.Failure
16
17
import com.juul.able.device.ConnectGattResult.Success
17
- import com.juul.able.device.ConnectionFailed
18
18
import com.juul.able.device.CoroutinesDevice
19
19
import com.juul.able.gatt.ConnectionLost
20
20
import com.juul.able.gatt.GATT_CONN_CANCEL
@@ -34,6 +34,8 @@ import kotlinx.coroutines.runBlocking
34
34
import kotlinx.coroutines.yield
35
35
import org.junit.Rule
36
36
37
+ private const val MAC_ADDRESS = " 00:11:22:33:FF:EE"
38
+
37
39
class CoroutinesDeviceTest {
38
40
39
41
@get:Rule
@@ -46,7 +48,7 @@ class CoroutinesDeviceTest {
46
48
val bluetoothDevice = mockk<BluetoothDevice > {
47
49
bluetoothGatt = createBluetoothGatt(this @mockk)
48
50
every { connectGatt(any(), false , capture(callbackSlot)) } returns bluetoothGatt
49
- every { this @mockk.toString() } returns " 00:11:22:33:FF:EE "
51
+ every { this @mockk.toString() } returns MAC_ADDRESS
50
52
}
51
53
val device = CoroutinesDevice (bluetoothDevice)
52
54
@@ -59,15 +61,11 @@ class CoroutinesDeviceTest {
59
61
callback.onConnectionStateChange(bluetoothGatt, GATT_CONN_CANCEL , STATE_CONNECTED )
60
62
}
61
63
62
- val failure = device.connectGatt(mockk()) as Failure
64
+ val failure = device.connectGatt(mockk()) as Failure . Connection
63
65
64
- assertEquals<Class <out Exception >>(
65
- expected = ConnectionFailed ::class .java,
66
- actual = failure.cause.javaClass
67
- )
68
66
assertEquals(
69
67
expected = OnConnectionStateChange (GATT_CONN_CANCEL , STATE_CONNECTED ),
70
- actual = (failure.cause.cause as GattStatusFailure ).event
68
+ actual = (failure.cause as GattStatusFailure ).event
71
69
)
72
70
verify(exactly = 1 ) { bluetoothGatt.close() }
73
71
}
@@ -79,7 +77,7 @@ class CoroutinesDeviceTest {
79
77
val bluetoothDevice = mockk<BluetoothDevice > {
80
78
bluetoothGatt = createBluetoothGatt(this @mockk)
81
79
every { connectGatt(any(), false , capture(callbackSlot)) } returns bluetoothGatt
82
- every { this @mockk.toString() } returns " 00:11:22:33:FF:EE "
80
+ every { this @mockk.toString() } returns MAC_ADDRESS
83
81
}
84
82
val device = CoroutinesDevice (bluetoothDevice)
85
83
@@ -108,7 +106,7 @@ class CoroutinesDeviceTest {
108
106
val bluetoothDevice = mockk<BluetoothDevice > {
109
107
bluetoothGatt = createBluetoothGatt(this @mockk)
110
108
every { connectGatt(any(), false , capture(callbackSlot)) } returns bluetoothGatt
111
- every { this @mockk.toString() } returns " 00:11:22:33:FF:EE "
109
+ every { this @mockk.toString() } returns MAC_ADDRESS
112
110
}
113
111
val device = CoroutinesDevice (bluetoothDevice)
114
112
@@ -119,15 +117,11 @@ class CoroutinesDeviceTest {
119
117
callback.onConnectionStateChange(bluetoothGatt, GATT_SUCCESS , STATE_DISCONNECTED )
120
118
}
121
119
122
- val failure = device.connectGatt(mockk()) as Failure
120
+ val failure = device.connectGatt(mockk()) as Failure . Connection
123
121
124
- assertEquals<Class <out Exception >>(
125
- expected = ConnectionFailed ::class .java,
126
- actual = failure.cause.javaClass
127
- )
128
122
assertEquals<Class <out Throwable >>(
129
123
expected = ConnectionLost ::class .java,
130
- actual = failure.cause.cause !! . javaClass
124
+ actual = failure.cause.javaClass
131
125
)
132
126
verify(exactly = 1 ) { bluetoothGatt.close() }
133
127
}
@@ -139,7 +133,7 @@ class CoroutinesDeviceTest {
139
133
val bluetoothDevice = mockk<BluetoothDevice > {
140
134
bluetoothGatt = createBluetoothGatt(this @mockk)
141
135
every { connectGatt(any(), false , capture(callbackSlot)) } returns bluetoothGatt
142
- every { this @mockk.toString() } returns " 00:11:22:33:FF:EE "
136
+ every { this @mockk.toString() } returns MAC_ADDRESS
143
137
}
144
138
val device = CoroutinesDevice (bluetoothDevice)
145
139
@@ -153,6 +147,21 @@ class CoroutinesDeviceTest {
153
147
154
148
verify(exactly = 1 ) { bluetoothGatt.close() }
155
149
}
150
+
151
+ @Test
152
+ fun `Null return from connectGatt results in Failure Rejected` () = runBlocking {
153
+ val bluetoothDevice = mockk<BluetoothDevice > {
154
+ every { connectGatt(any(), false , any()) } returns null
155
+ every { this @mockk.toString() } returns MAC_ADDRESS
156
+ }
157
+ val device = CoroutinesDevice (bluetoothDevice)
158
+ val failure = device.connectGatt(mockk()) as Failure .Rejected
159
+
160
+ assertEquals<Class <out Throwable >>(
161
+ expected = RemoteException ::class .java,
162
+ actual = failure.cause.javaClass
163
+ )
164
+ }
156
165
}
157
166
158
167
private fun createBluetoothGatt (
0 commit comments