SubspaceSemanticsNodeInteraction

class SubspaceSemanticsNodeInteraction


Represents a semantics node and the path to fetch it from the subspace semantics tree.

Allows performing assertions or retrieving the underlying semantics node information. An instance of this class can be obtained from onSubspaceNode or convenience methods that use a specific filter, such as onSubspaceNodeWithTag.

For real usage patterns and explicit semantic properties validation, see the referenced samples.

import androidx.compose.material3.Button
import androidx.compose.material3.Text
import androidx.compose.ui.test.onNodeWithText
import androidx.compose.ui.test.performClick
import androidx.xr.compose.spatial.Subspace
import androidx.xr.compose.subspace.SpatialPanel
import androidx.xr.compose.subspace.layout.SubspaceModifier
import androidx.xr.compose.subspace.semantics.testTag
import androidx.xr.compose.testing.assertPositionInRootIsEqualTo
import androidx.xr.compose.testing.onSubspaceNodeWithTag

var count = 0

composeTestRule.setContent {
    Subspace {
        SpatialPanel(SubspaceModifier.testTag("spatialPanel")) {
            Button(onClick = { count++ }) { Text("Increment") }
        }
    }
}

// Assert subspace node existence, position, and dimensions in the Spatial hierarchy
composeTestRule
    .onSubspaceNodeWithTag("spatialPanel")
    .assertExists()
    .assertPositionInRootIsEqualTo(0.toDp(), 0.toDp(), 0.toDp())

// Interact with the 2D Compose node nested within the Spatial container
composeTestRule.onNodeWithText("Increment").performClick()

composeTestRule.waitForIdle()

// Verify outcomes
assert(count == 1)
import androidx.compose.ui.unit.dp
import androidx.xr.compose.spatial.Subspace
import androidx.xr.compose.subspace.SpatialPanel
import androidx.xr.compose.subspace.layout.SubspaceModifier
import androidx.xr.compose.subspace.layout.height
import androidx.xr.compose.subspace.layout.width
import androidx.xr.compose.subspace.semantics.testTag
import androidx.xr.compose.testing.assertHeightIsEqualTo
import androidx.xr.compose.testing.assertPositionInRootIsEqualTo
import androidx.xr.compose.testing.assertWidthIsEqualTo
import androidx.xr.compose.testing.onSubspaceNodeWithTag

composeTestRule.setContent {
    Subspace {
        SpatialPanel(SubspaceModifier.width(100.dp).height(100.dp).testTag("myPanel")) {}
    }
}

// Check existence and exact spatial dimensions in DP using semantic matchers
composeTestRule
    .onSubspaceNodeWithTag("myPanel")
    .assertExists()
    .assertPositionInRootIsEqualTo(0.dp, 0.dp, 0.dp)
    .assertWidthIsEqualTo(100.toDp())
    .assertHeightIsEqualTo(100.toDp())

Summary

Public constructors

Public functions

Unit

Asserts that no matching item was found or that the item is no longer in the hierarchy.

SubspaceSemanticsNodeInteraction

Asserts that the component was found and is part of the component tree.

SubspaceSemanticsInfo
fetchSemanticsNode(errorMessageOnFail: String?)

Returns the semantics node captured by this interaction.

Extension functions

SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has depth that is greater than or equal to expectedMinDepth.

SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has depth equal to expectedDepth.

SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has depth that is NOT equal to expectedDepth.

SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has height that is greater than or equal to expectedMinHeight.

SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has height equal to expectedHeight.

SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has height that is NOT equal to expectedHeight.

SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has the left position in the root composable that is equal to the given position.

SubspaceSemanticsNodeInteraction
@CanIgnoreReturnValue
SubspaceSemanticsNodeInteraction.assertPositionInRootIsEqualTo(
    expectedX: Dp,
    expectedY: Dp,
    expectedZ: Dp
)

Asserts that the layout of this node has position in the root composable that is equal to the given position.

SubspaceSemanticsNodeInteraction
@CanIgnoreReturnValue
SubspaceSemanticsNodeInteraction.assertPositionIsEqualTo(
    expectedX: Dp,
    expectedY: Dp,
    expectedZ: Dp
)

Asserts that the layout of this node has position that is equal to the given position.

SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has rotation in the root composable that is equal to the given rotation.

SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has rotation that is equal to the given rotation.

SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has the top position in the root composable that is equal to the given position.

SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has width that is greater than or equal to expectedMinWidth.

SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has width equal to expectedWidth.

SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has width that is NOT equal to expectedWidth.

SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has the x position in the root composable that is equal to the given position.

SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has the x position that is equal to the given position.

SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has the y position in the root composable that is equal to the given position.

SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has the y position that is equal to the given position.

SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has the z position in the root composable that is equal to the given position.

SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has the z position that is equal to the given position.

Vector3

Returns the position of the node relative to its parent layout node.

Vector3

Returns the position of the node relative to the root node.

Quaternion

Returns the rotation of the node relative to its parent layout node.

Quaternion

Returns the rotation of the node relative to the root node.

DpVolumeSize

Returns the size of the node.

Public constructors

SubspaceSemanticsNodeInteraction

Added in 1.0.0-alpha15
SubspaceSemanticsNodeInteraction(
    testContext: SubspaceTestContext,
    matcher: SubspaceSemanticsMatcher
)

Public functions

assertDoesNotExist

Added in 1.0.0-alpha15
fun assertDoesNotExist(): Unit

Asserts that no matching item was found or that the item is no longer in the hierarchy.

This operation synchronizes with the UI and fetches all nodes to ensure it has the latest data. It is useful for verifying that an element has been correctly removed or was never present.

Throws
AssertionError

if one or more matching nodes are found.

assertExists

Added in 1.0.0-alpha15
@CanIgnoreReturnValue
fun assertExists(errorMessageOnFail: String? = null): SubspaceSemanticsNodeInteraction

Asserts that the component was found and is part of the component tree.

This operation synchronizes with the UI and fetches all nodes to ensure it has the latest data. Note that if you are already calling fetchSemanticsNode, calling this method is redundant and introduces unnecessary overhead.

Parameters
errorMessageOnFail: String? = null

the prefix to be added to the error message if the assertion fails. Typically used by operations that rely on this assertion.

Returns
SubspaceSemanticsNodeInteraction

this interaction object to allow chaining of further assertions.

Throws
AssertionError

if zero or multiple matching nodes are found.

fetchSemanticsNode

Added in 1.0.0-alpha15
fun fetchSemanticsNode(errorMessageOnFail: String? = null): SubspaceSemanticsInfo

Returns the semantics node captured by this interaction.

This operation synchronizes with the UI to ensure the latest semantics tree data is fetched. Since synchronization introduces performance overhead, it is recommended to cache the result if accessed multiple times within a single atomic operation.

Parameters
errorMessageOnFail: String? = null

the prefix to be added to the error message if the fetch operation fails. Typically used by higher-level operations that rely on this fetch.

Returns
SubspaceSemanticsInfo

the underlying SubspaceSemanticsInfo corresponding to the matched node.

Throws
AssertionError

if zero or multiple matching nodes are found.

Extension functions

SubspaceSemanticsNodeInteraction.assertDepthIsAtLeast

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertDepthIsAtLeast(
    expectedMinDepth: Dp
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has depth that is greater than or equal to expectedMinDepth.

Parameters
expectedMinDepth: Dp

The minimum depth to assert.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.assertDepthIsEqualTo

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertDepthIsEqualTo(
    expectedDepth: Dp
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has depth equal to expectedDepth.

Parameters
expectedDepth: Dp

The depth to assert.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.assertDepthIsNotEqualTo

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertDepthIsNotEqualTo(
    expectedDepth: Dp
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has depth that is NOT equal to expectedDepth.

Parameters
expectedDepth: Dp

The depth to assert.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.assertHeightIsAtLeast

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertHeightIsAtLeast(
    expectedMinHeight: Dp
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has height that is greater than or equal to expectedMinHeight.

Parameters
expectedMinHeight: Dp

The minimum height to assert.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.assertHeightIsEqualTo

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertHeightIsEqualTo(
    expectedHeight: Dp
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has height equal to expectedHeight.

import androidx.compose.ui.unit.dp
import androidx.xr.compose.spatial.Subspace
import androidx.xr.compose.subspace.SpatialPanel
import androidx.xr.compose.subspace.layout.SubspaceModifier
import androidx.xr.compose.subspace.layout.height
import androidx.xr.compose.subspace.layout.width
import androidx.xr.compose.subspace.semantics.testTag
import androidx.xr.compose.testing.assertHeightIsEqualTo
import androidx.xr.compose.testing.assertPositionInRootIsEqualTo
import androidx.xr.compose.testing.assertWidthIsEqualTo
import androidx.xr.compose.testing.onSubspaceNodeWithTag

composeTestRule.setContent {
    Subspace {
        SpatialPanel(SubspaceModifier.width(100.dp).height(100.dp).testTag("myPanel")) {}
    }
}

// Check existence and exact spatial dimensions in DP using semantic matchers
composeTestRule
    .onSubspaceNodeWithTag("myPanel")
    .assertExists()
    .assertPositionInRootIsEqualTo(0.dp, 0.dp, 0.dp)
    .assertWidthIsEqualTo(100.toDp())
    .assertHeightIsEqualTo(100.toDp())
Parameters
expectedHeight: Dp

The height to assert.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.assertHeightIsNotEqualTo

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertHeightIsNotEqualTo(
    expectedHeight: Dp
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has height that is NOT equal to expectedHeight.

Parameters
expectedHeight: Dp

The height to assert.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.assertLeftPositionInRootIsEqualTo

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertLeftPositionInRootIsEqualTo(
    expectedLeft: Dp
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has the left position in the root composable that is equal to the given position.

Parameters
expectedLeft: Dp

The left position to assert.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.assertPositionInRootIsEqualTo

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertPositionInRootIsEqualTo(
    expectedX: Dp,
    expectedY: Dp,
    expectedZ: Dp
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has position in the root composable that is equal to the given position.

Parameters
expectedX: Dp

The x position to assert.

expectedY: Dp

The y position to assert.

expectedZ: Dp

The z position to assert.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.assertPositionIsEqualTo

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertPositionIsEqualTo(
    expectedX: Dp,
    expectedY: Dp,
    expectedZ: Dp
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has position that is equal to the given position.

Parameters
expectedX: Dp

The x position to assert.

expectedY: Dp

The y position to assert.

expectedZ: Dp

The z position to assert.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.assertRotationInRootIsEqualTo

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertRotationInRootIsEqualTo(
    expected: Quaternion,
    tolerance: Float = 0.01f
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has rotation in the root composable that is equal to the given rotation.

This assertion uses a tolerance to account for floating-point inaccuracies.

Parameters
expected: Quaternion

The expected rotation in the root space to assert.

tolerance: Float = 0.01f

The maximum allowed angular distance in degrees between the orientations.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.assertRotationIsEqualTo

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertRotationIsEqualTo(
    expected: Quaternion,
    tolerance: Float = 0.01f
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has rotation that is equal to the given rotation.

This assertion uses a tolerance to account for floating-point inaccuracies.

Parameters
expected: Quaternion

The rotation to assert.

tolerance: Float = 0.01f

The maximum allowed angular distance in degrees between the orientations.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.assertTopPositionInRootIsEqualTo

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertTopPositionInRootIsEqualTo(
    expectedTop: Dp
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has the top position in the root composable that is equal to the given position.

Parameters
expectedTop: Dp

The top position to assert.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.assertWidthIsAtLeast

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertWidthIsAtLeast(
    expectedMinWidth: Dp
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has width that is greater than or equal to expectedMinWidth.

Parameters
expectedMinWidth: Dp

The minimum width to assert.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.assertWidthIsEqualTo

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertWidthIsEqualTo(
    expectedWidth: Dp
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has width equal to expectedWidth.

import androidx.compose.ui.unit.dp
import androidx.xr.compose.spatial.Subspace
import androidx.xr.compose.subspace.SpatialPanel
import androidx.xr.compose.subspace.layout.SubspaceModifier
import androidx.xr.compose.subspace.layout.height
import androidx.xr.compose.subspace.layout.width
import androidx.xr.compose.subspace.semantics.testTag
import androidx.xr.compose.testing.assertHeightIsEqualTo
import androidx.xr.compose.testing.assertPositionInRootIsEqualTo
import androidx.xr.compose.testing.assertWidthIsEqualTo
import androidx.xr.compose.testing.onSubspaceNodeWithTag

composeTestRule.setContent {
    Subspace {
        SpatialPanel(SubspaceModifier.width(100.dp).height(100.dp).testTag("myPanel")) {}
    }
}

// Check existence and exact spatial dimensions in DP using semantic matchers
composeTestRule
    .onSubspaceNodeWithTag("myPanel")
    .assertExists()
    .assertPositionInRootIsEqualTo(0.dp, 0.dp, 0.dp)
    .assertWidthIsEqualTo(100.toDp())
    .assertHeightIsEqualTo(100.toDp())
Parameters
expectedWidth: Dp

The width to assert.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.assertWidthIsNotEqualTo

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertWidthIsNotEqualTo(
    expectedWidth: Dp
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has width that is NOT equal to expectedWidth.

Parameters
expectedWidth: Dp

The width to assert.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.assertXPositionInRootIsEqualTo

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertXPositionInRootIsEqualTo(
    expectedX: Dp
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has the x position in the root composable that is equal to the given position.

Parameters
expectedX: Dp

The x position to assert.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.assertXPositionIsEqualTo

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertXPositionIsEqualTo(
    expectedX: Dp
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has the x position that is equal to the given position.

Parameters
expectedX: Dp

The x position to assert.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.assertYPositionInRootIsEqualTo

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertYPositionInRootIsEqualTo(
    expectedY: Dp
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has the y position in the root composable that is equal to the given position.

Parameters
expectedY: Dp

The y position to assert.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.assertYPositionIsEqualTo

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertYPositionIsEqualTo(
    expectedY: Dp
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has the y position that is equal to the given position.

Parameters
expectedY: Dp

The y position to assert.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.assertZPositionInRootIsEqualTo

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertZPositionInRootIsEqualTo(
    expectedZ: Dp
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has the z position in the root composable that is equal to the given position.

Parameters
expectedZ: Dp

The z position to assert.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.assertZPositionIsEqualTo

@CanIgnoreReturnValue
fun SubspaceSemanticsNodeInteraction.assertZPositionIsEqualTo(
    expectedZ: Dp
): SubspaceSemanticsNodeInteraction

Asserts that the layout of this node has the z position that is equal to the given position.

Parameters
expectedZ: Dp

The z position to assert.

Throws
AssertionError

if comparison fails.

SubspaceSemanticsNodeInteraction.getPosition

fun SubspaceSemanticsNodeInteraction.getPosition(): Vector3

Returns the position of the node relative to its parent layout node.

Additional assertions with custom tolerances may be performed on the individual values.

SubspaceSemanticsNodeInteraction.getPositionInRoot

fun SubspaceSemanticsNodeInteraction.getPositionInRoot(): Vector3

Returns the position of the node relative to the root node.

Additional assertions with custom tolerances may be performed on the individual values.

SubspaceSemanticsNodeInteraction.getRotation

fun SubspaceSemanticsNodeInteraction.getRotation(): Quaternion

Returns the rotation of the node relative to its parent layout node.

Additional assertions with custom tolerances may be performed on the individual values.

SubspaceSemanticsNodeInteraction.getRotationInRoot

fun SubspaceSemanticsNodeInteraction.getRotationInRoot(): Quaternion

Returns the rotation of the node relative to the root node.

Additional assertions with custom tolerances may be performed on the individual values.

SubspaceSemanticsNodeInteraction.getSize

fun SubspaceSemanticsNodeInteraction.getSize(): DpVolumeSize

Returns the size of the node.

Additional assertions with custom tolerances may be performed on the individual values.