Issue#67 Extract IdlingResources helpers into a separate module

This commit is contained in:
Gergely Hegedus 2022-05-27 16:45:01 +03:00
parent 756c74e174
commit 2c01fbba20
4 changed files with 6 additions and 2 deletions

View file

@ -10,7 +10,7 @@ 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.anyResourceIdling
import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.anyResourceIdling
import org.junit.Before
import org.junit.Rule
import org.junit.Test

View file

@ -3,11 +3,13 @@ package org.fnives.test.showcase.testutils.idling
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.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 {

View file

@ -6,11 +6,13 @@ 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
@OptIn(ExperimentalCoroutinesApi::class)
class MainDispatcherTestRule : TestRule {

View file

@ -1,39 +0,0 @@
package org.fnives.test.showcase.testutils.idling
import androidx.test.espresso.IdlingRegistry
import androidx.test.espresso.IdlingResource
import org.fnives.test.showcase.android.testutil.synchronization.loopMainThreadFor
import java.util.concurrent.Executors
// workaround, issue with idlingResources is tracked here https://github.com/robolectric/robolectric/issues/4807
fun anyResourceIdling(): Boolean = !IdlingRegistry.getInstance().resources.all(IdlingResource::isIdleNow)
fun awaitIdlingResources() {
val idlingRegistry = IdlingRegistry.getInstance()
if (idlingRegistry.resources.all(IdlingResource::isIdleNow)) return
val executor = Executors.newSingleThreadExecutor()
var isIdle = false
executor.submit {
do {
idlingRegistry.resources
.filterNot(IdlingResource::isIdleNow)
.forEach { idlingResource ->
idlingResource.awaitUntilIdle()
}
} while (!idlingRegistry.resources.all(IdlingResource::isIdleNow))
isIdle = true
}
while (!isIdle) {
loopMainThreadFor(200L)
}
executor.shutdown()
}
private fun IdlingResource.awaitUntilIdle() {
// using loop because some times, registerIdleTransitionCallback wasn't called
while (true) {
if (isIdleNow) return
Thread.sleep(100L)
}
}