Created
February 14, 2019 08:46
-
-
Save Atsumi3/d5ff8abf43b35750d4f13bedfd7f26b5 to your computer and use it in GitHub Desktop.
IdlingResource.use
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 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