FileDocCategorySizeDatePackage
WifiConnectionTest.javaAPI DocAndroid 5.1 API4181Thu Mar 12 22:22:12 GMT 2015com.android.connectivitymanagertest.functional

WifiConnectionTest

public class WifiConnectionTest extends com.android.connectivitymanagertest.ConnectivityManagerTestBase
Test Wi-Fi connection with different configuration To run this tests: adb shell am instrument \ -e class com.android.connectivitymanagertest.functional.WifiConnectionTest \ -w com.android.connectivitymanagertest/.ConnectivityManagerTestRunner

Fields Summary
private static final String
WIFI_CONFIG_FILE
private static final long
PAUSE_DURATION_MS
Constructors Summary
public WifiConnectionTest()


      
        super(WifiConnectionTest.class.getSimpleName());
    
Methods Summary
private java.util.ListloadConfigurations()
Load the configuration file from the root of the data partition

        BufferedReader reader = null;
        try {
            reader = new BufferedReader(new FileReader(new File(WIFI_CONFIG_FILE)));
            StringBuffer jsonBuffer = new StringBuffer();
            String line;
            while ((line = reader.readLine()) != null) {
                jsonBuffer.append(line);
            }
            return WifiConfigurationHelper.parseJson(jsonBuffer.toString());
        } catch (IllegalArgumentException | IOException e) {
            throw new AssertionError("Error parsing file", e);
        } finally {
            if (reader != null) {
                try {
                    reader.close();
                } catch (IOException e) {
                    // Ignore
                }
            }
        }
    
private voidprintWifiConfigurations(java.util.List wifiConfigs)
Print the wifi configurations to test against.

        logv("Wifi configurations to be tested");
        for (WifiConfiguration config : wifiConfigs) {
            logv(config.toString());
        }
    
public voidsetUp()

        super.setUp();
        assertTrue("Failed to enable wifi", enableWifi());
    
public voidtearDown()

        removeConfiguredNetworksAndDisableWifi();
        super.tearDown();
    
public voidtestWifiConnections()

        List<WifiConfiguration> wifiConfigs = loadConfigurations();

        printWifiConfigurations(wifiConfigs);

        assertFalse("No configurations to test against", wifiConfigs.isEmpty());

        boolean shouldPause = false;
        for (WifiConfiguration config : wifiConfigs) {
            if (shouldPause) {
                logv("Pausing for %d seconds", PAUSE_DURATION_MS / 1000);
                SystemClock.sleep(PAUSE_DURATION_MS);
            }
            logv("Start wifi connection test to: %s", config.SSID);
            connectToWifi(config);

            // verify that connection actually works
            assertTrue("No connectivity at end of test", checkNetworkConnectivity());

            // Disconnect and remove the network
            assertTrue("Unable to remove network", disconnectAP());
            logv("End wifi connection test to: %s", config.SSID);

            shouldPause = true;
        }