diff --git a/test/build.gradle.kts b/test/build.gradle.kts index b91e693a..d5c235ca 100644 --- a/test/build.gradle.kts +++ b/test/build.gradle.kts @@ -43,6 +43,11 @@ kotlin { implementation(libs.androidx.compose.runtime) } } + val androidUnitTest by getting { + dependencies { + implementation(libs.robolectric) + } + } } } diff --git a/test/src/androidDebugUnitTest/kotlin/io/github/kitakkun/backintime/test/BackInTimeDebugServiceTest.kt b/test/src/androidDebugUnitTest/kotlin/io/github/kitakkun/backintime/test/BackInTimeDebugServiceTest.kt deleted file mode 100644 index 0f0d03b1..00000000 --- a/test/src/androidDebugUnitTest/kotlin/io/github/kitakkun/backintime/test/BackInTimeDebugServiceTest.kt +++ /dev/null @@ -1,46 +0,0 @@ -package io.github.kitakkun.backintime.test - -import io.github.kitakkun.backintime.runtime.BackInTimeDebugService -import io.github.kitakkun.backintime.runtime.DebuggableStateHolderEvent -import io.mockk.every -import io.mockk.mockkObject -import io.mockk.slot -import io.mockk.unmockkAll -import org.junit.runner.RunWith -import org.robolectric.RobolectricTestRunner -import kotlin.test.AfterTest -import kotlin.test.BeforeTest - -/** - * copy of BackInTimeDebugServiceTest.kt in jvmTest - */ -@RunWith(RobolectricTestRunner::class) -abstract class BackInTimeDebugServiceTest { - private lateinit var service: BackInTimeDebugService - private val eventSlot = slot() - private val mutableEvents = mutableListOf() - val events: List get() = mutableEvents - - val registerInstanceEvents get() = events.filterIsInstance() - val propertyValueChangeEvents get() = events.filterIsInstance() - val methodCallEvents get() = events.filterIsInstance() - val registerRelationShipEvents get() = events.filterIsInstance() - - @BeforeTest - fun setup() { - mockkObject(BackInTimeDebugService) - service = BackInTimeDebugService - service.start() - every { - service.emitEvent(capture(eventSlot)) - } answers { - mutableEvents.add(eventSlot.captured) - } - } - - @AfterTest - fun teardown() { - service.suspend() - unmockkAll() - } -} diff --git a/test/src/androidUnitTest/kotlin/io/github/kitakkun/backintime/test/BackInTimeDebugServiceTest.kt b/test/src/androidUnitTest/kotlin/io/github/kitakkun/backintime/test/BackInTimeDebugServiceTest.kt new file mode 100644 index 00000000..ec0b7161 --- /dev/null +++ b/test/src/androidUnitTest/kotlin/io/github/kitakkun/backintime/test/BackInTimeDebugServiceTest.kt @@ -0,0 +1,38 @@ +package io.github.kitakkun.backintime.test + +import io.github.kitakkun.backintime.runtime.BackInTimeDebugService +import io.github.kitakkun.backintime.runtime.getBackInTimeDebugService +import io.github.kitakkun.backintime.runtime.internal.BackInTimeCompilerInternalApi +import io.github.kitakkun.backintime.test.base.MockConnector +import io.github.kitakkun.backintime.websocket.event.BackInTimeDebugServiceEvent +import org.junit.runner.RunWith +import org.robolectric.RobolectricTestRunner +import kotlin.test.AfterTest +import kotlin.test.BeforeTest + +/** + * copy of BackInTimeDebugServiceTest.kt in jvmTest + */ +@RunWith(RobolectricTestRunner::class) +abstract class BackInTimeDebugServiceTest { + @OptIn(BackInTimeCompilerInternalApi::class) + private val service: BackInTimeDebugService = getBackInTimeDebugService(useInUnitTest = true) + private val mockConnector = MockConnector() + private val events: List get() = mockConnector.eventsFromService + + val registerInstanceEvents get() = events.filterIsInstance() + val propertyValueChangeEvents get() = events.filterIsInstance() + val methodCallEvents get() = events.filterIsInstance() + val registerRelationShipEvents get() = events.filterIsInstance() + + @BeforeTest + fun setup() { + service.setConnector(mockConnector) + service.startService() + } + + @AfterTest + fun teardown() { + service.stopService() + } +} diff --git a/test/src/androidDebugUnitTest/kotlin/io/github/kitakkun/backintime/test/LambdaViewModelTest.kt b/test/src/androidUnitTest/kotlin/io/github/kitakkun/backintime/test/LambdaViewModelTest.kt similarity index 100% rename from test/src/androidDebugUnitTest/kotlin/io/github/kitakkun/backintime/test/LambdaViewModelTest.kt rename to test/src/androidUnitTest/kotlin/io/github/kitakkun/backintime/test/LambdaViewModelTest.kt diff --git a/test/src/androidDebugUnitTest/kotlin/io/github/kitakkun/backintime/test/MutableStateViewModelTest.kt b/test/src/androidUnitTest/kotlin/io/github/kitakkun/backintime/test/MutableStateViewModelTest.kt similarity index 100% rename from test/src/androidDebugUnitTest/kotlin/io/github/kitakkun/backintime/test/MutableStateViewModelTest.kt rename to test/src/androidUnitTest/kotlin/io/github/kitakkun/backintime/test/MutableStateViewModelTest.kt diff --git a/test/src/androidDebugUnitTest/kotlin/io/github/kitakkun/backintime/test/PlusAssignMutableStateViewModelTest.kt b/test/src/androidUnitTest/kotlin/io/github/kitakkun/backintime/test/PlusAssignMutableStateViewModelTest.kt similarity index 100% rename from test/src/androidDebugUnitTest/kotlin/io/github/kitakkun/backintime/test/PlusAssignMutableStateViewModelTest.kt rename to test/src/androidUnitTest/kotlin/io/github/kitakkun/backintime/test/PlusAssignMutableStateViewModelTest.kt diff --git a/test/src/androidDebugUnitTest/kotlin/io/github/kitakkun/backintime/test/StateFlowCaptureTest.kt b/test/src/androidUnitTest/kotlin/io/github/kitakkun/backintime/test/StateFlowCaptureTest.kt similarity index 57% rename from test/src/androidDebugUnitTest/kotlin/io/github/kitakkun/backintime/test/StateFlowCaptureTest.kt rename to test/src/androidUnitTest/kotlin/io/github/kitakkun/backintime/test/StateFlowCaptureTest.kt index 123a7c9d..a1c2b045 100644 --- a/test/src/androidDebugUnitTest/kotlin/io/github/kitakkun/backintime/test/StateFlowCaptureTest.kt +++ b/test/src/androidUnitTest/kotlin/io/github/kitakkun/backintime/test/StateFlowCaptureTest.kt @@ -10,8 +10,15 @@ class StateFlowCaptureTest : BackInTimeDebugServiceTest() { viewModel.updateValues() - val values = propertyValueChangeEvents.map { it.propertyValue } - val expectedValues = listOf("Hoge", "Fuga", "Piyo", "Foo", "Bar", "Baz") + val values = propertyValueChangeEvents.map { it.value } + val expectedValues = listOf( + "\"Hoge\"", + "\"Fuga\"", + "\"Piyo\"", + "\"Foo\"", + "\"Bar\"", + "\"Baz\"" + ) assertEquals(expectedValues, values) } } diff --git a/test/src/androidDebugUnitTest/kotlin/io/github/kitakkun/backintime/test/WeirdCodeStyleViewModelTest.kt b/test/src/androidUnitTest/kotlin/io/github/kitakkun/backintime/test/WeirdCodeStyleViewModelTest.kt similarity index 100% rename from test/src/androidDebugUnitTest/kotlin/io/github/kitakkun/backintime/test/WeirdCodeStyleViewModelTest.kt rename to test/src/androidUnitTest/kotlin/io/github/kitakkun/backintime/test/WeirdCodeStyleViewModelTest.kt diff --git a/test/src/commonTest/kotlin/io/github/kitakkun/backintime/test/base/MockConnector.kt b/test/src/commonTest/kotlin/io/github/kitakkun/backintime/test/base/MockConnector.kt index 5fb86d78..f328d8fe 100644 --- a/test/src/commonTest/kotlin/io/github/kitakkun/backintime/test/base/MockConnector.kt +++ b/test/src/commonTest/kotlin/io/github/kitakkun/backintime/test/base/MockConnector.kt @@ -3,6 +3,7 @@ package io.github.kitakkun.backintime.test.base import io.github.kitakkun.backintime.runtime.connector.BackInTimeWebSocketConnector import io.github.kitakkun.backintime.websocket.event.BackInTimeDebugServiceEvent import io.github.kitakkun.backintime.websocket.event.BackInTimeDebuggerEvent +import kotlinx.coroutines.awaitCancellation import kotlinx.coroutines.flow.Flow import kotlinx.coroutines.flow.MutableSharedFlow @@ -19,7 +20,7 @@ class MockConnector : BackInTimeWebSocketConnector { } override suspend fun awaitCloseSession() { - // no-op + awaitCancellation() } override suspend fun sendEventToDebugger(event: BackInTimeDebugServiceEvent) {