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.ConnectivityManagerTestBase; 20 import com.android.connectivitymanagertest.ConnectivityManagerTestRunner; 21 22 import android.content.Context; 23 import android.net.wifi.WifiConfiguration; 24 import android.net.wifi.WifiInfo; 25 import android.net.wifi.WifiManager; 26 import android.net.ConnectivityManager; 27 import android.net.NetworkInfo.State; 28 import android.test.suitebuilder.annotation.LargeTest; 29 import android.util.Log; 30 31 import java.util.ArrayList; 32 import java.util.List; 33 import java.util.Set; 34 35 /** 36 * Test Wi-Fi connection with different configuration 37 * To run this tests: 38 * adb shell am instrument -e class 39 * com.android.connectivitymanagertest.functional.WifiConnectionTest 40 * -w com.android.connectivitymanagertest/.ConnectivityManagerTestRunner 41 */ 42 public class WifiConnectionTest 43 extends ConnectivityManagerTestBase { 44 private static final String TAG = "WifiConnectionTest"; 45 private static final boolean DEBUG = false; 46 private List<WifiConfiguration> networks = new ArrayList<WifiConfiguration>(); 47 48 @Override 49 public void setUp() throws Exception { 50 super.setUp(); 51 networks = loadNetworkConfigurations(); 52 if (DEBUG) { 53 printNetworkConfigurations(); 54 } 55 56 // enable Wifi and verify wpa_supplicant is started 57 assertTrue("enable Wifi failed", enableWifi()); 58 sleep(2 * SHORT_TIMEOUT, "interrupted while waiting for WPA_SUPPLICANT to start"); 59 WifiInfo mConnection = mWifiManager.getConnectionInfo(); 60 assertNotNull(mConnection); 61 assertTrue("wpa_supplicant is not started ", mWifiManager.pingSupplicant()); 62 } 63 64 private void printNetworkConfigurations() { 65 log("==== print network configurations parsed from XML file ===="); 66 log("number of access points: " + networks.size()); 67 for (WifiConfiguration config : networks) { 68 log(config.toString()); 69 } 70 } 71 72 @Override 73 public void tearDown() throws Exception { 74 removeConfiguredNetworksAndDisableWifi(); 75 super.tearDown(); 76 } 77 78 /** 79 * Connect to the provided Wi-Fi network 80 * @param config is the network configuration 81 * @return true if the connection is successful. 82 */ 83 private void connectToWifi(WifiConfiguration config) { 84 // step 1: connect to the test access point 85 assertTrue("failed to connect to " + config.SSID, 86 connectToWifiWithConfiguration(config)); 87 88 // step 2: verify Wifi state and network state; 89 assertTrue(waitForNetworkState(ConnectivityManager.TYPE_WIFI, 90 State.CONNECTED, WIFI_CONNECTION_TIMEOUT)); 91 92 // step 3: verify the current connected network is the given SSID 93 assertNotNull("Wifi connection returns null", mWifiManager.getConnectionInfo()); 94 if (DEBUG) { 95 log("config.SSID = " + config.SSID); 96 log("mWifiManager.getConnectionInfo.getSSID()" + 97 mWifiManager.getConnectionInfo().getSSID()); 98 } 99 assertTrue(config.SSID.contains(mWifiManager.getConnectionInfo().getSSID())); 100 } 101 102 private void sleep(long sometime, String errorMsg) { 103 try { 104 Thread.sleep(sometime); 105 } catch (InterruptedException e) { 106 fail(errorMsg); 107 } 108 } 109 110 private void log(String message) { 111 Log.v(TAG, message); 112 } 113 114 @LargeTest 115 public void testWifiConnections() { 116 for (int i = 0; i < networks.size(); i++) { 117 String ssid = networks.get(i).SSID; 118 log("-- START Wi-Fi connection test to : " + ssid + " --"); 119 connectToWifi(networks.get(i)); 120 // wait for 2 minutes between wifi stop and start 121 sleep(WIFI_STOP_START_INTERVAL, "interruped while connected to wifi"); 122 log("-- END Wi-Fi connection test to " + ssid + " -- "); 123 } 124 } 125 } 126