Loading...
Loading...
Use this skill to drive Jetpack Compose text fields from tests with the text-specific actions — performTextInput (insert at cursor via the InsertTextAtCursor semantics action), performTextReplacement (clear + replace via SetText), performTextClearance (replace with empty string), performTextInputSelection (set TextRange selection), and performImeAction (fire the configured IME action such as Next, Done, Search). Covers the auto-focus path, the enabled / editable / focusable preconditions, the append vs replace gotcha that produces "helloworld" instead of "world", and the IME-action focus chain (Next advances focus to the next focusable). Use when the developer asks "how do I type into a TextField in a test", "test an IME Next button", "test a clear-text action", "BasicTextField won't accept input", "text input duplicated in test", or reports "Failed to perform text input" / "Failed to perform IME action" / "Default ImeAction" errors.
npx skill4agent add skydoves/android-testing-skills entering-textTextFieldOutlinedTextFieldBasicTextFieldInsertTextAtCursorSetTextImeAction.NextImeAction.DoneImeAction.SearchSendGoperformTextInputSelection(TextRange(...))Ctrl+ATab../injecting-mouse-and-keyboard/SKILL.md../../assertions/asserting-node-state-and-text/SKILL.md../../finders/finding-nodes-by-tag-text-content/SKILL.md../../setup/configuring-test-dependencies/SKILL.mdStateRestorationTesterandroidx.compose.ui:ui-test-junit4ui-testrunComposeUiTest../../setup/configuring-test-dependencies/SKILL.mdTextFieldModifier.testTag("…")enabled = trueperformTextInputassert(isEnabled())performImeActionImeAction != ImeAction.DefaultKeyboardOptions(imeAction = …)OnImeActionKeyboardActions(...)performTextInput(text)SemanticsActions.InsertTextAtCursorAnnotatedString(text)getNodeAndFocusAssertionError("Failed to perform text input.")InsertTextAtCursorrule.onNodeWithTag(Tag).performTextInput("Hello ")performTextReplacement(text)SemanticsActions.SetTextrule.onNodeWithTag(Tag).performTextReplacement("New value")performTextClearance()performTextReplacement("")SetTextrule.onNodeWithTag(Tag).performTextClearance()performTextInputSelection(selection: TextRange, relativeToOriginalText: Boolean = true)SemanticsActions.SetSelectionrelativeToOriginalText = truefalseVisualTransformationrule.onNodeWithTag(Tag).performTextInput("Hello World")
rule.onNodeWithTag(Tag).performTextInputSelection(TextRange(0, 5)) // selects "Hello"performImeAction()assert(hasPerformImeAction())OnImeActionassert(!hasImeAction(ImeAction.Default))tryPerformAccessibilityChecks()getNodeAndFocus(requireEditable = false)OnImeActiontrueAssertionError("Failed to perform IME action, handler returned false.")rule.onNodeWithTag(InitialFieldTag).performImeAction()assertTextEquals../../assertions/asserting-node-state-and-text/SKILL.mdperformTextInputperformTextReplacement// WRONG
rule.onNodeWithTag(Tag).performTextInput("hello")
rule.onNodeWithTag(Tag).performTextInput("world")
rule.onNodeWithTag(Tag).assertTextEquals("world")
// WRONG because: performTextInput maps to InsertTextAtCursor, not SetText. The cursor
// remains at the end of "hello" after the first call, so the second call inserts
// "world" there. Final value is "helloworld" — assertion fails.// RIGHT
rule.onNodeWithTag(Tag).performTextInput("hello")
rule.onNodeWithTag(Tag).performTextReplacement("world")
rule.onNodeWithTag(Tag).assertTextEquals("world")performTextReplacementSemanticsActions.SetTextBasicTextFieldTest.kt:171-194@Test
fun textFieldState_textChange_updatesState() {
val state = TextFieldState("Hello ", TextRange(Int.MAX_VALUE))
inputMethodInterceptor.setTextFieldTestContent {
BasicTextField(state = state, modifier = Modifier.fillMaxSize().testTag(Tag))
}
rule.onNodeWithTag(Tag).performTextInput("World!")
rule.runOnIdle { assertThat(state.text.toString()).isEqualTo("Hello World!") }
}
@Test
fun textFieldState_textChange_updatesSemantics() {
val state = TextFieldState("Hello ", TextRange(Int.MAX_VALUE))
inputMethodInterceptor.setTextFieldTestContent {
BasicTextField(state = state, modifier = Modifier.fillMaxSize().testTag(Tag))
}
rule.onNodeWithTag(Tag).performTextInput("World!")
rule.onNodeWithTag(Tag).assertTextEquals("Hello World!")
}TextRange(Int.MAX_VALUE)TextFieldStateperformTextInput("World!")"Hello World!"runOnIdleBasicTextFieldTest.kt:442-466@Test
fun textField_whenStateObjectChanges_restartsInput() {
val state1 = TextFieldState("Hello")
val state2 = TextFieldState("World")
var toggleState by mutableStateOf(true)
val state by derivedStateOf { if (toggleState) state1 else state2 }
inputMethodInterceptor.setTextFieldTestContent {
BasicTextField(
state = state,
enabled = true,
modifier = Modifier.fillMaxSize().testTag(Tag),
)
}
with(rule.onNodeWithTag(Tag)) {
performTextReplacement("Compose")
assertTextEquals("Compose")
}
toggleState = !toggleState
with(rule.onNodeWithTag(Tag)) {
performTextReplacement("Compose2")
assertTextEquals("Compose2")
}
assertThat(state1.text.toString()).isEqualTo("Compose")
assertThat(state2.text.toString()).isEqualTo("Compose2")
}performTextReplacementrule.onNodeWithTag(Tag).performTextClearance()
rule.onNodeWithTag(Tag).performTextInput("Different")
rule.onNodeWithTag(Tag).assertTextEquals("Different")performTextClearanceperformTextReplacement("")performTextInputNextDefaultKeyboardActionsTest.kt:130-149// Show keyboard.
rule.onNodeWithTag(initialTextField).performClick()
inputMethodInterceptor.assertSessionActive()
keyboardController.show()
// Act.
rule.onNodeWithTag(initialTextField).performImeAction()
// Assert.
when (imeAction) {
Next -> {
// Focus moves to the next item.
assertThat(focusState1).isFalse()
assertThat(focusState2).isFalse()
assertThat(focusState3).isTrue()
}
Previous -> {
// Focus moves to the previous item.
assertThat(focusState1).isTrue()
assertThat(focusState2).isFalse()
assertThat(focusState3).isFalse()
}
Done -> {
// No change to focus state.
assertThat(focusState1).isFalse()
assertThat(focusState2).isTrue()
assertThat(focusState3).isFalse()
// Software keyboard is hidden.
keyboardController.assertHidden()
}
}ImeActionperformImeActionBasicTextField(
state = state,
keyboardOptions = KeyboardOptions(imeAction = ImeAction.Next),
modifier = Modifier.testTag(InitialFieldTag),
)performImeAction!hasImeAction(ImeAction.Default)keyboardOptionsAssertionError("Failed to perform IME action.")rule.onNodeWithTag(Tag).performTextInput("Hello World")
rule.onNodeWithTag(Tag).performTextInputSelection(TextRange(6, 11)) // select "World"
rule.onNodeWithTag(Tag).performTextInput("Compose") // selection replaced
rule.onNodeWithTag(Tag).assertTextEquals("Hello Compose")SemanticsActions.SetSelectionInsertTextAtCursorrelativeToOriginalText = falseVisualTransformationenabled = falseTextTextFieldperformTextInputAssertionError("Failed to perform text input.")tryPerformAccessibilityChecks()assert(isEnabled())assert(hasRequestFocusAction())assert(hasSetTextAction())assert(hasInsertTextAtCursorAction())../../debug/printing-the-semantics-tree/SKILL.mdTextperformTextReplacementperformTextInputImeActionKeyboardOptions(imeAction = ImeAction.Next)performImeAction!hasImeAction(ImeAction.Default)Modifier.testTag(...)rule.runOnIdle { … }TextFieldState.textThread.sleeprunOnIdleKeyperformKeyInput { pressKey(Key.A) }performTextInputSelectionrelativeToOriginalTexttrueVisualTransformationperformTextReplacement(...)performTextClearance()performTextInput(...)performTextInputperformImeActionKeyboardOptions(imeAction = ImeAction.Next | Done | Search | …)rule.runOnIdle { … }Thread.sleep./gradlew :app:connectedDebugAndroidTest:app:testDebugUnitTestcompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/TextActions.ktperformTextClearanceperformTextInputperformTextInputSelectionperformTextReplacementperformImeActiongetNodeAndFocuscompose/ui/ui-test/src/commonMain/kotlin/androidx/compose/ui/test/Filters.kthasPerformImeActionhasImeActionhasSetTextActionhasInsertTextAtCursorActionisEnabledhasRequestFocusActioncompose/foundation/foundation/src/androidDeviceTest/.../BasicTextFieldTest.ktperformTextInputassertTextEqualsperformTextReplacementcompose/foundation/foundation/src/androidDeviceTest/.../DefaultKeyboardActionsTest.ktperformImeAction