Skip to content

Commit 3591beb

Browse files
Xia WangAndroid (Google) Code Review
authored andcommitted
Merge "Restore Wi-Fi configuration after the test. DO NOT MERGE" into gingerbread
2 parents d4c5478 + fddbe98 commit 3591beb

File tree

3 files changed

+100
-43
lines changed

3 files changed

+100
-43
lines changed

core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/AccessPointParserHelper.java

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@
3434
import java.util.HashMap;
3535
import java.util.List;
3636

37-
3837
/**
3938
* Help class to process configurations of access points saved in an XML file.
4039
* The configurations of an access point is included in tag
@@ -55,6 +54,9 @@
5554
* <password>abcdefgh</password>
5655
* </accesspoint>
5756
* </resources>
57+
*
58+
* Note:ssid and security have to be the first two tags
59+
* for static ip setting, tag "ip" should be listed before other fields: dns, gateway, netmask.
5860
*/
5961
public class AccessPointParserHelper {
6062
private static final String KEYSTORE_SPACE = "keystore://";
@@ -186,6 +188,7 @@ public void endElement(String uri, String localName, String tagName) throws SAXE
186188
networks.add(config);
187189
if (mDhcpInfo != null) {
188190
ssidToDhcpInfoHM.put(config.SSID, mDhcpInfo);
191+
mDhcpInfo = null;
189192
}
190193
}
191194
}
@@ -214,6 +217,13 @@ public void characters(char ch[], int start, int length) throws SAXException {
214217
case EAP:
215218
config.allowedKeyManagement.set(KeyMgmt.WPA_EAP);
216219
config.allowedKeyManagement.set(KeyMgmt.IEEE8021X);
220+
// Initialize other fields.
221+
config.phase2.setValue("");
222+
config.ca_cert.setValue("");
223+
config.client_cert.setValue("");
224+
config.private_key.setValue("");
225+
config.identity.setValue("");
226+
config.anonymous_identity.setValue("");
217227
break;
218228
default:
219229
throw new SAXException();

core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/ConnectivityManagerTestActivity.java

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -545,11 +545,6 @@ public boolean connectToWifiWithConfiguration(WifiConfiguration config) {
545545
// Connect to network by disabling others.
546546
mWifiManager.enableNetwork(networkId, true);
547547
mWifiManager.saveConfiguration();
548-
List<WifiConfiguration> wifiNetworks = mWifiManager.getConfiguredNetworks();
549-
for (WifiConfiguration netConfig : wifiNetworks) {
550-
Log.v(LOG_TAG, netConfig.toString());
551-
}
552-
553548
mWifiManager.reconnect();
554549
break;
555550
}
@@ -688,3 +683,4 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
688683
return super.onKeyDown(keyCode, event);
689684
}
690685
}
686+

core/tests/ConnectivityManagerTest/src/com/android/connectivitymanagertest/functional/WifiConnectionTest.java

Lines changed: 88 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@
2828
import android.content.Context;
2929
import android.content.res.Resources;
3030
import android.net.wifi.WifiConfiguration;
31+
import android.net.wifi.WifiConfiguration.Status;
3132
import android.net.wifi.WifiInfo;
3233
import android.net.wifi.WifiManager;
3334
import android.net.ConnectivityManager;
@@ -42,6 +43,7 @@
4243

4344
import java.util.ArrayList;
4445
import java.util.HashMap;
46+
import java.util.HashSet;
4547
import java.util.List;
4648
import java.util.Map.Entry;
4749
import java.util.Set;
@@ -56,13 +58,14 @@
5658
public class WifiConnectionTest
5759
extends ActivityInstrumentationTestCase2<ConnectivityManagerTestActivity> {
5860
private static final String TAG = "WifiConnectionTest";
59-
private static final boolean DEBUG = true;
60-
private static final String PKG_NAME = "com.android.connectivitymanagertests";
61+
private static final boolean DEBUG = false;
6162
private List<WifiConfiguration> networks = new ArrayList<WifiConfiguration>();
6263
private ConnectivityManagerTestActivity mAct;
6364
private HashMap<String, DhcpInfo> hm = null;
6465
private ConnectivityManagerTestRunner mRunner;
6566
private ContentResolver cr;
67+
private Set<WifiConfiguration> enabledNetworks = null;
68+
private WifiManager mWifiManager = null;
6669

6770
public WifiConnectionTest() {
6871
super(ConnectivityManagerTestActivity.class);
@@ -71,8 +74,12 @@ public WifiConnectionTest() {
7174
@Override
7275
public void setUp() throws Exception {
7376
super.setUp();
74-
mAct = getActivity();
77+
log("before we launch the test activity, we preserve all the configured networks.");
7578
mRunner = ((ConnectivityManagerTestRunner)getInstrumentation());
79+
mWifiManager = (WifiManager) mRunner.getContext().getSystemService(Context.WIFI_SERVICE);
80+
enabledNetworks = getEnabledNetworks(mWifiManager.getConfiguredNetworks());
81+
82+
mAct = getActivity();
7683
cr = mRunner.getContext().getContentResolver();
7784
networks = mAct.loadNetworkConfigurations();
7885
hm = mAct.getDhcpInfo();
@@ -83,21 +90,18 @@ public void setUp() throws Exception {
8390

8491
// enable Wifi and verify wpa_supplicant is started
8592
assertTrue("enable Wifi failed", mAct.enableWifi());
86-
try {
87-
Thread.sleep( 2 * ConnectivityManagerTestActivity.SHORT_TIMEOUT);
88-
} catch (Exception e) {
89-
fail("interrupted while waiting for WPA_SUPPLICANT to start");
90-
}
93+
sleep(2 * ConnectivityManagerTestActivity.SHORT_TIMEOUT,
94+
"interrupted while waiting for WPA_SUPPLICANT to start");
9195
WifiInfo mConnection = mAct.mWifiManager.getConnectionInfo();
9296
assertNotNull(mConnection);
9397
assertTrue("wpa_supplicant is not started ", mAct.mWifiManager.pingSupplicant());
9498
}
9599

96100
private void printNetworkConfigurations() {
97-
Log.v(TAG, "==== print network configurations parsed from XML file ====");
98-
Log.v(TAG, "number of access points: " + networks.size());
101+
log("==== print network configurations parsed from XML file ====");
102+
log("number of access points: " + networks.size());
99103
for (WifiConfiguration config : networks) {
100-
Log.v(TAG, config.toString());
104+
log(config.toString());
101105
}
102106
}
103107

@@ -107,30 +111,82 @@ private void printDhcpInfo() {
107111
} else {
108112
Set<Entry<String, DhcpInfo>> set = hm.entrySet();
109113
for (Entry<String, DhcpInfo> me: set) {
110-
Log.v(TAG, "SSID: " + me.getKey());
114+
log("SSID: " + me.getKey());
111115
DhcpInfo dhcp = me.getValue();
112-
Log.v(TAG, " dhcp: " + dhcp.toString());
113-
Log.v(TAG, "IP: " + intToIpString(dhcp.ipAddress));
114-
Log.v(TAG, "gateway: " + intToIpString(dhcp.gateway));
115-
Log.v(TAG, "Netmask: " + intToIpString(dhcp.netmask));
116-
Log.v(TAG, "DNS1: " + intToIpString(dhcp.dns1));
117-
Log.v(TAG, "DNS2: " + intToIpString(dhcp.dns2));
116+
log("IP: " + intToIpString(dhcp.ipAddress));
117+
log("gateway: " + intToIpString(dhcp.gateway));
118+
log("Netmask: " + intToIpString(dhcp.netmask));
119+
log("DNS1: " + intToIpString(dhcp.dns1));
120+
log("DNS2: " + intToIpString(dhcp.dns2));
118121
}
119122
}
120123
}
121124

122125
@Override
123126
public void tearDown() throws Exception {
127+
log("tear down");
124128
mAct.removeConfiguredNetworksAndDisableWifi();
129+
reEnableNetworks(enabledNetworks);
130+
mWifiManager.saveConfiguration();
125131
super.tearDown();
126132
}
127133

134+
private Set<WifiConfiguration> getEnabledNetworks(List<WifiConfiguration> configuredNetworks) {
135+
Set<WifiConfiguration> networks = new HashSet<WifiConfiguration>();
136+
for (WifiConfiguration wifiConfig : configuredNetworks) {
137+
if (wifiConfig.status == Status.ENABLED || wifiConfig.status == Status.CURRENT) {
138+
networks.add(wifiConfig);
139+
log("remembering enabled network " + wifiConfig.SSID +
140+
" status is " + wifiConfig.status);
141+
}
142+
}
143+
return networks;
144+
}
145+
146+
private void reEnableNetworks(Set<WifiConfiguration> enabledWifiConfig) {
147+
if (!mWifiManager.isWifiEnabled()) {
148+
log("reEnableNetworks: enable Wifi");
149+
mWifiManager.setWifiEnabled(true);
150+
sleep(ConnectivityManagerTestActivity.SHORT_TIMEOUT,
151+
"interruped while waiting for wifi to be enabled");
152+
}
153+
for (WifiConfiguration wifiConfig : enabledWifiConfig) {
154+
log("recover wifi configuration: " + wifiConfig.toString());
155+
int netId = mWifiManager.addNetwork(wifiConfig);
156+
if (wifiConfig.status == Status.CURRENT) {
157+
mWifiManager.enableNetwork(netId, true);
158+
mWifiManager.reconnect();
159+
sleep(ConnectivityManagerTestActivity.SHORT_TIMEOUT,
160+
String.format("interruped while connecting to %s", wifiConfig.SSID));
161+
log("re-connecting to network " + wifiConfig.SSID);
162+
}
163+
}
164+
List<WifiConfiguration> wifiConfigurations = mWifiManager.getConfiguredNetworks();
165+
for (WifiConfiguration wifiConfig: wifiConfigurations) {
166+
if (wifiConfig.status == Status.DISABLED) {
167+
mWifiManager.enableNetwork(wifiConfig.networkId, false);
168+
}
169+
}
170+
}
171+
128172
private String intToIpString(int i) {
129173
return ((i & 0xFF) + "." +
130174
((i >> 8) & 0xFF) + "." +
131175
((i >> 16) & 0xFF) + "." +
132176
((i >> 24) & 0xFF));
133177
}
178+
179+
private void sleep(long sometime, String errorMsg) {
180+
try {
181+
Thread.sleep(sometime);
182+
} catch (InterruptedException e) {
183+
fail(errorMsg);
184+
}
185+
}
186+
187+
private void log(String message) {
188+
Log.v(TAG, message);
189+
}
134190
/**
135191
* Connect to the provided Wi-Fi network
136192
* @param config is the network configuration
@@ -164,42 +220,37 @@ private void connectToWifi(WifiConfiguration config) {
164220
// step 2: verify Wifi state and network state;
165221
assertTrue(mAct.waitForWifiState(WifiManager.WIFI_STATE_ENABLED,
166222
ConnectivityManagerTestActivity.SHORT_TIMEOUT));
223+
// 802.1x requires long time for connection.
167224
assertTrue(mAct.waitForNetworkState(ConnectivityManager.TYPE_WIFI,
168-
State.CONNECTED, ConnectivityManagerTestActivity.LONG_TIMEOUT));
225+
State.CONNECTED, 2 * ConnectivityManagerTestActivity.LONG_TIMEOUT));
169226

170227
// step 3: verify the current connected network is the given SSID
228+
assertNotNull("Wifi connection returns null", mAct.mWifiManager.getConnectionInfo());
171229
if (DEBUG) {
172-
Log.v(TAG, "config.SSID = " + config.SSID);
173-
Log.v(TAG, "mAct.mWifiManager.getConnectionInfo.getSSID()" +
230+
log("config.SSID = " + config.SSID);
231+
log("mAct.mWifiManager.getConnectionInfo.getSSID()" +
174232
mAct.mWifiManager.getConnectionInfo().getSSID());
175233
}
176234
assertTrue(config.SSID.contains(mAct.mWifiManager.getConnectionInfo().getSSID()));
177-
178-
// Maintain the connection for 50 seconds before switching
179-
try {
180-
Thread.sleep(mAct.LONG_TIMEOUT);
181-
} catch (Exception e) {
182-
fail("interrupted while waiting for WPA_SUPPLICANT to start");
183-
}
184-
185235
if (isStaticIP) {
186236
Settings.System.putInt(cr, Settings.System.WIFI_USE_STATIC_IP, 0);
237+
Settings.System.putString(cr, Settings.System.WIFI_STATIC_IP, "");
238+
Settings.System.putString(cr, Settings.System.WIFI_STATIC_GATEWAY, "");
239+
Settings.System.putString(cr, Settings.System.WIFI_STATIC_NETMASK, "");
240+
Settings.System.putString(cr, Settings.System.WIFI_STATIC_DNS1, "");
241+
Settings.System.putString(cr, Settings.System.WIFI_STATIC_DNS2, "");
187242
}
188243
}
189244

190245
@LargeTest
191246
public void testWifiConnections() {
192247
for (int i = 0; i < networks.size(); i++) {
193248
String ssid = networks.get(i).SSID;
194-
Log.v(TAG, "-- start Wi-Fi connection test for SSID: " + ssid + " --");
249+
log("-- START Wi-Fi connection test for SSID: " + ssid + " --");
195250
connectToWifi(networks.get(i));
196-
mAct.removeConfiguredNetworksAndDisableWifi();
197-
try {
198-
Thread.sleep(4 * mAct.SHORT_TIMEOUT);
199-
} catch (Exception e) {
200-
fail("Interrupted while disabling wifi");
201-
}
202-
Log.v(TAG, "-- END Wi-Fi connection test for SSID: " + ssid + " --");
251+
sleep(2 * ConnectivityManagerTestActivity.SHORT_TIMEOUT,
252+
String.format("Interrupted while connecting to ", ssid));
253+
log("-- END Wi-Fi connection test for SSID: " + ssid + " --");
203254
}
204255
}
205256
}

0 commit comments

Comments
 (0)