Merge "Two panel workspace TAPL follow up CL"
diff --git a/tests/src/com/android/launcher3/ui/workspace/TwoPanelWorkspaceTest.java b/tests/src/com/android/launcher3/ui/workspace/TwoPanelWorkspaceTest.java
index b048cd4..122a130 100644
--- a/tests/src/com/android/launcher3/ui/workspace/TwoPanelWorkspaceTest.java
+++ b/tests/src/com/android/launcher3/ui/workspace/TwoPanelWorkspaceTest.java
@@ -19,6 +19,7 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assume.assumeTrue;
 
 import androidx.test.filters.LargeTest;
 import androidx.test.runner.AndroidJUnit4;
@@ -47,20 +48,12 @@
 @RunWith(AndroidJUnit4.class)
 public class TwoPanelWorkspaceTest extends AbstractLauncherUiTest {
 
-    Workspace mWorkspace;
-
     @Before
     public void setUp() throws Exception {
         super.setUp();
         TaplTestsLauncher3.initialize(this);
-        mWorkspace = mLauncher.getWorkspace();
-    }
 
-    @Test
-    public void testDragIconToRightPanel() {
-        if (!mLauncher.isTwoPanels()) {
-            return;
-        }
+        assumeTrue(mLauncher.isTwoPanels());
 
         // Pre verifying the screens
         executeOnLauncher(launcher -> {
@@ -68,8 +61,15 @@
             assertItemsOnPage(launcher, 0, "Play Store", "Maps");
             assertPageEmpty(launcher, 1);
         });
+    }
 
-        mWorkspace.dragIcon(mWorkspace.getHotseatAppIcon("Chrome"), 1);
+    @Test
+    // TODO(b/197631877) Enable in portrait.
+    // @PortraitLandscape
+    public void testDragIconToRightPanel() {
+        Workspace workspace = mLauncher.getWorkspace();
+
+        workspace.dragIcon(workspace.getHotseatAppIcon("Chrome"), 1);
 
         executeOnLauncher(launcher -> {
             assertPagesExist(launcher, 0, 1);
@@ -79,19 +79,67 @@
     }
 
     @Test
-    public void testDragIconToPage2() {
-        if (!mLauncher.isTwoPanels()) {
-            return;
-        }
+    // TODO(b/197631877) Enable in portrait.
+    // @PortraitLandscape
+    public void testSinglePageDragIconWhenMultiplePageScrollingIsPossible() {
+        Workspace workspace = mLauncher.getWorkspace();
 
-        // Pre verifying the screens
+        workspace.dragIcon(workspace.getHotseatAppIcon("Chrome"), 2);
+
+        workspace.flingBackward();
+
+        workspace.dragIcon(workspace.getWorkspaceAppIcon("Maps"), 3);
+
         executeOnLauncher(launcher -> {
-            assertPagesExist(launcher, 0, 1);
-            assertItemsOnPage(launcher, 0, "Play Store", "Maps");
+            assertPagesExist(launcher, 0, 1, 2, 3);
+            assertItemsOnPage(launcher, 0, "Play Store");
             assertPageEmpty(launcher, 1);
+            assertItemsOnPage(launcher, 2, "Chrome");
+            assertItemsOnPage(launcher, 3, "Maps");
         });
 
-        mWorkspace.dragIcon(mWorkspace.getWorkspaceAppIcon("Maps"), 2);
+        workspace.dragIcon(workspace.getWorkspaceAppIcon("Maps"), 3);
+
+        executeOnLauncher(launcher -> {
+            assertPagesExist(launcher, 0, 1, 2, 3, 4, 5);
+            assertItemsOnPage(launcher, 0, "Play Store");
+            assertPageEmpty(launcher, 1);
+            assertItemsOnPage(launcher, 2, "Chrome");
+            assertPageEmpty(launcher, 3);
+            assertPageEmpty(launcher, 4);
+            assertItemsOnPage(launcher, 5, "Maps");
+        });
+
+        workspace.dragIcon(workspace.getWorkspaceAppIcon("Maps"), -1);
+
+        executeOnLauncher(launcher -> {
+            assertPagesExist(launcher, 0, 1, 2, 3);
+            assertItemsOnPage(launcher, 0, "Play Store");
+            assertPageEmpty(launcher, 1);
+            assertItemsOnPage(launcher, 2, "Chrome");
+            assertItemsOnPage(launcher, 3, "Maps");
+        });
+
+        workspace.dragIcon(workspace.getWorkspaceAppIcon("Maps"), -1);
+
+        workspace.flingForward();
+
+        workspace.dragIcon(workspace.getWorkspaceAppIcon("Chrome"), -2);
+
+        executeOnLauncher(launcher -> {
+            assertPagesExist(launcher, 0, 1);
+            assertItemsOnPage(launcher, 0, "Chrome", "Play Store");
+            assertItemsOnPage(launcher, 1, "Maps");
+        });
+    }
+
+    @Test
+    // TODO(b/197631877) Enable in portrait.
+    // @PortraitLandscape
+    public void testDragIconToPage2() {
+        Workspace workspace = mLauncher.getWorkspace();
+
+        workspace.dragIcon(workspace.getWorkspaceAppIcon("Maps"), 2);
 
         executeOnLauncher(launcher -> {
             assertPagesExist(launcher, 0, 1, 2, 3);
@@ -103,19 +151,12 @@
     }
 
     @Test
+    // TODO(b/197631877) Enable in portrait.
+    // @PortraitLandscape
     public void testDragIconToPage3() {
-        if (!mLauncher.isTwoPanels()) {
-            return;
-        }
+        Workspace workspace = mLauncher.getWorkspace();
 
-        // Pre verifying the screens
-        executeOnLauncher(launcher -> {
-            assertPagesExist(launcher, 0, 1);
-            assertItemsOnPage(launcher, 0, "Play Store", "Maps");
-            assertPageEmpty(launcher, 1);
-        });
-
-        mWorkspace.dragIcon(mWorkspace.getHotseatAppIcon("Phone"), 3);
+        workspace.dragIcon(workspace.getHotseatAppIcon("Phone"), 3);
 
         executeOnLauncher(launcher -> {
             assertPagesExist(launcher, 0, 1, 2, 3);
@@ -126,22 +167,61 @@
         });
     }
 
-
     @Test
-    public void testEmptyPageDoesNotGetRemovedIfPagePairIsNotEmpty() {
-        if (!mLauncher.isTwoPanels()) {
-            return;
-        }
+    // TODO(b/197631877) Enable in portrait.
+    // @PortraitLandscape
+    public void testMultiplePageDragIcon() {
+        Workspace workspace = mLauncher.getWorkspace();
 
-        // Pre verifying the screens
+        workspace.dragIcon(workspace.getHotseatAppIcon("Messages"), 2);
+
+        workspace.flingBackward();
+
+        workspace.dragIcon(workspace.getWorkspaceAppIcon("Maps"), 5);
+
         executeOnLauncher(launcher -> {
-            assertPagesExist(launcher, 0, 1);
-            assertItemsOnPage(launcher, 0, "Play Store", "Maps");
+            assertPagesExist(launcher, 0, 1, 2, 3, 4, 5);
+            assertItemsOnPage(launcher, 0, "Play Store");
             assertPageEmpty(launcher, 1);
+            assertItemsOnPage(launcher, 2, "Messages");
+            assertPageEmpty(launcher, 3);
+            assertPageEmpty(launcher, 4);
+            assertItemsOnPage(launcher, 5, "Maps");
         });
 
-        mWorkspace.dragIcon(mWorkspace.getWorkspaceAppIcon("Maps"), 3);
-        mWorkspace.dragIcon(mWorkspace.getHotseatAppIcon("Chrome"), 0);
+        workspace.flingBackward();
+
+        workspace.dragIcon(workspace.getWorkspaceAppIcon("Messages"), 4);
+
+        executeOnLauncher(launcher -> {
+            assertPagesExist(launcher, 0, 1, 4, 5, 6, 7);
+            assertItemsOnPage(launcher, 0, "Play Store");
+            assertPageEmpty(launcher, 1);
+            assertPageEmpty(launcher, 4);
+            assertItemsOnPage(launcher, 5, "Maps");
+            assertItemsOnPage(launcher, 6, "Messages");
+            assertPageEmpty(launcher, 7);
+        });
+
+        workspace.dragIcon(workspace.getWorkspaceAppIcon("Messages"), -3);
+
+        executeOnLauncher(launcher -> {
+            assertPagesExist(launcher, 0, 1, 4, 5);
+            assertItemsOnPage(launcher, 0, "Play Store");
+            assertItemsOnPage(launcher, 1, "Messages");
+            assertPageEmpty(launcher, 4);
+            assertItemsOnPage(launcher, 5, "Maps");
+        });
+    }
+
+    @Test
+    // TODO(b/197631877) Enable in portrait.
+    // @PortraitLandscape
+    public void testEmptyPageDoesNotGetRemovedIfPagePairIsNotEmpty() {
+        Workspace workspace = mLauncher.getWorkspace();
+
+        workspace.dragIcon(workspace.getWorkspaceAppIcon("Maps"), 3);
+        workspace.dragIcon(workspace.getHotseatAppIcon("Chrome"), 0);
 
         executeOnLauncher(launcher -> {
             assertPagesExist(launcher, 0, 1, 2, 3);
@@ -151,7 +231,7 @@
             assertItemsOnPage(launcher, 3, "Maps");
         });
 
-        mWorkspace.dragIcon(mWorkspace.getWorkspaceAppIcon("Maps"), -1);
+        workspace.dragIcon(workspace.getWorkspaceAppIcon("Maps"), -1);
 
         executeOnLauncher(launcher -> {
             assertPagesExist(launcher, 0, 1, 2, 3);
@@ -163,8 +243,8 @@
 
         // Move Chrome to the right panel as well, to make sure pages are not deleted whichever
         // page is the empty one
-        mWorkspace.flingForward();
-        mWorkspace.dragIcon(mWorkspace.getWorkspaceAppIcon("Chrome"), 1);
+        workspace.flingForward();
+        workspace.dragIcon(workspace.getWorkspaceAppIcon("Chrome"), 1);
 
         executeOnLauncher(launcher -> {
             assertPagesExist(launcher, 0, 1, 2, 3);
@@ -175,22 +255,14 @@
         });
     }
 
-
     @Test
+    // TODO(b/197631877) Enable in portrait.
+    // @PortraitLandscape
     public void testEmptyPagesGetRemovedIfBothPagesAreEmpty() {
-        if (!mLauncher.isTwoPanels()) {
-            return;
-        }
+        Workspace workspace = mLauncher.getWorkspace();
 
-        // Pre verifying the screens
-        executeOnLauncher(launcher -> {
-            assertPagesExist(launcher, 0, 1);
-            assertItemsOnPage(launcher, 0, "Play Store", "Maps");
-            assertPageEmpty(launcher, 1);
-        });
-
-        mWorkspace.dragIcon(mWorkspace.getWorkspaceAppIcon("Play Store"), 2);
-        mWorkspace.dragIcon(mWorkspace.getHotseatAppIcon("Camera"), 1);
+        workspace.dragIcon(workspace.getWorkspaceAppIcon("Play Store"), 2);
+        workspace.dragIcon(workspace.getHotseatAppIcon("Camera"), 1);
 
         executeOnLauncher(launcher -> {
             assertPagesExist(launcher, 0, 1, 2, 3);
@@ -200,9 +272,9 @@
             assertItemsOnPage(launcher, 3, "Camera");
         });
 
-        mWorkspace.dragIcon(mWorkspace.getWorkspaceAppIcon("Camera"), -1);
-        mWorkspace.flingForward();
-        mWorkspace.dragIcon(mWorkspace.getWorkspaceAppIcon("Play Store"), -2);
+        workspace.dragIcon(workspace.getWorkspaceAppIcon("Camera"), -1);
+        workspace.flingForward();
+        workspace.dragIcon(workspace.getWorkspaceAppIcon("Play Store"), -2);
 
         executeOnLauncher(launcher -> {
             assertPagesExist(launcher, 0, 1);
@@ -212,20 +284,13 @@
     }
 
     @Test
+    // TODO(b/197631877) Enable in portrait.
+    // @PortraitLandscape
     public void testMiddleEmptyPagesGetRemoved() {
-        if (!mLauncher.isTwoPanels()) {
-            return;
-        }
+        Workspace workspace = mLauncher.getWorkspace();
 
-        // Pre verifying the screens
-        executeOnLauncher(launcher -> {
-            assertPagesExist(launcher, 0, 1);
-            assertItemsOnPage(launcher, 0, "Play Store", "Maps");
-            assertPageEmpty(launcher, 1);
-        });
-
-        mWorkspace.dragIcon(mWorkspace.getWorkspaceAppIcon("Maps"), 2);
-        mWorkspace.dragIcon(mWorkspace.getHotseatAppIcon("Messages"), 3);
+        workspace.dragIcon(workspace.getWorkspaceAppIcon("Maps"), 2);
+        workspace.dragIcon(workspace.getHotseatAppIcon("Messages"), 3);
 
         executeOnLauncher(launcher -> {
             assertPagesExist(launcher, 0, 1, 2, 3, 4, 5);
@@ -237,8 +302,8 @@
             assertItemsOnPage(launcher, 5, "Messages");
         });
 
-        mWorkspace.flingBackward();
-        mWorkspace.dragIcon(mWorkspace.getWorkspaceAppIcon("Maps"), 2);
+        workspace.flingBackward();
+        workspace.dragIcon(workspace.getWorkspaceAppIcon("Maps"), 2);
 
         executeOnLauncher(launcher -> {
             assertPagesExist(launcher, 0, 1, 4, 5);
diff --git a/tests/tapl/com/android/launcher3/tapl/Workspace.java b/tests/tapl/com/android/launcher3/tapl/Workspace.java
index 3f0d7fd..e457354 100644
--- a/tests/tapl/com/android/launcher3/tapl/Workspace.java
+++ b/tests/tapl/com/android/launcher3/tapl/Workspace.java
@@ -16,6 +16,8 @@
 
 package com.android.launcher3.tapl;
 
+import static android.view.accessibility.AccessibilityEvent.TYPE_VIEW_SCROLLED;
+
 import static com.android.launcher3.testing.TestProtocol.ALL_APPS_STATE_ORDINAL;
 import static com.android.launcher3.testing.TestProtocol.NORMAL_STATE_ORDINAL;
 import static com.android.launcher3.testing.TestProtocol.SPRING_LOADED_STATE_ORDINAL;
@@ -165,15 +167,22 @@
     }
 
     /**
-     * Drags an icon to the (currentPage + pageDelta) page if the page already exists.
-     * If the target page doesn't exist, the icon will be put onto an existing page that is the
-     * closest to the target page.
+     * Drags an icon to the (currentPage + pageDelta) page.
+     * If the target page doesn't exist yet, a new page will be created.
+     * In case the target page can't be created (e.g. existing pages are 0, 1, current: 0,
+     * pageDelta: 3, the latest page that can be created is 2) the icon will be dragged onto the
+     * page that can be created and is closest to the target page.
      *
      * @param appIcon   - icon to drag.
      * @param pageDelta - how many pages should the icon be dragged from the current page.
-     *                    It can be a negative value.
+     *                    It can be a negative value. currentPage + pageDelta should be greater
+     *                    than or equal to 0.
      */
     public void dragIcon(AppIcon appIcon, int pageDelta) {
+        if (mHotseat.getVisibleBounds().height() > mHotseat.getVisibleBounds().width()) {
+            throw new UnsupportedOperationException(
+                    "dragIcon does NOT support dragging when the hotseat is on the side.");
+        }
         try (LauncherInstrumentation.Closable e = mLauncher.eventsCheck()) {
             final UiObject2 workspace = verifyActiveContainer();
             try (LauncherInstrumentation.Closable c = mLauncher.addContextLayer(
@@ -338,9 +347,11 @@
             while (targetDest.x > displayX || targetDest.x < 0) {
                 int edgeX = targetDest.x > 0 ? displayX : 0;
                 Point screenEdge = new Point(edgeX, targetDest.y);
-                launcher.movePointer(dragStart, screenEdge, DEFAULT_DRAG_STEPS, isDecelerating,
-                        downTime, true, LauncherInstrumentation.GestureScope.INSIDE);
-                launcher.waitForIdle(); // Wait for the page change to happen
+                Point finalDragStart = dragStart;
+                executeAndWaitForPageScroll(launcher,
+                        () -> launcher.movePointer(finalDragStart, screenEdge, DEFAULT_DRAG_STEPS,
+                                isDecelerating, downTime, true,
+                                LauncherInstrumentation.GestureScope.INSIDE));
                 targetDest.x += displayX * (targetDest.x > 0 ? -1 : 1);
                 dragStart = screenEdge;
             }
@@ -353,6 +364,13 @@
         }
     }
 
+    private static void executeAndWaitForPageScroll(LauncherInstrumentation launcher,
+            Runnable command) {
+        launcher.executeAndWaitForEvent(command,
+                event -> event.getEventType() == TYPE_VIEW_SCROLLED,
+                () -> "Page scroll didn't happen", "Scrolling page");
+    }
+
     /**
      * Flings to get to screens on the right. Waits for scrolling and a possible overscroll
      * recoil to complete.