diff --git a/tests/cts/hostside/aidl/com/android/cts/net/hostside/IMyService.aidl b/tests/cts/hostside/aidl/com/android/cts/net/hostside/IMyService.aidl index 5aafdf06cb..fbbb68b747 100644 --- a/tests/cts/hostside/aidl/com/android/cts/net/hostside/IMyService.aidl +++ b/tests/cts/hostside/aidl/com/android/cts/net/hostside/IMyService.aidl @@ -16,6 +16,8 @@ package com.android.cts.net.hostside; +import android.app.job.JobInfo; + import com.android.cts.net.hostside.INetworkCallback; interface IMyService { @@ -26,4 +28,5 @@ interface IMyService { void sendNotification(int notificationId, String notificationType); void registerNetworkCallback(in INetworkCallback cb); void unregisterNetworkCallback(); + void scheduleJob(in JobInfo jobInfo); } diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java index e2dc1a1143..effe76b32a 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java @@ -27,6 +27,7 @@ import static com.android.cts.net.hostside.NetworkPolicyTestUtils.getContext; import static com.android.cts.net.hostside.NetworkPolicyTestUtils.getInstrumentation; import static com.android.cts.net.hostside.NetworkPolicyTestUtils.isDozeModeSupported; import static com.android.cts.net.hostside.NetworkPolicyTestUtils.restrictBackgroundValueToString; +import static com.android.cts.net.hostside.NetworkPolicyTestUtils.runSatisfiedJob; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertFalse; @@ -37,6 +38,7 @@ import static org.junit.Assert.fail; import android.app.ActivityManager; import android.app.Instrumentation; import android.app.NotificationManager; +import android.app.job.JobInfo; import android.content.BroadcastReceiver; import android.content.ComponentName; import android.content.Context; @@ -75,6 +77,12 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase { private static final String TEST_APP2_ACTIVITY_CLASS = TEST_APP2_PKG + ".MyActivity"; private static final String TEST_APP2_SERVICE_CLASS = TEST_APP2_PKG + ".MyForegroundService"; + private static final String TEST_APP2_JOB_SERVICE_CLASS = TEST_APP2_PKG + ".MyJobService"; + + private static final ComponentName TEST_JOB_COMPONENT = new ComponentName( + TEST_APP2_PKG, TEST_APP2_JOB_SERVICE_CLASS); + + private static final int TEST_JOB_ID = 7357437; private static final int SLEEP_TIME_SEC = 1; @@ -102,17 +110,21 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase { private static final String NETWORK_STATUS_SEPARATOR = "\\|"; private static final int SECOND_IN_MS = 1000; static final int NETWORK_TIMEOUT_MS = 15 * SECOND_IN_MS; + private static int PROCESS_STATE_FOREGROUND_SERVICE; + private static int PROCESS_STATE_IMPORTANT_FOREGROUND; private static final String KEY_NETWORK_STATE_OBSERVER = TEST_PKG + ".observer"; protected static final int TYPE_COMPONENT_ACTIVTIY = 0; protected static final int TYPE_COMPONENT_FOREGROUND_SERVICE = 1; + protected static final int TYPE_EXPEDITED_JOB = 2; private static final int BATTERY_STATE_TIMEOUT_MS = 5000; private static final int BATTERY_STATE_CHECK_INTERVAL_MS = 500; - private static final int FOREGROUND_PROC_NETWORK_TIMEOUT_MS = 6000; + private static final int ACTIVITY_NETWORK_STATE_TIMEOUT_MS = 6_000; + private static final int JOB_NETWORK_STATE_TIMEOUT_MS = 10_000; // Must be higher than NETWORK_TIMEOUT_MS private static final int ORDERED_BROADCAST_TIMEOUT_MS = NETWORK_TIMEOUT_MS * 4; @@ -137,8 +149,11 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase { .around(new MeterednessConfigurationRule()); protected void setUp() throws Exception { + // TODO: Annotate these constants with @TestApi instead of obtaining them using reflection PROCESS_STATE_FOREGROUND_SERVICE = (Integer) ActivityManager.class .getDeclaredField("PROCESS_STATE_FOREGROUND_SERVICE").get(null); + PROCESS_STATE_IMPORTANT_FOREGROUND = (Integer) ActivityManager.class + .getDeclaredField("PROCESS_STATE_IMPORTANT_FOREGROUND").get(null); mInstrumentation = getInstrumentation(); mContext = getContext(); mCm = getConnectivityManager(); @@ -252,10 +267,11 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase { } /** - * Asserts that an app always have access while on foreground or running a foreground service. + * Asserts that an app always have access while on foreground or running a foreground service + * or an expedited job. * - *
This method will launch an activity and a foreground service to make the assertion, but - * will finish the activity / stop the service afterwards. + *
This method will launch an activity, a foreground service, and an expedited job to make
+ * the assertion, but will finish the activity / stop the service / finish the job afterwards.
*/
protected void assertsForegroundAlwaysHasNetworkAccess() throws Exception{
// Checks foreground first.
@@ -265,6 +281,9 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
// Then foreground service
launchComponentAndAssertNetworkAccess(TYPE_COMPONENT_FOREGROUND_SERVICE);
stopForegroundService();
+
+ launchComponentAndAssertNetworkAccess(TYPE_EXPEDITED_JOB);
+ finishExpeditedJob();
}
protected final void assertBackgroundState() throws Exception {
@@ -323,7 +342,7 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
* Returns whether an app state should be considered "background" for restriction purposes.
*/
protected boolean isBackground(int state) {
- return state > PROCESS_STATE_FOREGROUND_SERVICE;
+ return state > PROCESS_STATE_IMPORTANT_FOREGROUND;
}
/**
@@ -750,17 +769,40 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
extras.putBinder(KEY_NETWORK_STATE_OBSERVER, getNewNetworkStateObserver(latch, errors));
launchIntent.putExtras(extras);
mContext.startActivity(launchIntent);
- if (latch.await(FOREGROUND_PROC_NETWORK_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
+ if (latch.await(ACTIVITY_NETWORK_STATE_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
if (!errors[0].isEmpty()) {
if (errors[0] == APP_NOT_FOREGROUND_ERROR) {
// App didn't come to foreground when the activity is started, so try again.
assertForegroundNetworkAccess();
} else {
- fail("Network is not available for app2 (" + mUid + "): " + errors[0]);
+ fail("Network is not available for activity in app2 (" + mUid
+ + "): " + errors[0]);
}
}
} else {
- fail("Timed out waiting for network availability status from app2 (" + mUid + ")");
+ fail("Timed out waiting for network availability status from app2's activity ("
+ + mUid + ")");
+ }
+ } else if (type == TYPE_EXPEDITED_JOB) {
+ final Bundle extras = new Bundle();
+ final String[] errors = new String[]{null};
+ final CountDownLatch latch = new CountDownLatch(1);
+ extras.putBinder(KEY_NETWORK_STATE_OBSERVER, getNewNetworkStateObserver(latch, errors));
+ final JobInfo jobInfo = new JobInfo.Builder(TEST_JOB_ID, TEST_JOB_COMPONENT)
+ .setExpedited(true)
+ .setRequiredNetworkType(JobInfo.NETWORK_TYPE_ANY)
+ .setTransientExtras(extras)
+ .build();
+ mServiceClient.scheduleJob(jobInfo);
+ runSatisfiedJob(TEST_APP2_PKG, TEST_JOB_ID);
+ if (latch.await(JOB_NETWORK_STATE_TIMEOUT_MS, TimeUnit.MILLISECONDS)) {
+ if (!errors[0].isEmpty()) {
+ fail("Network is not available for expedited job in app2 (" + mUid
+ + "): " + errors[0]);
+ }
+ } else {
+ fail("Timed out waiting for network availability status from app2's expedited job ("
+ + mUid + ")");
}
} else {
throw new IllegalArgumentException("Unknown type: " + type);
@@ -818,7 +860,7 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
}
/**
- * Finishes an activity on app2 so its process is demoted fromforeground status.
+ * Finishes an activity on app2 so its process is demoted from foreground status.
*/
protected void finishActivity() throws Exception {
executeShellCommand("am broadcast -a "
@@ -826,6 +868,15 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
+ "--receiver-foreground --receiver-registered-only");
}
+ /**
+ * Finishes the expedited job on app2 so its process is demoted from foreground status.
+ */
+ private void finishExpeditedJob() throws Exception {
+ executeShellCommand("am broadcast -a "
+ + " com.android.cts.net.hostside.app2.action.FINISH_JOB "
+ + "--receiver-foreground --receiver-registered-only");
+ }
+
protected void sendNotification(int notificationId, String notificationType) throws Exception {
Log.d(TAG, "Sending notification broadcast (id=" + notificationId
+ ", type=" + notificationType);
diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/MyServiceClient.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/MyServiceClient.java
index 6546e26ba7..1339be6e46 100644
--- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/MyServiceClient.java
+++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/MyServiceClient.java
@@ -16,6 +16,7 @@
package com.android.cts.net.hostside;
+import android.app.job.JobInfo;
import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
@@ -104,4 +105,8 @@ public class MyServiceClient {
public void unregisterNetworkCallback() throws RemoteException {
mService.unregisterNetworkCallback();
}
+
+ public void scheduleJob(JobInfo jobInfo) throws RemoteException {
+ mService.scheduleJob(jobInfo);
+ }
}
diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/NetworkPolicyTestUtils.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/NetworkPolicyTestUtils.java
index 3041dfa76b..6dd83b5e54 100644
--- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/NetworkPolicyTestUtils.java
+++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/NetworkPolicyTestUtils.java
@@ -41,18 +41,19 @@ import android.net.Network;
import android.net.NetworkCapabilities;
import android.net.wifi.WifiManager;
import android.os.Process;
+import android.os.UserHandle;
import android.text.TextUtils;
import android.util.Log;
import android.util.Pair;
+import androidx.test.platform.app.InstrumentationRegistry;
+
import com.android.compatibility.common.util.AppStandbyUtils;
import com.android.compatibility.common.util.BatteryUtils;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
-import androidx.test.platform.app.InstrumentationRegistry;
-
public class NetworkPolicyTestUtils {
private static final int TIMEOUT_CHANGE_METEREDNESS_MS = 10_000;
@@ -116,6 +117,12 @@ public class NetworkPolicyTestUtils {
return am.isLowRamDevice();
}
+ /** Asks (not forces) JobScheduler to run the job if constraints are met. */
+ public static void runSatisfiedJob(String pkg, int jobId) {
+ executeShellCommand("cmd jobscheduler run -s -u " + UserHandle.myUserId()
+ + " " + pkg + " " + jobId);
+ }
+
public static boolean isLocationEnabled() {
final LocationManager lm = (LocationManager) getContext().getSystemService(
Context.LOCATION_SERVICE);
diff --git a/tests/cts/hostside/app2/AndroidManifest.xml b/tests/cts/hostside/app2/AndroidManifest.xml
index eb777f2e31..b85d800fb5 100644
--- a/tests/cts/hostside/app2/AndroidManifest.xml
+++ b/tests/cts/hostside/app2/AndroidManifest.xml
@@ -55,6 +55,8 @@