Loading...
Loading...
Use this skill to drive Jetpack Compose UI with synthetic touch events through performTouchInput and the TouchInjectionScope DSL — click, longClick, doubleClick, swipe, swipeUp / swipeDown / swipeLeft / swipeRight, swipeWithVelocity, pinch, multiTouchSwipe, plus the low-level down / moveTo / moveBy / move / up / cancel primitives. Covers node-local coordinates, the geometry helpers (center, topLeft, bottomRight, percentOffset), event batching, splitting a gesture across multiple performTouchInput blocks, and why performGesture must not be used. Use when the developer asks "how do I simulate a swipe", "drag a node by 100 pixels", "long-press in a Compose test", "pinch to zoom", "test a fling with velocity", or reports "performGesture is deprecated", a Thread.sleep mid-gesture, or a flaky drag test using hardcoded screen coordinates.
npx skill4agent add skydoves/android-testing-skills injecting-touch-gesturesperformTouchInput { … }TouchInjectionScopeperformGesture { … }@Deprecated("Replaced by performTouchInput")centerperformTouchInputperformGesture { }performClick()../clicking-and-scrolling/SKILL.md../injecting-mouse-and-keyboard/SKILL.md../../assertions/asserting-node-state-and-text/SKILL.md../../synchronization/testing-animations-deterministically/SKILL.mdandroidx.compose.ui:ui-test-junit4ui-testrunComposeUiTest../../setup/configuring-test-dependencies/SKILL.mdModifier.testTag("…")pinchmultiTouchSwipeModifier.pointerInput { … }Modifier.transformableModifier.draggable2DdetectTransformGesturesperformTouchInputSemanticsNodeInteractionrule.onNodeWithTag(DragSurfaceTag).performTouchInput { /* DSL */ }(0, 0)InjectionScopecentertopLefttopCentertopRightcenterLeftcenterRightbottomLeftbottomCenterbottomRightwidthheightright == width - 1fbottom == height - 1fpercentOffset(.5f, .5f)| Helper | Default | What it does |
|---|---|---|
| tap at center | |
| 100 ms past the long-press timeout | press, hold, release (TouchInjectionScope.kt:390) |
| midway between min/max double-tap window | |
| 200 ms linear | linear interpolation between two points (TouchInjectionScope.kt:445) |
| along axis through center | edge-to-edge sweeps (TouchInjectionScope.kt:649+) |
| computed for feasibility | shapes the path so the final velocity matches (TouchInjectionScope.kt:617) |
| 400 ms two-pointer | two simultaneous linear paths (TouchInjectionScope.kt:574) |
| — | one curve per pointer id (TouchInjectionScope.kt:491) |
TouchInjectionScopedown(pointerId = 0, position)positionmoveTo(pointerId = 0, position, delayMillis = eventPeriodMillis)moveBy(pointerId = 0, delta, delayMillis = eventPeriodMillis)updatePointerTo(pointerId, position)updatePointerBy(pointerId, delta)move(delayMillis)move(delayMillis = eventPeriodMillis)up(pointerId = 0)cancel(delayMillis = eventPeriodMillis)MotionEvent.ACTION_CANCELcurrentPosition(pointerId = 0): Offset?performTouchInputrule.onNodeWithTag(Tag).performTouchInput { down(center) }
// recomposition + assertions happen here
rule.onNodeWithTag(Tag).performTouchInput { moveBy(Offset(0f, 100f)); up() }performTouchInput// WRONG
@Suppress("DEPRECATION")
rule.onNodeWithTag(Tag).performGesture { swipeUp() }
// WRONG because: performGesture is @Deprecated("Replaced by performTouchInput") (Actions.kt:337).
// The replaceWith metadata in the @Deprecated annotation explicitly suggests performTouchInput.// RIGHT
rule.onNodeWithTag(Tag).performTouchInput { swipeUp() }performGesture// WRONG
rule.onNodeWithTag(Tag).performTouchInput {
down(Offset(500f, 800f))
moveTo(Offset(500f, 700f))
up()
}
// WRONG because: (500, 800) only happens to land on the node on certain device profiles.
// On a tablet or in landscape the node sits elsewhere, and on a small phone it may not even
// be inside the visible bounds — the test becomes device-shaped.// RIGHT
rule.onNodeWithTag(Tag).performTouchInput {
down(center)
moveBy(Offset(0f, -100f))
up()
}centerbottomCentertopRightvisibleSizeDraggableTest.kt:114-146@Test
fun draggable_horizontalDrag() {
var total = 0f
setDraggableContent { Modifier.draggable(Orientation.Horizontal) { total += it } }
rule.onNodeWithTag(draggableBoxTag).performTouchInput {
this.swipe(
start = this.center,
end = Offset(this.center.x + 100f, this.center.y),
durationMillis = 100,
)
}
val lastTotal = rule.runOnIdle {
assertThat(total).isGreaterThan(0)
total
}
}this.centerdurationMillis = 100runOnIdleTransformableTest.kt:128-141rule.onNodeWithTag(TEST_TAG).performTouchInput {
val leftStartX = center.x - 10
val leftEndX = visibleSize.toSize().width * EDGE_FUZZ_FACTOR
val rightStartX = center.x + 10
val rightEndX = visibleSize.toSize().width * (1 - EDGE_FUZZ_FACTOR)
pinch(
Offset(leftStartX, center.y),
Offset(leftEndX, center.y),
Offset(rightStartX, center.y),
Offset(rightEndX, center.y),
)
}pinch(start0, end0, start1, end1)durationMillis = 400EDGE_FUZZ_FACTOR0.05f0fright == width - 1f@Test fun longPress_thenAssert_thenRelease() {
rule.setContent { /* … */ }
// Hold finger down at center, do not release.
rule.onNodeWithTag(Tag).performTouchInput { down(center) }
// Wait for the long-press timeout to elapse via the test clock.
rule.mainClock.advanceTimeBy(viewConfiguration.longPressTimeoutMillis + 100)
rule.onNodeWithTag(Tag).assertHasIndicationOfLongPress() // your assertion
// Release in a second block; the pointer is still "down" between blocks.
rule.onNodeWithTag(Tag).performTouchInput { up() }
}CombinedClickableTestCombinedClickableTest.kt:446-471rule.onNodeWithTag(Tag).performTouchInput {
swipeWithVelocity(
start = bottomCenter,
end = topCenter,
endVelocity = 4_000f, // px/second
durationMillis = 200,
)
}swipeWithVelocityIllegalArgumentExceptionperformTouchInput { … }performGesture { … }@DeprecatedcentertopLeftbottomRightpercentOffset(...)rule.runOnIdle { … }staterule.mainClock.advanceTimeBy(...)performTouchInputThread.sleep../../synchronization/synchronizing-with-idle/SKILL.mdcentermoveBymoveTo(topLeft)swipeswipeWithVelocitymultiTouchSwipeperformGesturegit grep -n 'performGesture' src/androidTest src/testThread.sleepperformTouchInputOffset(<screen-pixel>, <screen-pixel>)centertop*bottom*centerLeftrule.runOnIdle { … }./gradlew :app:connectedDebugAndroidTest:app:testDebugUnitTestcompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/Actions.ktperformTouchInputperformGesturecompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/TouchInjectionScope.ktdownmoveTomoveByupcancelclicklongClickdoubleClickswipeswipeUpDownLeftRightswipeWithVelocitypinchmultiTouchSwipecompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/InjectionScope.ktcentertopLefttopCenterbottomRightwidthheightrightbottompercentOffsetvisibleSizeadvanceEventTimecompose/foundation/foundation/src/androidDeviceTest/.../DraggableTest.ktswipe(start, end, durationMillis)compose/foundation/foundation/src/androidDeviceTest/.../TransformableTest.ktpinch(...)compose/foundation/foundation/src/androidDeviceTest/.../CombinedClickableTest.ktperformTouchInput { longClick() }down(center)up()compose/foundation/foundation/src/androidDeviceTest/.../ScrollableAreaTest.ktdown(); moveBy()