Gists for medium article titled Jetpack Compose: Assert Intent Data Consumption in Instrumented Tests
Last active
December 9, 2023 08:13
-
-
Save AungThiha/2570145242377de3510f5eb042d9a6b1 to your computer and use it in GitHub Desktop.
Jetpack Compose: Assert Intent Data Consumption in Instrumented Tests
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
fun <A : ComponentActivity> createAndroidComposeRule( | |
startActivityIntent: Intent | |
): AndroidComposeTestRule<ActivityScenarioRule<A>, A> = AndroidComposeTestRule( | |
activityRule = ActivityScenarioRule(startActivityIntent), | |
activityProvider = ::getActivityFromTestRule | |
) |
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
runAndroidComposeUiTest( | |
activityLauncher = { | |
ActivityScenario.launch<YourActivity>( | |
Intent(ApplicationProvider.getApplicationContext(), YourActivity::class.java) | |
.putExtra("key", "value"), | |
Bundle().apply { | |
putString("key", "value") | |
} | |
) | |
} | |
) { | |
// assert composables | |
} |
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
runAndroidComposeUiTest<YourActivity>( | |
startActivityIntent = Intent( | |
ApplicationProvider.getApplicationContext(), | |
YourActivity::class.java | |
).putExtra("key", "value") | |
) { | |
// assert composables | |
// example assertion below | |
// onNodeWithText("hello").assertExists().assertIsDisplayed() | |
} |
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
@get:Rule | |
val composeTestRule = createAndroidComposeRule<YourActivity>( | |
Intent(ApplicationProvider.getApplicationContext(), YourActivity::class.java) | |
.putExtra("key", "value") | |
) |
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
@ExperimentalTestApi | |
fun <A : ComponentActivity> runAndroidComposeUiTest( | |
activityLauncher: () -> ActivityScenario<A>, | |
effectContext: CoroutineContext = EmptyCoroutineContext, | |
block: AndroidComposeUiTest<A>.() -> Unit | |
) { | |
var scenario: ActivityScenario<A>? = null | |
val environment = AndroidComposeUiTestEnvironment(effectContext) { | |
requireNotNull(scenario) { | |
"ActivityScenario has not yet been launched, or has already finished. Make sure that " + | |
"any call to ComposeUiTest.setContent() and AndroidComposeUiTest.getActivity() " + | |
"is made within the lambda passed to AndroidComposeUiTestEnvironment.runTest()" | |
}.getActivity() | |
} | |
try { | |
environment.runTest { | |
scenario = activityLauncher() | |
block() | |
} | |
} finally { | |
scenario?.close() | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment