2828import android .content .Context ;
2929import android .content .res .Resources ;
3030import android .net .wifi .WifiConfiguration ;
31+ import android .net .wifi .WifiConfiguration .Status ;
3132import android .net .wifi .WifiInfo ;
3233import android .net .wifi .WifiManager ;
3334import android .net .ConnectivityManager ;
4243
4344import java .util .ArrayList ;
4445import java .util .HashMap ;
46+ import java .util .HashSet ;
4547import java .util .List ;
4648import java .util .Map .Entry ;
4749import java .util .Set ;
5658public 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