Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
OkHttpIdlingResource in Kotlin
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
You can’t perform that action at this time.