Issue#67 Extract OkHttp IdlingResources into a separate module
This commit is contained in:
parent
bbe077dde8
commit
756c74e174
9 changed files with 26 additions and 21 deletions
|
|
@ -11,12 +11,12 @@ import androidx.test.rule.ActivityTestRule
|
|||
import androidx.test.runner.AndroidJUnit4
|
||||
import kotlinx.coroutines.test.UnconfinedTestDispatcher
|
||||
import org.fnives.test.showcase.R
|
||||
import org.fnives.test.showcase.network.testutil.NetworkTestConfigurationHelper
|
||||
import org.fnives.test.showcase.testutils.idling.CompositeDisposable
|
||||
import org.fnives.test.showcase.testutils.idling.Disposable
|
||||
import org.fnives.test.showcase.testutils.idling.IdlingResourceDisposable
|
||||
import org.fnives.test.showcase.testutils.idling.OkHttp3IdlingResource
|
||||
import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.CompositeDisposable
|
||||
import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.Disposable
|
||||
import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.IdlingResourceDisposable
|
||||
import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.OkHttp3IdlingResource
|
||||
import org.fnives.test.showcase.android.testutil.synchronization.loopMainThreadFor
|
||||
import org.fnives.test.showcase.network.testutil.NetworkTestConfigurationHelper
|
||||
import org.fnives.test.showcase.testutils.storage.TestDatabaseInitialization
|
||||
import org.fnives.test.showcase.ui.splash.SplashActivity
|
||||
import org.hamcrest.Description
|
||||
|
|
|
|||
|
|
@ -11,15 +11,15 @@ import kotlinx.coroutines.test.TestDispatcher
|
|||
import kotlinx.coroutines.test.resetMain
|
||||
import kotlinx.coroutines.test.setMain
|
||||
import org.fnives.test.showcase.R
|
||||
import org.fnives.test.showcase.android.testutil.activity.safeClose
|
||||
import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.CompositeDisposable
|
||||
import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.Disposable
|
||||
import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.IdlingResourceDisposable
|
||||
import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.OkHttp3IdlingResource
|
||||
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.CompositeDisposable
|
||||
import org.fnives.test.showcase.testutils.idling.Disposable
|
||||
import org.fnives.test.showcase.testutils.idling.IdlingResourceDisposable
|
||||
import org.fnives.test.showcase.testutils.idling.MainDispatcherTestRule.Companion.advanceUntilIdleWithIdlingResources
|
||||
import org.fnives.test.showcase.testutils.idling.OkHttp3IdlingResource
|
||||
import org.fnives.test.showcase.android.testutil.activity.safeClose
|
||||
import org.fnives.test.showcase.testutils.storage.TestDatabaseInitialization
|
||||
import org.fnives.test.showcase.ui.auth.AuthActivity
|
||||
import org.junit.After
|
||||
|
|
|
|||
|
|
@ -1,19 +0,0 @@
|
|||
package org.fnives.test.showcase.testutils.idling
|
||||
|
||||
class CompositeDisposable(disposable: List<Disposable> = emptyList()) : Disposable {
|
||||
|
||||
constructor(vararg disposables: Disposable) : this(disposables.toList())
|
||||
|
||||
private val disposables = disposable.toMutableList()
|
||||
override val isDisposed: Boolean get() = disposables.all(Disposable::isDisposed)
|
||||
|
||||
fun add(disposable: Disposable) {
|
||||
disposables.add(disposable)
|
||||
}
|
||||
|
||||
override fun dispose() {
|
||||
disposables.forEach {
|
||||
it.dispose()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -1,6 +0,0 @@
|
|||
package org.fnives.test.showcase.testutils.idling
|
||||
|
||||
interface Disposable {
|
||||
val isDisposed: Boolean
|
||||
fun dispose()
|
||||
}
|
||||
|
|
@ -1,19 +0,0 @@
|
|||
package org.fnives.test.showcase.testutils.idling
|
||||
|
||||
import androidx.test.espresso.IdlingRegistry
|
||||
import androidx.test.espresso.IdlingResource
|
||||
|
||||
internal class IdlingResourceDisposable(private val idlingResource: IdlingResource) : Disposable {
|
||||
override var isDisposed: Boolean = false
|
||||
private set
|
||||
|
||||
init {
|
||||
IdlingRegistry.getInstance().register(idlingResource)
|
||||
}
|
||||
|
||||
override fun dispose() {
|
||||
if (isDisposed) return
|
||||
isDisposed = true
|
||||
IdlingRegistry.getInstance().unregister(idlingResource)
|
||||
}
|
||||
}
|
||||
|
|
@ -3,6 +3,10 @@ package org.fnives.test.showcase.testutils.idling
|
|||
import androidx.annotation.CheckResult
|
||||
import androidx.test.espresso.IdlingResource
|
||||
import okhttp3.OkHttpClient
|
||||
import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.CompositeDisposable
|
||||
import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.Disposable
|
||||
import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.IdlingResourceDisposable
|
||||
import org.fnives.test.showcase.android.testutil.synchronization.idlingresources.OkHttp3IdlingResource
|
||||
import org.fnives.test.showcase.network.testutil.NetworkTestConfigurationHelper
|
||||
import org.junit.rules.TestRule
|
||||
import org.junit.runner.Description
|
||||
|
|
|
|||
|
|
@ -1,50 +0,0 @@
|
|||
package org.fnives.test.showcase.testutils.idling
|
||||
|
||||
import androidx.annotation.CheckResult
|
||||
import androidx.annotation.NonNull
|
||||
import androidx.test.espresso.IdlingResource
|
||||
import okhttp3.Dispatcher
|
||||
import okhttp3.OkHttpClient
|
||||
|
||||
/**
|
||||
* AndroidX version of Jake Wharton's OkHttp3IdlingResource.
|
||||
*
|
||||
* Reference: https://github.com/JakeWharton/okhttp-idling-resource/blob/master/src/main/java/com/jakewharton/espresso/OkHttp3IdlingResource.java
|
||||
*/
|
||||
class OkHttp3IdlingResource private constructor(
|
||||
private val name: String,
|
||||
private val dispatcher: Dispatcher
|
||||
) : IdlingResource {
|
||||
@Volatile
|
||||
var callback: IdlingResource.ResourceCallback? = null
|
||||
|
||||
init {
|
||||
val currentCallback = dispatcher.idleCallback
|
||||
dispatcher.idleCallback = Runnable {
|
||||
callback?.onTransitionToIdle()
|
||||
currentCallback?.run()
|
||||
}
|
||||
}
|
||||
|
||||
override fun getName(): String = name
|
||||
|
||||
override fun isIdleNow(): Boolean = dispatcher.runningCallsCount() == 0
|
||||
|
||||
override fun registerIdleTransitionCallback(callback: IdlingResource.ResourceCallback?) {
|
||||
this.callback = callback
|
||||
}
|
||||
|
||||
companion object {
|
||||
/**
|
||||
* Create a new [IdlingResource] from `client` as `name`. You must register
|
||||
* this instance using `Espresso.registerIdlingResources`.
|
||||
*/
|
||||
@CheckResult
|
||||
@NonNull // Extra guards as a library.
|
||||
fun create(@NonNull name: String?, @NonNull client: OkHttpClient?): OkHttp3IdlingResource {
|
||||
if (name == null) throw NullPointerException("name == null")
|
||||
if (client == null) throw NullPointerException("client == null")
|
||||
return OkHttp3IdlingResource(name, client.dispatcher)
|
||||
}
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue