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

Commit e37dab0e authored by TreeHugger Robot's avatar TreeHugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes I679a0a38,I1c125029

* changes:
  Dispatch mouse events to the view under cursor.
  Stop splitting for mouse events.
parents d083cf5e 63e6a355
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -2615,7 +2615,9 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager
                    || actionMasked == MotionEvent.ACTION_CANCEL;

            // Update list of touch targets for pointer down, if needed.
            final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0;
            final boolean isMouseEvent = ev.getSource() == InputDevice.SOURCE_MOUSE;
            final boolean split = (mGroupFlags & FLAG_SPLIT_MOTION_EVENTS) != 0
                    && !isMouseEvent;
            TouchTarget newTouchTarget = null;
            boolean alreadyDispatchedToNewTouchTarget = false;
            if (!canceled && !intercepted) {
@@ -2632,8 +2634,10 @@ public abstract class ViewGroup extends View implements ViewParent, ViewManager

                    final int childrenCount = mChildrenCount;
                    if (newTouchTarget == null && childrenCount != 0) {
                        final float x = ev.getX(actionIndex);
                        final float y = ev.getY(actionIndex);
                        final float x =
                                isMouseEvent ? ev.getXCursorPosition() : ev.getX(actionIndex);
                        final float y =
                                isMouseEvent ? ev.getYCursorPosition() : ev.getY(actionIndex);
                        // Find a child that can receive the event.
                        // Scan children from front to back.
                        final ArrayList<View> preorderedList = buildTouchDispatchChildList();
+67 −4
Original line number Diff line number Diff line
@@ -16,9 +16,14 @@

package android.view;

import static androidx.test.InstrumentationRegistry.getContext;
import static androidx.test.platform.app.InstrumentationRegistry.getInstrumentation;

import static org.junit.Assert.assertEquals;
import static org.mockito.ArgumentMatchers.any;
import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.spy;
import static org.mockito.Mockito.verify;

import android.content.Context;
import android.graphics.Region;
@@ -39,6 +44,60 @@ import org.junit.Test;
@SmallTest
public class ViewGroupTest {

    @Test
    public void testDispatchMouseEventsUnderCursor() {
        final Context context = getInstrumentation().getContext();
        final TestView viewGroup = new TestView(context, 0 /* left */, 0 /* top */,
                200 /* right */, 200 /* bottom */);
        final TestView viewA = spy(new TestView(context, 0 /* left */, 0 /* top */,
                100 /* right */, 200 /* bottom */));
        final TestView viewB = spy(new TestView(context, 100 /* left */, 0 /* top */,
                200 /* right */, 200 /* bottom */));

        viewGroup.addView(viewA);
        viewGroup.addView(viewB);

        // Make sure all of them handle touch events dispatched to them.
        doReturn(true).when(viewA).dispatchTouchEvent(any());
        doReturn(true).when(viewB).dispatchTouchEvent(any());

        MotionEvent.PointerProperties[] properties = new MotionEvent.PointerProperties[2];
        properties[0] = new MotionEvent.PointerProperties();
        properties[0].id = 0;
        properties[0].toolType = MotionEvent.TOOL_TYPE_FINGER;
        properties[1] = new MotionEvent.PointerProperties();
        properties[1].id = 1;
        properties[1].toolType = MotionEvent.TOOL_TYPE_FINGER;

        MotionEvent.PointerCoords[] coords = new MotionEvent.PointerCoords[2];
        coords[0] = new MotionEvent.PointerCoords();
        coords[0].x = 80;
        coords[0].y = 100;
        coords[1] = new MotionEvent.PointerCoords();
        coords[1].x = 240;
        coords[1].y = 100;

        MotionEvent event;
        // Make sure the down event is active with a pointer which coordinate is different from the
        // cursor position, which is the midpoint of all 2 pointers above.
        event = MotionEvent.obtain(0 /* downTime */, 0 /* eventTime */, MotionEvent.ACTION_DOWN,
                2 /* pointerCount */, properties, coords, 0 /* metaState */, 0 /* buttonState */,
                0 /* xPrecision */, 0 /* yPrecision */, 0 /* deviceId */, 0 /* edgeFlags */,
                InputDevice.SOURCE_MOUSE, 0 /* flags */);
        viewGroup.dispatchTouchEvent(event);
        verify(viewB).dispatchTouchEvent(event);

        event = MotionEvent.obtain(0 /* downTime */, 0 /* eventTime */,
                MotionEvent.ACTION_POINTER_DOWN | (1 << MotionEvent.ACTION_POINTER_INDEX_SHIFT),
                2 /* pointerCount */, properties, coords, 0 /* metaState */, 0 /* buttonState */,
                0 /* xPrecision */, 0 /* yPrecision */, 0 /* deviceId */, 0 /* edgeFlags */,
                InputDevice.SOURCE_MOUSE, 0 /* flags */);
        viewGroup.dispatchTouchEvent(event);
        verify(viewB).dispatchTouchEvent(event);

        verify(viewA, never()).dispatchTouchEvent(any());
    }

    /**
     * Test if {@link ViewGroup#subtractObscuredTouchableRegion} works as expected.
     *
@@ -59,7 +118,7 @@ public class ViewGroupTest {
     */
    @Test
    public void testSubtractObscuredTouchableRegion() {
        final Context context = getContext();
        final Context context = getInstrumentation().getContext();
        final TestView viewA = new TestView(context, 8 /* right */);
        final TestView viewB = new TestView(context, 6 /* right */);
        final TestView viewC = new TestView(context, 10 /* right */);
@@ -119,10 +178,14 @@ public class ViewGroupTest {
                (contain ? "" : " not"), x), contain, region.contains(x, 0 /* y */));
    }

    private static class TestView extends ViewGroup {
    public static class TestView extends ViewGroup {
        TestView(Context context, int right) {
            this(context, 0 /* left */, 0 /* top */, right, 1 /* bottom */);
        }

        TestView(Context context, int left, int top, int right, int bottom) {
            super(context);
            setFrame(0 /* left */, 0 /* top */, right, 1 /* bottom */);
            setFrame(left, top, right, bottom);
        }

        @Override