Merge "Add polling for WifiManager#getConnectionInfo"

This commit is contained in:
Chris Ye
2022-12-08 07:10:06 +00:00
committed by Gerrit Code Review
3 changed files with 20 additions and 3 deletions

View File

@@ -2856,7 +2856,7 @@ public class ConnectivityManagerTest {
// Wifi will not automatically reconnect to the network. ensureWifiDisconnected cannot
// apply here. Thus, turn off wifi first and restart to restore.
mTestValidationConfigRule.runAfterNextCleanup(() -> {
runShellCommand("svc wifi disable");
mCtsNetUtils.disableWifi();
mCtsNetUtils.ensureWifiConnected();
});
}
@@ -2898,7 +2898,7 @@ public class ConnectivityManagerTest {
/// Wifi will not automatically reconnect to the network. ensureWifiDisconnected cannot
// apply here. Thus, turn off wifi first and restart to restore.
mTestValidationConfigRule.runAfterNextCleanup(() -> {
runShellCommand("svc wifi disable");
mCtsNetUtils.disableWifi();
mCtsNetUtils.ensureWifiConnected();
});
}

View File

@@ -57,6 +57,8 @@ import android.system.OsConstants;
import android.text.TextUtils;
import android.util.Log;
import com.android.compatibility.common.util.PollingCheck;
import com.android.compatibility.common.util.ShellIdentityUtils;
import com.android.compatibility.common.util.SystemUtil;
import com.android.net.module.util.ConnectivitySettingsUtils;
import com.android.testutils.ConnectUtil;
@@ -267,6 +269,19 @@ public final class CtsNetUtils {
disconnectFromWifi(wifiNetworkToCheck, false /* expectLegacyBroadcast */);
}
/**
* Disable WiFi and wait for the connection info to be cleared.
*/
public void disableWifi() throws Exception {
SystemUtil.runShellCommand("svc wifi disable");
PollingCheck.check(
"Wifi not disconnected! Current network is not null "
+ mWifiManager.getConnectionInfo().getNetworkId(),
TimeUnit.SECONDS.toMillis(CONNECTIVITY_CHANGE_TIMEOUT_SECS),
() -> ShellIdentityUtils.invokeWithShellPermissions(
() -> mWifiManager.getConnectionInfo().getNetworkId()) == -1);
}
/**
* Disable WiFi and wait for it to become disconnected from the network.
*

View File

@@ -478,7 +478,9 @@ public final class CtsTetheringUtils {
waitForWifiEnabled(ctx);
return runAsShell(ACCESS_WIFI_STATE, () -> wm.isPortableHotspotSupported());
} finally {
if (!previousWifiEnabledState) SystemUtil.runShellCommand("svc wifi disable");
if (!previousWifiEnabledState) {
new CtsNetUtils(ctx).disableWifi();
}
}
}