AndroidComposeUiTestEnvironment


A test environment that can run tests using the test receiver scope. Note that some of the properties and methods on test will only work during the call to runTest, as they require that the environment has been set up.

If the effectContext contains a TestDispatcher, that dispatcher will be used to run composition, and its TestCoroutineScheduler will be used to construct the MainTestClock. If the effectContext does not contain a TestDispatcher, a StandardTestDispatcher will be created for androidx.compose.ui.test.v2.* APIs; otherwise, an UnconfinedTestDispatcher will be created. In both cases, the TestCoroutineScheduler from the effectContext will be used if present.

Parameters
<A : 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

android
android
<A : ComponentActivity> AndroidComposeUiTestEnvironment(
    effectContext: CoroutineContext,
    runTestContext: CoroutineContext,
    testTimeout: Duration
)
android

Public functions

Unit

Recreates the CoroutineContext associated with Compose being cancelled.

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

Runs the given block, setting up all test hooks before running the test and tearing them down after running the test.

android

Public properties

AndroidComposeUiTest<A>

The receiver scope of the test passed to runTest.

android

Protected properties

abstract A?

Returns the current host activity of type A.

android

Public constructors

AndroidComposeUiTestEnvironment

<A : ComponentActivity> AndroidComposeUiTestEnvironment()

AndroidComposeUiTestEnvironment

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

AndroidComposeUiTestEnvironment

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

Public functions

cancelAndRecreateRecomposer

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

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

Runs the given block, setting up all test hooks before running the test and tearing them down after running the test.

Public properties

test

val testAndroidComposeUiTest<A>

The receiver scope of the test passed to runTest. Note that some of the properties and methods will only work during the call to runTest, as they require that the environment has been set up.

Protected properties

activity

protected abstract val activity: A?

Returns the current host activity of type A. 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.