From 3118dae54a34182c556dcd6609712333a9d35663 Mon Sep 17 00:00:00 2001 From: Ameer Armaly Date: Mon, 16 Mar 2020 17:47:37 -0700 Subject: [PATCH] TouchExplorerTest: insure that no-change move events don't crash the system. A device will sometimes generate move events that don't change coordinates from the previous event. We need to be sure that any calculations the system performs can handle zero-change values so we now test that in TouchExplorerTest. Specifically, we test it in both the touch exploring and dragging states. Also improved error reporting in MotionEventMatcher to report motion event actions as strings rather than numbers. Test: atest FrameworksServicesTests:TouchExplorerTest Change-Id: Id5b551327b54619fcf07f76c503a78e3d5059041 Change-Id: Ic36b80f8eecc3640092ec0a5cf893631d120f6c6 --- .../gestures/TouchExplorerTest.java | 54 ++++++++++++++++--- .../utils/MotionEventMatcher.java | 6 +-- 2 files changed, 50 insertions(+), 10 deletions(-) diff --git a/services/tests/servicestests/src/com/android/server/accessibility/gestures/TouchExplorerTest.java b/services/tests/servicestests/src/com/android/server/accessibility/gestures/TouchExplorerTest.java index cd3fb4887016b..6a965084c9572 100644 --- a/services/tests/servicestests/src/com/android/server/accessibility/gestures/TouchExplorerTest.java +++ b/services/tests/servicestests/src/com/android/server/accessibility/gestures/TouchExplorerTest.java @@ -30,7 +30,6 @@ import static com.android.server.accessibility.gestures.TouchState.STATE_DELEGAT import static com.android.server.accessibility.gestures.TouchState.STATE_DRAGGING; import static com.android.server.accessibility.gestures.TouchState.STATE_TOUCH_EXPLORING; -import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertEquals; import static org.junit.Assert.fail; import static org.mockito.Mockito.mock; @@ -48,7 +47,6 @@ import androidx.test.runner.AndroidJUnit4; import com.android.server.accessibility.AccessibilityManagerService; import com.android.server.accessibility.EventStreamTransformation; -import com.android.server.accessibility.utils.MotionEventMatcher; import org.junit.Before; import org.junit.Rule; @@ -101,11 +99,6 @@ public class TouchExplorerTest { @Override public void onMotionEvent(MotionEvent event, MotionEvent rawEvent, int policyFlags) { mEvents.add(0, event.copy()); - // LastEvent may not match if we're clearing the state - if (mLastEvent != null) { - MotionEventMatcher lastEventMatcher = new MotionEventMatcher(mLastEvent); - assertThat(rawEvent, lastEventMatcher); - } } @Override @@ -141,6 +134,53 @@ public class TouchExplorerTest { assertCapturedEvents(ACTION_HOVER_ENTER, ACTION_HOVER_MOVE, ACTION_HOVER_EXIT); } + /** + * Test the case where ACTION_DOWN is followed by a number of ACTION_MOVE events that do not + * change the coordinates. + */ + @Test + public void testOneFingerMoveWithExtraMoveEvents() { + goFromStateClearTo(STATE_TOUCH_EXPLORING_1FINGER); + // Inject a set of move events that have the same coordinates as the down event. + moveEachPointers(mLastEvent, p(0, 0)); + send(mLastEvent); + try { + Thread.sleep(2 * ViewConfiguration.getDoubleTapTimeout()); + } catch (InterruptedException e) { + fail("Interrupted while waiting for transition to touch exploring state."); + } + // Now move for real. + moveEachPointers(mLastEvent, p(10, 10)); + send(mLastEvent); + // One more move event with no change. + moveEachPointers(mLastEvent, p(0, 0)); + send(mLastEvent); + goToStateClearFrom(STATE_TOUCH_EXPLORING_1FINGER); + assertCapturedEvents( + ACTION_HOVER_ENTER, + ACTION_HOVER_MOVE, + ACTION_HOVER_MOVE, + ACTION_HOVER_MOVE, + ACTION_HOVER_EXIT); + } + + /** + * Test the case where ACTION_POINTER_DOWN is followed by a number of ACTION_MOVE events that do + * not change the coordinates. + */ + @Test + public void testTwoFingerDragWithExtraMoveEvents() { + goFromStateClearTo(STATE_DRAGGING_2FINGERS); + // Inject a set of move events that have the same coordinates as the down event. + moveEachPointers(mLastEvent, p(0, 0), p(0, 0)); + send(mLastEvent); + // Now move for real. + moveEachPointers(mLastEvent, p(10, 10), p(10, 10)); + send(mLastEvent); + goToStateClearFrom(STATE_DRAGGING_2FINGERS); + assertCapturedEvents(ACTION_DOWN, ACTION_MOVE, ACTION_MOVE, ACTION_UP); + } + @Test public void testTwoFingersMove_shouldDelegatingAndInjectActionDownPointerDown() { goFromStateClearTo(STATE_MOVING_2FINGERS); diff --git a/services/tests/servicestests/src/com/android/server/accessibility/utils/MotionEventMatcher.java b/services/tests/servicestests/src/com/android/server/accessibility/utils/MotionEventMatcher.java index 2b6d385fed48b..6927875701711 100644 --- a/services/tests/servicestests/src/com/android/server/accessibility/utils/MotionEventMatcher.java +++ b/services/tests/servicestests/src/com/android/server/accessibility/utils/MotionEventMatcher.java @@ -30,7 +30,7 @@ public class MotionEventMatcher extends TypeSafeMatcher { private static final String LOG_TAG = "MotionEventMatcher"; long mDownTime; long mEventTime; - long mActionMasked; + int mActionMasked; int mX; int mY; @@ -73,9 +73,9 @@ public class MotionEventMatcher extends TypeSafeMatcher { Log.e( LOG_TAG, "event.getActionMasked() = " - + event.getActionMasked() + + MotionEvent.actionToString(event.getActionMasked()) + ", expected " - + mActionMasked); + + MotionEvent.actionToString(mActionMasked)); Log.e(LOG_TAG, "event.getX() = " + event.getX() + ", expected " + mX); Log.e(LOG_TAG, "event.getY() = " + event.getY() + ", expected " + mY); return false;