Deflake test to ensure system default network as expected
CtsNetUtils.toggleWifi() expects to receive a CONNECTIVITY_ACTION broadcast after disconnecting from wifi if wifi is enabled. The wifi may be connected but not validated since wifi just turns back to connected from the previous test. In this case, the system default netwok will not be wifi, so there is no CONNECTIVITY_ACTION broadcast after disconnecting wifi. It should ensure the wifi is system default network first before proceeding with other verifications. Bug: 192213759 Test: atest CtsNetTestCases --iterations 20 Change-Id: I82f0634883362e35b88854aae28e61b75a3cd7cc
This commit is contained in:
parent
5b37955322
commit
ac059de6ad
3 changed files with 32 additions and 5 deletions
|
|
@ -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;
|
||||
|
|
|
|||
|
|
@ -1042,7 +1042,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();
|
||||
|
|
|
|||
|
|
@ -168,18 +168,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.
|
||||
*
|
||||
|
|
|
|||
Loading…
Reference in a new issue