Skip to content

Instantly share code, notes, and snippets.

@emmano
Created March 23, 2020 18:19
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 emmano/621b75a54a45d2205afed65972a6d8b8 to your computer and use it in GitHub Desktop.
Save emmano/621b75a54a45d2205afed65972a6d8b8 to your computer and use it in GitHub Desktop.
Testing Fragments that depend on a Activity.
abstract val destination: Int
inline fun <reified F : Fragment> onFragment(crossinline block: F.() -> Unit) {
val intent = Intent(
ApplicationProvider.getApplicationContext<Application>(),
MainActivity::class.java
).apply { flags = Intent.FLAG_ACTIVITY_NEW_TASK }
val scenario = ActivityScenario.launch<MainActivity>(intent)
scenario.onActivity {
Shadows.shadowOf(Looper.getMainLooper()).idle()
val navController = mock<NavController>()
ShadowNavigation.setNavController(navController)
ShadowNavHostFragment.setNavController(navController)
val fragment = it
.supportFragmentManager
.fragments
.first()
.childFragmentManager
.fragments
.first() as F
block(fragment)
}
ShadowNavigation.resetNavController()
scenario.close()
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment