Check and skip certain operations if they are unsupported.

Fixes: 178334463
Test: com.android.cts.net.HostsideRestrictBackgroundNetworkTests#testMeteredNetworkAccess_expeditedJob
Ignore-AOSP-First: Expedited jobs are not available in AOSP yet
Change-Id: Ie68e17063454e7feeffc93b20b1b8fbb276e837f
This commit is contained in:
Sudheer Shanka
2021-01-26 11:45:32 +00:00
parent 2b03d6d469
commit f0ba6291b1
2 changed files with 23 additions and 4 deletions

View File

@@ -25,6 +25,8 @@ import static com.android.cts.net.hostside.NetworkPolicyTestUtils.executeShellCo
import static com.android.cts.net.hostside.NetworkPolicyTestUtils.getConnectivityManager; import static com.android.cts.net.hostside.NetworkPolicyTestUtils.getConnectivityManager;
import static com.android.cts.net.hostside.NetworkPolicyTestUtils.getContext; 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.getInstrumentation;
import static com.android.cts.net.hostside.NetworkPolicyTestUtils.isAppStandbySupported;
import static com.android.cts.net.hostside.NetworkPolicyTestUtils.isBatterySaverSupported;
import static com.android.cts.net.hostside.NetworkPolicyTestUtils.isDozeModeSupported; 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.restrictBackgroundValueToString;
import static com.android.cts.net.hostside.NetworkPolicyTestUtils.runSatisfiedJob; import static com.android.cts.net.hostside.NetworkPolicyTestUtils.runSatisfiedJob;
@@ -653,6 +655,9 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
} }
protected void setBatterySaverMode(boolean enabled) throws Exception { protected void setBatterySaverMode(boolean enabled) throws Exception {
if (!isBatterySaverSupported()) {
return;
}
Log.i(TAG, "Setting Battery Saver Mode to " + enabled); Log.i(TAG, "Setting Battery Saver Mode to " + enabled);
if (enabled) { if (enabled) {
turnBatteryOn(); turnBatteryOn();
@@ -664,8 +669,9 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
} }
protected void setDozeMode(boolean enabled) throws Exception { protected void setDozeMode(boolean enabled) throws Exception {
// Check doze mode is supported. if (!isDozeModeSupported()) {
assertTrue("Device does not support Doze Mode", isDozeModeSupported()); return;
}
Log.i(TAG, "Setting Doze Mode to " + enabled); Log.i(TAG, "Setting Doze Mode to " + enabled);
if (enabled) { if (enabled) {
@@ -685,12 +691,18 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase {
} }
protected void setAppIdle(boolean enabled) throws Exception { protected void setAppIdle(boolean enabled) throws Exception {
if (!isAppStandbySupported()) {
return;
}
Log.i(TAG, "Setting app idle to " + enabled); Log.i(TAG, "Setting app idle to " + enabled);
executeSilentShellCommand("am set-inactive " + TEST_APP2_PKG + " " + enabled ); executeSilentShellCommand("am set-inactive " + TEST_APP2_PKG + " " + enabled );
assertAppIdle(enabled); assertAppIdle(enabled);
} }
protected void setAppIdleNoAssert(boolean enabled) throws Exception { protected void setAppIdleNoAssert(boolean enabled) throws Exception {
if (!isAppStandbySupported()) {
return;
}
Log.i(TAG, "Setting app idle to " + enabled); Log.i(TAG, "Setting app idle to " + enabled);
executeSilentShellCommand("am set-inactive " + TEST_APP2_PKG + " " + enabled ); executeSilentShellCommand("am set-inactive " + TEST_APP2_PKG + " " + enabled );
} }

View File

@@ -86,11 +86,11 @@ public class NetworkPolicyTestUtils {
if (mDataSaverSupported == null) { if (mDataSaverSupported == null) {
assertMyRestrictBackgroundStatus(RESTRICT_BACKGROUND_STATUS_DISABLED); assertMyRestrictBackgroundStatus(RESTRICT_BACKGROUND_STATUS_DISABLED);
try { try {
setRestrictBackground(true); setRestrictBackgroundInternal(true);
mDataSaverSupported = !isMyRestrictBackgroundStatus( mDataSaverSupported = !isMyRestrictBackgroundStatus(
RESTRICT_BACKGROUND_STATUS_DISABLED); RESTRICT_BACKGROUND_STATUS_DISABLED);
} finally { } finally {
setRestrictBackground(false); setRestrictBackgroundInternal(false);
} }
} }
return mDataSaverSupported; return mDataSaverSupported;
@@ -213,6 +213,13 @@ public class NetworkPolicyTestUtils {
} }
public static void setRestrictBackground(boolean enabled) { public static void setRestrictBackground(boolean enabled) {
if (!isDataSaverSupported()) {
return;
}
setRestrictBackgroundInternal(enabled);
}
private static void setRestrictBackgroundInternal(boolean enabled) {
executeShellCommand("cmd netpolicy set restrict-background " + enabled); executeShellCommand("cmd netpolicy set restrict-background " + enabled);
final String output = executeShellCommand("cmd netpolicy get restrict-background"); final String output = executeShellCommand("cmd netpolicy get restrict-background");
final String expectedSuffix = enabled ? "enabled" : "disabled"; final String expectedSuffix = enabled ? "enabled" : "disabled";