summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
author Ben Murdoch <benm@google.com> 2024-08-05 09:10:45 +0000
committer Android (Google) Code Review <android-gerrit@google.com> 2024-08-05 09:10:45 +0000
commita99107543b378b1d7a14555a7dd5ca1d9db4001e (patch)
tree558292fb08a9a94bbcbc3a0170f8d6b1fa9e136e
parent2d258767b7e9ffe050cb2ad40fb6160145e7bff6 (diff)
parent3cafe80ee8f3dce31278418a8e0bce73958b8802 (diff)
Merge "Only enable Autofill context menu item when no text selected." into main
-rw-r--r--core/java/android/widget/Editor.java4
-rw-r--r--core/tests/coretests/src/android/widget/TextViewContextMenuTest.java41
2 files changed, 44 insertions, 1 deletions
diff --git a/core/java/android/widget/Editor.java b/core/java/android/widget/Editor.java
index 8b6ead7d37c5..d28c953fe0bd 100644
--- a/core/java/android/widget/Editor.java
+++ b/core/java/android/widget/Editor.java
@@ -3285,9 +3285,11 @@ public class Editor {
.setEnabled(mTextView.canShare())
.setIcon(a.getDrawable(6))
.setOnMenuItemClickListener(mOnContextMenuItemClickListener);
+ final String selected = mTextView.getSelectedText();
menu.add(CONTEXT_MENU_GROUP_MISC, TextView.ID_AUTOFILL, menuItemOrderAutofill,
android.R.string.autofill)
- .setEnabled(mTextView.canRequestAutofill())
+ .setEnabled(mTextView.canRequestAutofill()
+ && (selected == null || selected.isEmpty()))
.setOnMenuItemClickListener(mOnContextMenuItemClickListener);
mPreserveSelection = true;
diff --git a/core/tests/coretests/src/android/widget/TextViewContextMenuTest.java b/core/tests/coretests/src/android/widget/TextViewContextMenuTest.java
index 12f8c9c1efc9..f9da832ba7de 100644
--- a/core/tests/coretests/src/android/widget/TextViewContextMenuTest.java
+++ b/core/tests/coretests/src/android/widget/TextViewContextMenuTest.java
@@ -240,4 +240,45 @@ public class TextViewContextMenuTest {
verify(mockNoIconMenu, times(0)).setIcon(any());
verify(mockNoIconMenu2, times(0)).setIcon(any());
}
+
+ @UiThreadTest
+ @Test
+ public void testAutofillMenuItemEnabledWhenNoTextSelected() {
+ ContextMenu menu = mock(ContextMenu.class);
+ MenuItem mockMenuItem = newMockMenuItem();
+ when(menu.add(anyInt(), anyInt(), anyInt(), anyInt())).thenReturn(mockMenuItem);
+ MenuItem mockAutofillMenuItem = newMockMenuItem();
+ when(menu.add(anyInt(), eq(TextView.ID_AUTOFILL), anyInt(), anyInt()))
+ .thenReturn(mockAutofillMenuItem);
+
+ EditText et = mActivity.findViewById(R.id.editText);
+ et.setText("Test");
+
+ Editor editor = et.getEditorForTesting();
+ editor.onCreateContextMenu(menu);
+
+ verify(menu).add(anyInt(), eq(TextView.ID_AUTOFILL), anyInt(), anyInt());
+ verify(mockAutofillMenuItem).setEnabled(true);
+ }
+
+ @UiThreadTest
+ @Test
+ public void testAutofillMenuItemNotEnabledWhenTextSelected() {
+ ContextMenu menu = mock(ContextMenu.class);
+ MenuItem mockMenuItem = newMockMenuItem();
+ when(menu.add(anyInt(), anyInt(), anyInt(), anyInt())).thenReturn(mockMenuItem);
+ MenuItem mockAutofillMenuItem = newMockMenuItem();
+ when(menu.add(anyInt(), eq(TextView.ID_AUTOFILL), anyInt(), anyInt()))
+ .thenReturn(mockAutofillMenuItem);
+
+ EditText et = mActivity.findViewById(R.id.editText);
+ et.setText("Test");
+ et.selectAll();
+ Editor editor = et.getEditorForTesting();
+ editor.onCreateContextMenu(menu);
+
+ verify(menu).add(anyInt(), eq(TextView.ID_AUTOFILL), anyInt(), anyInt());
+ verify(mockAutofillMenuItem).setEnabled(false);
+ }
+
}