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

Commit bd50f3b0 authored by Nikita Dubrovsky's avatar Nikita Dubrovsky
Browse files

Add explicit check to not trigger a cursor drag via the mouse

Cursor drag without the handle is only for touch and should not trigger
for events from a mouse. This was already the case before this CL but it
was not explicit: SelectionModifierCursorController happened to trigger
first so the cursor drag would not get activated. This change updates
the cursor drag logic so that mouse events are explicitly excluded from
the feature.

Bug: 145535274
Bug: 143852764
Test: Manual and automated tests
  atest FrameworksCoreTests:EditorCursorDragTest
  atest FrameworksCoreTests:TextViewActivityMouseTest

Change-Id: I3ccf5106b8e9a9e3c271dff38a4d6a0c0aa19597
parent 1f78b11b
Loading
Loading
Loading
Loading
+3 −3
Original line number Diff line number Diff line
@@ -5741,10 +5741,10 @@ public class Editor {
                return;
            }
            switch (event.getActionMasked()) {
                case MotionEvent.ACTION_DOWN:
                    mIsDraggingCursor = false;
                    break;
                case MotionEvent.ACTION_MOVE:
                    if (event.isFromSource(InputDevice.SOURCE_MOUSE)) {
                        break;
                    }
                    if (mIsDraggingCursor) {
                        performCursorDrag(event);
                    } else if (FLAG_ENABLE_CURSOR_DRAG
+51 −0
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import static org.junit.Assert.assertTrue;

import android.app.Activity;
import android.app.Instrumentation;
import android.view.InputDevice;
import android.view.MotionEvent;

import androidx.test.InstrumentationRegistry;
@@ -280,6 +281,35 @@ public class EditorCursorDragTest {
        assertTrue(editor.getSelectionController().isCursorBeingModified());
    }

    @Test
    public void testEditor_onTouchEvent_mouseDrag() throws Throwable {
        String text = "testEditor_onTouchEvent_mouseDrag";
        onView(withId(R.id.textview)).perform(replaceText(text));
        onView(withId(R.id.textview)).check(hasInsertionPointerAtIndex(0));

        TextView tv = mActivity.findViewById(R.id.textview);
        Editor editor = tv.getEditorForTesting();

        // Simulate a mouse click and drag. This should NOT trigger a cursor drag.
        long event1Time = 1001;
        MotionEvent event1 = mouseDownEvent(event1Time, event1Time, 20f, 30f);
        mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event1));
        assertFalse(editor.getInsertionController().isCursorBeingModified());
        assertFalse(editor.getSelectionController().isCursorBeingModified());

        long event2Time = 1002;
        MotionEvent event2 = mouseMoveEvent(event1Time, event2Time, 120f, 30f);
        mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event2));
        assertFalse(editor.getInsertionController().isCursorBeingModified());
        assertTrue(editor.getSelectionController().isCursorBeingModified());

        long event3Time = 1003;
        MotionEvent event3 = mouseUpEvent(event1Time, event3Time, 120f, 30f);
        mInstrumentation.runOnMainSync(() -> editor.onTouchEvent(event3));
        assertFalse(editor.getInsertionController().isCursorBeingModified());
        assertFalse(editor.getSelectionController().isCursorBeingModified());
    }

    @Test
    public void testEditor_onTouchEvent_cursorDrag() throws Throwable {
        String text = "testEditor_onTouchEvent_cursorDrag";
@@ -367,4 +397,25 @@ public class EditorCursorDragTest {
    private static MotionEvent moveEvent(long downTime, long eventTime, float x, float y) {
        return MotionEvent.obtain(downTime, eventTime, MotionEvent.ACTION_MOVE, x, y, 0);
    }

    private static MotionEvent mouseDownEvent(long downTime, long eventTime, float x, float y) {
        MotionEvent event = downEvent(downTime, eventTime, x, y);
        event.setSource(InputDevice.SOURCE_MOUSE);
        event.setButtonState(MotionEvent.BUTTON_PRIMARY);
        return event;
    }

    private static MotionEvent mouseUpEvent(long downTime, long eventTime, float x, float y) {
        MotionEvent event = upEvent(downTime, eventTime, x, y);
        event.setSource(InputDevice.SOURCE_MOUSE);
        event.setButtonState(0);
        return event;
    }

    private static MotionEvent mouseMoveEvent(long downTime, long eventTime, float x, float y) {
        MotionEvent event = moveEvent(downTime, eventTime, x, y);
        event.setSource(InputDevice.SOURCE_MOUSE);
        event.setButtonState(MotionEvent.BUTTON_PRIMARY);
        return event;
    }
}