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

Commit 0e9badef authored by ryanlwlin's avatar ryanlwlin
Browse files

Optmioze magnificoation test

Centeralize the stub logic of IWindowMagnificationConnection

Bug: 164327211
Test: atest com.android.server.accessibility.magnification
Change-Id: If636f749f4d0f4d28cdec87d1939b909c6bb85be
parent f61b5b0d
Loading
Loading
Loading
Loading
+40 −2
Original line number Diff line number Diff line
@@ -17,23 +17,33 @@
package com.android.server.accessibility.magnification;

import static org.mockito.ArgumentMatchers.any;
import static org.mockito.ArgumentMatchers.anyFloat;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import android.graphics.Rect;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.view.Display;
import android.view.accessibility.IWindowMagnificationConnection;
import android.view.accessibility.IWindowMagnificationConnectionCallback;

/**
 * Mocks the basic logic of window magnification in System UI. We assume the screen size is
 * unlimited, so source bounds is always on the center of the mirror window bounds.
 */
class MockWindowMagnificationConnection  {

    public static final int TEST_DISPLAY = Display.DEFAULT_DISPLAY;
    private final IWindowMagnificationConnection mConnection;
    private final Binder mBinder;
    private IBinder.DeathRecipient mDeathRecipient;
    private IWindowMagnificationConnectionCallback mIMirrorWindowCallback;
    private Rect mMirrorWindowFrame = new Rect(0, 0, 500, 500);

    MockWindowMagnificationConnection() throws RemoteException {
        mConnection = mock(IWindowMagnificationConnection.class);
@@ -50,6 +60,30 @@ class MockWindowMagnificationConnection {
            return null;
        }).when(mBinder).linkToDeath(
                any(IBinder.DeathRecipient.class), eq(0));
        stubConnection();
    }

    private void stubConnection() throws RemoteException {
        doAnswer((invocation) -> {
            final int displayId = invocation.getArgument(0);
            if (displayId != TEST_DISPLAY) {
                throw new IllegalArgumentException("only support default display :" + displayId);
            }
            computeMirrorWindowFrame(invocation.getArgument(1), invocation.getArgument(2));

            mIMirrorWindowCallback.onWindowMagnifierBoundsChanged(TEST_DISPLAY,
                    mMirrorWindowFrame);
            return null;
        }).when(mConnection).enableWindowMagnification(anyInt(),
                anyFloat(), anyFloat(), anyFloat());
    }

    private void computeMirrorWindowFrame(float centerX, float centerY) {
        final float offsetX = Float.isNaN(centerX) ? 0
                : centerX - mMirrorWindowFrame.exactCenterX();
        final float offsetY = Float.isNaN(centerY) ? 0
                : centerY - mMirrorWindowFrame.exactCenterY();
        mMirrorWindowFrame.offset((int) offsetX, (int) offsetY);
    }

    IWindowMagnificationConnection getConnection() {
@@ -60,12 +94,16 @@ class MockWindowMagnificationConnection {
        return mBinder;
    }

    public IBinder.DeathRecipient getDeathRecipient() {
    IBinder.DeathRecipient getDeathRecipient() {
        return mDeathRecipient;
    }

    public IWindowMagnificationConnectionCallback getConnectionCallback() {
    IWindowMagnificationConnectionCallback getConnectionCallback() {
        return mIMirrorWindowCallback;
    }

    public Rect getMirrorWindowFrame() {
        return new Rect(mMirrorWindowFrame);
    }
}
+4 −4
Original line number Diff line number Diff line
@@ -87,15 +87,15 @@ public class TwoFingersDownTest {
        secondPointerCoords.x = DEFAULT_X + 10;
        secondPointerCoords.y = DEFAULT_Y + 10;

        final MotionEvent pointerDownEvent = TouchEventGenerator.pointerDownEvent(
        final MotionEvent twoPointersDownEvent = TouchEventGenerator.twoPointersDownEvent(
                Display.DEFAULT_DISPLAY, defPointerCoords, secondPointerCoords);

        mGesturesObserver.onMotionEvent(downEvent, downEvent, 0);
        mGesturesObserver.onMotionEvent(pointerDownEvent, pointerDownEvent, 0);
        mGesturesObserver.onMotionEvent(twoPointersDownEvent, twoPointersDownEvent, 0);

        verify(mListener, timeout(sTimeoutMillis)).onGestureCompleted(
                MagnificationGestureMatcher.GESTURE_TWO_FINGER_DOWN, pointerDownEvent,
                pointerDownEvent, 0);
                MagnificationGestureMatcher.GESTURE_TWO_FINGER_DOWN, twoPointersDownEvent,
                twoPointersDownEvent, 0);
    }

    @Test
+19 −36
Original line number Diff line number Diff line
@@ -16,14 +16,9 @@

package com.android.server.accessibility.magnification;

import static android.view.MotionEvent.ACTION_POINTER_DOWN;

import static com.android.server.testutils.TestUtils.strictMock;

import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyFloat;
import static org.mockito.ArgumentMatchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;

import android.content.Context;
@@ -63,11 +58,9 @@ public class WindowMagnificationGestureHandlerTest {
    public static final int LAST_STATE = STATE_SHOW_MAGNIFIER_TRIPLE_TAP;

    // Co-prime x and y, to potentially catch x-y-swapped errors
    public static final float DEFAULT_X = 301;
    public static final float DEFAULT_Y = 299;
    //Assume first pointer position (DEFAULT_X,DEFAULT_Y) is in the window.
    public static Rect DEFAULT_WINDOW_FRAME = new Rect(0, 0, 500, 500);
    private static final int DISPLAY_0 = 0;
    public static final float DEFAULT_TAP_X = 301;
    public static final float DEFAULT_TAP_Y = 299;
    private static final int DISPLAY_0 = MockWindowMagnificationConnection.TEST_DISPLAY;

    private Context mContext;
    private WindowMagnificationManager mWindowMagnificationManager;
@@ -83,14 +76,6 @@ public class WindowMagnificationGestureHandlerTest {
                mContext, mWindowMagnificationManager, mock(ScaleChangedListener.class),
                /** detectTripleTap= */true,   /** detectShortcutTrigger= */true, DISPLAY_0);
        mWindowMagnificationManager.setConnection(mMockConnection.getConnection());
        mMockConnection.getConnectionCallback().onWindowMagnifierBoundsChanged(DISPLAY_0,
                DEFAULT_WINDOW_FRAME);
        doAnswer((invocation) -> {
            mMockConnection.getConnectionCallback().onWindowMagnifierBoundsChanged(DISPLAY_0,
                    DEFAULT_WINDOW_FRAME);
            return null;
        }).when(mMockConnection.getConnection()).enableWindowMagnification(eq(DISPLAY_0),
                anyFloat(), anyFloat(), anyFloat());
        mWindowMagnificationGestureHandler.setNext(strictMock(EventStreamTransformation.class));
    }

@@ -208,10 +193,11 @@ public class WindowMagnificationGestureHandlerTest {
                break;
                case STATE_TWO_FINGERS_DOWN: {
                    goFromStateIdleTo(STATE_SHOW_MAGNIFIER);
                    send(downEvent());
                    final Rect frame = mMockConnection.getMirrorWindowFrame();
                    send(downEvent(frame.centerX(), frame.centerY()));
                    //Second finger is outside the window.
                    send(pointerEvent(ACTION_POINTER_DOWN, DEFAULT_WINDOW_FRAME.right + 10,
                            DEFAULT_WINDOW_FRAME.bottom + 10));
                    send(twoPointerDownEvent(new float[]{frame.centerX(), frame.centerX() + 10},
                            new float[]{frame.centerY(), frame.centerY() + 10}));
                }
                break;
                case STATE_SHOW_MAGNIFIER_TRIPLE_TAP: {
@@ -243,7 +229,8 @@ public class WindowMagnificationGestureHandlerTest {
            }
            break;
            case STATE_TWO_FINGERS_DOWN: {
                send(upEvent());
                final Rect frame = mMockConnection.getMirrorWindowFrame();
                send(upEvent(frame.centerX(), frame.centerY()));
                returnToNormalFrom(STATE_SHOW_MAGNIFIER);
            }
            break;
@@ -286,12 +273,8 @@ public class WindowMagnificationGestureHandlerTest {
        }
    }

    private MotionEvent downEvent() {
        return TouchEventGenerator.downEvent(DISPLAY_0, DEFAULT_X, DEFAULT_Y);
    }

    private MotionEvent upEvent() {
        return upEvent(DEFAULT_X, DEFAULT_Y);
    private MotionEvent downEvent(float x, float y) {
        return TouchEventGenerator.downEvent(DISPLAY_0, x, y);
    }

    private MotionEvent upEvent(float x, float y) {
@@ -299,18 +282,18 @@ public class WindowMagnificationGestureHandlerTest {
    }

    private void tap() {
        send(downEvent());
        send(upEvent());
        send(downEvent(DEFAULT_TAP_X, DEFAULT_TAP_Y));
        send(upEvent(DEFAULT_TAP_X, DEFAULT_TAP_Y));
    }

    private MotionEvent pointerEvent(int action, float x, float y) {
    private MotionEvent twoPointerDownEvent(float[] x, float[] y) {
        final MotionEvent.PointerCoords defPointerCoords = new MotionEvent.PointerCoords();
        defPointerCoords.x = DEFAULT_X;
        defPointerCoords.y = DEFAULT_Y;
        defPointerCoords.x = x[0];
        defPointerCoords.y = y[0];
        final MotionEvent.PointerCoords pointerCoords = new MotionEvent.PointerCoords();
        pointerCoords.x = x;
        pointerCoords.y = y;
        return TouchEventGenerator.pointerDownEvent(DISPLAY_0, defPointerCoords, pointerCoords);
        pointerCoords.x = x[1];
        pointerCoords.y = y[1];
        return TouchEventGenerator.twoPointersDownEvent(DISPLAY_0, defPointerCoords, pointerCoords);
    }

    private String stateDump() {
+3 −3
Original line number Diff line number Diff line
@@ -43,9 +43,9 @@ public class TouchEventGenerator {
        return generateSingleTouchEvent(displayId, ACTION_UP, x, y);
    }

    public static MotionEvent pointerDownEvent(int displayId, PointerCoords defPointerCoords,
    public static MotionEvent twoPointersDownEvent(int displayId, PointerCoords defPointerCoords,
            PointerCoords pointerCoords) {
        return generatePointerEvent(displayId, ACTION_POINTER_DOWN, defPointerCoords,
        return generateTwoPointersEvent(displayId, ACTION_POINTER_DOWN, defPointerCoords,
                pointerCoords);
    }

@@ -59,7 +59,7 @@ public class TouchEventGenerator {
        return ev;
    }

    private static MotionEvent generatePointerEvent(int displayId, int action,
    private static MotionEvent generateTwoPointersEvent(int displayId, int action,
            PointerCoords defPointerCoords, PointerCoords pointerCoords) {
        final long  downTime = SystemClock.uptimeMillis();
        MotionEvent.PointerProperties defPointerProperties = new MotionEvent.PointerProperties();