Skip to content

Instantly share code, notes, and snippets.

@Atsumi3
Created February 14, 2019 08:46
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Atsumi3/d5ff8abf43b35750d4f13bedfd7f26b5 to your computer and use it in GitHub Desktop.
Save Atsumi3/d5ff8abf43b35750d4f13bedfd7f26b5 to your computer and use it in GitHub Desktop.
IdlingResource.use
import android.support.test.espresso.IdlingRegistry
import android.support.test.espresso.IdlingResource
inline fun <T: IdlingResource?, R> T.use(block: (T) -> R): R {
var exception: Throwable? = null
try {
IdlingRegistry.getInstance().register(this)
return block(this)
} catch (e: Throwable) {
exception = e
throw e
} finally {
when {
this == null -> {}
exception == null -> IdlingRegistry.getInstance().unregister(this)
else ->
try {
IdlingRegistry.getInstance().unregister(this)
} catch (closeException: Throwable) {
// ignore
}
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment