/ComposeUiTest

A tool to launch an activity with intent in instrumented test and assert composables in the activity, solving the limitation of ComposeTestRule that does not accept an intent as an input for launching activities.

Primary LanguageKotlinDo What The F*ck You Want To Public LicenseWTFPL

ComposeUiTest

A tool to launch an activity with intent in instrumented tests and assert composables in the activity, solving the limitation of ComposeTestRule that does not accept an intent as an input for launching activities. Read the medium article to understand why this is essential.

Download

dependencies {
    androidTestImplementation("io.github.aungthiha:compose-ui-test:1.0.1")
}

Usage With Intent

runAndroidComposeUiTest uses ActivityScenario internally to launch an Activity

runAndroidComposeUiTest<YourActivity>(
    startActivityIntent = Intent(
        ApplicationProvider.getApplicationContext(),
        MainActivity::class.java
    ).putExtra("key", "value")
) {
    // assert composables
    // example assertion below
    // onNodeWithText("hello").assertExists().assertIsDisplayed()
}

Usage With ActivityScenario

You can also directly use ActivityScenario to launch an Activity the way you prefer

runAndroidComposeUiTest(
    activityLauncher = {
        ActivityScenario.launch<YourActivity>(
            Intent(ApplicationProvider.getApplicationContext(), YourActivity::class.java)
                .putExtra("key", "value"),
            Bundle().apply {
                putString("key", "value")
            }
        )
    }
) {
    // assert composables
}

Usage as TestRule

This is a good way if you want to launch the activity the same way for all your test cases. This createAndroidComposeRule function is written using the idea from Michal Moczulski

@get:Rule
val composeTestRule = createAndroidComposeRule<YourActivity>(
    Intent(ApplicationProvider.getApplicationContext(), MainActivity::class.java)
        .putExtra("key", "value")
)