Add tests to verify that Expedited jobs have network access.

Bug: 177641226
Test: atest CtsHostsideNetworkTests:HostsideRestrictBackgroundNetworkTest
Ignore-AOSP-First: Expedited jobs are not available in AOSP yet
Change-Id: Idc0762093667d49f09d52050c47c29cbc55997e1
This commit is contained in:
Sudheer Shanka 2021-01-15 12:25:15 -08:00
commit 51691d6ab7
8 changed files with 173 additions and 11 deletions

View file

@ -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);
}

View file

@ -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.
*
* <p>This method will launch an activity and a foreground service to make the assertion, but
* will finish the activity / stop the service afterwards.
* <p>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);

View file

@ -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);
}
}

View file

@ -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);

View file

@ -55,6 +55,8 @@
<action android:name="com.android.cts.net.hostside.app2.action.SHOW_TOAST"/>
</intent-filter>
</receiver>
<service android:name=".MyJobService"
android:permission="android.permission.BIND_JOB_SERVICE" />
</application>
</manifest>

View file

@ -38,6 +38,8 @@ public final class Common {
"com.android.cts.net.hostside.app2.action.RECEIVER_READY";
static final String ACTION_FINISH_ACTIVITY =
"com.android.cts.net.hostside.app2.action.FINISH_ACTIVITY";
static final String ACTION_FINISH_JOB =
"com.android.cts.net.hostside.app2.action.FINISH_JOB";
static final String ACTION_SHOW_TOAST =
"com.android.cts.net.hostside.app2.action.SHOW_TOAST";
@ -66,6 +68,10 @@ public final class Common {
return;
}
final Bundle extras = intent.getExtras();
notifyNetworkStateObserver(context, extras);
}
static void notifyNetworkStateObserver(Context context, Bundle extras) {
if (extras == null) {
return;
}

View file

@ -0,0 +1,79 @@
/*
* Copyright (C) 2021 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.android.cts.net.hostside.app2;
import static com.android.cts.net.hostside.app2.Common.ACTION_FINISH_JOB;
import static com.android.cts.net.hostside.app2.Common.TAG;
import android.app.job.JobParameters;
import android.app.job.JobService;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
import android.util.Log;
public class MyJobService extends JobService {
private BroadcastReceiver mFinishCommandReceiver = null;
@Override
public void onCreate() {
super.onCreate();
Log.v(TAG, "MyJobService.onCreate()");
}
@Override
public boolean onStartJob(JobParameters params) {
Log.v(TAG, "MyJobService.onStartJob()");
Common.notifyNetworkStateObserver(this, params.getTransientExtras());
mFinishCommandReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
Log.v(TAG, "Finishing MyJobService");
try {
jobFinished(params, /*wantsReschedule=*/ false);
} finally {
if (mFinishCommandReceiver != null) {
unregisterReceiver(mFinishCommandReceiver);
mFinishCommandReceiver = null;
}
}
}
};
registerReceiver(mFinishCommandReceiver, new IntentFilter(ACTION_FINISH_JOB));
return true;
}
@Override
public boolean onStopJob(JobParameters params) {
// If this job is stopped before it had a chance to send network status via
// INetworkStateObserver, the test will fail. It could happen either due to test timing out
// or this app moving to a lower proc_state and losing network access.
Log.v(TAG, "MyJobService.onStopJob()");
if (mFinishCommandReceiver != null) {
unregisterReceiver(mFinishCommandReceiver);
mFinishCommandReceiver = null;
}
return false;
}
@Override
public void onDestroy() {
super.onDestroy();
Log.v(TAG, "MyJobService.onDestroy()");
}
}

View file

@ -24,6 +24,8 @@ import static com.android.cts.net.hostside.app2.Common.TAG;
import android.app.NotificationChannel;
import android.app.NotificationManager;
import android.app.Service;
import android.app.job.JobInfo;
import android.app.job.JobScheduler;
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
@ -154,6 +156,13 @@ public class MyService extends Service {
mNetworkCallback = null;
}
}
@Override
public void scheduleJob(JobInfo jobInfo) {
final JobScheduler jobScheduler = getApplicationContext()
.getSystemService(JobScheduler.class);
jobScheduler.schedule(jobInfo);
}
};
private NetworkRequest makeWifiNetworkRequest() {