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
diff --git a/tests/cts/net/api23Test/src/android/net/cts/api23test/ConnectivityManagerApi23Test.java b/tests/cts/net/api23Test/src/android/net/cts/api23test/ConnectivityManagerApi23Test.java
index cdb66e3..8d68c5f 100644
--- a/tests/cts/net/api23Test/src/android/net/cts/api23test/ConnectivityManagerApi23Test.java
+++ b/tests/cts/net/api23Test/src/android/net/cts/api23test/ConnectivityManagerApi23Test.java
@@ -57,7 +57,8 @@
/**
* 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 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 @@
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;
diff --git a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java
index e45aa98..a2ad645 100644
--- a/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java
+++ b/tests/cts/net/src/android/net/cts/ConnectivityManagerTest.java
@@ -1042,7 +1042,7 @@
*/
@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();
diff --git a/tests/cts/net/util/java/android/net/cts/util/CtsNetUtils.java b/tests/cts/net/util/java/android/net/cts/util/CtsNetUtils.java
index 103906a..7959c3c 100644
--- a/tests/cts/net/util/java/android/net/cts/util/CtsNetUtils.java
+++ b/tests/cts/net/util/java/android/net/cts/util/CtsNetUtils.java
@@ -168,18 +168,44 @@
}
// 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.
*