From f0ba6291b127ba23bf019918e391d0126931b33e Mon Sep 17 00:00:00 2001 From: Sudheer Shanka Date: Tue, 26 Jan 2021 11:45:32 +0000 Subject: [PATCH] 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 --- ...bstractRestrictBackgroundNetworkTestCase.java | 16 ++++++++++++++-- .../cts/net/hostside/NetworkPolicyTestUtils.java | 11 +++++++++-- 2 files changed, 23 insertions(+), 4 deletions(-) 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 f67de4baf0..ab8b83468c 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 @@ -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.getContext; 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.restrictBackgroundValueToString; import static com.android.cts.net.hostside.NetworkPolicyTestUtils.runSatisfiedJob; @@ -653,6 +655,9 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase { } protected void setBatterySaverMode(boolean enabled) throws Exception { + if (!isBatterySaverSupported()) { + return; + } Log.i(TAG, "Setting Battery Saver Mode to " + enabled); if (enabled) { turnBatteryOn(); @@ -664,8 +669,9 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase { } protected void setDozeMode(boolean enabled) throws Exception { - // Check doze mode is supported. - assertTrue("Device does not support Doze Mode", isDozeModeSupported()); + if (!isDozeModeSupported()) { + return; + } Log.i(TAG, "Setting Doze Mode to " + enabled); if (enabled) { @@ -685,12 +691,18 @@ public abstract class AbstractRestrictBackgroundNetworkTestCase { } protected void setAppIdle(boolean enabled) throws Exception { + if (!isAppStandbySupported()) { + return; + } Log.i(TAG, "Setting app idle to " + enabled); executeSilentShellCommand("am set-inactive " + TEST_APP2_PKG + " " + enabled ); assertAppIdle(enabled); } protected void setAppIdleNoAssert(boolean enabled) throws Exception { + if (!isAppStandbySupported()) { + return; + } Log.i(TAG, "Setting app idle to " + enabled); executeSilentShellCommand("am set-inactive " + TEST_APP2_PKG + " " + enabled ); } 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 6dd83b5e54..06b8dfe17d 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 @@ -86,11 +86,11 @@ public class NetworkPolicyTestUtils { if (mDataSaverSupported == null) { assertMyRestrictBackgroundStatus(RESTRICT_BACKGROUND_STATUS_DISABLED); try { - setRestrictBackground(true); + setRestrictBackgroundInternal(true); mDataSaverSupported = !isMyRestrictBackgroundStatus( RESTRICT_BACKGROUND_STATUS_DISABLED); } finally { - setRestrictBackground(false); + setRestrictBackgroundInternal(false); } } return mDataSaverSupported; @@ -213,6 +213,13 @@ public class NetworkPolicyTestUtils { } public static void setRestrictBackground(boolean enabled) { + if (!isDataSaverSupported()) { + return; + } + setRestrictBackgroundInternal(enabled); + } + + private static void setRestrictBackgroundInternal(boolean enabled) { executeShellCommand("cmd netpolicy set restrict-background " + enabled); final String output = executeShellCommand("cmd netpolicy get restrict-background"); final String expectedSuffix = enabled ? "enabled" : "disabled";