The Developer Preview for Android 11 is now available; test it out and share your feedback.

AndroidComposeTestRule

class AndroidComposeTestRule<T : Activity> : ComposeTestRule
kotlin.Any
   ↳ androidx.ui.test.android.AndroidComposeTestRule

Android specific implementation of ComposeTestRule.

Summary

Nested classes

inner

Public constructors

<init>(activityClass: Class<T>, disableTransitions: Boolean = false)

Android specific implementation of ComposeTestRule.

Public methods

Statement
apply(base: Statement, description: Description?)

Bitmap

Takes screenshot of the Activity's window after Compose UI gets idle.

ComposeTestCaseSetup
forGivenContent(composable: () -> Unit)

Takes the given content and prepares it for execution-controlled test via ComposeTestCaseSetup.

ComposeTestCaseSetup

Takes the given test case and prepares it for execution-controlled test via ComposeTestCaseSetup.

T
runOnIdleCompose(action: () -> T)

Executes the given action in the same way as runOnUiThread but also makes sure Compose is idle before executing it.

T
runOnUiThread(action: () -> T)

Runs the given action on the UI thread.

Unit
setContent(composable: () -> Unit)

Inherited extension functions

From androidx.ui.test
CollectedSizes
ComposeTestRule.setContentAndCollectSizes(parentConstraints: DpConstraints = BigTestConstraints, children: () -> Unit)

Set content as with ComposeTestRule.setContent, but return CollectedSizes to assert width and height of this content

PxSize
ComposeTestRule.setContentAndGetPixelSize(parentConstraints: DpConstraints = BigTestConstraints, performSetContent: (() -> Unit) -> Unit = { setContent(it) }, children: () -> Unit)

Set content as with ComposeTestRule.setContent, but return sizes of this content

From androidx.compose
operator T

IMPORTANT: This global operator is TEMPORARY, and should be removed whenever an answer for contextual composers is reached.

Properties

ActivityTestRule<T>

Density

Current device screen's density.

DisplayMetrics

Public constructors

<init>

AndroidComposeTestRule(
    activityClass: Class<T>,
    disableTransitions: Boolean = false)

Android specific implementation of ComposeTestRule.

Public methods

apply

fun apply(
    base: Statement,
    description: Description?
): Statement

captureScreenOnIdle

fun captureScreenOnIdle(): Bitmap

Takes screenshot of the Activity's window after Compose UI gets idle.

This function blocks until complete.

Note that this does not capture the full device screen as it has access only to the test Activity's window surface. The test Activity is the one that hosts the initial composition created via setContent. If there are windows on top of the Activity's window these won't be included. It will also not include any other Activities started afterwards.

You can also use SemanticsNodeInteraction.captureToBitmap to capture individual components. That one does not require any specific Activity.

forGivenContent

fun forGivenContent(composable: () -> Unit): ComposeTestCaseSetup

Takes the given content and prepares it for execution-controlled test via ComposeTestCaseSetup.

forGivenTestCase

fun forGivenTestCase(testCase: ComposeTestCase): ComposeTestCaseSetup

Takes the given test case and prepares it for execution-controlled test via ComposeTestCaseSetup.

runOnIdleCompose

fun <T> runOnIdleCompose(action: () -> T): T

Executes the given action in the same way as runOnUiThread but also makes sure Compose is idle before executing it. This is great place for doing your assertions on shared variables.

This method is blocking until the action is complete.

runOnUiThread

fun <T> runOnUiThread(action: () -> T): T

Runs the given action on the UI thread.

This method is blocking until the action is complete.

setContent

fun setContent(composable: () -> Unit): Unit
Exceptions
IllegalStateException if called more than once per test.

Properties

activityTestRule

val activityTestRule: ActivityTestRule<T>

density

val density: Density

Current device screen's density.

displayMetrics

val displayMetrics: DisplayMetrics