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

am: 9d9ca56079

* commit '9d9ca5607943a7b86f8ebf2b801032d541236a9f':
  Added test for required packages whitelisted for Data Saver Mode.

Change-Id: Id48eff1051969b37747987c37118b6a476419ce0
This commit is contained in:
Felipe Leme
2016-04-29 23:52:06 +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 {
final int maxTries = 5;
boolean actual = false;
final String expectedUid = Integer.toString(uid);
String uids = "";
for (int i = 1; i <= maxTries; i++) {
final String output =
executeShellCommand("cmd netpolicy list " + list);
actual = output.contains(Integer.toString(uid));
if (expected == actual) {
return;
uids = output.split(":")[1];
for (String candidate : uids.split(" ")) {
actual = candidate.trim().equals(expectedUid);
if (expected == actual) {
return;
}
}
Log.v(TAG, list + " check for uid " + uid + " doesn't match yet (expected "
+ expected + ", got " + actual + "); sleeping 1s before polling again");
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)

View File

@@ -31,6 +31,10 @@ import static android.net.ConnectivityManager.RESTRICT_BACKGROUND_STATUS_WHITELI
*/
public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase {
private static final String[] REQUIRED_WHITELISTED_PACKAGES = {
"com.android.providers.downloads"
};
@Override
public void setUp() throws Exception {
super.setUp();
@@ -108,4 +112,24 @@ public class DataSaverModeTest extends AbstractRestrictBackgroundNetworkTestCase
assertRestrictBackgroundChangedReceived(5);
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);
}
public void testDataSaverMode_requiredWhitelistedPackages() throws Exception {
runDeviceTests(TEST_PKG, TEST_PKG + ".DataSaverModeTest",
"testGetRestrictBackgroundStatus_requiredWhitelistedPackages");
}
public void testBatterySaverMode_disabled() throws Exception {
runDeviceTests(TEST_PKG, TEST_PKG + ".BatterySaverModeTest",
"testBackgroundNetworkAccess_disabled");