Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit a9910754 authored by Ben Murdoch's avatar Ben Murdoch Committed by Android (Google) Code Review
Browse files

Merge "Only enable Autofill context menu item when no text selected." into main

parents 2d258767 3cafe80e
Loading
Loading
Loading
Loading
+3 −1
Original line number Diff line number Diff line
@@ -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;
+41 −0
Original line number Diff line number Diff line
@@ -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);
    }

}