Fix WifiInfoTest flakiness
Poll a bit longer waiting for getNetworkId and getWifiState to return
the proper values. The bug reports seem to indicate that the WiFi
is shutting down properly.
Add a new static check method to PollingCheck that I think is simpler
than the existing way of creating a new PollingCheck, overriding
check, and then making sure to call run. This new variation also allows
you to specify a message rather than just "unexpected timeout."
Bug 6443337
Change-Id: I9f7c942f6e26b957bb717b58b1ab984acc556bf7
diff --git a/libs/util/src/android/cts/util/PollingCheck.java b/libs/util/src/android/cts/util/PollingCheck.java
index 2990992..be6b0b0 100644
--- a/libs/util/src/android/cts/util/PollingCheck.java
+++ b/libs/util/src/android/cts/util/PollingCheck.java
@@ -16,6 +16,8 @@
package android.cts.util;
+import java.util.concurrent.Callable;
+
import junit.framework.Assert;
public abstract class PollingCheck {
@@ -53,4 +55,18 @@
Assert.fail("unexpected timeout");
}
+
+ public static void check(CharSequence message, long timeout, Callable<Boolean> condition)
+ throws Exception {
+ while (timeout > 0) {
+ if (condition.call()) {
+ return;
+ }
+
+ Thread.sleep(TIME_SLICE);
+ timeout -= TIME_SLICE;
+ }
+
+ Assert.fail(message.toString());
+ }
}
diff --git a/tests/tests/net/Android.mk b/tests/tests/net/Android.mk
index 3e48cd2..5c70ad4 100644
--- a/tests/tests/net/Android.mk
+++ b/tests/tests/net/Android.mk
@@ -28,7 +28,7 @@
LOCAL_PACKAGE_NAME := CtsNetTestCases
-LOCAL_STATIC_JAVA_LIBRARIES := ctstestserver
+LOCAL_STATIC_JAVA_LIBRARIES := ctstestserver ctsutil
# uncomment when dalvik.annotation.Test* are removed or part of SDK
#LOCAL_SDK_VERSION := current
diff --git a/tests/tests/net/src/android/net/wifi/cts/WifiInfoTest.java b/tests/tests/net/src/android/net/wifi/cts/WifiInfoTest.java
index df1323b..16dc57d 100644
--- a/tests/tests/net/src/android/net/wifi/cts/WifiInfoTest.java
+++ b/tests/tests/net/src/android/net/wifi/cts/WifiInfoTest.java
@@ -21,12 +21,15 @@
import android.content.Context;
import android.content.Intent;
import android.content.IntentFilter;
+import android.cts.util.PollingCheck;
import android.net.wifi.SupplicantState;
import android.net.wifi.WifiInfo;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiManager.WifiLock;
import android.test.AndroidTestCase;
+import java.util.concurrent.Callable;
+
public class WifiInfoTest extends AndroidTestCase {
private static class MySync {
int expectedState = STATE_NULL;
@@ -127,10 +130,21 @@
wifiInfo.getHiddenSSID();
wifiInfo.getMacAddress();
setWifiEnabled(false);
- Thread.sleep(DURATION);
- wifiInfo = mWifiManager.getConnectionInfo();
- assertEquals(-1, wifiInfo.getNetworkId());
- assertEquals(WifiManager.WIFI_STATE_DISABLED, mWifiManager.getWifiState());
+
+ PollingCheck.check("getNetworkId not -1", 20000, new Callable<Boolean>() {
+ @Override
+ public Boolean call() throws Exception {
+ WifiInfo wifiInfo = mWifiManager.getConnectionInfo();
+ return wifiInfo.getNetworkId() == -1;
+ }
+ });
+
+ PollingCheck.check("getWifiState not disabled", 20000, new Callable<Boolean>() {
+ @Override
+ public Boolean call() throws Exception {
+ return mWifiManager.getWifiState() == WifiManager.WIFI_STATE_DISABLED;
+ }
+ });
}
}