Loading...
Loading...
Use this skill to drive Jetpack Compose UI from tests with non-touch input — performMouseInput (click, rightClick, doubleClick, tripleClick, longClick, animateMoveTo, dragAndDrop, smoothScroll, enter / exit, press / release / scroll), performKeyInput (keyDown, keyUp, isKeyDown, modifier-state vals isCtrlDown / isShiftDown / isAltDown / isMetaDown / isFnDown / isCapsLockOn / isNumLockOn / isScrollLockOn, helpers pressKey, withKeyDown, withKeysDown, withKeyToggled, withKeysToggled), performKeyPress for direct KeyEvent injection, and performMultiModalInput for hover-then-click flows. Covers MouseButton (Primary, Secondary, Tertiary), ScrollWheel (Horizontal, Vertical), and the repeat-key behaviour driven by advanceEventTime. Use when the developer asks "how do I right-click in a Compose test", "test a Ctrl+S shortcut", "send a keyboard shortcut", "simulate hover", "test a tooltip", "scroll the mouse wheel", or "drag and drop with a mouse" on desktop or Compose Multiplatform.
npx skill4agent add skydoves/android-testing-skills injecting-mouse-and-keyboardperformMultiModalInputperform.*InputCtrl+SShift+TabModifier.hoverableShift../injecting-touch-gestures/SKILL.mdperformClick()../clicking-and-scrolling/SKILL.mdTextField../entering-text/SKILL.md../../synchronization/testing-animations-deterministically/SKILL.mdandroidx.compose.ui:ui-test-junit4ui-testrunComposeUiTest../../setup/configuring-test-dependencies/SKILL.md@Config(minSdk = …)Modifier.focusable()Modifier.focusRequester(...)performMouseInput(block: MouseInjectionScope.() -> Unit)(0, 0)TouchInjectionScopeMouseInjectionScopeInjectionScopecentertopLeftbottomRightpercentOffset(...)MouseInjectionScope.kt| Helper | Default | What it does |
|---|---|---|
| primary click at center | press → 60 ms wait → release |
| secondary click at center | shorthand for |
| midway between min/max double-tap window | |
| same delays as double | three sequential clicks |
| | press, hold, release |
| 300 ms | streams move events along a linear path |
| relative form | — |
| arbitrary curve | — |
| primary drag | |
| vertical | streams scroll events |
press(button: MouseButton = Primary)release(button: MouseButton = Primary)moveTo(position, delayMillis = eventPeriodMillis)moveBy(delta, delayMillis = eventPeriodMillis)updatePointerTo(position)updatePointerBy(delta)enter(position)exit(position)scroll(delta, scrollWheel = Vertical)cancel(delayMillis)ACTION_CANCELcurrentPosition: OffsetMouseButtonScrollWheelMouse.ktMouseButton.PrimaryMouseButton.SecondaryMouseButton.Tertiaryexpect value class MouseButtonScrollWheel.VerticalScrollWheel.Horizontalvalue class ScrollWheelperformKeyInput(block: KeyInjectionScope.() -> Unit)keyDown(key: Key)keyUp(key: Key)IllegalStateExceptionisKeyDown(key: Key): BooleanisCtrlDownisAltDownisShiftDownisMetaDownisFnDownisCapsLockOnisNumLockOnisScrollLockOnpressKey(key, pressDurationMillis = 50L)keyDown(key); advanceEventTime(50); keyUp(key)withKeyDown(key) { … }blockkeyfinallyblockwithKeysDown(listOf(key1, key2)) { … }withKeyToggled(key) { … }withKeysToggled(keys) { … }pressKeyblockCapsLockNumLockScrollLockadvanceEventTimeInjectionScopeMainTestClock.advanceTimeBySemanticsNodeInteraction.performKeyPress(KeyEvent): BooleanKeyInputHelpers.kt:27trueKeyEventKeyEvent(NativeKeyEvent(...))performMultiModalInput(block: MultiModalInjectionScope.() -> Unit)touch { … }mouse { … }key { … }rotary { … }trackpad { … }indirectPointer(...)touchmouseperform.*Input// RIGHT
rule.onNodeWithTag(RowTag).performMouseInput {
rightClick(center)
}
rule.onNodeWithText("Delete").assertIsDisplayed()rightClickclick(position, MouseButton.Secondary)CombinedClickableTest.kt:3415-3422rule.onNodeWithTag("myClickable").performMouseInput { enter(center) }
rule.runOnIdle {
assertThat(interactions).hasSize(1)
assertThat(interactions.first()).isInstanceOf(HoverInteraction.Enter::class.java)
}
rule.onNodeWithTag("myClickable").performMouseInput { exit(Offset(-1f, -1f)) }enter(position)exit(position)Offset(-1f, -1f)exit// WRONG
rule.onNodeWithTag(EditorTag).performKeyInput {
keyDown(Key.CtrlLeft)
pressKey(Key.S)
keyUp(Key.CtrlLeft)
}
// WRONG because: a thrown assertion or early-return inside the block leaks Key.CtrlLeft
// in the "down" state, polluting subsequent tests. The release is not in a finally.// RIGHT
rule.onNodeWithTag(EditorTag).performKeyInput {
withKeyDown(Key.CtrlLeft) {
pressKey(Key.S)
}
}withKeyDownfinallywithKeysDown(listOf(Key.CtrlLeft, Key.ShiftLeft)) { pressKey(Key.S) }rule.onNodeWithTag(InputTag).performKeyInput {
keyDown(Key.DirectionDown)
advanceEventTime(800) // 500 ms initial delay + 6 repeats at 50 ms
keyUp(Key.DirectionDown)
}advanceEventTimerule.mainClock.advanceTimeBy(…)rule.onNodeWithTag(SourceTag).performMouseInput {
dragAndDrop(
start = center,
end = Offset(center.x + 200f, center.y),
durationMillis = 250,
)
}dragAndDropstartendMouseButton.Primarybutton = MouseButton.Tertiaryrule.onNodeWithTag(ScrollAreaTag).performMouseInput {
smoothScroll(
scrollAmount = -10f, // negative scrollAmount = scroll back; new content appears at the top (MouseInjectionScope.kt:537-542)
durationMillis = 200,
scrollWheel = ScrollWheel.Vertical,
)
}scrollAmountscroll(delta, scrollWheel)MultiModalInjectionScopemouse { }key { }touch { }key { withKeyDown(...) { } }performMultiModalInputmouse { }withKeyDownKeyInjectionScopemouse { }rule.onNodeWithTag(NodeTag).performMultiModalInput {
mouse { enter(center) }
key { keyDown(Key.ShiftLeft) }
mouse { click() } // dispatched with Shift still held
key { keyUp(Key.ShiftLeft) }
}performMultiModalInputperformMouseInputperformKeyInputimport androidx.compose.ui.input.key.KeyEvent
import androidx.compose.ui.test.performKeyPress
val event = KeyEvent(NativeKeyEvent(NativeKeyEvent.ACTION_DOWN, NativeKeyEvent.KEYCODE_TAB))
rule.onRoot().performKeyPress(event)performKeyPressKeyInputHelpers.kt:27KeyEventwithKeyDownwithKeysDownfinallykeyDownkeyUpadvanceEventTimeInjectionScopeMainTestClock.advanceTimeBycentertopLeftbottomRightpercentOffset(...)../injecting-touch-gestures/SKILL.mdrule.runOnIdle { … }performMouseInputperformClick()Thread.sleepadvanceEventTimemainClock.advanceTimeByperformMultiModalInputperformMouseInputperformKeyInputwithKeyDownwithKeysDownkeyDownkeyUpThread.sleepperformMouseInputperformKeyInputOffset(...)centertop*bottom*percentOffset(...)advanceEventTimemainClock.advanceTimeByrule.runOnIdle { … }./gradlew :app:connectedDebugAndroidTestcompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/Actions.ktperformMouseInputperformKeyInputperformMultiModalInputperformTrackpadInputperformRotaryScrollInputcompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/MouseInjectionScope.ktpressreleasemoveTomoveByenterexitscrollcancelclickrightClickdoubleClicktripleClicklongClickanimateMoveToanimateMoveByanimateMoveAlongdragAndDropsmoothScrollcompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/KeyInjectionScope.ktkeyDownkeyUpisKeyDownpressKeywithKeyDownwithKeysDownwithKeyToggledwithKeysToggledcompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/KeyInputHelpers.ktperformKeyPress(KeyEvent): Booleancompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/Mouse.ktMouseButton.PrimarySecondaryTertiaryScrollWheel.HorizontalVerticalcompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/MultiModalInjectionScope.kttouchmousekeyrotarytrackpadindirectPointercompose/foundation/foundation/src/androidDeviceTest/.../CombinedClickableTest.ktperformMouseInput { enter(center) }performMouseInput { exit(Offset(-1f, -1f)) }