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

Commit 3cafe80e authored by Ben Murdoch's avatar Ben Murdoch
Browse files

Only enable Autofill context menu item when no text selected.

Aligns behavior of the context menu with the action bar.

Bug: 345709107
Flag: EXEMPT trivial bug fix
Test: atest TextViewContextMenuTest
Change-Id: If3cdc1d3bbcb04348d25fe751c72c77aeddc2de6
parent 73a1f209
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);
    }

}