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

ComposeTestRule

interface ComposeTestRule : TestRule
androidx.ui.test.ComposeTestRule

Enables to run tests of individual composables without having to do manual setup. For Android tests see AndroidComposeTestRule. Normally this rule is obtained by using createComposeRule factory that provides proper implementation (depending if running host side or Android side).

However if you really need Android specific dependencies and don't want your test to be abstract you can still create AndroidComposeTestRule directly and access its underlying ActivityTestRule

Summary

Public methods

abstract Bitmap

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

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

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

abstract ComposeTestCaseSetup

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

abstract T
runOnIdleCompose(action: () -> T)

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

abstract T
runOnUiThread(action: () -> T)

Runs the given action on the UI thread.

abstract Unit
setContent(composable: () -> Unit)

Sets the given composable as a content of the current screen.

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

Inherited extension functions

From androidx.compose
operator T

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

Properties

abstract Density

Current device screen's density.

abstract DisplayMetrics

Public methods

captureScreenOnIdle

abstract 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

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

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

forGivenTestCase

abstract fun forGivenTestCase(testCase: ComposeTestCase): ComposeTestCaseSetup

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

runOnIdleCompose

abstract 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

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

Runs the given action on the UI thread.

This method is blocking until the action is complete.

setContent

abstract fun setContent(composable: () -> Unit): Unit

Sets the given composable as a content of the current screen.

Use this in your tests to setup the UI content to be tested. This should be called exactly once per test.

Exceptions
IllegalStateException if called more than once per test.

Properties

density

abstract val density: Density

Current device screen's density.

displayMetrics

abstract val displayMetrics: DisplayMetrics