diff --git a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java index 2f36b936f2..db7904e775 100644 --- a/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java +++ b/tests/cts/hostside/app/src/com/android/cts/net/hostside/AbstractRestrictBackgroundNetworkTestCase.java @@ -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);