1 /* 2 * Copyright (C) 2010, The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.connectivitymanagertest.functional; 18 19 import com.android.connectivitymanagertest.ConnectivityManagerTestActivity; 20 import com.android.connectivitymanagertest.ConnectivityManagerTestRunner; 21 22 import android.R; 23 import android.app.Activity; 24 import android.content.ContentResolver; 25 import android.content.Intent; 26 import android.content.Context; 27 import android.content.res.Resources; 28 import android.net.wifi.WifiConfiguration; 29 import android.net.wifi.WifiConfiguration.KeyMgmt; 30 import android.net.wifi.WifiConfiguration.Status; 31 import android.net.wifi.WifiInfo; 32 import android.net.wifi.WifiManager; 33 import android.net.ConnectivityManager; 34 import android.net.DhcpInfo; 35 import android.net.NetworkInfo; 36 import android.net.NetworkInfo.State; 37 import android.os.Handler; 38 import android.os.Message; 39 import android.provider.Settings; 40 import android.test.suitebuilder.annotation.LargeTest; 41 import android.test.ActivityInstrumentationTestCase2; 42 import android.util.Log; 43 44 import com.android.internal.util.AsyncChannel; 45 46 import java.util.ArrayList; 47 import java.util.HashSet; 48 import java.util.List; 49 import java.util.Set; 50 51 /** 52 * Test Wi-Fi connection with different configuration 53 * To run this tests: 54 * adb shell am instrument -e class 55 * com.android.connectivitymanagertest.functional.WifiConnectionTest 56 * -w com.android.connectivitymanagertest/.ConnectivityManagerTestRunner 57 */ 58 public class WifiConnectionTest 59 extends ActivityInstrumentationTestCase2<ConnectivityManagerTestActivity> { 60 private static final String TAG = "WifiConnectionTest"; 61 private static final boolean DEBUG = false; 62 private List<WifiConfiguration> networks = new ArrayList<WifiConfiguration>(); 63 private ConnectivityManagerTestActivity mAct; 64 private ConnectivityManagerTestRunner mRunner; 65 private WifiManager mWifiManager = null; 66 private WifiManager.Channel mChannel; 67 private Set<WifiConfiguration> enabledNetworks = null; 68 69 public WifiConnectionTest() { 70 super(ConnectivityManagerTestActivity.class); 71 } 72 73 @Override 74 public void setUp() throws Exception { 75 super.setUp(); 76 mRunner = ((ConnectivityManagerTestRunner)getInstrumentation()); 77 mWifiManager = (WifiManager) mRunner.getContext().getSystemService(Context.WIFI_SERVICE); 78 79 mAct = getActivity(); 80 mChannel = mWifiManager.initialize(mAct, mAct.getMainLooper(), null); 81 82 networks = mAct.loadNetworkConfigurations(); 83 if (DEBUG) { 84 printNetworkConfigurations(); 85 } 86 87 // enable Wifi and verify wpa_supplicant is started 88 assertTrue("enable Wifi failed", mAct.enableWifi()); 89 sleep(2 * ConnectivityManagerTestActivity.SHORT_TIMEOUT, 90 "interrupted while waiting for WPA_SUPPLICANT to start"); 91 WifiInfo mConnection = mAct.mWifiManager.getConnectionInfo(); 92 assertNotNull(mConnection); 93 assertTrue("wpa_supplicant is not started ", mAct.mWifiManager.pingSupplicant()); 94 } 95 96 private class WifiServiceHandler extends Handler { 97 @Override 98 public void handleMessage(Message msg) { 99 switch (msg.what) { 100 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: 101 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { 102 //AsyncChannel in msg.obj 103 } else { 104 log("Failed to establish AsyncChannel connection"); 105 } 106 break; 107 default: 108 //Ignore 109 break; 110 } 111 } 112 } 113 114 private void printNetworkConfigurations() { 115 log("==== print network configurations parsed from XML file ===="); 116 log("number of access points: " + networks.size()); 117 for (WifiConfiguration config : networks) { 118 log(config.toString()); 119 } 120 } 121 122 @Override 123 public void tearDown() throws Exception { 124 log("tearDown()"); 125 mAct.removeConfiguredNetworksAndDisableWifi(); 126 super.tearDown(); 127 } 128 129 /** 130 * Connect to the provided Wi-Fi network 131 * @param config is the network configuration 132 * @return true if the connection is successful. 133 */ 134 private void connectToWifi(WifiConfiguration config) { 135 // step 1: connect to the test access point 136 assertTrue("failed to connect to " + config.SSID, 137 mAct.connectToWifiWithConfiguration(config)); 138 139 // step 2: verify Wifi state and network state; 140 assertTrue(mAct.waitForNetworkState(ConnectivityManager.TYPE_WIFI, 141 State.CONNECTED, 6 * ConnectivityManagerTestActivity.LONG_TIMEOUT)); 142 143 // step 3: verify the current connected network is the given SSID 144 assertNotNull("Wifi connection returns null", mAct.mWifiManager.getConnectionInfo()); 145 if (DEBUG) { 146 log("config.SSID = " + config.SSID); 147 log("mAct.mWifiManager.getConnectionInfo.getSSID()" + 148 mAct.mWifiManager.getConnectionInfo().getSSID()); 149 } 150 assertTrue(config.SSID.contains(mAct.mWifiManager.getConnectionInfo().getSSID())); 151 } 152 153 private void sleep(long sometime, String errorMsg) { 154 try { 155 Thread.sleep(sometime); 156 } catch (InterruptedException e) { 157 fail(errorMsg); 158 } 159 } 160 161 private void log(String message) { 162 Log.v(TAG, message); 163 } 164 165 @LargeTest 166 public void testWifiConnections() { 167 for (int i = 0; i < networks.size(); i++) { 168 String ssid = networks.get(i).SSID; 169 log("-- START Wi-Fi connection test to : " + ssid + " --"); 170 connectToWifi(networks.get(i)); 171 // wait for 2 minutes between wifi stop and start 172 sleep(ConnectivityManagerTestActivity.WIFI_STOP_START_INTERVAL, 173 "interruped while connected to wifi"); 174 log("-- END Wi-Fi connection test to " + ssid + " -- "); 175 } 176 } 177 } 178