Add CTS test for IntentForwarderActivity starting share sheet in other tab

Fixes: 157572151
Test: atest ManagedProfileTest#testChooserActivityLaunchedFromPersonalProfileWithSelectedWorkTab
Test: atest ManagedProfileTest#testChooserActivityLaunchedFromWorkProfileWithSelectedPersonalTab
Change-Id: I684dd01ce4f1cad30b5b4d71fee89b2cc43c0eec
diff --git a/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/CrossProfileSharingTest.java b/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/CrossProfileSharingTest.java
index fa33e18..65261e1 100644
--- a/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/CrossProfileSharingTest.java
+++ b/hostsidetests/devicepolicy/app/ManagedProfile/src/com/android/cts/managedprofile/CrossProfileSharingTest.java
@@ -32,8 +32,6 @@
 import androidx.test.InstrumentationRegistry;
 import androidx.test.runner.AndroidJUnit4;
 
-import com.google.common.truth.Truth;
-
 import org.junit.Test;
 import org.junit.runner.RunWith;
 
@@ -47,7 +45,7 @@
 
     @Test
     public void startSwitchToOtherProfileIntent() {
-        Intent intent = getBrowserIntent();
+        Intent intent = getSendIntent();
         Context context = InstrumentationRegistry.getContext();
         List<ResolveInfo> resolveInfos =
                 context.getPackageManager().queryIntentActivities(intent,
@@ -67,6 +65,28 @@
     }
 
     @Test
+    public void startSwitchToOtherProfileIntent_chooser() {
+        Intent intent = getSendIntent();
+        Context context = InstrumentationRegistry.getContext();
+        List<ResolveInfo> resolveInfos =
+                context.getPackageManager().queryIntentActivities(intent,
+                        PackageManager.MATCH_DEFAULT_ONLY);
+        ResolveInfo switchToOtherProfileResolveInfo =
+                getSwitchToOtherProfileResolveInfo(resolveInfos);
+        assertWithMessage("Could not retrieve the switch to other profile resolve info.")
+                .that(switchToOtherProfileResolveInfo)
+                .isNotNull();
+        ActivityInfo activityInfo = switchToOtherProfileResolveInfo.activityInfo;
+        ComponentName componentName =
+                new ComponentName(activityInfo.packageName, activityInfo.name);
+        Intent chooserIntent = Intent.createChooser(intent, /* title */ null);
+        Intent switchToOtherProfileIntent = new Intent(chooserIntent);
+        switchToOtherProfileIntent.setComponent(componentName);
+        switchToOtherProfileIntent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
+        context.startActivity(switchToOtherProfileIntent);
+    }
+
+    @Test
     public void addCrossProfileIntents()
             throws IntentFilter.MalformedMimeTypeException {
         Context context = InstrumentationRegistry.getContext();
@@ -89,7 +109,7 @@
         devicePolicyManager.clearCrossProfileIntentFilters(ADMIN_RECEIVER_COMPONENT);
     }
 
-    private Intent getBrowserIntent() {
+    private Intent getSendIntent() {
         Intent intent = new Intent(Intent.ACTION_SEND);
         intent.addCategory(Intent.CATEGORY_DEFAULT);
         intent.putExtra(Intent.EXTRA_TEXT, "test example");
diff --git a/hostsidetests/devicepolicy/app/SharingApps/sharingapp1/AndroidManifest.xml b/hostsidetests/devicepolicy/app/SharingApps/sharingapp1/AndroidManifest.xml
index d859141..e6c7f42 100644
--- a/hostsidetests/devicepolicy/app/SharingApps/sharingapp1/AndroidManifest.xml
+++ b/hostsidetests/devicepolicy/app/SharingApps/sharingapp1/AndroidManifest.xml
@@ -22,8 +22,7 @@
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
         package="com.android.cts.sharingapps.sharingapp1">
     <uses-permission android:name="android.permission.INTERACT_ACROSS_PROFILES"/>
-    <application android:testOnly="true"
-            android:crossProfile="true">
+    <application android:testOnly="true">
         <uses-library android:name="android.test.runner" />
         <activity android:name=".SimpleActivity" >
             <intent-filter>
diff --git a/hostsidetests/devicepolicy/app/SharingApps/sharingapp2/AndroidManifest.xml b/hostsidetests/devicepolicy/app/SharingApps/sharingapp2/AndroidManifest.xml
index e55af7f..dbd3be3 100644
--- a/hostsidetests/devicepolicy/app/SharingApps/sharingapp2/AndroidManifest.xml
+++ b/hostsidetests/devicepolicy/app/SharingApps/sharingapp2/AndroidManifest.xml
@@ -20,10 +20,9 @@
   ~ and UID. For example, you could use it to set permissions or app-ops.
   -->
 <manifest xmlns:android="http://schemas.android.com/apk/res/android"
-          package="com.android.cts.sharingapps.sharingapp2">
+        package="com.android.cts.sharingapps.sharingapp2">
     <uses-permission android:name="android.permission.INTERACT_ACROSS_PROFILES"/>
-    <application android:testOnly="true"
-                 android:crossProfile="true">
+    <application android:testOnly="true">
         <uses-library android:name="android.test.runner" />
         <activity android:name=".SimpleActivity" >
             <intent-filter>
diff --git a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java
index d480605..fb3ee60 100644
--- a/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java
+++ b/hostsidetests/devicepolicy/src/com/android/cts/devicepolicy/ManagedProfileTest.java
@@ -37,7 +37,6 @@
 import org.junit.Ignore;
 import org.junit.Test;
 
-import java.io.FileNotFoundException;
 import java.util.HashMap;
 import java.util.Map;
 
@@ -713,7 +712,7 @@
 
     @Test
     public void testResolverActivityLaunchedFromPersonalProfileWithSelectedWorkTab()
-            throws FileNotFoundException, DeviceNotAvailableException {
+            throws Exception {
         if (!mHasFeature) {
             return;
         }
@@ -728,6 +727,7 @@
                     "startSwitchToOtherProfileIntent", mPrimaryUserId);
             assertResolverActivityInForeground(mPrimaryUserId);
         } finally {
+            pressHome();
             runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileSharingTest",
                     "clearCrossProfileIntents", mProfileUserId);
         }
@@ -735,7 +735,7 @@
 
     @Test
     public void testResolverActivityLaunchedFromWorkProfileWithSelectedPersonalTab()
-            throws FileNotFoundException, DeviceNotAvailableException {
+            throws Exception {
         if (!mHasFeature) {
             return;
         }
@@ -750,17 +750,77 @@
                     "startSwitchToOtherProfileIntent", mProfileUserId);
             assertResolverActivityInForeground(mProfileUserId);
         } finally {
+            pressHome();
             runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileSharingTest",
                     "clearCrossProfileIntents", mProfileUserId);
         }
     }
 
+    @Test
+    public void testChooserActivityLaunchedFromPersonalProfileWithSelectedWorkTab()
+            throws Exception {
+        if (!mHasFeature) {
+            return;
+        }
+        installAppAsUser(SHARING_APP_1_APK, mPrimaryUserId);
+        installAppAsUser(SHARING_APP_2_APK, mPrimaryUserId);
+        installAppAsUser(SHARING_APP_1_APK, mProfileUserId);
+        installAppAsUser(SHARING_APP_2_APK, mProfileUserId);
+        try {
+            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileSharingTest",
+                    "addCrossProfileIntents", mProfileUserId);
+            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileSharingTest",
+                    "startSwitchToOtherProfileIntent_chooser", mPrimaryUserId);
+            assertChooserActivityInForeground(mPrimaryUserId);
+        } finally {
+            pressHome();
+            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileSharingTest",
+                    "clearCrossProfileIntents", mProfileUserId);
+        }
+    }
+
+    @Test
+    public void testChooserActivityLaunchedFromWorkProfileWithSelectedPersonalTab()
+            throws Exception {
+        if (!mHasFeature) {
+            return;
+        }
+        installAppAsUser(SHARING_APP_1_APK, mPrimaryUserId);
+        installAppAsUser(SHARING_APP_2_APK, mPrimaryUserId);
+        installAppAsUser(SHARING_APP_1_APK, mProfileUserId);
+        installAppAsUser(SHARING_APP_2_APK, mProfileUserId);
+        try {
+            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileSharingTest",
+                    "addCrossProfileIntents", mProfileUserId);
+            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileSharingTest",
+                    "startSwitchToOtherProfileIntent_chooser", mProfileUserId);
+            assertChooserActivityInForeground(mProfileUserId);
+        } finally {
+            pressHome();
+            runDeviceTestsAsUser(MANAGED_PROFILE_PKG, ".CrossProfileSharingTest",
+                    "clearCrossProfileIntents", mProfileUserId);
+        }
+    }
+
+    private void pressHome() throws Exception {
+        executeShellCommand("input keyevent KEYCODE_HOME");
+    }
+
+    private void assertChooserActivityInForeground(int userId)
+            throws DeviceNotAvailableException {
+        assertActivityInForeground("android/com.android.internal.app.ChooserActivity", userId);
+    }
+
     private void assertResolverActivityInForeground(int userId)
             throws DeviceNotAvailableException {
+        assertActivityInForeground("android/com.android.internal.app.ResolverActivity", userId);
+    }
+
+    private void assertActivityInForeground(String fullActivityName, int userId)
+            throws DeviceNotAvailableException {
         String commandOutput =
-                getDevice().executeShellCommand("dumpsys activity | grep Recent #0");
-        assertThat(commandOutput).contains(
-                "android/com.android.internal.app.ResolverActivity U=" + userId);
+                getDevice().executeShellCommand("dumpsys activity activities | grep Resumed:");
+        assertThat(commandOutput).contains("u" + userId + " " + fullActivityName);
     }
 
     private void changeUserRestrictionOrFail(String key, boolean value, int userId)