diff --git a/app/src/androidTest/java/org/fnives/test/showcase/ui/AuthComposeInstrumentedTest.kt b/app/src/androidTest/java/org/fnives/test/showcase/ui/AuthComposeInstrumentedTest.kt index ad48717..4e3eee0 100644 --- a/app/src/androidTest/java/org/fnives/test/showcase/ui/AuthComposeInstrumentedTest.kt +++ b/app/src/androidTest/java/org/fnives/test/showcase/ui/AuthComposeInstrumentedTest.kt @@ -9,7 +9,7 @@ import org.fnives.test.showcase.core.integration.fake.FakeUserDataLocalStorage import org.fnives.test.showcase.core.login.IsUserLoggedInUseCase import org.fnives.test.showcase.network.mockserver.scenario.auth.AuthScenario import org.fnives.test.showcase.testutils.MockServerScenarioSetupResetingTestRule -import org.fnives.test.showcase.testutils.idling.DispatcherTestRule +import org.fnives.test.showcase.testutils.idling.DatabaseDispatcherTestRule import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.anyResourceIdling import org.junit.Before import org.junit.Rule @@ -27,7 +27,7 @@ class AuthComposeInstrumentedTest : KoinTest { private val mockServerScenarioSetupTestRule = MockServerScenarioSetupResetingTestRule() private val mockServerScenarioSetup get() = mockServerScenarioSetupTestRule.mockServerScenarioSetup - private val dispatcherTestRule = DispatcherTestRule() + private val dispatcherTestRule = DatabaseDispatcherTestRule() private lateinit var robot: ComposeLoginRobot private lateinit var navigationRobot: ComposeNavigationRobot diff --git a/app/src/robolectricTest/java/org/fnives/test/showcase/ui/RobolectricAuthActivityInstrumentedTest.kt b/app/src/robolectricTest/java/org/fnives/test/showcase/ui/RobolectricAuthActivityInstrumentedTest.kt index 1cc7276..c665d5a 100644 --- a/app/src/robolectricTest/java/org/fnives/test/showcase/ui/RobolectricAuthActivityInstrumentedTest.kt +++ b/app/src/robolectricTest/java/org/fnives/test/showcase/ui/RobolectricAuthActivityInstrumentedTest.kt @@ -19,7 +19,7 @@ import org.fnives.test.showcase.android.testutil.synchronization.idlingresources import org.fnives.test.showcase.network.mockserver.MockServerScenarioSetup import org.fnives.test.showcase.network.mockserver.scenario.auth.AuthScenario import org.fnives.test.showcase.network.testutil.NetworkTestConfigurationHelper -import org.fnives.test.showcase.testutils.idling.MainDispatcherTestRule.Companion.advanceUntilIdleWithIdlingResources +import org.fnives.test.showcase.testutils.idling.DatabaseDispatcherTestRule.Companion.advanceUntilIdleWithIdlingResources import org.fnives.test.showcase.testutils.storage.TestDatabaseInitialization import org.fnives.test.showcase.ui.auth.AuthActivity import org.junit.After diff --git a/app/src/sharedTest/java/org/fnives/test/showcase/testutils/idling/DispatcherTestRule.kt b/app/src/sharedTest/java/org/fnives/test/showcase/testutils/idling/DatabaseDispatcherTestRule.kt similarity index 97% rename from app/src/sharedTest/java/org/fnives/test/showcase/testutils/idling/DispatcherTestRule.kt rename to app/src/sharedTest/java/org/fnives/test/showcase/testutils/idling/DatabaseDispatcherTestRule.kt index 687a8ce..4708d8b 100644 --- a/app/src/sharedTest/java/org/fnives/test/showcase/testutils/idling/DispatcherTestRule.kt +++ b/app/src/sharedTest/java/org/fnives/test/showcase/testutils/idling/DatabaseDispatcherTestRule.kt @@ -4,15 +4,15 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.TestDispatcher import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.anyResourceIdling +import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.awaitIdlingResources import org.fnives.test.showcase.android.testutil.synchronization.runOnUIAwaitOnCurrent import org.fnives.test.showcase.testutils.storage.TestDatabaseInitialization import org.junit.rules.TestRule import org.junit.runner.Description import org.junit.runners.model.Statement -import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.awaitIdlingResources @OptIn(ExperimentalCoroutinesApi::class) -class DispatcherTestRule : TestRule { +class DatabaseDispatcherTestRule : TestRule { private lateinit var testDispatcher: TestDispatcher diff --git a/app/src/sharedTest/java/org/fnives/test/showcase/testutils/idling/MainDispatcherTestRule.kt b/app/src/sharedTest/java/org/fnives/test/showcase/testutils/idling/MainDispatcherTestRule.kt index 5721e0e..5c1319d 100644 --- a/app/src/sharedTest/java/org/fnives/test/showcase/testutils/idling/MainDispatcherTestRule.kt +++ b/app/src/sharedTest/java/org/fnives/test/showcase/testutils/idling/MainDispatcherTestRule.kt @@ -1,60 +1,14 @@ package org.fnives.test.showcase.testutils.idling -import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.ExperimentalCoroutinesApi -import kotlinx.coroutines.test.StandardTestDispatcher import kotlinx.coroutines.test.TestDispatcher -import kotlinx.coroutines.test.resetMain -import kotlinx.coroutines.test.setMain -import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.anyResourceIdling -import org.fnives.test.showcase.android.testutil.synchronization.runOnUIAwaitOnCurrent import org.fnives.test.showcase.testutils.storage.TestDatabaseInitialization -import org.junit.rules.TestRule -import org.junit.runner.Description -import org.junit.runners.model.Statement -import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.awaitIdlingResources +import org.fnives.test.showcase.android.testutil.synchronization.MainDispatcherTestRule as LibMainDispatcherTestRule @OptIn(ExperimentalCoroutinesApi::class) -class MainDispatcherTestRule : TestRule { +class MainDispatcherTestRule(useStandard: Boolean = true) : LibMainDispatcherTestRule(useStandard) { - private lateinit var testDispatcher: TestDispatcher - - override fun apply(base: Statement, description: Description): Statement = - object : Statement() { - @Throws(Throwable::class) - override fun evaluate() { - val dispatcher = StandardTestDispatcher() - Dispatchers.setMain(dispatcher) - testDispatcher = dispatcher - TestDatabaseInitialization.overwriteDatabaseInitialization(dispatcher) - try { - base.evaluate() - } finally { - Dispatchers.resetMain() - } - } - } - - fun advanceUntilIdleWithIdlingResources() = runOnUIAwaitOnCurrent { - testDispatcher.advanceUntilIdleWithIdlingResources() - } - - fun advanceUntilIdle() = runOnUIAwaitOnCurrent { - testDispatcher.scheduler.advanceUntilIdle() - } - - fun advanceTimeBy(delayInMillis: Long) = runOnUIAwaitOnCurrent { - testDispatcher.scheduler.advanceTimeBy(delayInMillis) - } - - companion object { - fun TestDispatcher.advanceUntilIdleWithIdlingResources() { - scheduler.advanceUntilIdle() // advance until a request is sent - while (anyResourceIdling()) { // check if any request is in progress - awaitIdlingResources() // complete all requests and other idling resources - scheduler.advanceUntilIdle() // run coroutines after request is finished - } - scheduler.advanceUntilIdle() - } + override fun onTestDispatcherInitialized(testDispatcher: TestDispatcher) { + TestDatabaseInitialization.overwriteDatabaseInitialization(testDispatcher) } } diff --git a/test-util-android/src/main/java/org/fnives/test/showcase/android/testutil/synchronization/MainDispatcherTestRule.kt b/test-util-android/src/main/java/org/fnives/test/showcase/android/testutil/synchronization/MainDispatcherTestRule.kt new file mode 100644 index 0000000..8e2676d --- /dev/null +++ b/test-util-android/src/main/java/org/fnives/test/showcase/android/testutil/synchronization/MainDispatcherTestRule.kt @@ -0,0 +1,64 @@ +package org.fnives.test.showcase.android.testutil.synchronization + +import kotlinx.coroutines.Dispatchers +import kotlinx.coroutines.ExperimentalCoroutinesApi +import kotlinx.coroutines.test.StandardTestDispatcher +import kotlinx.coroutines.test.TestDispatcher +import kotlinx.coroutines.test.UnconfinedTestDispatcher +import kotlinx.coroutines.test.resetMain +import kotlinx.coroutines.test.setMain +import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.anyResourceIdling +import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.awaitIdlingResources +import org.junit.rules.TestRule +import org.junit.runner.Description +import org.junit.runners.model.Statement + +@OptIn(ExperimentalCoroutinesApi::class) +open class MainDispatcherTestRule(private val useStandard: Boolean = true) : TestRule { + + private lateinit var testDispatcher: TestDispatcher + + override fun apply(base: Statement, description: Description): Statement = + object : Statement() { + @Throws(Throwable::class) + override fun evaluate() { + val dispatcher = if (useStandard) StandardTestDispatcher() else UnconfinedTestDispatcher() + Dispatchers.setMain(dispatcher) + testDispatcher = dispatcher + onTestDispatcherInitialized(testDispatcher) + try { + base.evaluate() + } finally { + Dispatchers.resetMain() + onTestDispatcherReset() + } + } + } + + open fun onTestDispatcherInitialized(testDispatcher: TestDispatcher) = Unit + + open fun onTestDispatcherReset() = Unit + + fun advanceUntilIdleWithIdlingResources() = runOnUIAwaitOnCurrent { + testDispatcher.advanceUntilIdleWithIdlingResources() + } + + fun advanceUntilIdle() = runOnUIAwaitOnCurrent { + testDispatcher.scheduler.advanceUntilIdle() + } + + fun advanceTimeBy(delayInMillis: Long) = runOnUIAwaitOnCurrent { + testDispatcher.scheduler.advanceTimeBy(delayInMillis) + } + + companion object { + fun TestDispatcher.advanceUntilIdleWithIdlingResources() { + scheduler.advanceUntilIdle() // advance until a request is sent + while (anyResourceIdling()) { // check if any request is in progress + awaitIdlingResources() // complete all requests and other idling resources + scheduler.advanceUntilIdle() // run coroutines after request is finished + } + scheduler.advanceUntilIdle() + } + } +}