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

AndroidComposeTestRule

class AndroidComposeTestRule<R : TestRule, A : ComponentActivity> : ComposeTestRule
kotlin.Any
   ↳ androidx.compose.ui.test.junit4.AndroidComposeTestRule

Android specific implementation of ComposeTestRule.

This rule wraps around the given activityRule, which is responsible for launching the activity. The activityProvider should return the launched activity instance when the activityRule is passed to it. In this way, you can provide any test rule that can launch an activity

Summary

Nested classes
inner

Public constructors
<init>(activityRule: R, activityProvider: (R) -> A)

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

suspend Unit

Suspends until compose is idle.

SemanticsNodeInteractionCollection
onAllNodes(matcher: SemanticsMatcher, useUnmergedTree: Boolean)

Finds all semantics nodes that match the given condition.

SemanticsNodeInteraction
onNode(matcher: SemanticsMatcher, useUnmergedTree: Boolean)

Finds a semantics node that matches the given condition.

Unit

Registers an IdlingResource in this test.

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

Unregisters an IdlingResource from this test.

Unit

Waits for compose to be idle.

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
R

Test rule to use to launch the activity.

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: R,
    activityProvider: (R) -> A)

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.

onAllNodes

fun onAllNodes(
    matcher: SemanticsMatcher,
    useUnmergedTree: Boolean
): SemanticsNodeInteractionCollection

Finds all semantics nodes that match the given condition.

If you are working with elements that are not supposed to occur multiple times use onNode instead.

For usage patterns and semantics concepts see SemanticsNodeInteraction

Parameters
useUnmergedTree: Boolean Find within merged composables like Buttons.

See Also

onNode

fun onNode(
    matcher: SemanticsMatcher,
    useUnmergedTree: Boolean
): SemanticsNodeInteraction

Finds a semantics node that matches the given condition.

Any subsequent operation on its result will expect exactly one element found (unless SemanticsNodeInteraction.assertDoesNotExist is used) and will throw AssertionError if none or more than one element is found.

For usage patterns and semantics concepts see SemanticsNodeInteraction

Parameters
useUnmergedTree: Boolean Find within merged composables like Buttons.

See Also

registerIdlingResource

fun registerIdlingResource(idlingResource: IdlingResource): Unit

Registers an IdlingResource in this test.

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.