Home | History | Annotate | Download | only in functional
      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.functional;
     18 
     19 import android.net.wifi.WifiConfiguration;
     20 import android.os.SystemClock;
     21 import android.test.suitebuilder.annotation.LargeTest;
     22 
     23 import com.android.connectivitymanagertest.ConnectivityManagerTestBase;
     24 import com.android.connectivitymanagertest.WifiConfigurationHelper;
     25 
     26 import java.io.BufferedReader;
     27 import java.io.File;
     28 import java.io.FileReader;
     29 import java.io.IOException;
     30 import java.util.List;
     31 
     32 /**
     33  * Test Wi-Fi connection with different configuration
     34  * To run this tests:
     35  *     adb shell am instrument \
     36  *         -e class com.android.connectivitymanagertest.functional.WifiConnectionTest \
     37  *         -w com.android.connectivitymanagertest/.ConnectivityManagerTestRunner
     38  */
     39 public class WifiConnectionTest extends ConnectivityManagerTestBase {
     40     private static final String WIFI_CONFIG_FILE = "/data/wifi_configs.json";
     41     private static final long PAUSE_DURATION_MS = 60 * 1000;
     42 
     43     public WifiConnectionTest() {
     44         super(WifiConnectionTest.class.getSimpleName());
     45     }
     46 
     47     @Override
     48     public void setUp() throws Exception {
     49         super.setUp();
     50         assertTrue("Failed to enable wifi", enableWifi());
     51     }
     52 
     53     @Override
     54     public void tearDown() throws Exception {
     55         removeConfiguredNetworksAndDisableWifi();
     56         super.tearDown();
     57     }
     58 
     59     @LargeTest
     60     public void testWifiConnections() {
     61         List<WifiConfiguration> wifiConfigs = loadConfigurations();
     62 
     63         printWifiConfigurations(wifiConfigs);
     64 
     65         assertFalse("No configurations to test against", wifiConfigs.isEmpty());
     66 
     67         boolean shouldPause = false;
     68         for (WifiConfiguration config : wifiConfigs) {
     69             if (shouldPause) {
     70                 logv("Pausing for %d seconds", PAUSE_DURATION_MS / 1000);
     71                 SystemClock.sleep(PAUSE_DURATION_MS);
     72             }
     73             logv("Start wifi connection test to: %s", config.SSID);
     74             connectToWifi(config);
     75 
     76             // verify that connection actually works
     77             assertTrue("No connectivity at end of test", checkNetworkConnectivity());
     78 
     79             // Disconnect and remove the network
     80             assertTrue("Unable to remove network", disconnectAP());
     81             logv("End wifi connection test to: %s", config.SSID);
     82 
     83             shouldPause = true;
     84         }
     85     }
     86 
     87     /**
     88      * Load the configuration file from the root of the data partition
     89      */
     90     private List<WifiConfiguration> loadConfigurations() {
     91         BufferedReader reader = null;
     92         try {
     93             reader = new BufferedReader(new FileReader(new File(WIFI_CONFIG_FILE)));
     94             StringBuffer jsonBuffer = new StringBuffer();
     95             String line;
     96             while ((line = reader.readLine()) != null) {
     97                 jsonBuffer.append(line);
     98             }
     99             return WifiConfigurationHelper.parseJson(jsonBuffer.toString());
    100         } catch (IllegalArgumentException | IOException e) {
    101             throw new AssertionError("Error parsing file", e);
    102         } finally {
    103             if (reader != null) {
    104                 try {
    105                     reader.close();
    106                 } catch (IOException e) {
    107                     // Ignore
    108                 }
    109             }
    110         }
    111     }
    112 
    113     /**
    114      * Print the wifi configurations to test against.
    115      */
    116     private void printWifiConfigurations(List<WifiConfiguration> wifiConfigs) {
    117         logv("Wifi configurations to be tested");
    118         for (WifiConfiguration config : wifiConfigs) {
    119             logv(config.toString());
    120         }
    121     }
    122 }
    123