Merge "Turn screen-on if needed, on every iteration network access is verified." into pi-dev

This commit is contained in:
Sudheer Shanka
2018-03-29 14:03:06 +00:00
committed by Android (Google) Code Review

View File

@@ -260,17 +260,21 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
protected void assertBackgroundNetworkAccess(boolean expectAllowed) throws Exception {
assertBackgroundState(); // Sanity check.
assertNetworkAccess(expectAllowed);
assertNetworkAccess(expectAllowed /* expectAvailable */, false /* needScreenOn */);
}
protected void assertForegroundNetworkAccess() throws Exception {
assertForegroundState(); // Sanity check.
assertNetworkAccess(true);
// We verified that app is in foreground state but if the screen turns-off while
// verifying for network access, the app will go into background state (in case app's
// foreground status was due to top activity). So, turn the screen on when verifying
// network connectivity.
assertNetworkAccess(true /* expectAvailable */, true /* needScreenOn */);
}
protected void assertForegroundServiceNetworkAccess() throws Exception {
assertForegroundServiceState(); // Sanity check.
assertNetworkAccess(true);
assertNetworkAccess(true /* expectAvailable */, false /* needScreenOn */);
}
/**
@@ -369,7 +373,8 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
/**
* Asserts whether the active network is available or not.
*/
private void assertNetworkAccess(boolean expectAvailable) throws Exception {
private void assertNetworkAccess(boolean expectAvailable, boolean needScreenOn)
throws Exception {
final int maxTries = 5;
String error = null;
int timeoutMs = 500;
@@ -387,6 +392,9 @@ abstract class AbstractRestrictBackgroundNetworkTestCase extends Instrumentation
Log.w(TAG, "Network status didn't match for expectAvailable=" + expectAvailable
+ " on attempt #" + i + ": " + error + "\n"
+ "Sleeping " + timeoutMs + "ms before trying again");
if (needScreenOn) {
turnScreenOn();
}
// No sleep after the last turn
if (i < maxTries) {
SystemClock.sleep(timeoutMs);