Issue#67 Extract MainDispatcherTestRule into Library
This commit is contained in:
parent
2c01fbba20
commit
a27f19302a
5 changed files with 73 additions and 55 deletions
|
|
@ -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.core.login.IsUserLoggedInUseCase
|
||||||
import org.fnives.test.showcase.network.mockserver.scenario.auth.AuthScenario
|
import org.fnives.test.showcase.network.mockserver.scenario.auth.AuthScenario
|
||||||
import org.fnives.test.showcase.testutils.MockServerScenarioSetupResetingTestRule
|
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.fnives.test.showcase.android.testutil.synchronization.idlingresources.anyResourceIdling
|
||||||
import org.junit.Before
|
import org.junit.Before
|
||||||
import org.junit.Rule
|
import org.junit.Rule
|
||||||
|
|
@ -27,7 +27,7 @@ class AuthComposeInstrumentedTest : KoinTest {
|
||||||
|
|
||||||
private val mockServerScenarioSetupTestRule = MockServerScenarioSetupResetingTestRule()
|
private val mockServerScenarioSetupTestRule = MockServerScenarioSetupResetingTestRule()
|
||||||
private val mockServerScenarioSetup get() = mockServerScenarioSetupTestRule.mockServerScenarioSetup
|
private val mockServerScenarioSetup get() = mockServerScenarioSetupTestRule.mockServerScenarioSetup
|
||||||
private val dispatcherTestRule = DispatcherTestRule()
|
private val dispatcherTestRule = DatabaseDispatcherTestRule()
|
||||||
private lateinit var robot: ComposeLoginRobot
|
private lateinit var robot: ComposeLoginRobot
|
||||||
private lateinit var navigationRobot: ComposeNavigationRobot
|
private lateinit var navigationRobot: ComposeNavigationRobot
|
||||||
|
|
||||||
|
|
|
||||||
|
|
@ -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.MockServerScenarioSetup
|
||||||
import org.fnives.test.showcase.network.mockserver.scenario.auth.AuthScenario
|
import org.fnives.test.showcase.network.mockserver.scenario.auth.AuthScenario
|
||||||
import org.fnives.test.showcase.network.testutil.NetworkTestConfigurationHelper
|
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.testutils.storage.TestDatabaseInitialization
|
||||||
import org.fnives.test.showcase.ui.auth.AuthActivity
|
import org.fnives.test.showcase.ui.auth.AuthActivity
|
||||||
import org.junit.After
|
import org.junit.After
|
||||||
|
|
|
||||||
|
|
@ -4,15 +4,15 @@ import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
import kotlinx.coroutines.test.StandardTestDispatcher
|
import kotlinx.coroutines.test.StandardTestDispatcher
|
||||||
import kotlinx.coroutines.test.TestDispatcher
|
import kotlinx.coroutines.test.TestDispatcher
|
||||||
import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.anyResourceIdling
|
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.android.testutil.synchronization.runOnUIAwaitOnCurrent
|
||||||
import org.fnives.test.showcase.testutils.storage.TestDatabaseInitialization
|
import org.fnives.test.showcase.testutils.storage.TestDatabaseInitialization
|
||||||
import org.junit.rules.TestRule
|
import org.junit.rules.TestRule
|
||||||
import org.junit.runner.Description
|
import org.junit.runner.Description
|
||||||
import org.junit.runners.model.Statement
|
import org.junit.runners.model.Statement
|
||||||
import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.awaitIdlingResources
|
|
||||||
|
|
||||||
@OptIn(ExperimentalCoroutinesApi::class)
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
class DispatcherTestRule : TestRule {
|
class DatabaseDispatcherTestRule : TestRule {
|
||||||
|
|
||||||
private lateinit var testDispatcher: TestDispatcher
|
private lateinit var testDispatcher: TestDispatcher
|
||||||
|
|
||||||
|
|
@ -1,60 +1,14 @@
|
||||||
package org.fnives.test.showcase.testutils.idling
|
package org.fnives.test.showcase.testutils.idling
|
||||||
|
|
||||||
import kotlinx.coroutines.Dispatchers
|
|
||||||
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
import kotlinx.coroutines.ExperimentalCoroutinesApi
|
||||||
import kotlinx.coroutines.test.StandardTestDispatcher
|
|
||||||
import kotlinx.coroutines.test.TestDispatcher
|
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.fnives.test.showcase.testutils.storage.TestDatabaseInitialization
|
||||||
import org.junit.rules.TestRule
|
import org.fnives.test.showcase.android.testutil.synchronization.MainDispatcherTestRule as LibMainDispatcherTestRule
|
||||||
import org.junit.runner.Description
|
|
||||||
import org.junit.runners.model.Statement
|
|
||||||
import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.awaitIdlingResources
|
|
||||||
|
|
||||||
@OptIn(ExperimentalCoroutinesApi::class)
|
@OptIn(ExperimentalCoroutinesApi::class)
|
||||||
class MainDispatcherTestRule : TestRule {
|
class MainDispatcherTestRule(useStandard: Boolean = true) : LibMainDispatcherTestRule(useStandard) {
|
||||||
|
|
||||||
private lateinit var testDispatcher: TestDispatcher
|
override fun onTestDispatcherInitialized(testDispatcher: TestDispatcher) {
|
||||||
|
TestDatabaseInitialization.overwriteDatabaseInitialization(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()
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -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()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue