FileDocCategorySizeDatePackage
WifiClientTest.javaAPI DocAndroid 5.1 API10583Thu Mar 12 22:22:12 GMT 2015com.android.connectivitymanagertest.unit

WifiClientTest

public class WifiClientTest extends android.test.AndroidTestCase
Test wifi client

Fields Summary
private android.net.wifi.WifiManager
mWifiManager
private static final int
DELAY
private WifiStateListener
mWifiStateListener
int
mWifiState
int
mDisableBroadcastCounter
int
mEnableBroadcastCounter
android.net.NetworkInfo
mNetworkInfo
boolean
mSupplicantConnection
android.net.wifi.SupplicantState
mSupplicantState
Constructors Summary
Methods Summary
protected voidsetUp()

        super.setUp();

        // register a connectivity receiver for CONNECTIVITY_ACTION;

        mWifiStateListener = new WifiStateListener();
        IntentFilter mIntentFilter = new IntentFilter();
        mIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION);
        mIntentFilter.addAction(WifiManager.SUPPLICANT_STATE_CHANGED_ACTION);
        mIntentFilter.addAction(WifiManager.SUPPLICANT_CONNECTION_CHANGE_ACTION);
        mIntentFilter.addAction(WifiManager.NETWORK_STATE_CHANGED_ACTION);
        getContext().registerReceiver(mWifiStateListener, mIntentFilter);

        mWifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
        mWifiManager.setWifiEnabled(true);
        assertNotNull(mWifiManager);
    
private voidsleepAfterWifiEnable()

        try {
            Thread.sleep(DELAY);
        } catch (Exception e) {
            fail("Sleep timeout " + e);
        }
    
public voidtestAddRemoveNetwork()

        WifiConfiguration config = new WifiConfiguration();
        config.SSID = "\"TestSSID1\"";
        config.allowedKeyManagement.set(KeyMgmt.NONE);

        //add
        int netId = mWifiManager.addNetwork(config);
        assertTrue(netId != -1);

        //check config list
        List<WifiConfiguration> configList = mWifiManager.getConfiguredNetworks();
        boolean found = false;
        for (WifiConfiguration c : configList) {
            if (c.networkId == netId && c.SSID.equals(config.SSID)) {
                found = true;
            }
        }
        assertTrue(found);

        //remove
        boolean ret = mWifiManager.removeNetwork(netId);
        assertTrue(ret);

        //check config list
        configList = mWifiManager.getConfiguredNetworks();
        found = false;
        for (WifiConfiguration c : configList) {
            if (c.networkId == netId) {
                found = true;
            }
        }

        assertFalse(found);
    
public voidtestEnableDisableNetwork()

        WifiConfiguration config = new WifiConfiguration();
        config.SSID = "\"TestSSID2\"";
        config.allowedKeyManagement.set(KeyMgmt.NONE);

        //add
        int netId = mWifiManager.addNetwork(config);
        assertTrue(netId != -1);

        //enable network and disable others
        boolean ret = mWifiManager.enableNetwork(netId, true);
        assertTrue(ret);

        //check config list
        List<WifiConfiguration> configList = mWifiManager.getConfiguredNetworks();
        for (WifiConfiguration c : configList) {
            if (c.networkId == netId) {
                assertTrue(c.status == Status.ENABLED);
            } else {
                assertFalse(c.status == Status.ENABLED);
            }
        }

        //disable network
        ret = mWifiManager.disableNetwork(netId);
        assertTrue(ret);

        //check config list
        configList = mWifiManager.getConfiguredNetworks();
        for (WifiConfiguration c : configList) {
            if (c.networkId == netId) {
                assertTrue(c.status == Status.DISABLED);
            }
        }
    
public voidtestPingSupplicant()

        assertTrue(mWifiManager.pingSupplicant());
        mWifiManager.setWifiEnabled(false);
        sleepAfterWifiEnable();

        assertFalse(mWifiManager.pingSupplicant());
        mWifiManager.setWifiEnabled(true);
        sleepAfterWifiEnable();
    
public voidtestSaveConfig()

        WifiConfiguration config = new WifiConfiguration();
        config.SSID = "\"TestSSID3\"";
        config.allowedKeyManagement.set(KeyMgmt.NONE);

        //add
        int netId = mWifiManager.addNetwork(config);
        assertTrue(netId != -1);

        mWifiManager.saveConfiguration();

        //restart wifi
        mWifiManager.setWifiEnabled(false);
        mWifiManager.setWifiEnabled(true);

        sleepAfterWifiEnable();

        //check config list
        List<WifiConfiguration> configList = mWifiManager.getConfiguredNetworks();
        boolean found = false;
        for (WifiConfiguration c : configList) {
            if (c.SSID.equals("TestSSID3")) {
                found = true;
            }
        }
        assertTrue(found);

        //restore config
        boolean ret = mWifiManager.removeNetwork(netId);
        assertTrue(ret);
        mWifiManager.saveConfiguration();
    
public voidtestWifiBroadcasts()


        /* Initialize */
        mWifiManager.setWifiEnabled(false);
        sleepAfterWifiEnable();
        mDisableBroadcastCounter = 0;
        mEnableBroadcastCounter = 0;
        mSupplicantConnection = false;
        mNetworkInfo = null;
        mSupplicantState = null;

        /* Enable wifi */
        mWifiManager.setWifiEnabled(true);
        sleepAfterWifiEnable();
        assertTrue(mEnableBroadcastCounter == 2);
        assertTrue(mSupplicantConnection == true);
        assertTrue(mNetworkInfo.isConnected());
        assertTrue(mSupplicantState == SupplicantState.COMPLETED);


        /* Disable wifi */
        mWifiManager.setWifiEnabled(false);
        sleepAfterWifiEnable();
        assertTrue(mDisableBroadcastCounter == 2);
        assertTrue(mSupplicantConnection == false);
        assertTrue(!mNetworkInfo.isConnected());
        assertTrue(mSupplicantState != SupplicantState.COMPLETED);

    
public voidtestWifiConfiguredNetworkStatus()


        /* Initialize */
        mWifiManager.setWifiEnabled(false);
        sleepAfterWifiEnable();

        /* Ensure no network is CURRENT */
        List<WifiConfiguration> configList = mWifiManager.getConfiguredNetworks();
        for (WifiConfiguration c : configList) {
            assertTrue(c.status != WifiConfiguration.Status.CURRENT);
        }

        /* Enable wifi */
        mWifiManager.setWifiEnabled(true);
        sleepAfterWifiEnable();

        /* Ensure connected network is CURRENT */
        String connectedSSID = mWifiManager.getConnectionInfo().getSSID();
        configList = mWifiManager.getConfiguredNetworks();
        for (WifiConfiguration c : configList) {
            if (c.SSID.contains(connectedSSID)) {
                assertTrue(c.status == WifiConfiguration.Status.CURRENT);
            } else {
                assertTrue(c.status != WifiConfiguration.Status.CURRENT);
            }
        }

        /* Disable wifi */
        mWifiManager.setWifiEnabled(false);
        sleepAfterWifiEnable();

        /* Ensure no network is CURRENT */
        configList = mWifiManager.getConfiguredNetworks();
        for (WifiConfiguration c : configList) {
            assertTrue(c.status != WifiConfiguration.Status.CURRENT);
        }