Merge "Added test for required packages whitelisted for Data Saver Mode." into nyc-dev

am: 16ae24badd

* commit '16ae24badd79bf100b26dc4e6dfb3ed649a0c3ad':
  Added test for required packages whitelisted for Data Saver Mode.

Change-Id: I1916d82a0fcc883679e200f79594381d29afd6b2
This commit is contained in:
Felipe Leme
2016-04-29 23:49:49 +00:00
committed by android-build-merger
3 changed files with 39 additions and 4 deletions

View File

@@ -377,18 +377,24 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
private void assertRestrictBackground(String list, int uid, boolean expected) throws Exception { private void assertRestrictBackground(String list, int uid, boolean expected) throws Exception {
final int maxTries = 5; final int maxTries = 5;
boolean actual = false; boolean actual = false;
final String expectedUid = Integer.toString(uid);
String uids = "";
for (int i = 1; i <= maxTries; i++) { for (int i = 1; i <= maxTries; i++) {
final String output = final String output =
executeShellCommand("cmd netpolicy list " + list); executeShellCommand("cmd netpolicy list " + list);
actual = output.contains(Integer.toString(uid)); uids = output.split(":")[1];
if (expected == actual) { for (String candidate : uids.split(" ")) {
return; actual = candidate.trim().equals(expectedUid);
if (expected == actual) {
return;
}
} }
Log.v(TAG, list + " check for uid " + uid + " doesn't match yet (expected " Log.v(TAG, list + " check for uid " + uid + " doesn't match yet (expected "
+ expected + ", got " + actual + "); sleeping 1s before polling again"); + expected + ", got " + actual + "); sleeping 1s before polling again");
Thread.sleep(SECOND_IN_MS); Thread.sleep(SECOND_IN_MS);
} }
fail(list + " check for uid " + uid + " failed: expected " + expected + ", got " + actual); fail(list + " check for uid " + uid + " failed: expected " + expected + ", got " + actual
+ ". Full list: " + uids);
} }
protected void assertPowerSaveModeWhitelist(String packageName, boolean expected) protected void assertPowerSaveModeWhitelist(String packageName, boolean expected)

View File

@@ -31,6 +31,10 @@ import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_WHITELI
*/ */
public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase { public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase {
private static final String[] REQUIRED_WHITELISTED_PACKAGES = {
"com.android.providers.downloads"
};
@Override @Override
public void setUp() throws Exception { public void setUp() throws Exception {
super.setUp(); super.setUp();
@@ -108,4 +112,24 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase
assertRestrictBackgroundChangedReceived(5); assertRestrictBackgroundChangedReceived(5);
assertRestrictBackgroundStatus(RESTRICT_BACKGROUND_STATUS_DISABLED); assertRestrictBackgroundStatus(RESTRICT_BACKGROUND_STATUS_DISABLED);
} }
public void testGetRestrictBackgroundStatus_requiredWhitelistedPackages() throws Exception {
final StringBuilder error = new StringBuilder();
for (String packageName : REQUIRED_WHITELISTED_PACKAGES) {
int uid = -1;
try {
uid = getUid(packageName);
assertRestrictBackgroundWhitelist(uid, true);
} catch (Throwable t) {
error.append("\nFailed for '").append(packageName).append("'");
if (uid > 0) {
error.append(" (uid ").append(uid).append(")");
}
error.append(": ").append(t).append("\n");
}
}
if (error.length() > 0) {
fail(error.toString());
}
}
} }

View File

@@ -73,6 +73,11 @@ public class HostsideRestrictBackgroundNetworkTests extends HostsideNetworkTestC
assertRestrictBackgroundWhitelist(newUid, false); assertRestrictBackgroundWhitelist(newUid, false);
} }
public void testDataSaverMode_requiredWhitelistedPackages() throws Exception {
runDeviceTests(TEST_PKG, TEST_PKG + ".DataSaverModeTest",
"testGetRestrictBackgroundStatus_requiredWhitelistedPackages");
}
public void testBatterySaverMode_disabled() throws Exception { public void testBatterySaverMode_disabled() throws Exception {
runDeviceTests(TEST_PKG, TEST_PKG + ".BatterySaverModeTest", runDeviceTests(TEST_PKG, TEST_PKG + ".BatterySaverModeTest",
"testBackgroundNetworkAccess_disabled"); "testBackgroundNetworkAccess_disabled");