androidx.compose.ui.test.junit4.v2

Top-level functions summary

AndroidComposeTestRule<R, A>
<R : TestRule, A : ComponentActivity> AndroidComposeTestRule(
    activityRule: R,
    effectContext: CoroutineContext,
    activityProvider: (R) -> A
)

Factory method to provide an implementation of AndroidComposeTestRule, where compose content is hosted by an Activity.

android
inline AndroidComposeTestRule<ActivityScenarioRule<A>, A>

Factory method to provide android specific implementation of createComposeRule, for a given activity class type A.

android
AndroidComposeTestRule<ActivityScenarioRule<A>, A>
<A : ComponentActivity> createAndroidComposeRule(
    activityClass: Class<A>,
    effectContext: CoroutineContext
)

Factory method to provide android specific implementation of createComposeRule, for a given activityClass.

android
ComposeContentTestRule

Factory method to provide an implementation of ComposeContentTestRule.

android
ComposeTestRule

Factory method to provide an implementation of ComposeTestRule that doesn't create a compose host for you in which you can set content.

android

Top-level functions

AndroidComposeTestRule

fun <R : TestRule, A : ComponentActivity> AndroidComposeTestRule(
    activityRule: R,
    effectContext: CoroutineContext = EmptyCoroutineContext,
    activityProvider: (R) -> A
): AndroidComposeTestRule<R, A>

Factory method to provide an implementation of AndroidComposeTestRule, where compose content is hosted by an Activity.

This API differs from the deprecated API by using kotlinx.coroutines.test.StandardTestDispatcher by default for running composition, instead of kotlinx.coroutines.test.UnconfinedTestDispatcher. This ensures that the test behavior is consistent with kotlinx.coroutines.test.runTest and provides explicit control over coroutine execution order. This means you may need to explicitly advance time or run current coroutines when testing complex coroutine logic, as tasks are queued on the scheduler rather than running eagerly.

The Activity is normally launched by the given activityRule before the test starts, but it is possible to pass a test rule that chooses to launch an Activity on a later time. The Activity is retrieved from the activityRule by means of the activityProvider, which can be thought of as a getter for the Activity on the activityRule. If you use an activityRule that launches an Activity on a later time, you should make sure that the Activity is launched by the time or while the activityProvider is called.

The AndroidComposeTestRule wraps around the given activityRule to make sure the Activity is launched after the AndroidComposeTestRule has completed all necessary steps to control and monitor the compose content.

Parameters
activityRule: R

Test rule to use to launch the Activity.

effectContext: CoroutineContext = EmptyCoroutineContext

The CoroutineContext used to run the composition. The context for LaunchedEffects and rememberCoroutineScope will be derived from this context. If this context contains a TestDispatcher or TestCoroutineScheduler (in that order), it will be used for composition and the MainTestClock.

activityProvider: (R) -> A

Function to retrieve the Activity from the given activityRule.

createAndroidComposeRule

inline fun <A : ComponentActivity> createAndroidComposeRule(
    effectContext: CoroutineContext = EmptyCoroutineContext
): AndroidComposeTestRule<ActivityScenarioRule<A>, A>

Factory method to provide android specific implementation of createComposeRule, for a given activity class type A.

This API differs from the deprecated API by using kotlinx.coroutines.test.StandardTestDispatcher by default for running composition, instead of kotlinx.coroutines.test.UnconfinedTestDispatcher. This ensures that the test behavior is consistent with kotlinx.coroutines.test.runTest and provides explicit control over coroutine execution order. This means you may need to explicitly advance time or run current coroutines when testing complex coroutine logic, as tasks are queued on the scheduler rather than running eagerly.

This method is useful for tests that require a custom Activity. This is usually the case for tests where the compose content is set by that Activity, instead of via the test rule's setContent. Make sure that you add the provided activity into your app's manifest file (usually in main/AndroidManifest.xml).

This creates a test rule that is using ActivityScenarioRule as the activity launcher. If you would like to use a different one you can create AndroidComposeTestRule directly and supply it with your own launcher.

If your test doesn't require a specific Activity, use createComposeRule instead.

Parameters
effectContext: CoroutineContext = EmptyCoroutineContext

The CoroutineContext used to run the composition. The context for LaunchedEffects and rememberCoroutineScope will be derived from this context. If this context contains a TestDispatcher, it is used for composition and the MainTestClock. Otherwise, a kotlinx.coroutines.test.StandardTestDispatcher is created and used. This new dispatcher will share the TestCoroutineScheduler from effectContext if one is present.

createAndroidComposeRule

fun <A : ComponentActivity> createAndroidComposeRule(
    activityClass: Class<A>,
    effectContext: CoroutineContext = EmptyCoroutineContext
): AndroidComposeTestRule<ActivityScenarioRule<A>, A>

Factory method to provide android specific implementation of createComposeRule, for a given activityClass.

This API differs from the deprecated API by using kotlinx.coroutines.test.StandardTestDispatcher by default for running composition, instead of kotlinx.coroutines.test.UnconfinedTestDispatcher. This ensures that the test behavior is consistent with kotlinx.coroutines.test.runTest and provides explicit control over coroutine execution order. This means you may need to explicitly advance time or run current coroutines when testing complex coroutine logic, as tasks are queued on the scheduler rather than running eagerly.

This method is useful for tests that require a custom Activity. This is usually the case for tests where the compose content is set by that Activity, instead of via the test rule's setContent. Make sure that you add the provided activity into your app's manifest file (usually in main/AndroidManifest.xml).

This creates a test rule that is using ActivityScenarioRule as the activity launcher. If you would like to use a different one you can create AndroidComposeTestRule directly and supply it with your own launcher.

If your test doesn't require a specific Activity, use createComposeRule instead.

Parameters
activityClass: Class<A>

The activity class to use in the activity scenario

effectContext: CoroutineContext = EmptyCoroutineContext

The CoroutineContext used to run the composition. The context for LaunchedEffects and rememberCoroutineScope will be derived from this context. If this context contains a TestDispatcher, it is used for composition and the MainTestClock. Otherwise, a kotlinx.coroutines.test.StandardTestDispatcher is created and used. This new dispatcher will share the TestCoroutineScheduler from effectContext if one is present.

createComposeRule

fun createComposeRule(effectContext: CoroutineContext): ComposeContentTestRule

Factory method to provide an implementation of ComposeContentTestRule.

This API differs from the deprecated API by using kotlinx.coroutines.test.StandardTestDispatcher by default for running composition, instead of kotlinx.coroutines.test.UnconfinedTestDispatcher. This ensures that the test behavior is consistent with kotlinx.coroutines.test.runTest and provides explicit control over coroutine execution order. This means you may need to explicitly advance time or run current coroutines when testing complex coroutine logic, as tasks are queued on the scheduler rather than running eagerly.

This method is useful for tests in compose libraries where it is irrelevant where the compose content is hosted (e.g. an Activity on Android). Such tests typically set compose content themselves via setContent and only instrument and assert that content.

For Android this will use the default Activity (android.app.Activity). You need to add a reference to this activity into the manifest file of the corresponding tests (usually in androidTest/AndroidManifest.xml). If your Android test requires a specific Activity to be launched, see createAndroidComposeRule.

Parameters
effectContext: CoroutineContext

The CoroutineContext used to run the composition. The context for LaunchedEffects and rememberCoroutineScope will be derived from this context. If this context contains a TestDispatcher, it is used for composition and the MainTestClock. Otherwise, a kotlinx.coroutines.test.StandardTestDispatcher is created and used. This new dispatcher will share the TestCoroutineScheduler from effectContext if one is present.

createEmptyComposeRule

fun createEmptyComposeRule(
    effectContext: CoroutineContext = EmptyCoroutineContext
): ComposeTestRule

Factory method to provide an implementation of ComposeTestRule that doesn't create a compose host for you in which you can set content.

This API differs from the deprecated API by using kotlinx.coroutines.test.StandardTestDispatcher by default for running composition, instead of kotlinx.coroutines.test.UnconfinedTestDispatcher. This ensures that the test behavior is consistent with kotlinx.coroutines.test.runTest and provides explicit control over coroutine execution order. This means you may need to explicitly advance time or run current coroutines when testing complex coroutine logic, as tasks are queued on the scheduler rather than running eagerly.

This method is useful for tests that need to create their own compose host during the test. The returned test rule will not create a host, and consequently does not provide a setContent method. To set content in tests using this rule, use the appropriate setContent methods from your compose host.

A typical use case on Android is when the test needs to launch an Activity (the compose host) after one or more dependencies have been injected.

Parameters
effectContext: CoroutineContext = EmptyCoroutineContext

The CoroutineContext used to run the composition. The context for LaunchedEffects and rememberCoroutineScope will be derived from this context. If this context contains a TestDispatcher, it is used for composition and the MainTestClock. Otherwise, a kotlinx.coroutines.test.StandardTestDispatcher is created and used. This new dispatcher will share the TestCoroutineScheduler from effectContext if one is present.