From d1f3d68694f49339397a3046c77bf0ee30d8d14f Mon Sep 17 00:00:00 2001 From: Felipe Leme Date: Thu, 2 Jun 2016 12:01:26 -0700 Subject: [PATCH] Skip Doze Mode-related tests when device does not support it. Fixes: 29072117 Change-Id: I7ca37eae58258c021ed6297a9f1ee3b2749da7d7 --- .../net/hostside/AbstractAppIdleTestCase.java | 22 +++++++++++++++++ .../AbstractBatterySaverModeTestCase.java | 22 +++++++++++++++++ .../hostside/AbstractDozeModeTestCase.java | 23 ++++++++++++++++++ ...ractRestrictBackgroundNetworkTestCase.java | 24 +++++++++++++++++++ .../cts/net/hostside/DataSaverModeTest.java | 14 +++++++++++ .../cts/net/hostside/MixedModesTest.java | 20 ++++++++++++++++ ...ostsideRestrictBackgroundNetworkTests.java | 11 +++++++++ 7 files changed, 136 insertions(+) diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractAppIdleTestCase.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractAppIdleTestCase.java index 13ce6cee0a..ba56665fbc 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractAppIdleTestCase.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractAppIdleTestCase.java @@ -16,6 +16,8 @@ package com.android.cts.net.hostside; +import android.util.Log; + /** * Base class for metered and non-metered tests on idle apps. */ @@ -25,6 +27,8 @@ abstract class AbstractAppIdleTestCase extends AbstractRestrictBackgroundNetwork protected final void setUp() throws Exception { super.setUp(); + if (!isSupported()) return; + // Set initial state. setUpMeteredNetwork(); removePowerSaveModeWhitelist(TEST_APP2_PKG); @@ -38,6 +42,8 @@ abstract class AbstractAppIdleTestCase extends AbstractRestrictBackgroundNetwork protected final void tearDown() throws Exception { super.tearDown(); + if (!isSupported()) return; + try { tearDownMeteredNetwork(); } finally { @@ -46,6 +52,16 @@ abstract class AbstractAppIdleTestCase extends AbstractRestrictBackgroundNetwork } } + @Override + protected boolean isSupported() throws Exception { + boolean supported = isDozeModeEnabled(); + if (!supported) { + Log.i(TAG, "Skipping " + getClass() + "." + getName() + + "() because device does not support Doze Mode"); + } + return supported; + } + /** * Sets the initial (non) metered network state. * @@ -63,6 +79,8 @@ abstract class AbstractAppIdleTestCase extends AbstractRestrictBackgroundNetwork } public void testBackgroundNetworkAccess_enabled() throws Exception { + if (!isSupported()) return; + setAppIdle(true); assertBackgroundNetworkAccess(false); @@ -92,6 +110,8 @@ abstract class AbstractAppIdleTestCase extends AbstractRestrictBackgroundNetwork } public void testBackgroundNetworkAccess_whitelisted() throws Exception { + if (!isSupported()) return; + setAppIdle(true); assertBackgroundNetworkAccess(false); @@ -111,6 +131,8 @@ abstract class AbstractAppIdleTestCase extends AbstractRestrictBackgroundNetwork } public void testBackgroundNetworkAccess_disabled() throws Exception { + if (!isSupported()) return; + assertBackgroundNetworkAccess(true); assertsForegroundAlwaysHasNetworkAccess(); diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractBatterySaverModeTestCase.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractBatterySaverModeTestCase.java index 2acc670800..c1c91dac80 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractBatterySaverModeTestCase.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractBatterySaverModeTestCase.java @@ -16,6 +16,8 @@ package com.android.cts.net.hostside; +import android.util.Log; + /** * Base class for metered and non-metered Battery Saver Mode tests. */ @@ -25,6 +27,8 @@ abstract class AbstractBatterySaverModeTestCase extends AbstractRestrictBackgrou protected final void setUp() throws Exception { super.setUp(); + if (!isSupported()) return; + // Set initial state. setUpMeteredNetwork(); removePowerSaveModeWhitelist(TEST_APP2_PKG); @@ -37,6 +41,8 @@ abstract class AbstractBatterySaverModeTestCase extends AbstractRestrictBackgrou protected final void tearDown() throws Exception { super.tearDown(); + if (!isSupported()) return; + try { tearDownMeteredNetwork(); } finally { @@ -44,6 +50,16 @@ abstract class AbstractBatterySaverModeTestCase extends AbstractRestrictBackgrou } } + @Override + protected boolean isSupported() throws Exception { + boolean supported = isDozeModeEnabled(); + if (!supported) { + Log.i(TAG, "Skipping " + getClass() + "." + getName() + + "() because device does not support Doze Mode"); + } + return supported; + } + /** * Sets the initial (non) metered network state. * @@ -61,6 +77,8 @@ abstract class AbstractBatterySaverModeTestCase extends AbstractRestrictBackgrou } public void testBackgroundNetworkAccess_enabled() throws Exception { + if (!isSupported()) return; + setBatterySaverMode(true); assertBackgroundNetworkAccess(false); @@ -87,6 +105,8 @@ abstract class AbstractBatterySaverModeTestCase extends AbstractRestrictBackgrou } public void testBackgroundNetworkAccess_whitelisted() throws Exception { + if (!isSupported()) return; + setBatterySaverMode(true); assertBackgroundNetworkAccess(false); @@ -101,6 +121,8 @@ abstract class AbstractBatterySaverModeTestCase extends AbstractRestrictBackgrou } public void testBackgroundNetworkAccess_disabled() throws Exception { + if (!isSupported()) return; + assertBackgroundNetworkAccess(true); assertsForegroundAlwaysHasNetworkAccess(); diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractDozeModeTestCase.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractDozeModeTestCase.java index e0ba76b866..b89cf93b99 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractDozeModeTestCase.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractDozeModeTestCase.java @@ -17,6 +17,7 @@ package com.android.cts.net.hostside; import android.os.SystemClock; +import android.util.Log; /** * Base class for metered and non-metered Doze Mode tests. @@ -27,6 +28,8 @@ abstract class AbstractDozeModeTestCase extends AbstractRestrictBackgroundNetwor protected final void setUp() throws Exception { super.setUp(); + if (!isSupported()) return; + // Set initial state. setUpMeteredNetwork(); removePowerSaveModeWhitelist(TEST_APP2_PKG); @@ -39,6 +42,8 @@ abstract class AbstractDozeModeTestCase extends AbstractRestrictBackgroundNetwor protected final void tearDown() throws Exception { super.tearDown(); + if (!isSupported()) return; + try { tearDownMeteredNetwork(); } finally { @@ -46,6 +51,16 @@ abstract class AbstractDozeModeTestCase extends AbstractRestrictBackgroundNetwor } } + @Override + protected boolean isSupported() throws Exception { + boolean supported = isDozeModeEnabled(); + if (!supported) { + Log.i(TAG, "Skipping " + getClass() + "." + getName() + + "() because device does not support Doze Mode"); + } + return supported; + } + /** * Sets the initial (non) metered network state. * @@ -63,6 +78,8 @@ abstract class AbstractDozeModeTestCase extends AbstractRestrictBackgroundNetwor } public void testBackgroundNetworkAccess_enabled() throws Exception { + if (!isSupported()) return; + setDozeMode(true); assertBackgroundNetworkAccess(false); @@ -81,6 +98,8 @@ abstract class AbstractDozeModeTestCase extends AbstractRestrictBackgroundNetwor } public void testBackgroundNetworkAccess_whitelisted() throws Exception { + if (!isSupported()) return; + setDozeMode(true); assertBackgroundNetworkAccess(false); @@ -95,6 +114,8 @@ abstract class AbstractDozeModeTestCase extends AbstractRestrictBackgroundNetwor } public void testBackgroundNetworkAccess_disabled() throws Exception { + if (!isSupported()) return; + assertBackgroundNetworkAccess(true); assertsForegroundAlwaysHasNetworkAccess(); @@ -103,6 +124,8 @@ abstract class AbstractDozeModeTestCase extends AbstractRestrictBackgroundNetwor public void testBackgroundNetworkAccess_enabledButWhitelistedOnNotificationAction() throws Exception { + if (!isSupported()) return; + setPendingIntentWhitelistDuration(NETWORK_TIMEOUT_MS); try { registerNotificationListenerService(); 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 3125dfa50b..439fbbe0c9 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 @@ -187,6 +187,22 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation assertNetworkAccess(true); } + /** + * Whether this device suport this type of test. + * + *

Should be overridden when necessary, and explicitly used before each test. Example: + * + *


+     * public void testSomething() {
+     *    if (!isSupported()) return;
+     * 
+ * + * @return {@code true} by default. + */ + protected boolean isSupported() throws Exception { + return true; + } + /** * Asserts that an app always have access while on foreground or running a foreground service. * @@ -598,6 +614,9 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation } protected void setDozeMode(boolean enabled) throws Exception { + // Sanity check, since tests should check beforehand.... + assertTrue("Device does not support Doze Mode", isDozeModeEnabled()); + Log.i(TAG, "Setting Doze Mode to " + enabled); if (enabled) { turnBatteryOff(); @@ -616,6 +635,11 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation assertDelayedShellCommand("dumpsys deviceidle get deep", enabled ? "IDLE" : "ACTIVE"); } + protected boolean isDozeModeEnabled() throws Exception { + final String result = executeShellCommand("cmd deviceidle enabled deep").trim(); + return result.equals("1"); + } + protected void setAppIdle(boolean enabled) throws Exception { 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/DataSaverModeTest.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/DataSaverModeTest.java index 189515683c..3e6bd3320a 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/DataSaverModeTest.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/DataSaverModeTest.java @@ -30,6 +30,8 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase public void setUp() throws Exception { super.setUp(); + if (!isSupported()) return; + // Set initial state. setMeteredNetwork(); setRestrictBackground(false); @@ -44,6 +46,8 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase protected void tearDown() throws Exception { super.tearDown(); + if (!isSupported()) return; + try { resetMeteredNetwork(); } finally { @@ -52,6 +56,8 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase } public void testGetRestrictBackgroundStatus_disabled() throws Exception { + if (!isSupported()) return; + assertDataSaverStatusOnBackground(RESTRICT_BACKGROUND_STATUS_DISABLED); // Sanity check: make sure status is always disabled, never whitelisted @@ -64,6 +70,8 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase } public void testGetRestrictBackgroundStatus_whitelisted() throws Exception { + if (!isSupported()) return; + setRestrictBackground(true); assertRestrictBackgroundChangedReceived(1); assertDataSaverStatusOnBackground(RESTRICT_BACKGROUND_STATUS_ENABLED); @@ -81,6 +89,8 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase } public void testGetRestrictBackgroundStatus_enabled() throws Exception { + if (!isSupported()) return; + setRestrictBackground(true); assertRestrictBackgroundChangedReceived(1); assertDataSaverStatusOnBackground(RESTRICT_BACKGROUND_STATUS_ENABLED); @@ -108,6 +118,8 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase } public void testGetRestrictBackgroundStatus_blacklisted() throws Exception { + if (!isSupported()) return; + addRestrictBackgroundBlacklist(mUid); assertRestrictBackgroundChangedReceived(1); assertDataSaverStatusOnBackground(RESTRICT_BACKGROUND_STATUS_ENABLED); @@ -136,6 +148,8 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase } public void testGetRestrictBackgroundStatus_requiredWhitelistedPackages() throws Exception { + if (!isSupported()) return; + final StringBuilder error = new StringBuilder(); for (String packageName : REQUIRED_WHITELISTED_PACKAGES) { int uid = -1; diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/MixedModesTest.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/MixedModesTest.java index c97a0f91f3..af52eeece4 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/MixedModesTest.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/MixedModesTest.java @@ -32,6 +32,8 @@ public class MixedModesTest extends AbstractRestrictBackgroundNetworkTestCase { public void setUp() throws Exception { super.setUp(); + if (!isSupported()) return; + // Set initial state. removeRestrictBackgroundWhitelist(mUid); removeRestrictBackgroundBlacklist(mUid); @@ -44,6 +46,8 @@ public class MixedModesTest extends AbstractRestrictBackgroundNetworkTestCase { protected void tearDown() throws Exception { super.tearDown(); + if (!isSupported()) return; + try { setRestrictBackground(false); } finally { @@ -55,6 +59,14 @@ public class MixedModesTest extends AbstractRestrictBackgroundNetworkTestCase { * Tests all DS ON and BS ON scenarios from network-policy-restrictions.md on metered networks. */ public void testDataAndBatterySaverModes_meteredNetwork() throws Exception { + if (!isSupported()) return; + + if (!isDozeModeEnabled()) { + Log.w(TAG, "testDataAndBatterySaverModes_meteredNetwork() skipped because " + + "device does not support Doze Mode"); + return; + } + Log.i(TAG, "testDataAndBatterySaverModes_meteredNetwork() tests"); setMeteredNetwork(); @@ -119,6 +131,14 @@ public class MixedModesTest extends AbstractRestrictBackgroundNetworkTestCase { * networks. */ public void testDataAndBatterySaverModes_nonMeteredNetwork() throws Exception { + if (!isSupported()) return; + + if (!isDozeModeEnabled()) { + Log.w(TAG, "testDataAndBatterySaverModes_nonMeteredNetwork() skipped because " + + "device does not support Doze Mode"); + return; + } + if (mCm.isActiveNetworkMetered()) { Log.w(TAG, "testDataAndBatterySaverModes_nonMeteredNetwork() skipped because network" + " is metered"); diff --git a/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java b/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java index c741b12fd0..7d5f817798 100644 --- a/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java +++ b/tests/cts/hostside/src/com/android/cts/net/HostsideRestrictBackgroundNetworkTests.java @@ -101,6 +101,12 @@ public class HostsideRestrictBackgroundNetworkTests extends HostsideNetworkTestC } public void testBatterySaverMode_reinstall() throws Exception { + if (!isDozeModeEnabled()) { + Log.w(TAG, "testBatterySaverMode_reinstall() skipped because device does not support " + + "Doze Mode"); + return; + } + addPowerSaveModeWhitelist(TEST_APP2_PKG); uninstallPackage(TEST_APP2_PKG, true); @@ -287,4 +293,9 @@ public class HostsideRestrictBackgroundNetworkTests extends HostsideNetworkTestC runCommand("dumpsys deviceidle whitelist +" + packageName); assertPowerSaveModeWhitelist(packageName, true); // Sanity check } + + protected boolean isDozeModeEnabled() throws Exception { + final String result = runCommand("cmd deviceidle enabled deep").trim(); + return result.equals("1"); + } }