Reducing flakiness of swipe gestures

Swipe to home now injects a fixed number of points even if the test
thread wakes up irregularly, and sends model (not actual) time in
events.

Bug: 132173901
Bug: 132107664
Change-Id: I0a19bbc2a0c3312f353ad49ebe496eef1f172276
diff --git a/tests/tapl/com/android/launcher3/tapl/Background.java b/tests/tapl/com/android/launcher3/tapl/Background.java
index 3b2a7b8..60d2850 100644
--- a/tests/tapl/com/android/launcher3/tapl/Background.java
+++ b/tests/tapl/com/android/launcher3/tapl/Background.java
@@ -78,7 +78,8 @@
 
                 final long downTime = SystemClock.uptimeMillis();
                 mLauncher.sendPointer(downTime, downTime, MotionEvent.ACTION_DOWN, start);
-                mLauncher.movePointer(downTime, ZERO_BUTTON_SWIPE_UP_GESTURE_DURATION, start, end);
+                mLauncher.movePointer(
+                        downTime, downTime, ZERO_BUTTON_SWIPE_UP_GESTURE_DURATION, start, end);
                 LauncherInstrumentation.sleep(ZERO_BUTTON_SWIPE_UP_HOLD_DURATION);
                 mLauncher.sendPointer(
                         downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, end);
diff --git a/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java b/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java
index 87ef044..fd2eabb 100644
--- a/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java
+++ b/tests/tapl/com/android/launcher3/tapl/LauncherInstrumentation.java
@@ -70,6 +70,7 @@
 
     private static final String TAG = "Tapl";
     private static final int ZERO_BUTTON_STEPS_FROM_BACKGROUND_TO_HOME = 20;
+    private static final int GESTURE_STEP_MS = 16;
 
     // Types for launcher containers that the user is interacting with. "Background" is a
     // pseudo-container corresponding to inactive launcher covered by another app.
@@ -359,7 +360,7 @@
                         ? NORMAL_STATE_ORDINAL : BACKGROUND_APP_STATE_ORDINAL;
                 final Point displaySize = getRealDisplaySize();
 
-                swipe(
+                swipeViaMovePointer(
                         displaySize.x / 2, displaySize.y - 1,
                         displaySize.x / 2, 0,
                         finalState, ZERO_BUTTON_STEPS_FROM_BACKGROUND_TO_HOME);
@@ -543,8 +544,28 @@
     }
 
     void swipe(int startX, int startY, int endX, int endY, int expectedState, int steps) {
+        changeStateViaGesture(startX, startY, endX, endY, expectedState,
+                () -> mDevice.swipe(startX, startY, endX, endY, steps));
+    }
+
+    void swipeViaMovePointer(
+            int startX, int startY, int endX, int endY, int expectedState, int steps) {
+        changeStateViaGesture(startX, startY, endX, endY, expectedState, () -> {
+            final long downTime = SystemClock.uptimeMillis();
+            final Point start = new Point(startX, startY);
+            final Point end = new Point(endX, endY);
+            sendPointer(downTime, downTime, MotionEvent.ACTION_DOWN, start);
+            final long endTime = movePointer(downTime, downTime, steps * GESTURE_STEP_MS, start,
+                    end);
+            sendPointer(
+                    downTime, endTime, MotionEvent.ACTION_UP, end);
+        });
+    }
+
+    private void changeStateViaGesture(int startX, int startY, int endX, int endY,
+            int expectedState, Runnable gesture) {
         final Bundle parcel = (Bundle) executeAndWaitForEvent(
-                () -> mDevice.swipe(startX, startY, endX, endY, steps),
+                gesture,
                 event -> TestProtocol.SWITCHED_TO_STATE_MESSAGE.equals(event.getClassName()),
                 "Swipe failed to receive an event for the swipe end: " + startX + ", " + startY
                         + ", " + endX + ", " + endY);
@@ -589,21 +610,22 @@
         event.recycle();
     }
 
-    void movePointer(long downTime, long duration, Point from, Point to) {
+    long movePointer(long downTime, long startTime, long duration, Point from, Point to) {
         final Point point = new Point();
-        final long startTime = SystemClock.uptimeMillis();
-        for (; ; ) {
-            sleep(16);
+        long steps = duration / GESTURE_STEP_MS;
+        long currentTime = startTime;
+        for (long i = 0; i < steps; ++i) {
+            sleep(GESTURE_STEP_MS);
 
-            final long currentTime = SystemClock.uptimeMillis();
+            currentTime += GESTURE_STEP_MS;
             final float progress = (currentTime - startTime) / (float) duration;
-            if (progress > 1) return;
 
             point.x = from.x + (int) (progress * (to.x - from.x));
             point.y = from.y + (int) (progress * (to.y - from.y));
 
             sendPointer(downTime, currentTime, MotionEvent.ACTION_MOVE, point);
         }
+        return currentTime;
     }
 
     public static boolean isGesturalMode(Context context) {
diff --git a/tests/tapl/com/android/launcher3/tapl/Workspace.java b/tests/tapl/com/android/launcher3/tapl/Workspace.java
index 9a47aef..e8a0b54 100644
--- a/tests/tapl/com/android/launcher3/tapl/Workspace.java
+++ b/tests/tapl/com/android/launcher3/tapl/Workspace.java
@@ -150,7 +150,8 @@
         LauncherInstrumentation.log("dragIconToWorkspace: sent down");
         launcher.waitForLauncherObject(longPressIndicator);
         LauncherInstrumentation.log("dragIconToWorkspace: indicator");
-        launcher.movePointer(downTime, DRAG_DURACTION, launchableCenter, dest);
+        launcher.movePointer(
+                downTime, SystemClock.uptimeMillis(), DRAG_DURACTION, launchableCenter, dest);
         LauncherInstrumentation.log("dragIconToWorkspace: moved pointer");
         launcher.sendPointer(
                 downTime, SystemClock.uptimeMillis(), MotionEvent.ACTION_UP, dest);