Add TaplTestsQuickstep#testExcludeFromRecents

- Add ExcludeFromRecentsTestActivity, and add it to the manifest
  with excludeFromRecents="true"
- Add the test to check the current Overview task content
  description, ensuring that the excludeFromRecents task is
  present directly after a gesture but not after going home.
Bug: 324495241
Flag: None
Test: TaplTestsQuickstep#testExcludeFromRecents

Change-Id: I06be53673f0ac06924f79b44f19744da066cc7ea
diff --git a/quickstep/Android.bp b/quickstep/Android.bp
index a290e84..f14cebd 100644
--- a/quickstep/Android.bp
+++ b/quickstep/Android.bp
@@ -52,5 +52,6 @@
         "tests/src/com/android/quickstep/TaplOverviewIconTest.java",
         "tests/src/com/android/quickstep/TaplTestsQuickstep.java",
         "tests/src/com/android/quickstep/TaplTestsSplitscreen.java",
+        "tests/src/com/android/launcher3/testcomponent/ExcludeFromRecentsTestActivity.java"
     ],
 }
diff --git a/quickstep/tests/src/com/android/launcher3/testcomponent/ExcludeFromRecentsTestActivity.java b/quickstep/tests/src/com/android/launcher3/testcomponent/ExcludeFromRecentsTestActivity.java
new file mode 100644
index 0000000..68ac3d5
--- /dev/null
+++ b/quickstep/tests/src/com/android/launcher3/testcomponent/ExcludeFromRecentsTestActivity.java
@@ -0,0 +1,22 @@
+/*
+ * Copyright (C) 2024 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.testcomponent;
+
+/**
+ * Extension of BaseTestingActivity to help test excludeFromRecents="true".
+ */
+public class ExcludeFromRecentsTestActivity extends BaseTestingActivity {}
diff --git a/quickstep/tests/src/com/android/quickstep/TaplTestsQuickstep.java b/quickstep/tests/src/com/android/quickstep/TaplTestsQuickstep.java
index 45a9527..ca9d823 100644
--- a/quickstep/tests/src/com/android/quickstep/TaplTestsQuickstep.java
+++ b/quickstep/tests/src/com/android/quickstep/TaplTestsQuickstep.java
@@ -21,6 +21,7 @@
 import static com.android.quickstep.TaskbarModeSwitchRule.Mode.TRANSIENT;
 
 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;
@@ -38,6 +39,7 @@
 import com.android.launcher3.Flags;
 import com.android.launcher3.Launcher;
 import com.android.launcher3.LauncherState;
+import com.android.launcher3.tapl.BaseOverview;
 import com.android.launcher3.tapl.LaunchedAppState;
 import com.android.launcher3.tapl.LauncherInstrumentation.NavigationModel;
 import com.android.launcher3.tapl.Overview;
@@ -583,4 +585,25 @@
             mLauncher.getDevice().setOrientationNatural();
         }
     }
+
+    @Test
+    public void testExcludeFromRecents() throws Exception {
+        startExcludeFromRecentsTestActivity();
+        OverviewTask currentTask = getAndAssertLaunchedApp().switchToOverview().getCurrentTask();
+        // TODO(b/326565120): the expected content description shouldn't be null but for now there
+        // is a bug that causes it to sometimes be for excludeForRecents tasks.
+        assertTrue("Can't find ExcludeFromRecentsTestActivity after entering Overview from it",
+                currentTask.containsContentDescription("ExcludeFromRecents")
+                        || currentTask.containsContentDescription(null));
+        // Going home should clear out the excludeFromRecents task.
+        BaseOverview overview = mLauncher.goHome().switchToOverview();
+        if (overview.hasTasks()) {
+            currentTask = overview.getCurrentTask();
+            assertFalse("Found ExcludeFromRecentsTestActivity after entering Overview from Home",
+                    currentTask.containsContentDescription("ExcludeFromRecents")
+                            || currentTask.containsContentDescription(null));
+        } else {
+            // Presumably the test started with 0 tasks and remains that way after going home.
+        }
+    }
 }
diff --git a/tests/AndroidManifest-common.xml b/tests/AndroidManifest-common.xml
index 7cb7964..27dd2a9 100644
--- a/tests/AndroidManifest-common.xml
+++ b/tests/AndroidManifest-common.xml
@@ -388,6 +388,15 @@
                 <category android:name="android.intent.category.LAUNCHER"/>
             </intent-filter>
         </activity-alias>
+        <activity android:name="com.android.launcher3.testcomponent.ExcludeFromRecentsTestActivity"
+            android:label="ExcludeFromRecentsTestActivity"
+            android:exported="true"
+            android:excludeFromRecents="true">
+            <intent-filter>
+                <action android:name="android.intent.action.MAIN"/>
+                <category android:name="android.intent.category.LAUNCHER"/>
+            </intent-filter>
+        </activity>
 
         <!-- [b/197780098] Disable eager initialization of Jetpack libraries. -->
         <provider
diff --git a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java
index a1ff346..4e38847 100644
--- a/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java
+++ b/tests/src/com/android/launcher3/ui/AbstractLauncherUiTest.java
@@ -589,6 +589,17 @@
                 false /* newTask */);
     }
 
+    /** Starts ExcludeFromRecentsTestActivity, which has excludeFromRecents="true". */
+    public static void startExcludeFromRecentsTestActivity() {
+        final String packageName = getAppPackageName();
+        final Intent intent = getInstrumentation().getContext().getPackageManager()
+                .getLaunchIntentForPackage(packageName);
+        intent.setComponent(new ComponentName(packageName,
+                "com.android.launcher3.testcomponent.ExcludeFromRecentsTestActivity"));
+        startIntent(intent, By.pkg(packageName).text("ExcludeFromRecentsTestActivity"),
+                false /* newTask */);
+    }
+
     private static void startIntent(Intent intent, BySelector selector, boolean newTask) {
         intent.addCategory(Intent.CATEGORY_LAUNCHER);
         if (newTask) {
diff --git a/tests/tapl/com/android/launcher3/tapl/OverviewTask.java b/tests/tapl/com/android/launcher3/tapl/OverviewTask.java
index afe5722..6f420af 100644
--- a/tests/tapl/com/android/launcher3/tapl/OverviewTask.java
+++ b/tests/tapl/com/android/launcher3/tapl/OverviewTask.java
@@ -23,6 +23,7 @@
 import android.graphics.Rect;
 
 import androidx.annotation.NonNull;
+import androidx.annotation.Nullable;
 import androidx.test.uiautomator.By;
 import androidx.test.uiautomator.BySelector;
 import androidx.test.uiautomator.UiObject2;
@@ -256,6 +257,23 @@
     }
 
     /**
+     * Returns whether the given String is contained in this Task's contentDescription. Also returns
+     * true if both Strings are null.
+     *
+     * TODO(b/326565120): remove Nullable support once the bug causing it to be null is fixed.
+     */
+    public boolean containsContentDescription(@Nullable String expected) {
+        String actual = mTask.getContentDescription();
+        if (actual == null && expected == null) {
+            return true;
+        }
+        if (actual == null || expected == null) {
+            return false;
+        }
+        return actual.contains(expected);
+    }
+
+    /**
      * Enum used to specify  which task is retrieved when it is a split task.
      */
     public enum OverviewSplitTask {