Merge "Moving testWidgets from TaplTestsLauncher3 to TaplWorkspaceTest" into main
diff --git a/tests/Android.bp b/tests/Android.bp
index 62d232c..a397307 100644
--- a/tests/Android.bp
+++ b/tests/Android.bp
@@ -42,7 +42,7 @@
 filegroup {
     name: "launcher-oop-tests-src",
     srcs: [
-      "src/com/android/launcher3/allapps/OopTaplOpenCloseAllApps.java",
+      "src/com/android/launcher3/allapps/TaplOpenCloseAllApps.java",
       "src/com/android/launcher3/appiconmenu/TaplAppIconMenuTest.java",
       "src/com/android/launcher3/dragging/TaplDragTest.java",
       "src/com/android/launcher3/dragging/TaplUninstallRemove.java",
@@ -50,6 +50,7 @@
       "src/com/android/launcher3/ui/PortraitLandscapeRunner.java",
       "src/com/android/launcher3/ui/TaplTestsLauncher3.java",
       "src/com/android/launcher3/ui/widget/TaplWidgetPickerTest.java",
+      "src/com/android/launcher3/ui/workspace/TaplWorkspaceTest.java",
       "src/com/android/launcher3/util/LauncherLayoutBuilder.java",
       "src/com/android/launcher3/util/TestUtil.java",
       "src/com/android/launcher3/util/Wait.java",
diff --git a/tests/src/com/android/launcher3/allapps/OopTaplOpenCloseAllApps.java b/tests/src/com/android/launcher3/allapps/TaplOpenCloseAllApps.java
similarity index 84%
rename from tests/src/com/android/launcher3/allapps/OopTaplOpenCloseAllApps.java
rename to tests/src/com/android/launcher3/allapps/TaplOpenCloseAllApps.java
index f9dadaa..39dbcb2 100644
--- a/tests/src/com/android/launcher3/allapps/OopTaplOpenCloseAllApps.java
+++ b/tests/src/com/android/launcher3/allapps/TaplOpenCloseAllApps.java
@@ -21,11 +21,17 @@
 import static org.junit.Assert.assertEquals;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
+import static org.junit.Assume.assumeFalse;
 import static org.junit.Assume.assumeTrue;
 
+import android.content.Intent;
 import android.platform.test.annotations.PlatinumTest;
 
+import androidx.test.filters.FlakyTest;
+import androidx.test.platform.app.InstrumentationRegistry;
+
 import com.android.launcher3.LauncherState;
+import com.android.launcher3.config.FeatureFlags;
 import com.android.launcher3.tapl.AllApps;
 import com.android.launcher3.ui.AbstractLauncherUiTest;
 import com.android.launcher3.ui.PortraitLandscapeRunner.PortraitLandscape;
@@ -37,7 +43,10 @@
  * Test that we can open and close the all apps in multiple situations.
  * The test runs in Out of process (Oop) and in process.
  */
-public class OopTaplOpenCloseAllApps extends AbstractLauncherUiTest {
+public class TaplOpenCloseAllApps extends AbstractLauncherUiTest {
+
+    public static final String READ_DEVICE_CONFIG_PERMISSION =
+            "android.permission.READ_DEVICE_CONFIG";
 
     /**
      * Calls static method initialize
@@ -188,4 +197,24 @@
             allApps.unfreeze();
         }
     }
+
+    /**
+     * Makes sure that when pressing back when AllApps is open we go back to the Home screen.
+     */
+    @FlakyTest(bugId = 256615483)
+    @Test
+    @PortraitLandscape
+    public void testPressBackFromAllAppsToHome() {
+        InstrumentationRegistry.getInstrumentation().getUiAutomation().adoptShellPermissionIdentity(
+                READ_DEVICE_CONFIG_PERMISSION);
+        assumeFalse(FeatureFlags.ENABLE_BACK_SWIPE_LAUNCHER_ANIMATION.get());
+        mLauncher.getWorkspace().switchToAllApps();
+        mLauncher.pressBack();
+        mLauncher.getWorkspace();
+        waitForState("Launcher internal state didn't switch to Home", () -> LauncherState.NORMAL);
+        startAppFast(resolveSystemApp(Intent.CATEGORY_APP_CALCULATOR));
+        mLauncher.pressBack();
+        mLauncher.getWorkspace();
+        waitForState("Launcher internal state didn't switch to Home", () -> LauncherState.NORMAL);
+    }
 }
diff --git a/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java b/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java
index 9aaca54..25da9ed 100644
--- a/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java
+++ b/tests/src/com/android/launcher3/ui/TaplTestsLauncher3.java
@@ -16,34 +16,19 @@
 
 package com.android.launcher3.ui;
 
-import static org.junit.Assert.assertEquals;
-import static org.junit.Assert.assertFalse;
 import static org.junit.Assert.assertNotNull;
 import static org.junit.Assert.assertTrue;
-import static org.junit.Assume.assumeFalse;
 
-import android.content.Intent;
-import android.platform.test.annotations.PlatinumTest;
-
-import androidx.test.filters.FlakyTest;
 import androidx.test.filters.LargeTest;
-import androidx.test.platform.app.InstrumentationRegistry;
 import androidx.test.runner.AndroidJUnit4;
 
-import com.android.launcher3.Launcher;
 import com.android.launcher3.LauncherState;
-import com.android.launcher3.config.FeatureFlags;
 import com.android.launcher3.tapl.AllApps;
 import com.android.launcher3.tapl.AppIcon;
 import com.android.launcher3.tapl.HomeAllApps;
-import com.android.launcher3.tapl.HomeAppIcon;
-import com.android.launcher3.tapl.Workspace;
 import com.android.launcher3.ui.PortraitLandscapeRunner.PortraitLandscape;
-import com.android.launcher3.util.LauncherLayoutBuilder;
-import com.android.launcher3.util.TestUtil;
 import com.android.launcher3.util.rule.TISBindRule;
 
-import org.junit.After;
 import org.junit.Before;
 import org.junit.Rule;
 import org.junit.Test;
@@ -57,14 +42,10 @@
     public static final String MAPS_APP_NAME = "Maps";
     public static final String STORE_APP_NAME = "Play Store";
     public static final String GMAIL_APP_NAME = "Gmail";
-    private static final String READ_DEVICE_CONFIG_PERMISSION =
-            "android.permission.READ_DEVICE_CONFIG";
 
     @Rule
     public TISBindRule mTISBindRule = new TISBindRule();
 
-    private AutoCloseable mLauncherLayout;
-
     @Before
     public void setUp() throws Exception {
         super.setUp();
@@ -88,13 +69,6 @@
         AbstractLauncherUiTest.checkDetectedLeaks(test.mLauncher, true);
     }
 
-    @After
-    public void tearDown() throws Exception {
-        if (mLauncherLayout != null) {
-            mLauncherLayout.close();
-        }
-    }
-
     // Please don't add negative test cases for methods that fail only after a long wait.
     public static void expectFail(String message, Runnable action) {
         boolean failed = false;
@@ -106,14 +80,6 @@
         assertTrue(message, failed);
     }
 
-    public static boolean isWorkspaceScrollable(Launcher launcher) {
-        return launcher.getWorkspace().getPageCount() > launcher.getWorkspace().getPanelCount();
-    }
-
-    private int getCurrentWorkspacePage(Launcher launcher) {
-        return launcher.getWorkspace().getCurrentPage();
-    }
-
     @Test
     public void testDevicePressMenu() throws Exception {
         mDevice.pressMenu();
@@ -125,53 +91,6 @@
         mLauncher.goHome();
     }
 
-    @PlatinumTest(focusArea = "launcher")
-    @Test
-    public void testWorkspace() throws Exception {
-        // Set workspace  that includes the chrome Activity app icon on the hotseat.
-        LauncherLayoutBuilder builder = new LauncherLayoutBuilder()
-                .atHotseat(0).putApp("com.android.chrome", "com.google.android.apps.chrome.Main");
-        mLauncherLayout = TestUtil.setLauncherDefaultLayout(mTargetContext, builder);
-        reinitializeLauncherData();
-
-        final Workspace workspace = mLauncher.getWorkspace();
-
-        // Test that ensureWorkspaceIsScrollable adds a page by dragging an icon there.
-        executeOnLauncher(launcher -> assertFalse("Initial workspace state is scrollable",
-                isWorkspaceScrollable(launcher)));
-        assertEquals("Initial workspace doesn't have the correct page", workspace.pagesPerScreen(),
-                workspace.getPageCount());
-        workspace.verifyWorkspaceAppIconIsGone("Chrome app was found on empty workspace", "Chrome");
-        workspace.ensureWorkspaceIsScrollable();
-
-        executeOnLauncher(
-                launcher -> assertEquals(
-                        "Ensuring workspace scrollable didn't switch to next screen",
-                        workspace.pagesPerScreen(), getCurrentWorkspacePage(launcher)));
-        executeOnLauncher(
-                launcher -> assertTrue("ensureScrollable didn't make workspace scrollable",
-                        isWorkspaceScrollable(launcher)));
-        assertNotNull("ensureScrollable didn't add Chrome app",
-                workspace.getWorkspaceAppIcon("Chrome"));
-
-        // Test flinging workspace.
-        workspace.flingBackward();
-        assertTrue("Launcher internal state is not Home", isInState(() -> LauncherState.NORMAL));
-        executeOnLauncher(
-                launcher -> assertEquals("Flinging back didn't switch workspace to page #0",
-                        0, getCurrentWorkspacePage(launcher)));
-
-        workspace.flingForward();
-        executeOnLauncher(
-                launcher -> assertEquals("Flinging forward didn't switch workspace to next screen",
-                        workspace.pagesPerScreen(), getCurrentWorkspacePage(launcher)));
-        assertTrue("Launcher internal state is not Home", isInState(() -> LauncherState.NORMAL));
-
-        // Test starting a workspace app.
-        final HomeAppIcon app = workspace.getWorkspaceAppIcon("Chrome");
-        assertNotNull("No Chrome app in workspace", app);
-    }
-
     public static void runIconLaunchFromAllAppsTest(AbstractLauncherUiTest test, AllApps allApps) {
         allApps.freeze();
         try {
@@ -196,23 +115,6 @@
         runIconLaunchFromAllAppsTest(this, allApps);
     }
 
-    @FlakyTest(bugId = 256615483)
-    @Test
-    @PortraitLandscape
-    public void testPressBack() throws Exception {
-        InstrumentationRegistry.getInstrumentation().getUiAutomation().adoptShellPermissionIdentity(
-                READ_DEVICE_CONFIG_PERMISSION);
-        assumeFalse(FeatureFlags.ENABLE_BACK_SWIPE_LAUNCHER_ANIMATION.get());
-        mLauncher.getWorkspace().switchToAllApps();
-        mLauncher.pressBack();
-        mLauncher.getWorkspace();
-        waitForState("Launcher internal state didn't switch to Home", () -> LauncherState.NORMAL);
-        startAppFast(resolveSystemApp(Intent.CATEGORY_APP_CALCULATOR));
-        mLauncher.pressBack();
-        mLauncher.getWorkspace();
-        waitForState("Launcher internal state didn't switch to Home", () -> LauncherState.NORMAL);
-    }
-
     @Test
     @PortraitLandscape
     public void testAddDeleteShortcutOnHotseat() {
@@ -236,28 +138,4 @@
             allApps.unfreeze();
         }
     }
-
-    @PlatinumTest(focusArea = "launcher")
-    @Test
-    public void testAddAndDeletePageAndFling() {
-        Workspace workspace = mLauncher.getWorkspace();
-        // Get the first app from the hotseat
-        HomeAppIcon hotSeatIcon = workspace.getHotseatAppIcon(0);
-        String appName = hotSeatIcon.getIconName();
-
-        // Add one page by dragging app to page 1.
-        workspace.dragIcon(hotSeatIcon, workspace.pagesPerScreen());
-        assertEquals("Incorrect Page count Number",
-                workspace.pagesPerScreen() * 2,
-                workspace.getPageCount());
-
-        // Delete one page by dragging app to hot seat.
-        workspace.getWorkspaceAppIcon(appName).dragToHotseat(0);
-
-        // Refresh workspace to avoid using stale container error.
-        workspace = mLauncher.getWorkspace();
-        assertEquals("Incorrect Page count Number",
-                workspace.pagesPerScreen(),
-                workspace.getPageCount());
-    }
 }
diff --git a/tests/src/com/android/launcher3/ui/workspace/TaplWorkspaceTest.java b/tests/src/com/android/launcher3/ui/workspace/TaplWorkspaceTest.java
new file mode 100644
index 0000000..d8ae99c
--- /dev/null
+++ b/tests/src/com/android/launcher3/ui/workspace/TaplWorkspaceTest.java
@@ -0,0 +1,147 @@
+/*
+ * Copyright (C) 2023 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ *      http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package com.android.launcher3.ui.workspace;
+
+import static com.android.launcher3.ui.TaplTestsLauncher3.initialize;
+
+import static org.junit.Assert.assertEquals;
+import static org.junit.Assert.assertFalse;
+import static org.junit.Assert.assertNotNull;
+import static org.junit.Assert.assertTrue;
+
+import android.platform.test.annotations.PlatinumTest;
+
+import com.android.launcher3.Launcher;
+import com.android.launcher3.LauncherState;
+import com.android.launcher3.tapl.HomeAppIcon;
+import com.android.launcher3.tapl.Workspace;
+import com.android.launcher3.ui.AbstractLauncherUiTest;
+import com.android.launcher3.util.LauncherLayoutBuilder;
+import com.android.launcher3.util.TestUtil;
+
+import org.junit.After;
+import org.junit.Before;
+import org.junit.Test;
+
+/**
+ * Test the basic interactions of the Workspace, adding pages, moving the pages and removing pages.
+ */
+public class TaplWorkspaceTest extends AbstractLauncherUiTest {
+
+    private AutoCloseable mLauncherLayout;
+
+    private static boolean isWorkspaceScrollable(Launcher launcher) {
+        return launcher.getWorkspace().getPageCount() > launcher.getWorkspace().getPanelCount();
+    }
+
+    private int getCurrentWorkspacePage(Launcher launcher) {
+        return launcher.getWorkspace().getCurrentPage();
+    }
+
+    @Before
+    public void setUp() throws Exception {
+        super.setUp();
+        initialize(this);
+    }
+
+    @After
+    public void tearDown() throws Exception {
+        if (mLauncherLayout != null) {
+            mLauncherLayout.close();
+        }
+    }
+
+    /**
+     * Add an icon and add a page to ensure the Workspace is scrollable and also make sure we can
+     * move between workspaces. After, make sure we can launch an app from the Workspace.
+     * @throws Exception if we can't set the defaults icons that will appear at the beginning.
+     */
+    @PlatinumTest(focusArea = "launcher")
+    @Test
+    public void testWorkspace() throws Exception {
+        // Set workspace  that includes the chrome Activity app icon on the hotseat.
+        LauncherLayoutBuilder builder = new LauncherLayoutBuilder()
+                .atHotseat(0).putApp("com.android.chrome", "com.google.android.apps.chrome.Main");
+        mLauncherLayout = TestUtil.setLauncherDefaultLayout(mTargetContext, builder);
+        reinitializeLauncherData();
+
+        final Workspace workspace = mLauncher.getWorkspace();
+
+        // Test that ensureWorkspaceIsScrollable adds a page by dragging an icon there.
+        executeOnLauncher(launcher -> assertFalse("Initial workspace state is scrollable",
+                isWorkspaceScrollable(launcher)));
+        assertEquals("Initial workspace doesn't have the correct page", workspace.pagesPerScreen(),
+                workspace.getPageCount());
+        workspace.verifyWorkspaceAppIconIsGone("Chrome app was found on empty workspace", "Chrome");
+        workspace.ensureWorkspaceIsScrollable();
+
+        executeOnLauncher(
+                launcher -> assertEquals(
+                        "Ensuring workspace scrollable didn't switch to next screen",
+                        workspace.pagesPerScreen(), getCurrentWorkspacePage(launcher)));
+        executeOnLauncher(
+                launcher -> assertTrue("ensureScrollable didn't make workspace scrollable",
+                        isWorkspaceScrollable(launcher)));
+        assertNotNull("ensureScrollable didn't add Chrome app",
+                workspace.getWorkspaceAppIcon("Chrome"));
+
+        // Test flinging workspace.
+        workspace.flingBackward();
+        assertTrue("Launcher internal state is not Home", isInState(() -> LauncherState.NORMAL));
+        executeOnLauncher(
+                launcher -> assertEquals("Flinging back didn't switch workspace to page #0",
+                        0, getCurrentWorkspacePage(launcher)));
+
+        workspace.flingForward();
+        executeOnLauncher(
+                launcher -> assertEquals("Flinging forward didn't switch workspace to next screen",
+                        workspace.pagesPerScreen(), getCurrentWorkspacePage(launcher)));
+        assertTrue("Launcher internal state is not Home", isInState(() -> LauncherState.NORMAL));
+
+        // Test starting a workspace app.
+        final HomeAppIcon app = workspace.getWorkspaceAppIcon("Chrome");
+        assertNotNull("No Chrome app in workspace", app);
+    }
+
+
+    /**
+     * Similar to {@link TaplWorkspaceTest#testWorkspace} but here we also make sure we can delete
+     * the pages.
+     */
+    @PlatinumTest(focusArea = "launcher")
+    @Test
+    public void testAddAndDeletePageAndFling() {
+        Workspace workspace = mLauncher.getWorkspace();
+        // Get the first app from the hotseat
+        HomeAppIcon hotSeatIcon = workspace.getHotseatAppIcon(0);
+        String appName = hotSeatIcon.getIconName();
+
+        // Add one page by dragging app to page 1.
+        workspace.dragIcon(hotSeatIcon, workspace.pagesPerScreen());
+        assertEquals("Incorrect Page count Number",
+                workspace.pagesPerScreen() * 2,
+                workspace.getPageCount());
+
+        // Delete one page by dragging app to hot seat.
+        workspace.getWorkspaceAppIcon(appName).dragToHotseat(0);
+
+        // Refresh workspace to avoid using stale container error.
+        workspace = mLauncher.getWorkspace();
+        assertEquals("Incorrect Page count Number",
+                workspace.pagesPerScreen(),
+                workspace.getPageCount());
+    }
+}