Merge "Fix a possible flake in disconnectFromWiFi"

This commit is contained in:
Treehugger Robot
2021-11-22 07:34:57 +00:00
committed by Gerrit Code Review
2 changed files with 10 additions and 6 deletions

View File

@@ -40,7 +40,6 @@ import android.net.cts.NetworkValidationTestUtil.setUrlExpirationDeviceConfig
import android.net.cts.util.CtsNetUtils import android.net.cts.util.CtsNetUtils
import android.net.util.NetworkStackUtils.TEST_CAPTIVE_PORTAL_HTTPS_URL import android.net.util.NetworkStackUtils.TEST_CAPTIVE_PORTAL_HTTPS_URL
import android.net.util.NetworkStackUtils.TEST_CAPTIVE_PORTAL_HTTP_URL import android.net.util.NetworkStackUtils.TEST_CAPTIVE_PORTAL_HTTP_URL
import android.net.wifi.WifiManager
import android.os.Build import android.os.Build
import android.platform.test.annotations.AppModeFull import android.platform.test.annotations.AppModeFull
import android.provider.DeviceConfig import android.provider.DeviceConfig
@@ -77,7 +76,7 @@ private const val TEST_PORTAL_URL_PATH = "/portal_path"
private const val LOCALHOST_HOSTNAME = "localhost" private const val LOCALHOST_HOSTNAME = "localhost"
// Re-connecting to the AP, obtaining an IP address, revalidating can take a long time // Re-connecting to the AP, obtaining an IP address, revalidating can take a long time
private const val WIFI_CONNECT_TIMEOUT_MS = 120_000L private const val WIFI_CONNECT_TIMEOUT_MS = 40_000L
private const val TEST_TIMEOUT_MS = 10_000L private const val TEST_TIMEOUT_MS = 10_000L
private const val TAG = "CaptivePortalTest" private const val TAG = "CaptivePortalTest"
@@ -94,7 +93,6 @@ private fun <T> CompletableFuture<T>.assertGet(timeoutMs: Long, message: String)
@RunWith(AndroidJUnit4::class) @RunWith(AndroidJUnit4::class)
class CaptivePortalTest { class CaptivePortalTest {
private val context: android.content.Context by lazy { getInstrumentation().context } private val context: android.content.Context by lazy { getInstrumentation().context }
private val wm by lazy { context.getSystemService(WifiManager::class.java) }
private val cm by lazy { context.getSystemService(ConnectivityManager::class.java) } private val cm by lazy { context.getSystemService(ConnectivityManager::class.java) }
private val pm by lazy { context.packageManager } private val pm by lazy { context.packageManager }
private val utils by lazy { CtsNetUtils(context) } private val utils by lazy { CtsNetUtils(context) }

View File

@@ -306,14 +306,18 @@ public final class CtsNetUtils {
} }
try { try {
if (wasWifiConnected) {
// Make sure the callback is registered before turning off WiFi.
callback.waitForAvailable();
}
SystemUtil.runShellCommand("svc wifi disable"); SystemUtil.runShellCommand("svc wifi disable");
if (wasWifiConnected) { if (wasWifiConnected) {
// Ensure we get both an onLost callback and a CONNECTIVITY_ACTION. // Ensure we get both an onLost callback and a CONNECTIVITY_ACTION.
assertNotNull("Did not receive onLost callback after disabling wifi", assertNotNull("Did not receive onLost callback after disabling wifi",
callback.waitForLost()); callback.waitForLost());
} if (expectLegacyBroadcast) {
if (wasWifiConnected && expectLegacyBroadcast) { assertTrue("Wifi failed to reach DISCONNECTED state.", receiver.waitForState());
assertTrue("Wifi failed to reach DISCONNECTED state.", receiver.waitForState()); }
} }
} catch (InterruptedException ex) { } catch (InterruptedException ex) {
fail("disconnectFromWifi was interrupted"); fail("disconnectFromWifi was interrupted");
@@ -599,12 +603,14 @@ public final class CtsNetUtils {
@Override @Override
public void onAvailable(Network network) { public void onAvailable(Network network) {
Log.i(TAG, "CtsNetUtils TestNetworkCallback onAvailable " + network);
currentNetwork = network; currentNetwork = network;
mAvailableCv.open(); mAvailableCv.open();
} }
@Override @Override
public void onLost(Network network) { public void onLost(Network network) {
Log.i(TAG, "CtsNetUtils TestNetworkCallback onLost " + network);
lastLostNetwork = network; lastLostNetwork = network;
if (network.equals(currentNetwork)) { if (network.equals(currentNetwork)) {
mAvailableCv.close(); mAvailableCv.close();