Home | History | Annotate | Download | only in stress
      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.stress;
     18 
     19 
     20 import android.net.wifi.WifiConfiguration;
     21 import android.net.wifi.WifiConfiguration.AuthAlgorithm;
     22 import android.net.wifi.WifiConfiguration.KeyMgmt;
     23 import android.net.wifi.WifiManager;
     24 import android.os.Environment;
     25 import android.test.suitebuilder.annotation.LargeTest;
     26 
     27 import com.android.connectivitymanagertest.ConnectivityManagerStressTestRunner;
     28 import com.android.connectivitymanagertest.ConnectivityManagerTestBase;
     29 
     30 import java.io.BufferedWriter;
     31 import java.io.File;
     32 import java.io.FileWriter;
     33 
     34 /**
     35  * Stress test setting up device as wifi hotspot
     36  */
     37 public class WifiApStress extends ConnectivityManagerTestBase {
     38     private static String NETWORK_ID = "AndroidAPTest";
     39     private static String PASSWD = "androidwifi";
     40     private final static String OUTPUT_FILE = "WifiStressTestOutput.txt";
     41     private int mTotalIterations;
     42     private BufferedWriter mOutputWriter = null;
     43     private int mLastIteration = 0;
     44     private boolean mWifiOnlyFlag;
     45 
     46     public WifiApStress() {
     47         super(WifiApStress.class.getSimpleName());
     48     }
     49 
     50     @Override
     51     protected void setUp() throws Exception {
     52         super.setUp();
     53         ConnectivityManagerStressTestRunner mRunner =
     54             (ConnectivityManagerStressTestRunner)getInstrumentation();
     55         mTotalIterations = mRunner.getSoftApInterations();
     56         mWifiOnlyFlag = mRunner.isWifiOnly();
     57         turnScreenOn();
     58     }
     59 
     60     @Override
     61     protected void tearDown() throws Exception {
     62         // write the total number of iterations into output file
     63         mOutputWriter = new BufferedWriter(new FileWriter(new File(
     64                 Environment.getExternalStorageDirectory(), OUTPUT_FILE)));
     65         mOutputWriter.write(String.format("iteration %d out of %d\n",
     66                 mLastIteration + 1, mTotalIterations));
     67         mOutputWriter.flush();
     68         mOutputWriter.close();
     69         super.tearDown();
     70     }
     71 
     72     @LargeTest
     73     public void testWifiHotSpot() {
     74         if (mWifiOnlyFlag) {
     75             logv(getName() + " is excluded for wi-fi only test");
     76             return;
     77         }
     78         WifiConfiguration config = new WifiConfiguration();
     79         config.SSID = NETWORK_ID;
     80         config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
     81         config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
     82         config.preSharedKey = PASSWD;
     83 
     84         // if wifiap enabled, disable it
     85         assertTrue("failed to disable wifi hotspot",
     86                 mWifiManager.setWifiApEnabled(config, false));
     87         assertTrue("wifi hotspot not enabled", waitForWifiApState(
     88                 WifiManager.WIFI_AP_STATE_DISABLED, 2 * LONG_TIMEOUT));
     89 
     90         // if Wifi is enabled, disable it
     91         if (mWifiManager.isWifiEnabled()) {
     92             assertTrue("failed to disable wifi", disableWifi());
     93             // wait for the wifi state to be DISABLED
     94             assertTrue("wifi state not disabled", waitForWifiState(
     95                     WifiManager.WIFI_STATE_DISABLED, LONG_TIMEOUT));
     96         }
     97         int i;
     98         for (i = 0; i < mTotalIterations; i++) {
     99             logv("iteration: " + i);
    100             mLastIteration = i;
    101             // enable Wifi tethering
    102             assertTrue("failed to enable wifi hotspot",
    103                     mWifiManager.setWifiApEnabled(config, true));
    104             // wait for wifi ap state to be ENABLED
    105             assertTrue("wifi hotspot not enabled", waitForWifiApState(
    106                     WifiManager.WIFI_AP_STATE_ENABLED, 2 * LONG_TIMEOUT));
    107             // wait for wifi tethering result
    108             assertTrue("tether state not changed", waitForTetherStateChange(LONG_TIMEOUT));
    109             // allow the wifi tethering to be enabled for 10 seconds
    110             try {
    111                 Thread.sleep(2 * SHORT_TIMEOUT);
    112             } catch (Exception e) {
    113                 // ignore
    114             }
    115             assertTrue("no uplink data connection after Wi-Fi tethering", pingTest());
    116             // disable wifi hotspot
    117             assertTrue("failed to disable wifi hotspot",
    118                     mWifiManager.setWifiApEnabled(config, false));
    119             assertTrue("wifi hotspot not enabled", waitForWifiApState(
    120                     WifiManager.WIFI_AP_STATE_DISABLED, 2 * LONG_TIMEOUT));
    121             assertFalse("wifi hotspot still enabled", mWifiManager.isWifiApEnabled());
    122         }
    123     }
    124 
    125 }
    126