Skip to content

Commit 6bab0ea

Browse files
committed
tests: avoid using mockkstatic and return vendorId and productId
1 parent cc2197c commit 6bab0ea

File tree

1 file changed

+15
-32
lines changed

1 file changed

+15
-32
lines changed

app/src/test/java/org/kabiri/android/usbterminal/viewmodel/MainActivityViewModelTest.kt

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,15 @@ import org.kabiri.android.usbterminal.util.IResourceProvider
2727
import org.kabiri.android.usbterminal.util.isCloneArduinoBoard
2828
import org.kabiri.android.usbterminal.util.isOfficialArduinoBoard
2929

30-
private const val OFFICIAL_VENDOR_ID = 0x0043
30+
private const val OFFICIAL_VENDOR_ID = 0x2341
31+
private const val OFFICIAL_PRODUCT_ID = 0x0043
32+
33+
private const val CLONE_VENDOR_ID = 0x1A86
34+
private const val CLONE_PRODUCT_ID = 0x7523
35+
36+
37+
private const val OTHER_VENDOR_ID = 0x1234 // random id
38+
private const val OTHER_PRODUCT_ID = 0x1234 // random id
3139

3240
@OptIn(ExperimentalCoroutinesApi::class)
3341
internal class MainActivityViewModelTest {
@@ -119,9 +127,8 @@ internal class MainActivityViewModelTest {
119127
val expectedInfo = "connecting anyways"
120128

121129
val fakeDevice: UsbDevice = mockk(relaxed = true)
122-
mockkStatic(UsbDevice::isOfficialArduinoBoard, UsbDevice::isCloneArduinoBoard)
123-
every { fakeDevice.isOfficialArduinoBoard() } returns false
124-
every { fakeDevice.isCloneArduinoBoard() } returns false
130+
every { fakeDevice.vendorId } returns OTHER_VENDOR_ID
131+
every { fakeDevice.productId } returns OTHER_PRODUCT_ID
125132

126133
every { mockUsbUseCase.scanForUsbDevices() } returns listOf(fakeDevice)
127134
every { mockResourceProvider.getString(R.string.helper_error_arduino_device_not_found) } returns expectedError
@@ -136,38 +143,15 @@ internal class MainActivityViewModelTest {
136143
verify(exactly = 1) { mockUsbUseCase.requestPermission(fakeDevice) }
137144
}
138145

139-
@Test
140-
fun `connect emits expected message and calls requestPermission when the device is neither official nor a clone`() =
141-
runTest {
142-
// arrange
143-
val expected = "connecting anyways"
144-
145-
val fakeDevice: UsbDevice = mockk(relaxed = true)
146-
mockkStatic(UsbDevice::isOfficialArduinoBoard, UsbDevice::isCloneArduinoBoard)
147-
every { fakeDevice.isOfficialArduinoBoard() } returns false
148-
every { fakeDevice.isCloneArduinoBoard() } returns false
149-
150-
every { mockUsbUseCase.scanForUsbDevices() } returns listOf(fakeDevice)
151-
every { mockResourceProvider.getString(R.string.helper_error_connecting_anyway) } returns expected
152-
153-
// act
154-
sut.connect()
155-
156-
// assert
157-
assertThat(sut.infoMessage.value).isEqualTo(expected)
158-
verify(exactly = 1) { mockUsbUseCase.requestPermission(fakeDevice) }
159-
}
160-
161146
@Test
162147
fun `connect emits expected message and calls requestPermission when the device is a clone`() =
163148
runTest {
164149
// arrange
165150
val expected = "connecting anyways"
166151

167152
val fakeDevice: UsbDevice = mockk(relaxed = true)
168-
mockkStatic(UsbDevice::isOfficialArduinoBoard, UsbDevice::isCloneArduinoBoard)
169-
every { fakeDevice.isOfficialArduinoBoard() } returns false
170-
every { fakeDevice.isCloneArduinoBoard() } returns true
153+
every { fakeDevice.vendorId } returns CLONE_VENDOR_ID
154+
every { fakeDevice.productId } returns CLONE_PRODUCT_ID
171155

172156
every { mockUsbUseCase.scanForUsbDevices() } returns listOf(fakeDevice)
173157
every { mockResourceProvider.getString(R.string.helper_error_connecting_anyway) } returns expected
@@ -185,9 +169,8 @@ internal class MainActivityViewModelTest {
185169
runTest {
186170
// arrange
187171
val fakeDevice: UsbDevice = mockk(relaxed = true)
188-
mockkStatic(UsbDevice::isOfficialArduinoBoard, UsbDevice::isCloneArduinoBoard)
189-
every { fakeDevice.isOfficialArduinoBoard() } returns true
190-
every { fakeDevice.isCloneArduinoBoard() } returns false
172+
every { fakeDevice.vendorId } returns OFFICIAL_VENDOR_ID
173+
every { fakeDevice.productId } returns OFFICIAL_PRODUCT_ID
191174

192175
every { mockUsbUseCase.scanForUsbDevices() } returns listOf(fakeDevice)
193176

0 commit comments

Comments
 (0)