AndroidComposeUiTestEnvironment

Class

Android
@ExperimentalTestApi
abstract class AndroidComposeUiTestEnvironment<A : ComponentActivity>(
    private val effectContext: CoroutineContext = EmptyCoroutineContext,
    private val runTestContext: CoroutineContext = EmptyCoroutineContext,
    private val testTimeout: Duration = 60.seconds,
)

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

AThe Activity type to be interacted with, which typically (but not necessarily) is the activity that was launched and hosts the Compose content.
effectContextThe 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.
runTestContextThe 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.
testTimeoutThe Duration within which the test is expected to complete, otherwise a platform specific timeout exception will be thrown.

Secondary Constructors

constructor(
    effectContext: CoroutineContext = EmptyCoroutineContext
) : this(effectContext, EmptyCoroutineContext, 60.seconds)

Properties

Android
val test: AndroidComposeUiTest<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.

Functions

fun cancelAndRecreateRecomposer()

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

fun <R> runTestNonSuspendingLambda(block: AndroidComposeUiTest<A>.() -> R?): R?
fun <R> runTest(block: suspend AndroidComposeUiTest<A>.() -> R): TestResult

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

Companion Object

Properties

Android
val TAG = "ComposeUiTest"