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 on and its TestCoroutineScheduler will be used to construct the MainTestClock. If the effectContext does not contain a TestDispatcher, an UnconfinedTestDispatcher will be created, using the TestCoroutineScheduler from the effectContext 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

Public functions

Unit

Recreates the CoroutineContext associated with Compose being cancelled.

android
R
<R : Any?> runTest(block: 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(
    effectContext: CoroutineContext = EmptyCoroutineContext
)
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.

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.

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: AndroidComposeUiTest<A>.() -> R): R

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.