Skip Doze Mode-related tests when device does not support it.

Fixes: 29072117
Change-Id: I7ca37eae58258c021ed6297a9f1ee3b2749da7d7
This commit is contained in:
Felipe Leme
2016-06-02 12:01:26 -07:00
parent b9b85ce0cd
commit d1f3d68694
7 changed files with 136 additions and 0 deletions

View File

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

View File

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

View File

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

View File

@@ -187,6 +187,22 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
assertNetworkAccess(true);
}
/**
* Whether this device suport this type of test.
*
* <p>Should be overridden when necessary, and explicitly used before each test. Example:
*
* <pre><code>
* public void testSomething() {
* if (!isSupported()) return;
* </code></pre>
*
* @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 );

View File

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

View File

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

View File

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