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

Commit 573ea5b2 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "MotionEvent: Add precondition checking to split()" into main

parents 0ab6b25f 5722b0f7
Loading
Loading
Loading
Loading
+7 −0
Original line number Diff line number Diff line
@@ -3783,6 +3783,13 @@ public final class MotionEvent extends InputEvent implements Parcelable {
            throw new IllegalArgumentException(
                    "idBits must contain at least one pointer from this motion event");
        }
        final int currentBits = getPointerIdBits();
        if ((currentBits & idBits) != idBits) {
            throw new IllegalArgumentException(
                    "idBits must be a non-empty subset of the pointer IDs from this MotionEvent, "
                            + "got idBits: "
                            + String.format("0x%x", idBits) + " for " + this);
        }
        MotionEvent event = obtain();
        event.mNativePtr = nativeSplit(event.mNativePtr, this.mNativePtr, idBits);
        return event;
+63 −0
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import static junit.framework.Assert.assertTrue;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.fail;

import android.graphics.Matrix;
import android.platform.test.annotations.Presubmit;
@@ -232,4 +233,66 @@ public class MotionEventTest {
        assertEquals(10, (int) event.getX());
        assertEquals(20, (int) event.getY());
    }

    @Test
    public void testSplit() {
        final int pointerCount = 2;
        final var properties = new PointerProperties[]{fingerProperties(0), fingerProperties(1)};
        final var coords = new PointerCoords[]{pointerCoords(20, 60), pointerCoords(40, 40)};

        final MotionEvent event = MotionEvent.obtain(0 /* downTime */,
                0 /* eventTime */, MotionEvent.ACTION_MOVE, pointerCount, properties, coords,
                0 /* metaState */, 0 /* buttonState */, 1 /* xPrecision */, 1 /* yPrecision */,
                0 /* deviceId */, 0 /* edgeFlags */, InputDevice.SOURCE_TOUCHSCREEN,
                0 /* flags */);

        final int idBits = ~0b1 & event.getPointerIdBits();
        final MotionEvent splitEvent = event.split(idBits);
        assertEquals(1, splitEvent.getPointerCount());
        assertEquals(1, splitEvent.getPointerId(0));
        assertEquals(40, (int) splitEvent.getX());
        assertEquals(40, (int) splitEvent.getY());
    }

    @Test
    public void testSplitFailsWhenNoIdsSpecified() {
        final int pointerCount = 2;
        final var properties = new PointerProperties[]{fingerProperties(0), fingerProperties(1)};
        final var coords = new PointerCoords[]{pointerCoords(20, 60), pointerCoords(40, 40)};

        final MotionEvent event = MotionEvent.obtain(0 /* downTime */,
                0 /* eventTime */, MotionEvent.ACTION_MOVE, pointerCount, properties, coords,
                0 /* metaState */, 0 /* buttonState */, 1 /* xPrecision */, 1 /* yPrecision */,
                0 /* deviceId */, 0 /* edgeFlags */, InputDevice.SOURCE_TOUCHSCREEN,
                0 /* flags */);

        try {
            final MotionEvent splitEvent = event.split(0);
            fail("Splitting event with id bits 0 should throw: " + splitEvent);
        } catch (IllegalArgumentException e) {
            // Expected
        }
    }

    @Test
    public void testSplitFailsWhenIdBitsDoNotMatch() {
        final int pointerCount = 2;
        final var properties = new PointerProperties[]{fingerProperties(0), fingerProperties(1)};
        final var coords = new PointerCoords[]{pointerCoords(20, 60), pointerCoords(40, 40)};

        final MotionEvent event = MotionEvent.obtain(0 /* downTime */,
                0 /* eventTime */, MotionEvent.ACTION_MOVE, pointerCount, properties, coords,
                0 /* metaState */, 0 /* buttonState */, 1 /* xPrecision */, 1 /* yPrecision */,
                0 /* deviceId */, 0 /* edgeFlags */, InputDevice.SOURCE_TOUCHSCREEN,
                0 /* flags */);

        try {
            final int idBits = 0b100;
            final MotionEvent splitEvent = event.split(idBits);
            fail("Splitting event with id bits that do not match any pointers should throw: "
                    + splitEvent);
        } catch (IllegalArgumentException e) {
            // Expected
        }
    }
}