diff --git a/ReactAndroid/src/androidTest/java/com/facebook/react/tests/TextInputTestCase.java b/ReactAndroid/src/androidTest/java/com/facebook/react/tests/TextInputTestCase.java index 3d955e4b4e5..920ce021812 100644 --- a/ReactAndroid/src/androidTest/java/com/facebook/react/tests/TextInputTestCase.java +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/tests/TextInputTestCase.java @@ -199,6 +199,37 @@ public class TextInputTestCase extends ReactAppInstrumentationTestCase { assertFalse(reactEditText.isFocused()); } + public void testAccessibilityFocus_notEmpty_selectionSetAtEnd() throws Throwable { + String testId = "textInput1"; + String text = "Testing"; + + final ReactEditText reactEditText = getViewByTestId(testId); + reactEditText.setText(text); + runTestOnUiThread( + new Runnable() { + @Override + public void run() { + reactEditText.clearFocus(); + } + }); + waitForBridgeAndUIIdle(); + assertFalse(reactEditText.isFocused()); + assertEquals(0, reactEditText.getSelectionStart()); + + runTestOnUiThread( + new Runnable() { + @Override + public void run() { + reactEditText.performAccessibilityAction( + AccessibilityNodeInfo.ACTION_ACCESSIBILITY_FOCUS, null); + reactEditText.performAccessibilityAction(AccessibilityNodeInfo.ACTION_CLICK, null); + } + }); + waitForBridgeAndUIIdle(); + assertTrue(reactEditText.isFocused()); + assertEquals(text.length(), reactEditText.getSelectionStart()); + } + private void fireEditorActionAndCheckRecording( final ReactEditText reactEditText, final int actionId) throws Throwable { fireEditorActionAndCheckRecording(reactEditText, actionId, true); diff --git a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java index 8995949bb76..79f35211152 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java +++ b/ReactAndroid/src/main/java/com/facebook/react/views/textinput/ReactEditText.java @@ -145,6 +145,14 @@ public class ReactEditText extends AppCompatEditText @Override public boolean performAccessibilityAction(View host, int action, Bundle args) { if (action == AccessibilityNodeInfo.ACTION_CLICK) { + int length = getText().length(); + if (length > 0) { + // For some reason, when you swipe to focus on a text input that already has text in + // it, it clears the selection and resets the cursor to the beginning of the input. + // Since this is not typically (ever?) what you want, let's just explicitly set the + // selection on accessibility click to undo that. + setSelection(length); + } return requestFocusInternal(); } return super.performAccessibilityAction(host, action, args);