# AndroidComposeUiTestEnvironment

Artifact: [androidx.compose.ui:ui-test](https://developer.android.com/jetpack/androidx/releases/compose-ui) [View Source](https://cs.android.com/search?q=file:androidx/compose/ui/test/ComposeUiTest.android.kt+class:androidx.compose.ui.test.AndroidComposeUiTestEnvironment) Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.2.0) Common/AllAndroid/JVM
- android

  ```
  abstract class AndroidComposeUiTestEnvironment<A : ComponentActivity>
  ```

*** ** * ** ***

A test environment that can `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#runTest(kotlin.coroutines.SuspendFunction1)` using the `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#test()`. Note that some of the properties and methods on `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#test()` will only work during the call to `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#runTest(kotlin.coroutines.SuspendFunction1)`, as they require that the environment has been set up.

If the `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/ComposeUiTestConfig#effectContext()` contains a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-dispatcher/index.html`, that dispatcher will be used to run composition, and its `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-coroutine-scheduler/index.html` will be used to construct the `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/MainTestClock`. If the `effectContext` does not contain a `TestDispatcher`, a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/index.html` will be created for `androidx.compose.ui.test.v2.*` APIs; otherwise, an `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/index.html` will be created. In both cases, the `TestCoroutineScheduler` from the `effectContext` will be used if present.

| Parameters |
|---|---|
| `<A : https://developer.android.com/reference/kotlin/androidx/activity/ComponentActivity>` | The Activity type to be interacted with, which typically (but not necessarily) is the activity that was launched and hosts the Compose content. |

## Summary

| ### Public constructors |
|---|---|
| `<A : https://developer.android.com/reference/kotlin/androidx/activity/ComponentActivity> https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#AndroidComposeUiTestEnvironment()()` | android |
| `<A : https://developer.android.com/reference/kotlin/androidx/activity/ComponentActivity> https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#AndroidComposeUiTestEnvironment(androidx.compose.ui.test.ComposeUiTestConfig)( config: https://developer.android.com/reference/kotlin/androidx/compose/ui/test/ComposeUiTestConfig )` | android |
| `<A : https://developer.android.com/reference/kotlin/androidx/activity/ComponentActivity> https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#AndroidComposeUiTestEnvironment(kotlin.coroutines.CoroutineContext)( effectContext: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html )` A test environment that can `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#runTest(kotlin.coroutines.SuspendFunction1)` using the `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#test()`. | android |
| `<A : https://developer.android.com/reference/kotlin/androidx/activity/ComponentActivity> https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#AndroidComposeUiTestEnvironment(kotlin.coroutines.CoroutineContext,kotlin.coroutines.CoroutineContext,kotlin.time.Duration)( effectContext: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html, runTestContext: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html, testTimeout: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.time/-duration/index.html )` A test environment that can `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#runTest(kotlin.coroutines.SuspendFunction1)` using the `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#test()`. | android |

| ### Public functions |
|---|---|---|
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#cancelAndRecreateRecomposer()()` Recreates the CoroutineContext associated with Compose being cancelled. | android |
| `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html` | `<R : https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-any/index.html?> https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#runTest(kotlin.coroutines.SuspendFunction1)(block: suspend https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTest<A>.() -> R)` Runs the given `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#runTest(kotlin.coroutines.SuspendFunction1)`, setting up all test hooks before running the test and tearing them down after running the test. | android |

| ### Public properties |
|---|---|---|
| `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTest<A>` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#test()` The receiver scope of the test passed to `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#runTest(kotlin.coroutines.SuspendFunction1)`. | android |

| ### Protected properties |
|---|---|---|
| `abstract A?` | `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#activity()` Returns the current host activity of type `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment`. | android |

## Public constructors

### AndroidComposeUiTestEnvironment

android

```
<A : ComponentActivity> AndroidComposeUiTestEnvironment()
```

### AndroidComposeUiTestEnvironment

android

```
<A : ComponentActivity> AndroidComposeUiTestEnvironment(
    config: ComposeUiTestConfig
)
```

### AndroidComposeUiTestEnvironment

android

```
<A : ComponentActivity> AndroidComposeUiTestEnvironment(
    effectContext: CoroutineContext = EmptyCoroutineContext
)
```

A test environment that can `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#runTest(kotlin.coroutines.SuspendFunction1)` using the `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#test()`. Note that some of the properties and methods on `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#test()` will only work during the call to `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#runTest(kotlin.coroutines.SuspendFunction1)`, as they require that the environment has been set up.

If the `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/ComposeUiTestConfig#effectContext()` contains a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-dispatcher/index.html`, that dispatcher will be used to run composition, and its `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-coroutine-scheduler/index.html` will be used to construct the `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/MainTestClock`. If the `effectContext` does not contain a `TestDispatcher`, a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/index.html` will be created for `androidx.compose.ui.test.v2.*` APIs; otherwise, an `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/index.html` will be created. In both cases, the `TestCoroutineScheduler` from the `effectContext` will be used if present.

| Parameters |
|---|---|
| `effectContext: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html = EmptyCoroutineContext` | The `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` used to run the composition. The context for `LaunchedEffect`s and `rememberCoroutineScope` will be derived from this context. If this context contains a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-dispatcher/index.html` or `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-coroutine-scheduler/index.html` (in that order), it will be used for composition and the `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/MainTestClock`. |

### AndroidComposeUiTestEnvironment

android

```
<A : ComponentActivity> AndroidComposeUiTestEnvironment(
    effectContext: CoroutineContext = EmptyCoroutineContext,
    runTestContext: CoroutineContext = EmptyCoroutineContext,
    testTimeout: Duration = 60.seconds
)
```

A test environment that can `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#runTest(kotlin.coroutines.SuspendFunction1)` using the `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#test()`. Note that some of the properties and methods on `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#test()` will only work during the call to `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#runTest(kotlin.coroutines.SuspendFunction1)`, as they require that the environment has been set up.

If the `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#(kotlin.coroutines.CoroutineContext,kotlin.coroutines.CoroutineContext,kotlin.time.Duration)` contains a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-dispatcher/index.html`, that dispatcher will be used to run composition, and its `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-coroutine-scheduler/index.html` will be used to construct the `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/MainTestClock`. If the `effectContext` does not contain a `TestDispatcher`, a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/index.html` will be created for `androidx.compose.ui.test.v2.*` APIs; otherwise, an `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/index.html` will be created. In both cases, the `TestCoroutineScheduler` from the `effectContext` will be used if present.

| Parameters |
|---|---|
| `effectContext: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html = EmptyCoroutineContext` | The `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` used to run the composition. The context for `LaunchedEffect`s and `rememberCoroutineScope` will be derived from this context. If this context contains a `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-dispatcher/index.html` or `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-coroutine-scheduler/index.html` (in that order), it will be used for composition and the `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/MainTestClock`. |
| `runTestContext: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html = EmptyCoroutineContext` | The `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.coroutines/-coroutine-context/index.html` used to create the context to run the test. By default, it will run using `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/index.html`. `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#(kotlin.coroutines.CoroutineContext,kotlin.coroutines.CoroutineContext,kotlin.time.Duration)` and `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#(kotlin.coroutines.CoroutineContext,kotlin.coroutines.CoroutineContext,kotlin.time.Duration)` must not share `https://kotlinlang.org/api/kotlinx.coroutines/kotlinx-coroutines-test/kotlinx.coroutines.test/-test-coroutine-scheduler/index.html`. |
| `testTimeout: https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.time/-duration/index.html = 60.seconds` | The `https://kotlinlang.org/api/core/kotlin-stdlib/kotlin.time/-duration/index.html` within which the test is expected to complete, otherwise a platform specific timeout exception will be thrown. |

## Public functions

### cancelAndRecreateRecomposer

android

```
fun cancelAndRecreateRecomposer(): Unit
```

Recreates the CoroutineContext associated with Compose being cancelled. This happens when an app moves from a regular ("Full screen") view of the app to a "Pop up" view AND certain properties in the manifest's android:configChanges are set to prevent a full tear down of the app. This is a somewhat rare case (see issuetracker.google.com/issues/309326720 for more details).

It does this by canceling the existing Recomposer and creates a new Recomposer (including a new recomposer coroutine scope for that new Recomposer) and a new ComposeIdlingResource.

To see full test: click_viewAddedAndRemovedWithRecomposerCancelledAndRecreated_clickStillWorks

### runTest

android

```
fun <R : Any?> runTest(block: suspend AndroidComposeUiTest<A>.() -> R): Unit
```

Runs the given `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#runTest(kotlin.coroutines.SuspendFunction1)`, setting up all test hooks before running the test and tearing them down after running the test.

## Public properties

### test

android Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.2.0)

```
val test: AndroidComposeUiTest<A>
```

The receiver scope of the test passed to `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#runTest(kotlin.coroutines.SuspendFunction1)`. Note that some of the properties and methods will only work during the call to `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment#runTest(kotlin.coroutines.SuspendFunction1)`, as they require that the environment has been set up.

## Protected properties

### activity

android Added in [1.2.0](https://developer.android.com/jetpack/androidx/releases/compose-ui#1.2.0)

```
protected abstract val activity: A?
```

Returns the current host activity of type `https://developer.android.com/reference/kotlin/androidx/compose/ui/test/AndroidComposeUiTestEnvironment`. If no such activity is available, for example if you've navigated to a different activity and the original host has now been destroyed, this will return `null`.