Google is committed to advancing racial equity for Black communities. See how.

ComposeTestRule

interface ComposeTestRule : TestRule, SemanticsNodeInteractionsProvider
androidx.compose.ui.test.junit4.ComposeTestRule

Enables to run tests of individual composables without having to do manual setup. For Android tests see createAndroidComposeRule. 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 createAndroidComposeRule directly and access its underlying Activity.

Summary

Public methods
abstract suspend Unit

Suspends until compose is idle.

abstract Unit

Registers an IdlingResource in this test.

abstract T
runOnIdle(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.

abstract Unit

Unregisters an IdlingResource from this test.

abstract Unit

Waits for compose to be idle.

Inherited functions
Inherited extension functions
From androidx.compose.ui.test
SemanticsNodeInteractionCollection
SemanticsNodeInteractionsProvider.onAllNodesWithContentDescription(label: String, ignoreCase: Boolean = false, useUnmergedTree: Boolean = false)

Finds all semantics nodes with the given label as ContentDescription.

SemanticsNodeInteractionCollection
SemanticsNodeInteractionsProvider.onAllNodesWithSubstring(text: String, ignoreCase: Boolean = false, useUnmergedTree: Boolean = false)

Finds all semantics nodes with text that contains the given substring.

SemanticsNodeInteractionCollection
SemanticsNodeInteractionsProvider.onAllNodesWithTag(testTag: String, useUnmergedTree: Boolean = false)

Finds all semantics nodes identified by the given tag.

SemanticsNodeInteractionCollection
SemanticsNodeInteractionsProvider.onAllNodesWithText(text: String, ignoreCase: Boolean = false, useUnmergedTree: Boolean = false)

Finds all semantics nodes with the given text.

SemanticsNodeInteraction
SemanticsNodeInteractionsProvider.onNodeWithContentDescription(label: String, ignoreCase: Boolean = false, useUnmergedTree: Boolean = false)

Finds a semantics node with the given contentDescription.

SemanticsNodeInteraction
SemanticsNodeInteractionsProvider.onNodeWithSubstring(text: String, ignoreCase: Boolean = false, useUnmergedTree: Boolean = false)

Finds a semantics node with text that contains the given substring.

SemanticsNodeInteraction
SemanticsNodeInteractionsProvider.onNodeWithTag(testTag: String, useUnmergedTree: Boolean = false)

Finds a semantics node identified by the given tag.

SemanticsNodeInteraction
SemanticsNodeInteractionsProvider.onNodeWithText(text: String, ignoreCase: Boolean = false, useUnmergedTree: Boolean = false)

Finds a semantincs node with the given text.

SemanticsNodeInteraction
SemanticsNodeInteractionsProvider.onRoot(useUnmergedTree: Boolean = false)

Finds the root semantics node of the Compose tree.

Properties
abstract AnimationClockTestRule

A test rule that allows you to control the animation clock

abstract Density

Current device screen's density.

abstract IntSize

Current device display's size.

Public methods

awaitIdle

abstract suspend fun awaitIdle(): Unit

Suspends until compose is idle. Compose is idle if there are no pending compositions, no pending changes that could lead to another composition, and no pending draw calls.

registerIdlingResource

abstract fun registerIdlingResource(idlingResource: IdlingResource): Unit

Registers an IdlingResource in this test.

runOnIdle

abstract fun <T> runOnIdle(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.

unregisterIdlingResource

abstract fun unregisterIdlingResource(idlingResource: IdlingResource): Unit

Unregisters an IdlingResource from this test.

waitForIdle

abstract fun waitForIdle(): Unit

Waits for compose to be idle.

This is a blocking call. Returns only after compose is idle.

Can crash in case there is a time out. This is not supposed to be handled as it surfaces only in incorrect tests.

Properties

clockTestRule

abstract val clockTestRule: AnimationClockTestRule

A test rule that allows you to control the animation clock

density

abstract val density: Density

Current device screen's density.

displaySize

abstract val displaySize: IntSize

Current device display's size.