Merge "Deflake test to ensure system default network as expected" am: 575fa2f103
Original change: https://android-review.googlesource.com/c/platform/packages/modules/Connectivity/+/1764308 Change-Id: I7deef58610d456bdcf0f1c1a93b861bf45653164
This commit is contained in:
@@ -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;
|
||||
|
||||
@@ -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();
|
||||
|
||||
@@ -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.
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user