---
title: "AndroidComposeUiTestEnvironment"
description: "Creates an [AndroidComposeUiTestEnvironment] that retrieves the
[host Activity][AndroidComposeUiTest.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][androidx.compose.ui.test.junit4.AndroidComposeTestRule] for a reference
implementation.

The [activityProvider] is called every time [activity][AndroidComposeUiTest.activity] is called,
which in turn is called when [setContent][ComposeUiTest.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."
type: "function"
---

<div class='type'>Function</div>


<a id='references'></a>
<div class='sourceset sourceset-android'>Android</div>


> **Deprecated** 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.

```kotlin
@ExperimentalTestApi
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 `LaunchedEffect`s 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. |




<div class='sourceset sourceset-android'>Android</div>


```kotlin
@ExperimentalTestApi
inline fun <A : ComponentActivity> AndroidComposeUiTestEnvironment(
    effectContext: CoroutineContext = EmptyCoroutineContext,
    runTestContext: CoroutineContext = EmptyCoroutineContext,
    testTimeout: Duration = 60.seconds,
    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

| | |
| --- | --- |
| 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 `LaunchedEffect`s 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. |




