Created
January 28, 2020 00:32
-
-
Save firoze/3fb6cb292e5f934270069d44cf3585c1 to your computer and use it in GitHub Desktop.
OkHttpIdlingResource in Kotlin
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import androidx.annotation.CheckResult | |
import androidx.annotation.NonNull | |
import androidx.test.espresso.IdlingResource | |
import okhttp3.Dispatcher | |
import okhttp3.OkHttpClient | |
class OkHttpIdlingResource private constructor( | |
private val resourceName: String, private val dispatcher: Dispatcher | |
) : IdlingResource { | |
companion object { | |
fun create( | |
@NonNull name: String, | |
@NonNull client: OkHttpClient | |
): OkHttpIdlingResource { | |
return OkHttpIdlingResource(name, client.dispatcher) | |
} | |
} | |
@Volatile | |
var resourceCallback: IdlingResource.ResourceCallback? = null | |
init { | |
dispatcher.idleCallback = Runnable { resourceCallback?.onTransitionToIdle() } | |
} | |
override fun getName(): String = resourceName | |
override fun isIdleNow(): Boolean = dispatcher.runningCallsCount() == 0 | |
override fun registerIdleTransitionCallback(callback: IdlingResource.ResourceCallback?) { | |
resourceCallback = callback | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment