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

Commit a7dcedc6 authored by Phil Weaver's avatar Phil Weaver
Browse files

Add tests for accessibility motion event injector.

Change-Id: If871f0a67d9265683fcbbabea382d54f3898a4c5
parent 8a8d8163
Loading
Loading
Loading
Loading
+33 −27
Original line number Diff line number Diff line
@@ -40,7 +40,7 @@ import java.util.List;
 *
 * All methods except {@code injectEvents} must be called only from the main thread.
 */
public class MotionEventInjector implements EventStreamTransformation {
public class MotionEventInjector implements EventStreamTransformation, Handler.Callback {
    private static final String LOG_TAG = "MotionEventInjector";
    private static final int MESSAGE_SEND_MOTION_EVENT = 1;
    private static final int MESSAGE_INJECT_EVENTS = 2;
@@ -64,7 +64,14 @@ public class MotionEventInjector implements EventStreamTransformation {
     * @param looper A looper on the main thread to use for dispatching new events
     */
    public MotionEventInjector(Looper looper) {
        mHandler = new Handler(looper, new Callback());
        mHandler = new Handler(looper, this);
    }

    /**
     * @param handler A handler to post messages. Exposes internal state for testing only.
     */
    public MotionEventInjector(Handler handler) {
        mHandler = handler;
    }

    /**
@@ -127,6 +134,29 @@ public class MotionEventInjector implements EventStreamTransformation {
        mIsDestroyed = true;
    }

    @Override
    public boolean handleMessage(Message message) {
        if (message.what == MESSAGE_INJECT_EVENTS) {
            SomeArgs args = (SomeArgs) message.obj;
            injectEventsMainThread((List<MotionEvent>) args.arg1,
                    (IAccessibilityServiceClient) args.arg2, args.argi1);
            args.recycle();
            return true;
        }
        if (message.what != MESSAGE_SEND_MOTION_EVENT) {
            Slog.e(LOG_TAG, "Unknown message: " + message.what);
            return false;
        }
        MotionEvent motionEvent = (MotionEvent) message.obj;
        sendMotionEventToNext(motionEvent, motionEvent,
                WindowManagerPolicy.FLAG_PASS_TO_USER);
        // If the message queue is now empty, then this gesture is complete
        if (!mHandler.hasMessages(MESSAGE_SEND_MOTION_EVENT)) {
            notifyService(true);
        }
        return true;
    }

    private void injectEventsMainThread(List<MotionEvent> events,
            IAccessibilityServiceClient serviceInterface, int sequence) {
        if (mIsDestroyed) {
@@ -201,6 +231,7 @@ public class MotionEventInjector implements EventStreamTransformation {
                    MotionEvent.obtain(now, now, MotionEvent.ACTION_CANCEL, 0.0f, 0.0f, 0);
            sendMotionEventToNext(cancelEvent, cancelEvent,
                    WindowManagerPolicy.FLAG_PASS_TO_USER);
            mOpenGesturesInProgress.put(source, false);
        }
    }

@@ -210,7 +241,6 @@ public class MotionEventInjector implements EventStreamTransformation {
            mHandler.removeMessages(MESSAGE_SEND_MOTION_EVENT);
            notifyService(false);
        }

    }

    private void notifyService(boolean success) {
@@ -222,28 +252,4 @@ public class MotionEventInjector implements EventStreamTransformation {
                    + mServiceInterfaceForCurrentGesture, re);
        }
    }

    private class Callback implements Handler.Callback {
        @Override
        public boolean handleMessage(Message message) {
            if (message.what == MESSAGE_INJECT_EVENTS) {
                SomeArgs args = (SomeArgs) message.obj;
                injectEventsMainThread((List<MotionEvent>) args.arg1,
                        (IAccessibilityServiceClient) args.arg2, args.argi1);
                args.recycle();
                return true;
            }
            if (message.what != MESSAGE_SEND_MOTION_EVENT) {
                throw new IllegalArgumentException("Unknown message: " + message.what);
            }
            MotionEvent motionEvent = (MotionEvent) message.obj;
            sendMotionEventToNext(motionEvent, motionEvent,
                    WindowManagerPolicy.FLAG_PASS_TO_USER);
            // If the message queue is now empty, then this gesture is complete
            if (!mHandler.hasMessages(MESSAGE_SEND_MOTION_EVENT)) {
                notifyService(true);
            }
            return true;
        }
    }
}
+6 −2
Original line number Diff line number Diff line
@@ -47,6 +47,7 @@ import android.view.KeyEvent;
import android.view.WindowManagerPolicy;
import com.android.server.accessibility.KeyEventDispatcher.KeyEventFilter;
import org.junit.Before;
import org.junit.BeforeClass;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.mockito.ArgumentCaptor;
@@ -71,9 +72,12 @@ public class KeyEventDispatcherTest {
    private ArgumentCaptor<Integer> mFilter1SequenceCaptor = ArgumentCaptor.forClass(Integer.class);
    private ArgumentCaptor<Integer> mFilter2SequenceCaptor = ArgumentCaptor.forClass(Integer.class);

    static {
    @BeforeClass
    public static void oneTimeInitialization() {
        if (Looper.myLooper() == null) {
            Looper.prepare();
        }
    }

    @Before
    public void setUp() {
+548 −0

File added.

Preview size limit exceeded, changes collapsed.