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 17package com.android.connectivitymanagertest.functional; 18 19import com.android.connectivitymanagertest.ConnectivityManagerTestActivity; 20import com.android.connectivitymanagertest.ConnectivityManagerTestRunner; 21 22import android.R; 23import android.app.Activity; 24import android.content.ContentResolver; 25import android.content.Intent; 26import android.content.Context; 27import android.content.res.Resources; 28import android.net.wifi.WifiConfiguration; 29import android.net.wifi.WifiConfiguration.KeyMgmt; 30import android.net.wifi.WifiConfiguration.Status; 31import android.net.wifi.WifiInfo; 32import android.net.wifi.WifiManager; 33import android.net.ConnectivityManager; 34import android.net.DhcpInfo; 35import android.net.NetworkInfo; 36import android.net.NetworkInfo.State; 37import android.os.Handler; 38import android.os.Message; 39import android.provider.Settings; 40import android.test.suitebuilder.annotation.LargeTest; 41import android.test.ActivityInstrumentationTestCase2; 42import android.util.Log; 43 44import com.android.internal.util.AsyncChannel; 45 46import java.util.ArrayList; 47import java.util.HashSet; 48import java.util.List; 49import 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 */ 58public 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 Set<WifiConfiguration> enabledNetworks = null; 67 68 public WifiConnectionTest() { 69 super(ConnectivityManagerTestActivity.class); 70 } 71 72 @Override 73 public void setUp() throws Exception { 74 super.setUp(); 75 mRunner = ((ConnectivityManagerTestRunner)getInstrumentation()); 76 mWifiManager = (WifiManager) mRunner.getContext().getSystemService(Context.WIFI_SERVICE); 77 78 mAct = getActivity(); 79 mWifiManager.asyncConnect(mAct, new WifiServiceHandler()); 80 networks = mAct.loadNetworkConfigurations(); 81 if (DEBUG) { 82 printNetworkConfigurations(); 83 } 84 85 // enable Wifi and verify wpa_supplicant is started 86 assertTrue("enable Wifi failed", mAct.enableWifi()); 87 sleep(2 * ConnectivityManagerTestActivity.SHORT_TIMEOUT, 88 "interrupted while waiting for WPA_SUPPLICANT to start"); 89 WifiInfo mConnection = mAct.mWifiManager.getConnectionInfo(); 90 assertNotNull(mConnection); 91 assertTrue("wpa_supplicant is not started ", mAct.mWifiManager.pingSupplicant()); 92 } 93 94 private class WifiServiceHandler extends Handler { 95 @Override 96 public void handleMessage(Message msg) { 97 switch (msg.what) { 98 case AsyncChannel.CMD_CHANNEL_HALF_CONNECTED: 99 if (msg.arg1 == AsyncChannel.STATUS_SUCCESSFUL) { 100 //AsyncChannel in msg.obj 101 } else { 102 log("Failed to establish AsyncChannel connection"); 103 } 104 break; 105 default: 106 //Ignore 107 break; 108 } 109 } 110 } 111 112 private void printNetworkConfigurations() { 113 log("==== print network configurations parsed from XML file ===="); 114 log("number of access points: " + networks.size()); 115 for (WifiConfiguration config : networks) { 116 log(config.toString()); 117 } 118 } 119 120 @Override 121 public void tearDown() throws Exception { 122 log("tearDown()"); 123 mAct.removeConfiguredNetworksAndDisableWifi(); 124 super.tearDown(); 125 } 126 127 /** 128 * Connect to the provided Wi-Fi network 129 * @param config is the network configuration 130 * @return true if the connection is successful. 131 */ 132 private void connectToWifi(WifiConfiguration config) { 133 // step 1: connect to the test access point 134 assertTrue("failed to connect to " + config.SSID, 135 mAct.connectToWifiWithConfiguration(config)); 136 137 // step 2: verify Wifi state and network state; 138 assertTrue(mAct.waitForNetworkState(ConnectivityManager.TYPE_WIFI, 139 State.CONNECTED, 6 * ConnectivityManagerTestActivity.LONG_TIMEOUT)); 140 141 // step 3: verify the current connected network is the given SSID 142 assertNotNull("Wifi connection returns null", mAct.mWifiManager.getConnectionInfo()); 143 if (DEBUG) { 144 log("config.SSID = " + config.SSID); 145 log("mAct.mWifiManager.getConnectionInfo.getSSID()" + 146 mAct.mWifiManager.getConnectionInfo().getSSID()); 147 } 148 assertTrue(config.SSID.contains(mAct.mWifiManager.getConnectionInfo().getSSID())); 149 } 150 151 private void sleep(long sometime, String errorMsg) { 152 try { 153 Thread.sleep(sometime); 154 } catch (InterruptedException e) { 155 fail(errorMsg); 156 } 157 } 158 159 private void log(String message) { 160 Log.v(TAG, message); 161 } 162 163 @LargeTest 164 public void testWifiConnections() { 165 for (int i = 0; i < networks.size(); i++) { 166 String ssid = networks.get(i).SSID; 167 log("-- START Wi-Fi connection test to : " + ssid + " --"); 168 connectToWifi(networks.get(i)); 169 // wait for 2 minutes between wifi stop and start 170 sleep(ConnectivityManagerTestActivity.WIFI_STOP_START_INTERVAL, 171 "interruped while connected to wifi"); 172 log("-- END Wi-Fi connection test to " + ssid + " -- "); 173 } 174 } 175} 176