Fix race condition causing occasional CTS failures.
WifiManagerTest was waiting for SUPPLICANT_STATE_CHANGED_ACTION after enabling or disabling WiFi. Fix the code to check if the WiFi state is already the desired state, and if not, to wait for the WIFI_STATE_CHANGED_ACTION broadcast intent. Bug: 7082455 Change-Id: Id1c2242c32311084f5587ea5403f6b227d1b8b04
This commit is contained in:
@@ -55,9 +55,10 @@ public class WifiManagerTest extends AndroidTestCase {
|
|||||||
|
|
||||||
private static final int STATE_NULL = 0;
|
private static final int STATE_NULL = 0;
|
||||||
private static final int STATE_WIFI_CHANGING = 1;
|
private static final int STATE_WIFI_CHANGING = 1;
|
||||||
private static final int STATE_WIFI_CHANGED = 2;
|
private static final int STATE_WIFI_ENABLED = 2;
|
||||||
private static final int STATE_SCANING = 3;
|
private static final int STATE_WIFI_DISABLED = 3;
|
||||||
private static final int STATE_SCAN_RESULTS_AVAILABLE = 4;
|
private static final int STATE_SCANNING = 4;
|
||||||
|
private static final int STATE_SCAN_RESULTS_AVAILABLE = 5;
|
||||||
|
|
||||||
private static final String TAG = "WifiManagerTest";
|
private static final String TAG = "WifiManagerTest";
|
||||||
private static final String SSID1 = "\"WifiManagerTest\"";
|
private static final String SSID1 = "\"WifiManagerTest\"";
|
||||||
@@ -76,20 +77,29 @@ public class WifiManagerTest extends AndroidTestCase {
|
|||||||
mScanResult = mWifiManager.getScanResults();
|
mScanResult = mWifiManager.getScanResults();
|
||||||
mMySync.expectedState = STATE_SCAN_RESULTS_AVAILABLE;
|
mMySync.expectedState = STATE_SCAN_RESULTS_AVAILABLE;
|
||||||
mScanResult = mWifiManager.getScanResults();
|
mScanResult = mWifiManager.getScanResults();
|
||||||
mMySync.notify();
|
mMySync.notifyAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else if (action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
|
} else if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
|
||||||
|
int newState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
|
||||||
|
WifiManager.WIFI_STATE_UNKNOWN);
|
||||||
synchronized (mMySync) {
|
synchronized (mMySync) {
|
||||||
mMySync.expectedState = STATE_WIFI_CHANGED;
|
if (newState == WifiManager.WIFI_STATE_ENABLED) {
|
||||||
mMySync.notify();
|
Log.d(TAG, "*** New WiFi state is ENABLED ***");
|
||||||
|
mMySync.expectedState = STATE_WIFI_ENABLED;
|
||||||
|
mMySync.notifyAll();
|
||||||
|
} else if (newState == WifiManager.WIFI_STATE_DISABLED) {
|
||||||
|
Log.d(TAG, "*** New WiFi state is DISABLED ***");
|
||||||
|
mMySync.expectedState = STATE_WIFI_DISABLED;
|
||||||
|
mMySync.notifyAll();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
} else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
|
} else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
|
||||||
synchronized (mMySync) {
|
synchronized (mMySync) {
|
||||||
mNetworkInfo =
|
mNetworkInfo =
|
||||||
(NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
|
(NetworkInfo) intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
|
||||||
if (mNetworkInfo.getState() == NetworkInfo.State.CONNECTED)
|
if (mNetworkInfo.getState() == NetworkInfo.State.CONNECTED)
|
||||||
mMySync.notify();
|
mMySync.notifyAll();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -122,7 +132,9 @@ public class WifiManagerTest extends AndroidTestCase {
|
|||||||
setWifiEnabled(true);
|
setWifiEnabled(true);
|
||||||
Thread.sleep(DURATION);
|
Thread.sleep(DURATION);
|
||||||
assertTrue(mWifiManager.isWifiEnabled());
|
assertTrue(mWifiManager.isWifiEnabled());
|
||||||
mMySync.expectedState = STATE_NULL;
|
synchronized (mMySync) {
|
||||||
|
mMySync.expectedState = STATE_NULL;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
@@ -132,31 +144,34 @@ public class WifiManagerTest extends AndroidTestCase {
|
|||||||
super.tearDown();
|
super.tearDown();
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
mWifiLock.release();
|
|
||||||
mContext.unregisterReceiver(mReceiver);
|
|
||||||
if (!mWifiManager.isWifiEnabled())
|
if (!mWifiManager.isWifiEnabled())
|
||||||
setWifiEnabled(true);
|
setWifiEnabled(true);
|
||||||
|
mWifiLock.release();
|
||||||
|
mContext.unregisterReceiver(mReceiver);
|
||||||
Thread.sleep(DURATION);
|
Thread.sleep(DURATION);
|
||||||
super.tearDown();
|
super.tearDown();
|
||||||
}
|
}
|
||||||
|
|
||||||
private void setWifiEnabled(boolean enable) throws Exception {
|
private void setWifiEnabled(boolean enable) throws Exception {
|
||||||
synchronized (mMySync) {
|
synchronized (mMySync) {
|
||||||
mMySync.expectedState = STATE_WIFI_CHANGING;
|
|
||||||
assertTrue(mWifiManager.setWifiEnabled(enable));
|
assertTrue(mWifiManager.setWifiEnabled(enable));
|
||||||
long timeout = System.currentTimeMillis() + TIMEOUT_MSEC;
|
if (mWifiManager.isWifiEnabled() != enable) {
|
||||||
while (System.currentTimeMillis() < timeout
|
mMySync.expectedState = STATE_WIFI_CHANGING;
|
||||||
&& mMySync.expectedState == STATE_WIFI_CHANGING)
|
long timeout = System.currentTimeMillis() + TIMEOUT_MSEC;
|
||||||
mMySync.wait(WAIT_MSEC);
|
int expectedState = (enable ? STATE_WIFI_ENABLED : STATE_WIFI_DISABLED);
|
||||||
|
while (System.currentTimeMillis() < timeout
|
||||||
|
&& mMySync.expectedState != expectedState)
|
||||||
|
mMySync.wait(WAIT_MSEC);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
private void startScan() throws Exception {
|
private void startScan() throws Exception {
|
||||||
synchronized (mMySync) {
|
synchronized (mMySync) {
|
||||||
mMySync.expectedState = STATE_SCANING;
|
mMySync.expectedState = STATE_SCANNING;
|
||||||
assertTrue(mWifiManager.startScan());
|
assertTrue(mWifiManager.startScan());
|
||||||
long timeout = System.currentTimeMillis() + TIMEOUT_MSEC;
|
long timeout = System.currentTimeMillis() + TIMEOUT_MSEC;
|
||||||
while (System.currentTimeMillis() < timeout && mMySync.expectedState == STATE_SCANING)
|
while (System.currentTimeMillis() < timeout && mMySync.expectedState == STATE_SCANNING)
|
||||||
mMySync.wait(WAIT_MSEC);
|
mMySync.wait(WAIT_MSEC);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user