Loading...
Loading...
Use this skill to verify Compose layout measurements from a UI test using `assertWidthIsEqualTo`, `assertHeightIsEqualTo`, `assertWidthIsAtLeast`, `assertHeightIsAtLeast`, `assertTouchWidthIsEqualTo`, `assertTouchHeightIsEqualTo`, `assertPositionInRootIsEqualTo`, `assertTopPositionInRootIsEqualTo`, `assertLeftPositionInRootIsEqualTo`, plus read helpers `getUnclippedBoundsInRoot`, `getBoundsInRoot`, `getAlignmentLinePosition`, `getFirstLinkBounds`, and the underlying `Dp.assertIsEqualTo(expected, subject, tolerance = Dp(.5f))`. Covers the half-dp default tolerance, the unclipped vs clipped distinction, the canonical "compute padding from two unclipped rects" pattern, and minimum-touch-target assertions like `assertHeightIsAtLeast(MinHeight + 1.dp)`. Use when the developer wants to assert sizes, padding, alignment, position in dp, or asks about `getUnclippedBoundsInRoot`, `DpRect`, touch-target size, or compares widths in pixels. If the developer is comparing layout dimensions from a test, use this skill.
npx skill4agent add skydoves/android-testing-skills asserting-bounds-and-dimensionsgetUnclippedBoundsInRoot()material3/ButtonTest.kt(24.dp, 12.dp)ChipDefaults.MinHeight + 1.dpassertWidthIsEqualTogetUnclippedBoundsInRootDpRectgetAlignmentLinePositiongetFirstLinkBounds./asserting-node-state-and-text/SKILL.mdassertIsDisplayed()../../synchronization/testing-animations-deterministically/SKILL.mdComposeTestRuleComposeUiTest../../setup/configuring-test-dependencies/SKILL.md../../synchronization/controlling-the-test-clock/SKILL.mdtouchBoundsInRootcommonMain/.../BoundsAssertions.kt| Question | API | File:line |
|---|---|---|
| Is the layout exactly W dp wide? | | |
| Is the layout exactly H dp tall? | | |
| At least W wide? | | |
| At least H tall? | | |
| Touch-target width? | | |
| Touch-target height? | | |
| Exact position in root? | | |
| Top position only? | | |
| Left position only? | | |
| Read full unclipped bounds | | |
| Read clipped bounds | | |
| Alignment line in dp | | |
Bounds of a | | |
Compare any two | | |
getUnclippedBoundsInRoot()BoundsAssertions.kt:148-152unclippedBoundsInRootBoundsAssertions.kt:284-291getBoundsInRoot()BoundsAssertions.kt:158-165Dp.assertIsEqualTo(expected, subject, tolerance = Dp(.5f))BoundsAssertions.kt:319Dp.assertIsEqualTomaterial3/.../ButtonTest.kt:213-225val buttonBounds = rule.onNodeWithTag(ButtonTestTag).getUnclippedBoundsInRoot()
val textBounds = rule.onNodeWithTag(TextTestTag).getUnclippedBoundsInRoot()
(textBounds.left - buttonBounds.left).assertIsEqualTo(
24.dp,
"padding between the start of the button and the start of the text.",
)
(buttonBounds.right - textBounds.right).assertIsEqualTo(
24.dp,
"padding between the end of the text and the end of the button.",
)
buttonBounds.height.assertIsEqualTo(ButtonDefaults.MinHeight, "height of button.")subject"Actual padding between the start of the button and the start of the text. is 22.dp, expected 24.dp (tolerance: .5.dp)"assertHeightIsAtLeastassertWidthIsAtLeastmaterial/ChipTest.kt:226-233rule.setMaterialContent { Chip(onClick = {}) { Text(text = "Test chip", fontSize = 50.sp) } }
rule.onNode(hasClickAction()).assertHeightIsAtLeast(ChipDefaults.MinHeight + 1.dp)assertTouchWidthIsEqualToassertTouchHeightIsEqualTonode.touchBoundsInRootBoundsAssertions.kt:270-282Modifier.minimumInteractiveComponentSize()getUnclippedBoundsInRoot// WRONG
@Test
fun submit_isMin48dpTall() {
rule.setContent { CheckoutScreen() }
val node = rule.onNodeWithTag(SubmitTag).fetchSemanticsNode()
val heightPx = node.size.height
assert(heightPx >= 48 * Resources.getSystem().displayMetrics.density)
}
// WRONG because: pixel-typed and density-dependent. Reads outside the framework's tolerance
// model. Failure prints raw integers, no node dump, no subject label.// RIGHT
@Test
fun submit_isMin48dpTall() {
rule.setContent { CheckoutScreen() }
rule.onNodeWithTag(SubmitTag).assertHeightIsAtLeast(48.dp)
}@Test
fun button_text_has24dpPadding() {
rule.setContent {
Button(onClick = {}, modifier = Modifier.testTag(ButtonTestTag)) {
Text("Submit", modifier = Modifier.testTag(TextTestTag).semantics(mergeDescendants = true) {})
}
}
val buttonBounds = rule.onNodeWithTag(ButtonTestTag).getUnclippedBoundsInRoot()
val textBounds = rule.onNodeWithTag(TextTestTag).getUnclippedBoundsInRoot()
(textBounds.left - buttonBounds.left).assertIsEqualTo(24.dp, "start padding")
(buttonBounds.right - textBounds.right).assertIsEqualTo(24.dp, "end padding")
}Textmaterial3/ButtonTest.kt:202-226assertPositionInRootIsEqualTo// "the close button sits at (320.dp, 0.dp) in the root"
rule.onNodeWithTag(CloseButtonTag)
.assertPositionInRootIsEqualTo(expectedLeft = 320.dp, expectedTop = 0.dp)assertLeftPositionInRootIsEqualToassertTopPositionInRootIsEqualToval baselineDp = rule.onNodeWithTag(LabelTag)
.getAlignmentLinePosition(FirstBaseline)
require(!baselineDp.isUnspecified) { "Label has no first baseline" }
baselineDp.assertIsEqualTo(20.dp, "first baseline of label")getAlignmentLinePositionDp.UnspecifiedBoundsAssertions.kt:172-179isUnspecified// Most tests want the default ½ dp tolerance:
rule.onNodeWithTag(IconTag).getUnclippedBoundsInRoot().width.assertIsEqualTo(24.dp, "icon width")
// Stricter tolerance when measuring a hand-aligned constant:
val width = rule.onNodeWithTag(IconTag).getUnclippedBoundsInRoot().width
width.assertIsEqualTo(expected = 24.dp, subject = "icon width", tolerance = 0.1.dp)val unclipped = rule.onNodeWithTag(BannerTag).getUnclippedBoundsInRoot()
val clipped = rule.onNodeWithTag(BannerTag).getBoundsInRoot()
// Banner laid out 200 dp tall but only 80 dp visible (rest clipped by parent):
unclipped.height.assertIsEqualTo(200.dp, "banner intrinsic height")
clipped.height.assertIsEqualTo(80.dp, "banner visible height")assertIsDisplayed()./asserting-node-state-and-text/SKILL.mdassertWidthIsEqualToassertHeightIsEqualToassertPositionInRootIsEqualTofetchSemanticsNode().size.widthgetUnclippedBoundsInRoot()getBoundsInRoot()subjectDp.assertIsEqualToassertHeightIsAtLeast(MinHeight + 1.dp)assertHeightIsEqualTo(MinHeight + N.dp)mainClock.autoAdvance = falsenode.size.widthnode.size.heightassert*IsEqualToDp.assertIsEqualTogetUnclippedBoundsInRootDp.assertIsEqualTo(...)subjectassertTouchWidthIsEqualToassertTouchHeightIsEqualTo./gradlew :app:connectedDebugAndroidTestsubjectcompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/BoundsAssertions.ktassertWidthIsEqualToassertHeightIsAtLeastassertPositionInRootIsEqualTogetUnclippedBoundsInRootgetBoundsInRootgetAlignmentLinePositiongetFirstLinkBoundsDp.assertIsEqualTocompose/material3/material3/src/androidDeviceTest/kotlin/androidx/compose/material3/ButtonTest.kt:202-226compose/material/material/src/androidDeviceTest/kotlin/androidx/compose/material/ChipTest.kt:226-233assertHeightIsAtLeast(MinHeight + 1.dp)