Loading core/java/android/widget/Editor.java +3 −1 Original line number Diff line number Diff line Loading @@ -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; Loading core/tests/coretests/src/android/widget/TextViewContextMenuTest.java +41 −0 Original line number Diff line number Diff line Loading @@ -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); } } Loading
core/java/android/widget/Editor.java +3 −1 Original line number Diff line number Diff line Loading @@ -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; Loading
core/tests/coretests/src/android/widget/TextViewContextMenuTest.java +41 −0 Original line number Diff line number Diff line Loading @@ -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); } }