AndroidComposeUiTestEnvironment
@Deprecated(
message =
"Use `androidx.compose.ui.test.v2.AndroidComposeUiTestEnvironment` instead. The v2 APIs align with " +
"standard coroutine behavior by queuing tasks rather than executing them " +
"immediately. Tests relying on immediate execution may require explicit " +
"synchronization. Please refer to the migration guide for more details.",
level = DeprecationLevel.WARNING,
)
public fun <A : ComponentActivity> AndroidComposeUiTestEnvironment(
effectContext: CoroutineContext = EmptyCoroutineContext,
runTestContext: CoroutineContext = EmptyCoroutineContext,
testTimeout: Duration = 60.seconds,
activityProvider: () -> A?,
): AndroidComposeUiTestEnvironment<A>
Creates an AndroidComposeUiTestEnvironment that retrieves the host Activity by delegating to the given activityProvider. Use this if you need to launch an Activity in a way that is not compatible with any of the existing runComposeUiTest, runAndroidComposeUiTest, or runEmptyComposeUiTest methods.
Valid use cases include, but are not limited to, creating your own JUnit test rule that implements AndroidComposeUiTest by delegating to AndroidComposeUiTestEnvironment.test. See AndroidComposeTestRule for a reference implementation.
The activityProvider is called every time activity is called, which in turn is called when setContent is called.
The most common implementation of an activityProvider retrieves the activity from a backing ActivityScenario (that the caller launches within the lambda passed to runTest), but one is not limited to this pattern.
Parameters
| activityProvider | A lambda that should return the current Activity instance of type A, if it is available. If it is not available, it should return null. |
| A | The Activity type to be interacted with, which typically (but not necessarily) is the activity that was launched and hosts the Compose content. |
| effectContext | 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. |
| runTestContext | The CoroutineContext used to create the context to run the test. By default it will run using kotlinx.coroutines.test.StandardTestDispatcher. runTestContext and effectContext must not share TestCoroutineScheduler. |
| testTimeout | The Duration within which the test is expected to complete, otherwise a platform specific timeout exception will be thrown. |
AndroidComposeUiTestEnvironment
@Deprecated(
level = DeprecationLevel.WARNING,
message =
"Use AndroidComposeUiTestEnvironment(config, activityProvider) instead. " +
"The individual parameters `effectContext`, `runTestContext`, and `testTimeout` " +
"have been consolidated into [ComposeUiTestConfig] to allow for more flexible test " +
"environment configuration.\n" +
"Before:\n" +
"AndroidComposeUiTestEnvironment(effectContext, runTestContext, testTimeout, activityProvider)\n" +
"After:\n" +
"AndroidComposeUiTestEnvironment(ComposeUiTestConfig(effectContext, runTestContext, testTimeout), activityProvider)",
replaceWith =
ReplaceWith(
"AndroidComposeUiTestEnvironment(ComposeUiTestConfig(effectContext, runTestContext, testTimeout), activityProvider)"
),
)
public inline fun <A : ComponentActivity> AndroidComposeUiTestEnvironment(
effectContext: CoroutineContext = kotlin.coroutines.EmptyCoroutineContext,
runTestContext: CoroutineContext = kotlin.coroutines.EmptyCoroutineContext,
testTimeout: Duration = kotlin.time.Duration.parse("60s"),
crossinline activityProvider: () -> A?,
): androidx.compose.ui.test.AndroidComposeUiTestEnvironment<A>
Creates an AndroidComposeUiTestEnvironment that retrieves the host Activity by delegating to the given activityProvider. Use this if you need to launch an Activity in a way that is not compatible with any of the existing runComposeUiTest, runAndroidComposeUiTest, or runEmptyComposeUiTest methods.
This implementation uses kotlinx.coroutines.test.StandardTestDispatcher by default for running composition. 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.
Valid use cases include, but are not limited to, creating your own JUnit test rule that implements AndroidComposeUiTest by delegating to androidx.compose.ui.test.AndroidComposeUiTestEnvironment.test. See AndroidComposeTestRule for a reference implementation.
The activityProvider is called every time activity is called, which in turn is called when setContent is called.
The most common implementation of an activityProvider retrieves the activity from a backing ActivityScenario (that the caller launches within the lambda passed to runTest), but one is not limited to this pattern.
Parameters
| effectContext | 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. |
| runTestContext | The CoroutineContext used to create the context to run the test. By default it will run using kotlinx.coroutines.test.StandardTestDispatcher. runTestContext and effectContext must not share TestCoroutineScheduler. |
| testTimeout | The Duration within which the test is expected to complete, otherwise a platform specific timeout exception will be thrown. |
| activityProvider | A lambda that should return the current Activity instance of type A, if it is available. If it is not available, it should return null. |
| A | The Activity type to be interacted with, which typically (but not necessarily) is the activity that was launched and hosts the Compose content. |
AndroidComposeUiTestEnvironment
public inline fun <A : ComponentActivity> AndroidComposeUiTestEnvironment(
config: ComposeUiTestConfig,
crossinline activityProvider: () -> A?,
): androidx.compose.ui.test.AndroidComposeUiTestEnvironment<A>
Creates an AndroidComposeUiTestEnvironment that retrieves the host Activity by delegating to the given activityProvider. Use this if you need to launch an Activity in a way that is not compatible with any of the existing runComposeUiTest, runAndroidComposeUiTest, or runEmptyComposeUiTest methods.
Valid use cases include, but are not limited to, creating your own JUnit test rule that implements AndroidComposeUiTest by delegating to androidx.compose.ui.test.AndroidComposeUiTestEnvironment.test. See AndroidComposeTestRule for a reference implementation.
The activityProvider is called every time activity is called, which in turn is called when setContent is called.
The most common implementation of an activityProvider retrieves the activity from a backing ActivityScenario (that the caller launches within the lambda passed to runTest), but one is not limited to this pattern.
Parameters
| A | The Activity type to be interacted with, which typically (but not necessarily) is the activity that was launched and hosts the Compose content. |
| config | The ComposeUiTestConfig used to set up the test environment, providing control over the CoroutineContext used for composition, the test timeout, and other environment-specific settings. |
| activityProvider | A lambda that should return the current Activity instance of type A, if it is available. If it is not available, it should return null. |
AndroidComposeUiTestEnvironment
public fun <A : ComponentActivity> AndroidComposeUiTestEnvironment(
activityProvider: () -> A?
): androidx.compose.ui.test.AndroidComposeUiTestEnvironment<A>
Creates an AndroidComposeUiTestEnvironment that retrieves the host Activity by delegating to the given activityProvider. Use this if you need to launch an Activity in a way that is not compatible with any of the existing runComposeUiTest, runAndroidComposeUiTest, or runEmptyComposeUiTest methods.
Valid use cases include, but are not limited to, creating your own JUnit test rule that implements AndroidComposeUiTest by delegating to androidx.compose.ui.test.AndroidComposeUiTestEnvironment.test. See AndroidComposeTestRule for a reference implementation.
The activityProvider is called every time activity is called, which in turn is called when setContent is called.
The most common implementation of an activityProvider retrieves the activity from a backing ActivityScenario (that the caller launches within the lambda passed to runTest), but one is not limited to this pattern.
The default ComposeUiTestConfig sets the InputMode to Touch for each test. To configure the test to run with a different input mode (such as Keyboard) or customize other environment settings, use the overload that accepts a ComposeUiTestConfig.
Parameters
| activityProvider | A lambda that should return the current Activity instance of type A, if it is available. If it is not available, it should return null. |
| A | The Activity type to be interacted with, which typically (but not necessarily) is the activity that was launched and hosts the Compose content. |