Skip to content

Instantly share code, notes, and snippets.

@oscargrgm
Last active February 26, 2021 17:30
Show Gist options
  • Save oscargrgm/642f9139a99e02d88d3acce1b1e65841 to your computer and use it in GitHub Desktop.
Save oscargrgm/642f9139a99e02d88d3acce1b1e65841 to your computer and use it in GitHub Desktop.
Useful testing code from Udacity's Android Kotlin Developer Nanodegree.
/**
* Extension function to obtain the content description for the navigation icon.
*/
fun <T : Activity> ActivityScenario<T>.getToolbarNavigationContentDescription(): String {
var description = ""
onActivity {
description =
it.findViewById<Toolbar>(R.id.toolbar).navigationContentDescription as String
}
return description
}
/**
* Useful function to test long running operations without adding too much boilerplate.
*/
inline fun <T> wrapEspressoIdlingResource(function: () -> T): T {
// Espresso does not work well with coroutines yet. See
// https://github.com/Kotlin/kotlinx.coroutines/issues/982
EspressoIdlingResource.increment() // Set app as busy.
return try {
function()
} finally {
EspressoIdlingResource.decrement() // Set app as idle.
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment