Home | History | Annotate | Download | only in unit
      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.unit;
     18 
     19 import android.content.BroadcastReceiver;
     20 import android.content.Intent;
     21 import android.content.IntentFilter;
     22 import android.content.Context;
     23 import android.app.Instrumentation;
     24 import android.os.Handler;
     25 import android.os.Message;
     26 import android.net.NetworkInfo;
     27 import android.net.wifi.WifiManager;
     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.SupplicantState;
     32 
     33 import android.test.suitebuilder.annotation.LargeTest;
     34 import android.test.AndroidTestCase;
     35 
     36 import java.util.ArrayList;
     37 import java.util.List;
     38 
     39 import android.util.Log;
     40 
     41 /**
     42  * Test wifi client
     43  */
     44 public class WifiClientTest extends AndroidTestCase {
     45 
     46     private WifiManager mWifiManager;
     47     private final String TAG = "WifiClientTest";
     48 
     49     //10s delay for turning on wifi
     50     private static final int DELAY = 10000;
     51     private WifiStateListener mWifiStateListener;
     52     int mWifiState;
     53     int mDisableBroadcastCounter = 0;
     54     int mEnableBroadcastCounter = 0;
     55     NetworkInfo mNetworkInfo;
     56     boolean mSupplicantConnection;
     57     SupplicantState mSupplicantState;
     58 
     59     private class WifiStateListener extends BroadcastReceiver {
     60         @Override
     61         public void onReceive(Context context, Intent intent) {
     62             String action = intent.getAction();
     63             if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) {
     64                 mWifiState = intent.getIntExtra(WifiManager.EXTRA_WIFI_STATE,
     65                         WifiManager.WIFI_STATE_UNKNOWN);
     66                 switch (mWifiState) {
     67                     case WifiManager.WIFI_STATE_DISABLING:
     68                         if (mDisableBroadcastCounter == 0) mDisableBroadcastCounter++;
     69                         break;
     70                     case WifiManager.WIFI_STATE_DISABLED:
     71                         if (mDisableBroadcastCounter == 1) mDisableBroadcastCounter++;
     72                         break;
     73                     case WifiManager.WIFI_STATE_ENABLING:
     74                         if (mEnableBroadcastCounter == 0) mEnableBroadcastCounter++;
     75                         break;
     76                     case WifiManager.WIFI_STATE_ENABLED:
     77                         if (mEnableBroadcastCounter == 1) mEnableBroadcastCounter++;
     78                         break;
     79                 }
     80             } else if (action.equals(WifiManager.NETWORK_STATE_CHANGED_ACTION)) {
     81                 mNetworkInfo = (NetworkInfo)
     82                         intent.getParcelableExtra(WifiManager.EXTRA_NETWORK_INFO);
     83             } else if (action.equals(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION)) {
     84                 mSupplicantState = (SupplicantState)
     85                         intent.getParcelableExtra(WifiManager.EXTRA_NEW_STATE);
     86             } else if (action.equals(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION)) {
     87                 mSupplicantConnection =
     88                         intent.getBooleanExtra(WifiManager.EXTRA_SUPPLICANT_CONNECTED, false);
     89             }
     90         }
     91     }
     92 
     93     @Override
     94     protected void setUp() throws Exception {
     95         super.setUp();
     96 
     97         // register a connectivity receiver for CONNECTIVITY_ACTION;
     98 
     99         mWifiStateListener = new WifiStateListener();
    100         IntentFilter mIntentFilter = new IntentFilter();
    101         mIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
    102         mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
    103         mIntentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
    104         mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
    105         getContext().registerReceiver(mWifiStateListener, mIntentFilter);
    106 
    107         mWifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
    108         mWifiManager.setWifiEnabled(true);
    109         assertNotNull(mWifiManager);
    110     }
    111 
    112     private void sleepAfterWifiEnable() {
    113         try {
    114             Thread.sleep(DELAY);
    115         } catch (Exception e) {
    116             fail("Sleep timeout " + e);
    117         }
    118     }
    119 
    120     // Test case 1: add/remove a open network
    121     @LargeTest
    122     public void testAddRemoveNetwork() {
    123         WifiConfiguration config = new WifiConfiguration();
    124         config.SSID = "\"TestSSID1\"";
    125         config.allowedKeyManagement.set(KeyMgmt.NONE);
    126 
    127         //add
    128         int netId = mWifiManager.addNetwork(config);
    129         assertTrue(netId != -1);
    130 
    131         //check config list
    132         List<WifiConfiguration> configList = mWifiManager.getConfiguredNetworks();
    133         boolean found = false;
    134         for (WifiConfiguration c : configList) {
    135             if (c.networkId == netId && c.SSID.equals(config.SSID)) {
    136                 found = true;
    137             }
    138         }
    139         assertTrue(found);
    140 
    141         //remove
    142         boolean ret = mWifiManager.removeNetwork(netId);
    143         assertTrue(ret);
    144 
    145         //check config list
    146         configList = mWifiManager.getConfiguredNetworks();
    147         found = false;
    148         for (WifiConfiguration c : configList) {
    149             if (c.networkId == netId) {
    150                 found = true;
    151             }
    152         }
    153 
    154         assertFalse(found);
    155     }
    156 
    157     // Test case 2: enable/disable a open network
    158     @LargeTest
    159     public void testEnableDisableNetwork() {
    160         WifiConfiguration config = new WifiConfiguration();
    161         config.SSID = "\"TestSSID2\"";
    162         config.allowedKeyManagement.set(KeyMgmt.NONE);
    163 
    164         //add
    165         int netId = mWifiManager.addNetwork(config);
    166         assertTrue(netId != -1);
    167 
    168         //enable network and disable others
    169         boolean ret = mWifiManager.enableNetwork(netId, true);
    170         assertTrue(ret);
    171 
    172         //check config list
    173         List<WifiConfiguration> configList = mWifiManager.getConfiguredNetworks();
    174         for (WifiConfiguration c : configList) {
    175             if (c.networkId == netId) {
    176                 assertTrue(c.status == Status.ENABLED);
    177             } else {
    178                 assertFalse(c.status == Status.ENABLED);
    179             }
    180         }
    181 
    182         //disable network
    183         ret = mWifiManager.disableNetwork(netId);
    184         assertTrue(ret);
    185 
    186         //check config list
    187         configList = mWifiManager.getConfiguredNetworks();
    188         for (WifiConfiguration c : configList) {
    189             if (c.networkId == netId) {
    190                 assertTrue(c.status == Status.DISABLED);
    191             }
    192         }
    193     }
    194 
    195     // Test case 3: ping supplicant
    196     @LargeTest
    197     public void testPingSupplicant() {
    198         assertTrue(mWifiManager.pingSupplicant());
    199         mWifiManager.setWifiEnabled(false);
    200         sleepAfterWifiEnable();
    201 
    202         assertFalse(mWifiManager.pingSupplicant());
    203         mWifiManager.setWifiEnabled(true);
    204         sleepAfterWifiEnable();
    205     }
    206 
    207     // Test case 4: save config
    208     @LargeTest
    209     public void testSaveConfig() {
    210         WifiConfiguration config = new WifiConfiguration();
    211         config.SSID = "\"TestSSID3\"";
    212         config.allowedKeyManagement.set(KeyMgmt.NONE);
    213 
    214         //add
    215         int netId = mWifiManager.addNetwork(config);
    216         assertTrue(netId != -1);
    217 
    218         mWifiManager.saveConfiguration();
    219 
    220         //restart wifi
    221         mWifiManager.setWifiEnabled(false);
    222         mWifiManager.setWifiEnabled(true);
    223 
    224         sleepAfterWifiEnable();
    225 
    226         //check config list
    227         List<WifiConfiguration> configList = mWifiManager.getConfiguredNetworks();
    228         boolean found = false;
    229         for (WifiConfiguration c : configList) {
    230             if (c.SSID.equals("TestSSID3")) {
    231                 found = true;
    232             }
    233         }
    234         assertTrue(found);
    235 
    236         //restore config
    237         boolean ret = mWifiManager.removeNetwork(netId);
    238         assertTrue(ret);
    239         mWifiManager.saveConfiguration();
    240     }
    241 
    242     // Test case 5: test wifi state change broadcasts
    243     @LargeTest
    244     public void testWifiBroadcasts() {
    245 
    246         /* Initialize */
    247         mWifiManager.setWifiEnabled(false);
    248         sleepAfterWifiEnable();
    249         mDisableBroadcastCounter = 0;
    250         mEnableBroadcastCounter = 0;
    251         mSupplicantConnection = false;
    252         mNetworkInfo = null;
    253         mSupplicantState = null;
    254 
    255         /* Enable wifi */
    256         mWifiManager.setWifiEnabled(true);
    257         sleepAfterWifiEnable();
    258         assertTrue(mEnableBroadcastCounter == 2);
    259         assertTrue(mSupplicantConnection == true);
    260         assertTrue(mNetworkInfo.isConnected());
    261         assertTrue(mSupplicantState == SupplicantState.COMPLETED);
    262 
    263 
    264         /* Disable wifi */
    265         mWifiManager.setWifiEnabled(false);
    266         sleepAfterWifiEnable();
    267         assertTrue(mDisableBroadcastCounter == 2);
    268         assertTrue(mSupplicantConnection == false);
    269         assertTrue(!mNetworkInfo.isConnected());
    270         assertTrue(mSupplicantState != SupplicantState.COMPLETED);
    271 
    272     }
    273 
    274     // Test case 6: test configured network status
    275     @LargeTest
    276     public void testWifiConfiguredNetworkStatus() {
    277 
    278         /* Initialize */
    279         mWifiManager.setWifiEnabled(false);
    280         sleepAfterWifiEnable();
    281 
    282         /* Ensure no network is CURRENT */
    283         List<WifiConfiguration> configList = mWifiManager.getConfiguredNetworks();
    284         for (WifiConfiguration c : configList) {
    285             assertTrue(c.status != WifiConfiguration.Status.CURRENT);
    286         }
    287 
    288         /* Enable wifi */
    289         mWifiManager.setWifiEnabled(true);
    290         sleepAfterWifiEnable();
    291 
    292         /* Ensure connected network is CURRENT */
    293         String connectedSSID = mWifiManager.getConnectionInfo().getSSID();
    294         configList = mWifiManager.getConfiguredNetworks();
    295         for (WifiConfiguration c : configList) {
    296             if (c.SSID.contains(connectedSSID)) {
    297                 assertTrue(c.status == WifiConfiguration.Status.CURRENT);
    298             } else {
    299                 assertTrue(c.status != WifiConfiguration.Status.CURRENT);
    300             }
    301         }
    302 
    303         /* Disable wifi */
    304         mWifiManager.setWifiEnabled(false);
    305         sleepAfterWifiEnable();
    306 
    307         /* Ensure no network is CURRENT */
    308         configList = mWifiManager.getConfiguredNetworks();
    309         for (WifiConfiguration c : configList) {
    310             assertTrue(c.status != WifiConfiguration.Status.CURRENT);
    311         }
    312     }
    313 
    314 
    315 
    316 }
    317