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

AndroidComposeTestRule

class AndroidComposeTestRule<T : ComponentActivity> : ComposeTestRuleJUnit
kotlin.Any
   ↳ androidx.ui.test.AndroidComposeTestRule

Android specific implementation of ComposeTestRule.

Summary

Nested classes
inner

Public constructors
<init>(activityRule: ActivityScenarioRule<T>, disableTransitions: Boolean = false, disableBlinkingCursor: Boolean = true)

Android specific implementation of ComposeTestRule.

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

suspend Unit

Suspends until compose is idle.

T
runOnIdle(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)

Unit

Waits for compose to be idle.

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

Finds all semantics nodes with the given label as AccessibilityLabel.

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

Finds all semantics nodes identified by the given tag.

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

Finds all semantics nodes with the given text.

SemanticsNodeInteraction
ComposeTestRule.onNodeWithLabel(label: String, ignoreCase: Boolean = false, useUnmergedTree: Boolean = false)

Finds a semantics node with the given label as its accessibilityLabel.

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

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

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

Finds a semantics node identified by the given tag.

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

Finds a semantincs node with the given text.

SemanticsNodeInteraction
ComposeTestRule.onRoot(useUnmergedTree: Boolean = false)

Finds the root semantics node of the Compose tree.

Properties
ActivityScenarioRule<T>

AnimationClockTestRule

A test rule that allows you to control the animation clock

Density

Current device screen's density.

IntSize

Current device display's size.

Public constructors

<init>

AndroidComposeTestRule(
    activityRule: ActivityScenarioRule<T>,
    disableTransitions: Boolean = false,
    disableBlinkingCursor: Boolean = true)

Android specific implementation of ComposeTestRule.

Public methods

apply

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

awaitIdle

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.

runOnIdle

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

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.

waitForIdle

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

activityRule

val activityRule: ActivityScenarioRule<T>

clockTestRule

val clockTestRule: AnimationClockTestRule

A test rule that allows you to control the animation clock

density

val density: Density

Current device screen's density.

displaySize

val displaySize: IntSize

Current device display's size.