Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit acc863ca authored by Irfan Sheriff's avatar Irfan Sheriff Committed by Android (Google) Code Review
Browse files

Merge "Use AsynChannel for synchronous API"

parents c68134b7 1406bcb7
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
@@ -20,6 +20,7 @@ import android.os.Bundle;
import android.test.InstrumentationTestRunner;
import android.test.InstrumentationTestSuite;
import android.util.Log;
import com.android.connectivitymanagertest.unit.WifiClientTest;
import com.android.connectivitymanagertest.unit.WifiSoftAPTest;

import junit.framework.TestSuite;
@@ -35,6 +36,7 @@ public class ConnectivityManagerUnitTestRunner extends InstrumentationTestRunner
    @Override
    public TestSuite getAllTests() {
        TestSuite suite = new InstrumentationTestSuite(this);
        suite.addTestSuite(WifiClientTest.class);
        suite.addTestSuite(WifiSoftAPTest.class);
        return suite;
    }
+186 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.connectivitymanagertest.unit;

import android.content.BroadcastReceiver;
import android.content.Intent;
import android.content.Context;
import android.app.Instrumentation;
import android.os.Handler;
import android.os.Message;
import android.net.wifi.WifiManager;
import android.net.wifi.WifiConfiguration;
import android.net.wifi.WifiConfiguration.KeyMgmt;
import android.net.wifi.WifiConfiguration.Status;

import android.test.suitebuilder.annotation.LargeTest;
import android.test.AndroidTestCase;

import java.util.ArrayList;
import java.util.List;

import android.util.Log;

/**
 * Test wifi client
 */
public class WifiClientTest extends AndroidTestCase {

    private WifiManager mWifiManager;
    private final String TAG = "WifiClientTest";

    //10s delay for turning on wifi
    private static final int DELAY = 10000;

    @Override
    protected void setUp() throws Exception {
        super.setUp();
        mWifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE);
        mWifiManager.setWifiEnabled(true);
        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) {
                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();
    }
}
+30 −5
Original line number Diff line number Diff line
@@ -423,7 +423,12 @@ public class WifiService extends IWifiManager.Stub {
     */
    public boolean pingSupplicant() {
        enforceAccessPermission();
        return mWifiStateMachine.syncPingSupplicant();
        if (mChannel != null) {
            return mWifiStateMachine.syncPingSupplicant(mChannel);
        } else {
            Slog.e(TAG, "mChannel is not initialized");
            return false;
        }
    }

    /**
@@ -634,7 +639,12 @@ public class WifiService extends IWifiManager.Stub {
     */
    public int addOrUpdateNetwork(WifiConfiguration config) {
        enforceChangePermission();
        return mWifiStateMachine.syncAddOrUpdateNetwork(config);
        if (mChannel != null) {
            return mWifiStateMachine.syncAddOrUpdateNetwork(mChannel, config);
        } else {
            Slog.e(TAG, "mChannel is not initialized");
            return -1;
        }
    }

     /**
@@ -662,7 +672,12 @@ public class WifiService extends IWifiManager.Stub {
     */
    public boolean enableNetwork(int netId, boolean disableOthers) {
        enforceChangePermission();
        return mWifiStateMachine.syncEnableNetwork(netId, disableOthers);
        if (mChannel != null) {
            return mWifiStateMachine.syncEnableNetwork(mChannel, netId, disableOthers);
        } else {
            Slog.e(TAG, "mChannel is not initialized");
            return false;
        }
    }

    /**
@@ -673,7 +688,12 @@ public class WifiService extends IWifiManager.Stub {
     */
    public boolean disableNetwork(int netId) {
        enforceChangePermission();
        return mWifiStateMachine.syncDisableNetwork(netId);
        if (mChannel != null) {
            return mWifiStateMachine.syncDisableNetwork(mChannel, netId);
        } else {
            Slog.e(TAG, "mChannel is not initialized");
            return false;
        }
    }

    /**
@@ -708,7 +728,12 @@ public class WifiService extends IWifiManager.Stub {
    public boolean saveConfiguration() {
        boolean result = true;
        enforceChangePermission();
        return mWifiStateMachine.syncSaveConfig();
        if (mChannel != null) {
            return mWifiStateMachine.syncSaveConfig(mChannel);
        } else {
            Slog.e(TAG, "mChannel is not initialized");
            return false;
        }
    }

    /**
+76 −218

File changed.

Preview size limit exceeded, changes collapsed.