Merge "Deflake test to ensure system default network as expected"

This commit is contained in:
Chiachang Wang
2021-07-20 01:20:07 +00:00
committed by Gerrit Code Review
3 changed files with 32 additions and 5 deletions

View File

@@ -57,7 +57,8 @@ public class ConnectivityManagerApi23Test extends AndroidTestCase {
/**
* Tests reporting of connectivity changed.
*/
public void testConnectivityChanged_manifestRequestOnly_shouldNotReceiveIntent() {
public void testConnectivityChanged_manifestRequestOnly_shouldNotReceiveIntent()
throws Exception {
if (!mPackageManager.hasSystemFeature(FEATURE_WIFI)) {
Log.i(TAG, "testConnectivityChanged_manifestRequestOnly_shouldNotReceiveIntent cannot execute unless device supports WiFi");
return;
@@ -75,7 +76,7 @@ public class ConnectivityManagerApi23Test extends AndroidTestCase {
}
public void testConnectivityChanged_manifestRequestOnlyPreN_shouldReceiveIntent()
throws InterruptedException {
throws Exception {
if (!mPackageManager.hasSystemFeature(FEATURE_WIFI)) {
Log.i(TAG, "testConnectivityChanged_manifestRequestOnlyPreN_shouldReceiveIntent cannot"
+ "execute unless device supports WiFi");
@@ -94,7 +95,7 @@ public class ConnectivityManagerApi23Test extends AndroidTestCase {
getConnectivityCount, SEND_BROADCAST_TIMEOUT));
}
public void testConnectivityChanged_whenRegistered_shouldReceiveIntent() {
public void testConnectivityChanged_whenRegistered_shouldReceiveIntent() throws Exception {
if (!mPackageManager.hasSystemFeature(FEATURE_WIFI)) {
Log.i(TAG, "testConnectivityChanged_whenRegistered_shouldReceiveIntent cannot execute unless device supports WiFi");
return;

View File

@@ -1043,7 +1043,7 @@ public class ConnectivityManagerTest {
*/
@AppModeFull(reason = "Cannot get WifiManager in instant app mode")
@Test
public void testToggleWifiConnectivityAction() {
public void testToggleWifiConnectivityAction() throws Exception {
// toggleWifi calls connectToWifi and disconnectFromWifi, which both wait for
// CONNECTIVITY_ACTION broadcasts.
mCtsNetUtils.toggleWifi();

View File

@@ -169,18 +169,44 @@ public final class CtsNetUtils {
}
// Toggle WiFi twice, leaving it in the state it started in
public void toggleWifi() {
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);
}
}
private Network expectNetworkIsSystemDefault(Network network)
throws Exception {
final CompletableFuture<Network> future = new CompletableFuture();
final NetworkCallback cb = new NetworkCallback() {
@Override
public void onAvailable(Network n) {
if (n.equals(network)) future.complete(network);
}
};
try {
mCm.registerDefaultNetworkCallback(cb);
return future.get(CONNECTIVITY_CHANGE_TIMEOUT_SECS, TimeUnit.SECONDS);
} catch (TimeoutException e) {
throw new AssertionError("Timed out waiting for system default network to switch"
+ " to network " + network + ". Current default network is network "
+ mCm.getActiveNetwork(), e);
} finally {
mCm.unregisterNetworkCallback(cb);
}
}
/**
* Enable WiFi and wait for it to become connected to a network.
*