Merge "Update CTS test for FGS while-in-use permission restriction to reflect enforcement mode" into rvc-dev
diff --git a/tests/app/ActivityManagerApi29Test/src/android/app/cts/LocationForegroundService.java b/tests/app/ActivityManagerApi29Test/src/android/app/cts/LocationForegroundService.java
index 86ea295..b9fa812 100644
--- a/tests/app/ActivityManagerApi29Test/src/android/app/cts/LocationForegroundService.java
+++ b/tests/app/ActivityManagerApi29Test/src/android/app/cts/LocationForegroundService.java
@@ -27,6 +27,9 @@
     private static final String NOTIFICATION_CHANNEL_ID =
             LocationForegroundService.class.getSimpleName();
 
+    public static String ACTION_SERVICE_START_RESULT =
+            "android.app.cts.activitymanager.api29.LocationForegroundService.RESULT";
+
     @Override
     public int onStartCommand(Intent intent, int flags, int startId) {
         getSystemService(NotificationManager.class).createNotificationChannel(
@@ -37,6 +40,8 @@
                 .setSmallIcon(R.drawable.black)
                 .build();
         startForeground(1, status);
+        sendBroadcast(
+                new Intent(ACTION_SERVICE_START_RESULT).setFlags(Intent.FLAG_RECEIVER_FOREGROUND));
         return START_STICKY;
     }
 
diff --git a/tests/app/ActivityManagerApi29Test/src/android/app/cts/SimpleActivity.java b/tests/app/ActivityManagerApi29Test/src/android/app/cts/SimpleActivity.java
index aa28b1f..6af83d0 100644
--- a/tests/app/ActivityManagerApi29Test/src/android/app/cts/SimpleActivity.java
+++ b/tests/app/ActivityManagerApi29Test/src/android/app/cts/SimpleActivity.java
@@ -24,6 +24,9 @@
  * A simple activity to install for various users to test LauncherApps.
  */
 public class SimpleActivity extends Activity {
+    public static String ACTION_SIMPLE_ACTIVITY_START_RESULT =
+            "android.app.cts.activitymanager.api29.SimpleActivity.RESULT";
+
     @Override
     public void onCreate(Bundle icicle) {
         super.onCreate(icicle);
@@ -35,6 +38,14 @@
     }
 
     @Override
+    public void onResume() {
+        super.onResume();
+        Intent reply = new Intent(ACTION_SIMPLE_ACTIVITY_START_RESULT);
+        reply.setFlags(Intent.FLAG_RECEIVER_FOREGROUND);
+        sendBroadcast(reply);
+    }
+
+    @Override
     protected void onNewIntent(Intent intent) {
         super.onNewIntent(intent);
         if (intent.getExtras().getBoolean("finish")) {
diff --git a/tests/app/src/android/app/cts/ActivityManagerApi29Test.java b/tests/app/src/android/app/cts/ActivityManagerApi29Test.java
index a1fad70..26b44f1 100644
--- a/tests/app/src/android/app/cts/ActivityManagerApi29Test.java
+++ b/tests/app/src/android/app/cts/ActivityManagerApi29Test.java
@@ -17,6 +17,7 @@
 
 import static android.Manifest.permission.ACCESS_COARSE_LOCATION;
 import static android.app.ActivityManager.PROCESS_CAPABILITY_ALL;
+import static android.app.ActivityManager.PROCESS_CAPABILITY_FOREGROUND_LOCATION;
 import static android.app.ActivityManager.PROCESS_CAPABILITY_NONE;
 import static android.app.AppOpsManager.MODE_ALLOWED;
 import static android.app.AppOpsManager.MODE_FOREGROUND;
@@ -38,6 +39,7 @@
 import android.app.AppOpsManager.HistoricalOps;
 import android.app.AppOpsManager.HistoricalOpsRequest;
 import android.app.Instrumentation;
+import android.app.cts.android.app.cts.tools.WaitForBroadcast;
 import android.app.cts.android.app.cts.tools.WatchUidRunner;
 import android.content.Context;
 import android.content.Intent;
@@ -46,7 +48,6 @@
 import android.provider.DeviceConfig;
 import android.provider.Settings;
 
-import androidx.test.filters.Suppress;
 import androidx.test.InstrumentationRegistry;
 import androidx.test.runner.AndroidJUnit4;
 
@@ -69,17 +70,27 @@
  * when the process is in background state.
  */
 @RunWith(AndroidJUnit4.class)
-@Suppress
+//@Suppress
 public class ActivityManagerApi29Test {
     private static final String PACKAGE_NAME = "android.app.cts.activitymanager.api29";
     private static final String SIMPLE_ACTIVITY = ".SimpleActivity";
+    private static final String ACTION_SIMPLE_ACTIVITY_START_RESULT =
+            "android.app.cts.activitymanager.api29.SimpleActivity.RESULT";
+    private static final String ACTION_SERVICE_START_RESULT =
+            "android.app.cts.activitymanager.api29.LocationForegroundService.RESULT";
     private static final String SERVICE_NAME = ".LocationForegroundService";
     private static final String PROPERTY_PERMISSIONS_HUB_ENABLED = "permissions_hub_enabled";
     private static final int WAITFOR_MSEC = 10000;
     private static final int NOTEOP_COUNT = 5;
-    private static Instrumentation sInstrumentation = InstrumentationRegistry.getInstrumentation();
-    private static Context sContext = sInstrumentation.getContext();
-    private static AppOpsManager sAppOps =
+
+    //TODO: remove this when development is done.
+    private static final int TEMP_PROCESS_CAPABILITY_FOREGROUND_LOCATION = 1 << 31;
+
+    private static final Instrumentation sInstrumentation =
+            InstrumentationRegistry.getInstrumentation();
+    private static final Context sContext = sInstrumentation.getContext();
+    private static final Context sTargetContext = sInstrumentation.getTargetContext();
+    private static final AppOpsManager sAppOps =
             (AppOpsManager) sContext.getSystemService(AppOpsManager.class);
     private static Intent sActivityIntent;
     private static Intent sServiceIntent = new Intent().setClassName(
@@ -142,21 +153,21 @@
         sActivityIntent = new Intent(Intent.ACTION_MAIN)
                 .setClassName(PACKAGE_NAME, PACKAGE_NAME + SIMPLE_ACTIVITY)
                 .addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
-        sContext.startActivity(sActivityIntent);
+        sTargetContext.startActivity(sActivityIntent);
     }
 
     private void stopSimpleActivity() {
         sActivityIntent.addFlags(Intent.FLAG_ACTIVITY_SINGLE_TOP)
                 .putExtra("finish", true);
-        sContext.startActivity(sActivityIntent);
+        sTargetContext.startActivity(sActivityIntent);
     }
 
     private void startSimpleService() {
-        sContext.startForegroundService(sServiceIntent);
+        sTargetContext.startForegroundService(sServiceIntent);
     }
 
     private void stopSimpleService() {
-        sContext.stopService(sServiceIntent);
+        sTargetContext.stopService(sServiceIntent);
     }
 
     /**
@@ -198,7 +209,7 @@
         // Wait for state and capability change.
         // BG started FGS does not have location capability.
         mUidWatcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_FG_SERVICE,
-                new Integer(PROCESS_CAPABILITY_NONE));
+                new Integer(TEMP_PROCESS_CAPABILITY_FOREGROUND_LOCATION));
 
         startSimpleActivity();
         mUidWatcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_TOP,
@@ -209,7 +220,7 @@
 
         stopSimpleActivity();
         mUidWatcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_FG_SERVICE,
-                new Integer(PROCESS_CAPABILITY_NONE));
+                new Integer(TEMP_PROCESS_CAPABILITY_FOREGROUND_LOCATION));
 
         // AppOps location access should be denied.
         assertEquals(MODE_IGNORED, noteOp(OPSTR_COARSE_LOCATION));
@@ -231,23 +242,33 @@
      */
     @Test
     public void testAppOpsHistoricalOps() throws Exception {
-        startSimpleActivity();
-        startSimpleService();
-        // Wait for state and capability change.
-        mUidWatcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_TOP,
-                new Integer(PROCESS_CAPABILITY_ALL));
-
         runWithShellPermissionIdentity(
                 () ->  sAppOps.setHistoryParameters(AppOpsManager.HISTORICAL_MODE_ENABLED_ACTIVE,
                         1000, 10)
         );
+        WaitForBroadcast waiter = new WaitForBroadcast(sInstrumentation.getTargetContext());
+        waiter.prepare(ACTION_SIMPLE_ACTIVITY_START_RESULT);
+        startSimpleActivity();
+        mUidWatcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_TOP,
+                new Integer(PROCESS_CAPABILITY_ALL));
+        waiter.doWait(WAITFOR_MSEC);
+
+        waiter = new WaitForBroadcast(sInstrumentation.getTargetContext());
+        waiter.prepare(ACTION_SERVICE_START_RESULT);
+        startSimpleService();
+        waiter.doWait(WAITFOR_MSEC);
+
         for (int i = 0; i < NOTEOP_COUNT; i++) {
             noteOp(OPSTR_COARSE_LOCATION);
         }
 
         stopSimpleActivity();
+        // The callingPackage to start FGS is in background.
         mUidWatcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_FG_SERVICE,
-                new Integer(PROCESS_CAPABILITY_NONE));
+                new Integer(TEMP_PROCESS_CAPABILITY_FOREGROUND_LOCATION));
+        for (int i = 0; i < NOTEOP_COUNT; i++) {
+            noteOp(OPSTR_COARSE_LOCATION);
+        }
         stopSimpleService();
         mUidWatcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_CACHED_EMPTY,
                 new Integer(PROCESS_CAPABILITY_NONE));
@@ -272,7 +293,7 @@
             assertEquals(NOTEOP_COUNT, hOp.getAccessCount(UID_STATE_TOP,
                     UID_STATE_FOREGROUND_SERVICE, AppOpsManager.OP_FLAGS_ALL));
             assertEquals(NOTEOP_COUNT, hOp.getForegroundAccessCount(OP_FLAGS_ALL));
-            assertEquals(0, hOp.getForegroundRejectCount(OP_FLAGS_ALL));
+            assertEquals(NOTEOP_COUNT, hOp.getForegroundRejectCount(OP_FLAGS_ALL));
             assertEquals(0, hOp.getBackgroundAccessCount(OP_FLAGS_ALL));
             // denied access one time in background.
             assertEquals(NOTEOP_COUNT, hOp.getBackgroundRejectCount(OP_FLAGS_ALL)); }
@@ -288,7 +309,7 @@
         startSimpleService();
         // Wait for state and capability change.
         mUidWatcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_FG_SERVICE,
-                new Integer(PROCESS_CAPABILITY_NONE));
+                new Integer(TEMP_PROCESS_CAPABILITY_FOREGROUND_LOCATION));
 
         // Non-Top started FGS do not have while-in-use permission, camera/microphone access is
         // denied.
@@ -308,7 +329,7 @@
         // Tell the activity to finalize.
         stopSimpleActivity();
         mUidWatcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_FG_SERVICE,
-                new Integer(PROCESS_CAPABILITY_NONE));
+                new Integer(TEMP_PROCESS_CAPABILITY_FOREGROUND_LOCATION));
 
         // App not in Top, camera/microphone access should be denied.
         assertEquals(MODE_IGNORED, noteOp(OPSTR_CAMERA));
diff --git a/tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java b/tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java
index 4961bff7..70dd07a 100644
--- a/tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java
+++ b/tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java
@@ -38,9 +38,12 @@
     private static final String PACKAGE_NAME_APP2 = "com.android.app2";
     private static final String PACKAGE_NAME_APP3 = "com.android.app3";
 
-    public static String ACTION_START_FGS_RESULT =
+    //TODO: remove this when development is done.
+    private static final int TEMP_PROCESS_CAPABILITY_FOREGROUND_LOCATION = 1 << 31;
+
+    private static final String ACTION_START_FGS_RESULT =
             "android.app.stubs.LocalForegroundService.RESULT";
-    public static String ACTION_START_FGSL_RESULT =
+    private static final String ACTION_START_FGSL_RESULT =
             "android.app.stubs.LocalForegroundServiceLocation.RESULT";
 
     private static final int WAITFOR_MSEC = 10000;
@@ -88,7 +91,7 @@
             // Package1 is in FGS state, but won't get location capability.
             uid1Watcher.waitFor(WatchUidRunner.CMD_PROCSTATE,
                     WatchUidRunner.STATE_FG_SERVICE,
-                    new Integer(PROCESS_CAPABILITY_NONE));
+                    new Integer(TEMP_PROCESS_CAPABILITY_FOREGROUND_LOCATION));
             // stop FGSL
             CommandReceiver.sendCommand(mContext,
                     CommandReceiver.COMMAND_STOP_FOREGROUND_SERVICE_LOCATION,
@@ -108,7 +111,7 @@
             // Package1 is in STATE_FG_SERVICE, but won't get location capability.
             uid1Watcher.waitFor(WatchUidRunner.CMD_PROCSTATE,
                     WatchUidRunner.STATE_FG_SERVICE,
-                    new Integer(PROCESS_CAPABILITY_NONE));
+                    new Integer(TEMP_PROCESS_CAPABILITY_FOREGROUND_LOCATION));
             // stop FGSL.
             CommandReceiver.sendCommand(mContext,
                     CommandReceiver.COMMAND_STOP_FOREGROUND_SERVICE_LOCATION,
@@ -167,7 +170,7 @@
             // Package2 won't have location capability because package1 is not in TOP state.
             uid2Watcher.waitFor(WatchUidRunner.CMD_PROCSTATE,
                     WatchUidRunner.STATE_FG_SERVICE,
-                    new Integer(PROCESS_CAPABILITY_NONE));
+                    new Integer(TEMP_PROCESS_CAPABILITY_FOREGROUND_LOCATION));
             waiter.doWait(WAITFOR_MSEC);
 
             CommandReceiver.sendCommand(mContext,
@@ -243,7 +246,7 @@
             // Package2 won't have location capability.
             uid2Watcher.waitFor(WatchUidRunner.CMD_PROCSTATE,
                     WatchUidRunner.STATE_FG_SERVICE,
-                    new Integer(PROCESS_CAPABILITY_NONE));
+                    new Integer(TEMP_PROCESS_CAPABILITY_FOREGROUND_LOCATION));
             // Stop FGSL in package2.
             CommandReceiver.sendCommand(mContext,
                     CommandReceiver.COMMAND_STOP_FOREGROUND_SERVICE_LOCATION,
@@ -271,7 +274,7 @@
             // Package2 won't have location capability.
             uid2Watcher.waitFor(WatchUidRunner.CMD_PROCSTATE,
                     WatchUidRunner.STATE_FG_SERVICE,
-                    new Integer(PROCESS_CAPABILITY_NONE));
+                    new Integer(TEMP_PROCESS_CAPABILITY_FOREGROUND_LOCATION));
             waiter.doWait(WAITFOR_MSEC);
             // stop FGSL in package2.
             CommandReceiver.sendCommand(mContext,
diff --git a/tests/app/src/android/app/cts/ActivityManagerProcessStateTest.java b/tests/app/src/android/app/cts/ActivityManagerProcessStateTest.java
index 79c1778..0a3c795 100644
--- a/tests/app/src/android/app/cts/ActivityManagerProcessStateTest.java
+++ b/tests/app/src/android/app/cts/ActivityManagerProcessStateTest.java
@@ -1774,7 +1774,7 @@
             mWatchers[0].waitFor(WatchUidRunner.CMD_PROCSTATE,
                     WatchUidRunner.STATE_TOP,
                     new Integer(PROCESS_CAPABILITY_FOREGROUND_LOCATION
-                            | PROCESS_CAPABILITY_ALL_IMPLICIT));
+                            | PROCESS_CAPABILITY_ALL));
 
             // Start a FGS
             CommandReceiver.sendCommand(mContext,
@@ -1805,7 +1805,7 @@
             // Verify app1 does NOT have capability.
             mWatchers[1].waitFor(WatchUidRunner.CMD_PROCSTATE,
                     WatchUidRunner.STATE_FG_SERVICE,
-                    new Integer(PROCESS_CAPABILITY_NONE));
+                    new Integer(PROCESS_CAPABILITY_ALL_IMPLICIT));
 
             // Bind App 0 -> App 2, include capability.
             bundle = new Bundle();