Created
March 23, 2020 18:19
-
-
Save emmano/621b75a54a45d2205afed65972a6d8b8 to your computer and use it in GitHub Desktop.
Testing Fragments that depend on a Activity.
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
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