summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorHui Yu <huiyu@google.com>2022-02-09 18:14:14 +0000
committerHui Yu <huiyu@google.com>2022-02-09 18:14:14 +0000
commit9e9a19b29298c5c59d310b098add614e6531551e (patch)
treeb0ae7f860f70630fd96b481b1b9b4f166a70c0bc
parenta82d7550bc9c256375ecf8872bdbdf805daba83c (diff)
downloadcts-9e9a19b29298c5c59d310b098add614e6531551e.tar.gz
Revert "Merge "Add test cases for background startForeground() i..."
Revert submission 16823812-am-d28c94b9fe6445eba259ecb421887d7e Reason for revert: It cause down stream automerge problem into sv-dev. Reverted Changes: I0f8fb6de7:Merge "BG-FGS-start while-in-use permission restri... I6305f8e37:Merge "Add test cases for background startForegrou... Change-Id: I7f9fc6e09191f66297b7e9949f68a7298b44e76e
-rw-r--r--tests/app/app/src/android/app/stubs/CommandReceiver.java3
-rw-r--r--tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java188
2 files changed, 2 insertions, 189 deletions
diff --git a/tests/app/app/src/android/app/stubs/CommandReceiver.java b/tests/app/app/src/android/app/stubs/CommandReceiver.java
index 9fcd4effa02..9b56652f911 100644
--- a/tests/app/app/src/android/app/stubs/CommandReceiver.java
+++ b/tests/app/app/src/android/app/stubs/CommandReceiver.java
@@ -152,6 +152,7 @@ public class CommandReceiver extends BroadcastReceiver {
context.unbindService(sServiceMap.remove(targetPackage));
}
+
private void doStartService(Context context, Intent commandIntent, String serviceName) {
String targetPackage = getTargetPackage(commandIntent);
Intent fgsIntent = new Intent();
@@ -182,7 +183,7 @@ public class CommandReceiver extends BroadcastReceiver {
Intent fgsIntent = new Intent();
fgsIntent.putExtras(commandIntent); // include the fg service type if any.
fgsIntent.setComponent(new ComponentName(targetPackage, FG_LOCATION_SERVICE_NAME));
- int command = LocalForegroundService.COMMAND_START_FOREGROUND_WITH_TYPE;
+ int command = LocalForegroundServiceLocation.COMMAND_START_FOREGROUND_WITH_TYPE;
fgsIntent.putExtras(LocalForegroundService.newCommand(new Binder(), command));
context.startForegroundService(fgsIntent);
}
diff --git a/tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java b/tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java
index 04f58664ad0..18fd8f45521 100644
--- a/tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java
+++ b/tests/app/src/android/app/cts/ActivityManagerFgsBgStartTest.java
@@ -53,10 +53,6 @@ public class ActivityManagerFgsBgStartTest extends InstrumentationTestCase {
private static final String PACKAGE_NAME_APP2 = "com.android.app2";
private static final String PACKAGE_NAME_APP3 = "com.android.app3";
- private static final String KEY_FGS_START_FOREGROUND_TIMEOUT =
- "fgs_start_foreground_timeout";
- private static final int DEFAULT_FGS_START_FOREGROUND_TIMEOUT_MS = 10 * 1000;
-
private static final int WAITFOR_MSEC = 10000;
public static final Integer LOCATION_SERVICE_PROCESS_CAPABILITY = new Integer(
@@ -413,188 +409,4 @@ public class ActivityManagerFgsBgStartTest extends InstrumentationTestCase {
uid1Watcher.finish();
}
}
-
- /**
- * After background service is started, after 10 seconds timeout, the service can have
- * while-in-use access or not depends on the service's app proc state.
- * Test starService() -> (wait for 10 seconds) -> startForeground()
- */
- @Test
- public void testStartForegroundTimeout() throws Exception {
- ApplicationInfo app1Info = mContext.getPackageManager().getApplicationInfo(
- PACKAGE_NAME_APP1, 0);
- WatchUidRunner uid1Watcher = new WatchUidRunner(mInstrumentation, app1Info.uid,
- WAITFOR_MSEC);
- try {
- setFgsStartForegroundTimeout(DEFAULT_FGS_START_FOREGROUND_TIMEOUT_MS);
-
- // Put app to a TOP proc state.
- CommandReceiver.sendCommand(mContext, CommandReceiver.COMMAND_START_ACTIVITY,
- PACKAGE_NAME_APP1, PACKAGE_NAME_APP1, 0, null);
- uid1Watcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_TOP,
- new Integer(PROCESS_CAPABILITY_ALL));
-
- // start background service, do not call Service.startForeground().
- Bundle extras = LocalForegroundService.newCommand(
- LocalForegroundService.COMMAND_START_NO_FOREGROUND);
- CommandReceiver.sendCommand(mContext, CommandReceiver.COMMAND_START_FGSL_SERVICE,
- PACKAGE_NAME_APP1, PACKAGE_NAME_APP1, 0, extras);
-
- // stop the activity.
- CommandReceiver.sendCommand(mContext, CommandReceiver.COMMAND_STOP_ACTIVITY,
- PACKAGE_NAME_APP1, PACKAGE_NAME_APP1, 0, null);
- // this is a background service.
- uid1Watcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_SERVICE);
-
- // Sleep after the timeout DEFAULT_FGS_START_FOREGROUND_TIMEOUT_MS
- SystemClock.sleep(DEFAULT_FGS_START_FOREGROUND_TIMEOUT_MS + 1000);
-
- extras = LocalForegroundService.newCommand(
- LocalForegroundService.COMMAND_START_FOREGROUND);
- // call Service.startForeground().
- CommandReceiver.sendCommand(mContext, CommandReceiver.COMMAND_START_FGSL_SERVICE,
- PACKAGE_NAME_APP1, PACKAGE_NAME_APP1, 0, extras);
- // APP1 enters FGS state, but has no while-in-use location capability.
- // because startForeground() is called after 10 seconds FgsStartForegroundTimeout.
- try {
- uid1Watcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_FG_SERVICE,
- LOCATION_SERVICE_PROCESS_CAPABILITY);
- fail("FGS should not have while-in-use capability");
- } catch (Exception e) {
- }
-
- // Stop the FGS.
- CommandReceiver.sendCommand(mContext, CommandReceiver.COMMAND_STOP_FGSL_SERVICE,
- PACKAGE_NAME_APP1, PACKAGE_NAME_APP1, 0, null);
- uid1Watcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_CACHED_EMPTY,
- new Integer(PROCESS_CAPABILITY_NONE));
-
- // Put app to a TOP proc state.
- CommandReceiver.sendCommand(mContext, CommandReceiver.COMMAND_START_ACTIVITY,
- PACKAGE_NAME_APP1, PACKAGE_NAME_APP1, 0, null);
- uid1Watcher.waitFor(WatchUidRunner.CMD_PROCSTATE,
- WatchUidRunner.STATE_TOP, new Integer(PROCESS_CAPABILITY_ALL));
-
- // Call Service.startForeground().
- WaitForBroadcast waiter = new WaitForBroadcast(mInstrumentation.getTargetContext());
- waiter.prepare(LocalForegroundServiceLocation.ACTION_START_FGSL_RESULT);
- extras = LocalForegroundService.newCommand(
- LocalForegroundService.COMMAND_START_FOREGROUND);
- CommandReceiver.sendCommand(mContext, CommandReceiver.COMMAND_START_FGSL_SERVICE,
- PACKAGE_NAME_APP1, PACKAGE_NAME_APP1, 0, extras);
- CommandReceiver.sendCommand(mContext, CommandReceiver.COMMAND_STOP_ACTIVITY,
- PACKAGE_NAME_APP1, PACKAGE_NAME_APP1, 0, null);
-
- uid1Watcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_FG_SERVICE,
- LOCATION_SERVICE_PROCESS_CAPABILITY);
- waiter.doWait(WAITFOR_MSEC);
-
- // Stop the FGS.
- CommandReceiver.sendCommand(mContext,
- CommandReceiver.COMMAND_STOP_FGSL_SERVICE,
- PACKAGE_NAME_APP1, PACKAGE_NAME_APP1, 0, null);
- uid1Watcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_CACHED_EMPTY,
- new Integer(PROCESS_CAPABILITY_NONE));
- } finally {
- uid1Watcher.finish();
- setFgsStartForegroundTimeout(DEFAULT_FGS_START_FOREGROUND_TIMEOUT_MS);
- }
- }
-
- /**
- * After startForeground() and stopForeground(), the second startForeground() can have
- * while-in-use access or not depends on the service's app proc state.
- * Test startForegroundService() -> startForeground() -> stopForeground() -> startForeground().
- */
- @Test
- public void testSecondStartForeground() throws Exception {
- ApplicationInfo app1Info = mContext.getPackageManager().getApplicationInfo(
- PACKAGE_NAME_APP1, 0);
- WatchUidRunner uid1Watcher = new WatchUidRunner(mInstrumentation, app1Info.uid,
- WAITFOR_MSEC);
- try {
- setFgsStartForegroundTimeout(DEFAULT_FGS_START_FOREGROUND_TIMEOUT_MS);
- // Put app to a TOP proc state.
- CommandReceiver.sendCommand(mContext, CommandReceiver.COMMAND_START_ACTIVITY,
- PACKAGE_NAME_APP1, PACKAGE_NAME_APP1, 0, null);
- uid1Watcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_TOP,
- new Integer(PROCESS_CAPABILITY_ALL));
-
- WaitForBroadcast waiter = new WaitForBroadcast(mInstrumentation.getTargetContext());
- waiter.prepare(LocalForegroundServiceLocation.ACTION_START_FGSL_RESULT);
- // start foreground service, call Context.startForegroundService().
- CommandReceiver.sendCommand(mContext,
- CommandReceiver.COMMAND_START_FOREGROUND_SERVICE_LOCATION,
- PACKAGE_NAME_APP1, PACKAGE_NAME_APP1, 0, null);
- // stop the activity.
- CommandReceiver.sendCommand(mContext, CommandReceiver.COMMAND_STOP_ACTIVITY,
- PACKAGE_NAME_APP1, PACKAGE_NAME_APP1, 0, null);
-
- uid1Watcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_FG_SERVICE,
- LOCATION_SERVICE_PROCESS_CAPABILITY);
- waiter.doWait(WAITFOR_MSEC);
-
- // Call Service.stopForeground()
- Bundle extras = LocalForegroundService.newCommand(
- LocalForegroundService.COMMAND_STOP_FOREGROUND_REMOVE_NOTIFICATION);
- CommandReceiver.sendCommand(mContext, CommandReceiver.COMMAND_START_FGSL_SERVICE,
- PACKAGE_NAME_APP1, PACKAGE_NAME_APP1, 0, extras);
- uid1Watcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_SERVICE,
- new Integer(PROCESS_CAPABILITY_NONE));
-
- // Sleep after the timeout DEFAULT_FGS_START_FOREGROUND_TIMEOUT_MS
- SystemClock.sleep(DEFAULT_FGS_START_FOREGROUND_TIMEOUT_MS + 1000);
-
- // Call Service.startForeground() again, this time it is started from background.
- extras = LocalForegroundService.newCommand(
- LocalForegroundService.COMMAND_START_FOREGROUND);
- CommandReceiver.sendCommand(mContext, CommandReceiver.COMMAND_START_FGSL_SERVICE,
- PACKAGE_NAME_APP1, PACKAGE_NAME_APP1, 0, extras);
- try {
- uid1Watcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_FG_SERVICE,
- LOCATION_SERVICE_PROCESS_CAPABILITY);
- fail("FGS should not have while-in-use capability");
- } catch (Exception e) {
- }
-
- // Put app to a TOP proc state.
- CommandReceiver.sendCommand(mContext, CommandReceiver.COMMAND_START_ACTIVITY,
- PACKAGE_NAME_APP1, PACKAGE_NAME_APP1, 0, null);
- uid1Watcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_TOP,
- new Integer(PROCESS_CAPABILITY_ALL));
-
- // Call Service.startForeground() second time.
- waiter = new WaitForBroadcast(mInstrumentation.getTargetContext());
- waiter.prepare(LocalForegroundServiceLocation.ACTION_START_FGSL_RESULT);
- extras = LocalForegroundService.newCommand(
- LocalForegroundService.COMMAND_START_FOREGROUND);
- CommandReceiver.sendCommand(mContext, CommandReceiver.COMMAND_START_FGSL_SERVICE,
- PACKAGE_NAME_APP1, PACKAGE_NAME_APP1, 0, extras);
- CommandReceiver.sendCommand(mContext, CommandReceiver.COMMAND_STOP_ACTIVITY,
- PACKAGE_NAME_APP1, PACKAGE_NAME_APP1, 0, null);
- // This Service.startForeground() is called from the foreground, it has while-in-use
- // capabilities.
- uid1Watcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_FG_SERVICE,
- LOCATION_SERVICE_PROCESS_CAPABILITY);
- waiter.doWait(WAITFOR_MSEC);
-
- // Stop the FGS.
- CommandReceiver.sendCommand(mContext, CommandReceiver.COMMAND_STOP_FGSL_SERVICE,
- PACKAGE_NAME_APP1, PACKAGE_NAME_APP1, 0, null);
- uid1Watcher.waitFor(WatchUidRunner.CMD_PROCSTATE, WatchUidRunner.STATE_CACHED_EMPTY,
- new Integer(PROCESS_CAPABILITY_NONE));
- } finally {
- uid1Watcher.finish();
- setFgsStartForegroundTimeout(DEFAULT_FGS_START_FOREGROUND_TIMEOUT_MS);
- }
- }
-
- private void setFgsStartForegroundTimeout(int timeoutMs) throws Exception {
- runWithShellPermissionIdentity(() -> {
- DeviceConfig.setProperty("activity_manager",
- KEY_FGS_START_FOREGROUND_TIMEOUT,
- Integer.toString(timeoutMs), false);
- }
- );
- }
}