Merge "Add reconnectCell util, refactor reconnectWifi" into main

This commit is contained in:
Remi NGUYEN VAN 2024-01-30 02:44:28 +00:00 • committed by Gerrit Code Review
commit d7773093ee
5 changed files with 42 additions and 25 deletions

View file

@ -865,12 +865,12 @@ public abstract class EthernetTetheringTestBase {
// trigger the reselection, the total test time may over test suite 1 minmute timeout.
// Probably need to disable/restore all internet networks in a common place of test
// process. Currently, EthernetTetheringTest is part of CTS test which needs wifi network
// connection if device has wifi feature. CtsNetUtils#toggleWifi() checks wifi connection
// during the toggling process.
// See Tethering#chooseUpstreamType, CtsNetUtils#toggleWifi.
// connection if device has wifi feature.
// See Tethering#chooseUpstreamType
// TODO: toggle cellular network if the device has no WIFI feature.
Log.d(TAG, "Toggle WIFI to retry upstream selection");
sCtsNetUtils.toggleWifi();
sCtsNetUtils.disableWifi();
sCtsNetUtils.ensureWifiConnected();
// Wait for expected upstream.
final CompletableFuture<Network> future = new CompletableFuture<>();

View file

@ -65,7 +65,7 @@ public class ConnectivityManagerApi23Test extends AndroidTestCase {
}
ConnectivityReceiver.prepare();
mCtsNetUtils.toggleWifi();
mCtsNetUtils.reconnectWifiAndWaitForConnectivityAction();
// The connectivity broadcast has been sent; push through a terminal broadcast
// to wait for in the receive to confirm it didn't see the connectivity change.
@ -88,7 +88,7 @@ public class ConnectivityManagerApi23Test extends AndroidTestCase {
.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK));
Thread.sleep(200);
mCtsNetUtils.toggleWifi();
mCtsNetUtils.reconnectWifiAndWaitForConnectivityAction();
Intent getConnectivityCount = new Intent(GET_WIFI_CONNECTIVITY_ACTION_COUNT);
assertEquals(2, sendOrderedBroadcastAndReturnResultCode(
@ -106,7 +106,7 @@ public class ConnectivityManagerApi23Test extends AndroidTestCase {
filter.addAction(ConnectivityManager.CONNECTIVITY_ACTION);
mContext.registerReceiver(receiver, filter);
mCtsNetUtils.toggleWifi();
mCtsNetUtils.reconnectWifiAndWaitForConnectivityAction();
Intent finalIntent = new Intent(ConnectivityReceiver.FINAL_ACTION);
finalIntent.setClass(mContext, ConnectivityReceiver.class);
mContext.sendBroadcast(finalIntent);

View file

@ -199,7 +199,8 @@ public class BatteryStatsManagerTest{
Log.d(TAG, "Generate traffic on wifi network.");
generateNetworkTraffic(wifiNetwork, url);
// Wifi battery stats are updated when wifi on.
mCtsNetUtils.toggleWifi();
mCtsNetUtils.disableWifi();
mCtsNetUtils.ensureWifiConnected();
// Check wifi battery stats are updated.
runAsShell(UPDATE_DEVICE_STATS,

View file

@ -1353,9 +1353,7 @@ public class ConnectivityManagerTest {
public void testToggleWifiConnectivityAction() throws Exception {
assumeTrue(mPackageManager.hasSystemFeature(FEATURE_WIFI));
// toggleWifi calls connectToWifi and disconnectFromWifi, which both wait for
// CONNECTIVITY_ACTION broadcasts.
mCtsNetUtils.toggleWifi();
mCtsNetUtils.reconnectWifiAndWaitForConnectivityAction();
}
/** Verify restricted networks cannot be requested. */

View file

@ -173,21 +173,39 @@ public final class CtsNetUtils {
return cb;
}
// Toggle WiFi twice, leaving it in the state it started in
public void toggleWifi() throws Exception {
if (mWifiManager.isWifiEnabled()) {
Network wifiNetwork = getWifiNetwork();
// Ensure system default network is WIFI because it's expected in disconnectFromWifi()
expectNetworkIsSystemDefault(wifiNetwork);
disconnectFromWifi(wifiNetwork);
connectToWifi();
} else {
connectToWifi();
Network wifiNetwork = getWifiNetwork();
// Ensure system default network is WIFI because it's expected in disconnectFromWifi()
expectNetworkIsSystemDefault(wifiNetwork);
disconnectFromWifi(wifiNetwork);
/**
* Toggle Wi-Fi off and on, waiting for the {@link ConnectivityManager#CONNECTIVITY_ACTION}
* broadcast in both cases.
*/
public void reconnectWifiAndWaitForConnectivityAction() throws Exception {
assertTrue(mWifiManager.isWifiEnabled());
Network wifiNetwork = getWifiNetwork();
// Ensure system default network is WIFI because it's expected in disconnectFromWifi()
expectNetworkIsSystemDefault(wifiNetwork);
disconnectFromWifi(wifiNetwork, true /* expectLegacyBroadcast */);
connectToWifi(true /* expectLegacyBroadcast */);
}
/**
* Turn Wi-Fi off, then back on and make sure it connects, if it is supported.
*/
public void reconnectWifiIfSupported() throws Exception {
if (!mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_WIFI)) {
return;
}
disableWifi();
ensureWifiConnected();
}
/**
* Turn cell data off, then back on and make sure it connects, if it is supported.
*/
public void reconnectCellIfSupported() throws Exception {
if (!mContext.getPackageManager().hasSystemFeature(PackageManager.FEATURE_TELEPHONY)) {
return;
}
setMobileDataEnabled(false);
setMobileDataEnabled(true);
}
public Network expectNetworkIsSystemDefault(Network network)