Code example for WifiManager

Methods: setWifiEnabled, sleep

0
        assertNotNull(mWifiManager);
    } 
 
    private void sleepAfterWifiEnable() { 
        try { 
            Thread.sleep(DELAY);
        } catch (Exception e) {
            fail("Sleep timeout " + e);
        } 
    } 
 
    // Test case 1: add/remove a open network 
    @LargeTest 
    public void testAddRemoveNetwork() { 
        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);
    } 
 
    // Test case 2: enable/disable a open network 
    @LargeTest 
    public void testEnableDisableNetwork() { 
        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);
            } 
        } 
    } 
 
    // Test case 3: ping supplicant 
    @LargeTest 
    public void testPingSupplicant() { 
        assertTrue(mWifiManager.pingSupplicant());
        mWifiManager.setWifiEnabled(false);
        sleepAfterWifiEnable(); 
 
        assertFalse(mWifiManager.pingSupplicant());
        mWifiManager.setWifiEnabled(true);
        sleepAfterWifiEnable(); 
    } 
 
    // Test case 4: save config 
    @LargeTest 
    public void testSaveConfig() { 
        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();
    } 
 
    // Test case 5: test wifi state change broadcasts 
    @LargeTest 
    public void testWifiBroadcasts() { 
 
        /* 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);