Merge "Skip Doze Mode-related tests when device does not support it." into nyc-dev

am: 07924c0489

* commit '07924c0489b60f460b285eddc69533cbda51f5a9':
  Skip Doze Mode-related tests when device does not support it.

Change-Id: Ibbe3e77b88ffdcf7fe773aa18314661b59afca75
This commit is contained in:
Felipe Leme
2016-06-02 21:43:46 +00:00
committed by android-build-merger
7 changed files with 136 additions and 0 deletions

View File

@@ -16,6 +16,8 @@
package com.android.cts.net.hostside; package com.android.cts.net.hostside;
import android.util.Log;
/** /**
* Base class for metered and non-metered tests on idle apps. * 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 { protected final void setUp() throws Exception {
super.setUp(); super.setUp();
if (!isSupported()) return;
// Set initial state. // Set initial state.
setUpMeteredNetwork(); setUpMeteredNetwork();
removePowerSaveModeWhitelist(TEST_APP2_PKG); removePowerSaveModeWhitelist(TEST_APP2_PKG);
@@ -38,6 +42,8 @@ abstract class AbstractAppIdleTestCase extends AbstractRestrictBackgroundNetwork
protected final void tearDown() throws Exception { protected final void tearDown() throws Exception {
super.tearDown(); super.tearDown();
if (!isSupported()) return;
try { try {
tearDownMeteredNetwork(); tearDownMeteredNetwork();
} finally { } 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. * Sets the initial (non) metered network state.
* *
@@ -63,6 +79,8 @@ abstract class AbstractAppIdleTestCase extends AbstractRestrictBackgroundNetwork
} }
public void testBackgroundNetworkAccess_enabled() throws Exception { public void testBackgroundNetworkAccess_enabled() throws Exception {
if (!isSupported()) return;
setAppIdle(true); setAppIdle(true);
assertBackgroundNetworkAccess(false); assertBackgroundNetworkAccess(false);
@@ -92,6 +110,8 @@ abstract class AbstractAppIdleTestCase extends AbstractRestrictBackgroundNetwork
} }
public void testBackgroundNetworkAccess_whitelisted() throws Exception { public void testBackgroundNetworkAccess_whitelisted() throws Exception {
if (!isSupported()) return;
setAppIdle(true); setAppIdle(true);
assertBackgroundNetworkAccess(false); assertBackgroundNetworkAccess(false);
@@ -111,6 +131,8 @@ abstract class AbstractAppIdleTestCase extends AbstractRestrictBackgroundNetwork
} }
public void testBackgroundNetworkAccess_disabled() throws Exception { public void testBackgroundNetworkAccess_disabled() throws Exception {
if (!isSupported()) return;
assertBackgroundNetworkAccess(true); assertBackgroundNetworkAccess(true);
assertsForegroundAlwaysHasNetworkAccess(); assertsForegroundAlwaysHasNetworkAccess();

View File

@@ -16,6 +16,8 @@
package com.android.cts.net.hostside; package com.android.cts.net.hostside;
import android.util.Log;
/** /**
* Base class for metered and non-metered Battery Saver Mode tests. * 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 { protected final void setUp() throws Exception {
super.setUp(); super.setUp();
if (!isSupported()) return;
// Set initial state. // Set initial state.
setUpMeteredNetwork(); setUpMeteredNetwork();
removePowerSaveModeWhitelist(TEST_APP2_PKG); removePowerSaveModeWhitelist(TEST_APP2_PKG);
@@ -37,6 +41,8 @@ abstract class AbstractBatterySaverModeTestCase extends AbstractRestrictBackgrou
protected final void tearDown() throws Exception { protected final void tearDown() throws Exception {
super.tearDown(); super.tearDown();
if (!isSupported()) return;
try { try {
tearDownMeteredNetwork(); tearDownMeteredNetwork();
} finally { } 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. * Sets the initial (non) metered network state.
* *
@@ -61,6 +77,8 @@ abstract class AbstractBatterySaverModeTestCase extends AbstractRestrictBackgrou
} }
public void testBackgroundNetworkAccess_enabled() throws Exception { public void testBackgroundNetworkAccess_enabled() throws Exception {
if (!isSupported()) return;
setBatterySaverMode(true); setBatterySaverMode(true);
assertBackgroundNetworkAccess(false); assertBackgroundNetworkAccess(false);
@@ -87,6 +105,8 @@ abstract class AbstractBatterySaverModeTestCase extends AbstractRestrictBackgrou
} }
public void testBackgroundNetworkAccess_whitelisted() throws Exception { public void testBackgroundNetworkAccess_whitelisted() throws Exception {
if (!isSupported()) return;
setBatterySaverMode(true); setBatterySaverMode(true);
assertBackgroundNetworkAccess(false); assertBackgroundNetworkAccess(false);
@@ -101,6 +121,8 @@ abstract class AbstractBatterySaverModeTestCase extends AbstractRestrictBackgrou
} }
public void testBackgroundNetworkAccess_disabled() throws Exception { public void testBackgroundNetworkAccess_disabled() throws Exception {
if (!isSupported()) return;
assertBackgroundNetworkAccess(true); assertBackgroundNetworkAccess(true);
assertsForegroundAlwaysHasNetworkAccess(); assertsForegroundAlwaysHasNetworkAccess();

View File

@@ -17,6 +17,7 @@
package com.android.cts.net.hostside; package com.android.cts.net.hostside;
import android.os.SystemClock; import android.os.SystemClock;
import android.util.Log;
/** /**
* Base class for metered and non-metered Doze Mode tests. * 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 { protected final void setUp() throws Exception {
super.setUp(); super.setUp();
if (!isSupported()) return;
// Set initial state. // Set initial state.
setUpMeteredNetwork(); setUpMeteredNetwork();
removePowerSaveModeWhitelist(TEST_APP2_PKG); removePowerSaveModeWhitelist(TEST_APP2_PKG);
@@ -39,6 +42,8 @@ abstract class AbstractDozeModeTestCase extends AbstractRestrictBackgroundNetwor
protected final void tearDown() throws Exception { protected final void tearDown() throws Exception {
super.tearDown(); super.tearDown();
if (!isSupported()) return;
try { try {
tearDownMeteredNetwork(); tearDownMeteredNetwork();
} finally { } 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. * Sets the initial (non) metered network state.
* *
@@ -63,6 +78,8 @@ abstract class AbstractDozeModeTestCase extends AbstractRestrictBackgroundNetwor
} }
public void testBackgroundNetworkAccess_enabled() throws Exception { public void testBackgroundNetworkAccess_enabled() throws Exception {
if (!isSupported()) return;
setDozeMode(true); setDozeMode(true);
assertBackgroundNetworkAccess(false); assertBackgroundNetworkAccess(false);
@@ -81,6 +98,8 @@ abstract class AbstractDozeModeTestCase extends AbstractRestrictBackgroundNetwor
} }
public void testBackgroundNetworkAccess_whitelisted() throws Exception { public void testBackgroundNetworkAccess_whitelisted() throws Exception {
if (!isSupported()) return;
setDozeMode(true); setDozeMode(true);
assertBackgroundNetworkAccess(false); assertBackgroundNetworkAccess(false);
@@ -95,6 +114,8 @@ abstract class AbstractDozeModeTestCase extends AbstractRestrictBackgroundNetwor
} }
public void testBackgroundNetworkAccess_disabled() throws Exception { public void testBackgroundNetworkAccess_disabled() throws Exception {
if (!isSupported()) return;
assertBackgroundNetworkAccess(true); assertBackgroundNetworkAccess(true);
assertsForegroundAlwaysHasNetworkAccess(); assertsForegroundAlwaysHasNetworkAccess();
@@ -103,6 +124,8 @@ abstract class AbstractDozeModeTestCase extends AbstractRestrictBackgroundNetwor
public void testBackgroundNetworkAccess_enabledButWhitelistedOnNotificationAction() public void testBackgroundNetworkAccess_enabledButWhitelistedOnNotificationAction()
throws Exception { throws Exception {
if (!isSupported()) return;
setPendingIntentWhitelistDuration(NETWORK_TIMEOUT_MS); setPendingIntentWhitelistDuration(NETWORK_TIMEOUT_MS);
try { try {
registerNotificationListenerService(); registerNotificationListenerService();

View File

@@ -187,6 +187,22 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
assertNetworkAccess(true); 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. * 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 { 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); Log.i(TAG, "Setting Doze Mode to " + enabled);
if (enabled) { if (enabled) {
turnBatteryOff(); turnBatteryOff();
@@ -616,6 +635,11 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
assertDelayedShellCommand("dumpsys deviceidle get deep", enabled ? "IDLE" : "ACTIVE"); 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 { protected void setAppIdle(boolean enabled) throws Exception {
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

@@ -30,6 +30,8 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
if (!isSupported()) return;
// Set initial state. // Set initial state.
setMeteredNetwork(); setMeteredNetwork();
setRestrictBackground(false); setRestrictBackground(false);
@@ -44,6 +46,8 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
super.tearDown(); super.tearDown();
if (!isSupported()) return;
try { try {
resetMeteredNetwork(); resetMeteredNetwork();
} finally { } finally {
@@ -52,6 +56,8 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase
} }
public void testGetRestrictBackgroundStatus_disabled() throws Exception { public void testGetRestrictBackgroundStatus_disabled() throws Exception {
if (!isSupported()) return;
assertDataSaverStatusOnBackground(RESTRICT_BACKGROUND_STATUS_DISABLED); assertDataSaverStatusOnBackground(RESTRICT_BACKGROUND_STATUS_DISABLED);
// Sanity check: make sure status is always disabled, never whitelisted // 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 { public void testGetRestrictBackgroundStatus_whitelisted() throws Exception {
if (!isSupported()) return;
setRestrictBackground(true); setRestrictBackground(true);
assertRestrictBackgroundChangedReceived(1); assertRestrictBackgroundChangedReceived(1);
assertDataSaverStatusOnBackground(RESTRICT_BACKGROUND_STATUS_ENABLED); assertDataSaverStatusOnBackground(RESTRICT_BACKGROUND_STATUS_ENABLED);
@@ -81,6 +89,8 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase
} }
public void testGetRestrictBackgroundStatus_enabled() throws Exception { public void testGetRestrictBackgroundStatus_enabled() throws Exception {
if (!isSupported()) return;
setRestrictBackground(true); setRestrictBackground(true);
assertRestrictBackgroundChangedReceived(1); assertRestrictBackgroundChangedReceived(1);
assertDataSaverStatusOnBackground(RESTRICT_BACKGROUND_STATUS_ENABLED); assertDataSaverStatusOnBackground(RESTRICT_BACKGROUND_STATUS_ENABLED);
@@ -108,6 +118,8 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase
} }
public void testGetRestrictBackgroundStatus_blacklisted() throws Exception { public void testGetRestrictBackgroundStatus_blacklisted() throws Exception {
if (!isSupported()) return;
addRestrictBackgroundBlacklist(mUid); addRestrictBackgroundBlacklist(mUid);
assertRestrictBackgroundChangedReceived(1); assertRestrictBackgroundChangedReceived(1);
assertDataSaverStatusOnBackground(RESTRICT_BACKGROUND_STATUS_ENABLED); assertDataSaverStatusOnBackground(RESTRICT_BACKGROUND_STATUS_ENABLED);
@@ -136,6 +148,8 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase
} }
public void testGetRestrictBackgroundStatus_requiredWhitelistedPackages() throws Exception { public void testGetRestrictBackgroundStatus_requiredWhitelistedPackages() throws Exception {
if (!isSupported()) return;
final StringBuilder error = new StringBuilder(); final StringBuilder error = new StringBuilder();
for (String packageName : REQUIRED_WHITELISTED_PACKAGES) { for (String packageName : REQUIRED_WHITELISTED_PACKAGES) {
int uid = -1; int uid = -1;

View File

@@ -32,6 +32,8 @@ public class MixedModesTest extends AbstractRestrictBackgroundNetworkTestCase {
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
if (!isSupported()) return;
// Set initial state. // Set initial state.
removeRestrictBackgroundWhitelist(mUid); removeRestrictBackgroundWhitelist(mUid);
removeRestrictBackgroundBlacklist(mUid); removeRestrictBackgroundBlacklist(mUid);
@@ -44,6 +46,8 @@ public class MixedModesTest extends AbstractRestrictBackgroundNetworkTestCase {
protected void tearDown() throws Exception { protected void tearDown() throws Exception {
super.tearDown(); super.tearDown();
if (!isSupported()) return;
try { try {
setRestrictBackground(false); setRestrictBackground(false);
} finally { } 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. * Tests all DS ON and BS ON scenarios from network-policy-restrictions.md on metered networks.
*/ */
public void testDataAndBatterySaverModes_meteredNetwork() throws Exception { 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"); Log.i(TAG, "testDataAndBatterySaverModes_meteredNetwork() tests");
setMeteredNetwork(); setMeteredNetwork();
@@ -119,6 +131,14 @@ public class MixedModesTest extends AbstractRestrictBackgroundNetworkTestCase {
* networks. * networks.
*/ */
public void testDataAndBatterySaverModes_nonMeteredNetwork() throws Exception { 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()) { if (mCm.isActiveNetworkMetered()) {
Log.w(TAG, "testDataAndBatterySaverModes_nonMeteredNetwork() skipped because network" Log.w(TAG, "testDataAndBatterySaverModes_nonMeteredNetwork() skipped because network"
+ " is metered"); + " is metered");

View File

@@ -101,6 +101,12 @@ public class HostsideRestrictBackgroundNetworkTests extends HostsideNetworkTestC
} }
public void testBatterySaverMode_reinstall() throws Exception { 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); addPowerSaveModeWhitelist(TEST_APP2_PKG);
uninstallPackage(TEST_APP2_PKG, true); uninstallPackage(TEST_APP2_PKG, true);
@@ -287,4 +293,9 @@ public class HostsideRestrictBackgroundNetworkTests extends HostsideNetworkTestC
runCommand("dumpsys deviceidle whitelist +" + packageName); runCommand("dumpsys deviceidle whitelist +" + packageName);
assertPowerSaveModeWhitelist(packageName, true); // Sanity check assertPowerSaveModeWhitelist(packageName, true); // Sanity check
} }
protected boolean isDozeModeEnabled() throws Exception {
final String result = runCommand("cmd deviceidle enabled deep").trim();
return result.equals("1");
}
} }