Add a test for START_ACTIVITIES_FROM_BACKGROUND permission.

The shell has this permissions, so we're using it to test it (it's a
system permission, we can't give it to a test app). The idea is to
disable all other ways that the shell is allowed to launch from the
background until this permission is the only thing that allows it to
work. I've tested whether I've accomplished this by locally removing the
permission from shell and observing this test fail.

Test: atest BackgroundActivityLaunchTest
Bug: 171196794
Change-Id: I8fec8a3ca5abb80831820b49b5208f06c82acb41
diff --git a/tests/framework/base/windowmanager/backgroundactivity/src/android/server/wm/BackgroundActivityLaunchTest.java b/tests/framework/base/windowmanager/backgroundactivity/src/android/server/wm/BackgroundActivityLaunchTest.java
index 3cc589a..0b91a9f 100644
--- a/tests/framework/base/windowmanager/backgroundactivity/src/android/server/wm/BackgroundActivityLaunchTest.java
+++ b/tests/framework/base/windowmanager/backgroundactivity/src/android/server/wm/BackgroundActivityLaunchTest.java
@@ -89,6 +89,7 @@
 
     private static final String TEST_PACKAGE_APP_A = "android.server.wm.backgroundactivity.appa";
     private static final String TEST_PACKAGE_APP_B = "android.server.wm.backgroundactivity.appb";
+    private static final String SHELL_PACKAGE = "com.android.shell";
 
     /**
      * Tests can be executed as soon as the device has booted. When that happens the broadcast queue
@@ -127,6 +128,7 @@
         stopTestPackage(TEST_PACKAGE_APP_A);
         stopTestPackage(TEST_PACKAGE_APP_B);
         AppOpsUtils.reset(APP_A_PACKAGE_NAME);
+        AppOpsUtils.reset(SHELL_PACKAGE);
     }
 
     @Test
@@ -141,6 +143,27 @@
     }
 
     @Test
+    public void testStartBgActivity_usingStartActivitiesFromBackgroundPermission()
+            throws Exception {
+        // Disable SAW app op for shell, since that can also allow starting activities from bg.
+        AppOpsUtils.setOpMode(SHELL_PACKAGE, "android:system_alert_window", MODE_ERRORED);
+
+        // Launch the activity via a shell command, this way the system doesn't have info on which
+        // app launched the activity and thus won't use instrumentation privileges to launch it. But
+        // the shell has the START_ACTIVITIES_FROM_BACKGROUND permission, so we expect it to
+        // succeed.
+        // See testBackgroundActivityBlocked() for a case where an app without the
+        // START_ACTIVITIES_FROM_BACKGROUND permission is blocked from launching the activity from
+        // the background.
+        launchActivity(APP_A_BACKGROUND_ACTIVITY);
+
+        // If the activity launches, it means the START_ACTIVITIES_FROM_BACKGROUND permission works.
+        assertEquals("Launched activity should be at the top",
+                ComponentNameUtils.getActivityName(APP_A_BACKGROUND_ACTIVITY),
+                mWmState.getTopActivityName(0));
+    }
+
+    @Test
     @FlakyTest(bugId = 155454710)
     public void testBackgroundActivityNotBlockedWithinGracePeriod() throws Exception {
         // Start AppA foreground activity