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 com.android.connectivitymanagertest.ConnectivityManagerStressTestRunner;
     21 import com.android.connectivitymanagertest.ConnectivityManagerTestActivity;
     22 
     23 import android.content.Context;
     24 import android.net.wifi.WifiConfiguration;
     25 import android.net.wifi.WifiConfiguration.KeyMgmt;
     26 import android.net.wifi.WifiConfiguration.AuthAlgorithm;
     27 import android.net.wifi.WifiManager;
     28 import android.os.Environment;
     29 import android.os.IPowerManager;
     30 import android.os.PowerManager;
     31 import android.os.ServiceManager;
     32 import android.os.SystemClock;
     33 import android.test.ActivityInstrumentationTestCase2;
     34 import android.test.suitebuilder.annotation.LargeTest;
     35 import android.util.Log;
     36 
     37 import java.io.BufferedWriter;
     38 import java.io.File;
     39 import java.io.FileWriter;
     40 
     41 /**
     42  * Stress the wifi driver as access point.
     43  */
     44 public class WifiApStress
     45     extends ActivityInstrumentationTestCase2<ConnectivityManagerTestActivity> {
     46     private final static String TAG = "WifiApStress";
     47     private static String NETWORK_ID = "AndroidAPTest";
     48     private static String PASSWD = "androidwifi";
     49     private final static String OUTPUT_FILE = "WifiStressTestOutput.txt";
     50     private ConnectivityManagerTestActivity mAct;
     51     private int iterations;
     52     private BufferedWriter mOutputWriter = null;
     53     private int mLastIteration = 0;
     54 
     55     public WifiApStress() {
     56         super(ConnectivityManagerTestActivity.class);
     57     }
     58 
     59     @Override
     60     public void setUp() throws Exception {
     61         super.setUp();
     62         mAct = getActivity();
     63         ConnectivityManagerStressTestRunner mRunner =
     64             (ConnectivityManagerStressTestRunner)getInstrumentation();
     65         iterations = mRunner.mSoftapIterations;
     66         mAct.turnScreenOn();
     67     }
     68 
     69     @Override
     70     public void tearDown() throws Exception {
     71         // write the total number of iterations into output file
     72         mOutputWriter = new BufferedWriter(new FileWriter(new File(
     73                 Environment.getExternalStorageDirectory(), OUTPUT_FILE)));
     74         mOutputWriter.write(String.format("iteration %d out of %d\n", mLastIteration, iterations));
     75         mOutputWriter.flush();
     76         mOutputWriter.close();
     77         super.tearDown();
     78     }
     79 
     80     @LargeTest
     81     public void testWifiHotSpot() {
     82         WifiConfiguration config = new WifiConfiguration();
     83         config.SSID = NETWORK_ID;
     84         config.allowedKeyManagement.set(KeyMgmt.WPA_PSK);
     85         config.allowedAuthAlgorithms.set(AuthAlgorithm.OPEN);
     86         config.preSharedKey = PASSWD;
     87 
     88         // If Wifi is enabled, disable it
     89         if (mAct.mWifiManager.isWifiEnabled()) {
     90             mAct.disableWifi();
     91         }
     92         int i;
     93         for (i = 0; i < iterations; i++) {
     94             Log.v(TAG, "iteration: " + i);
     95             mLastIteration = i;
     96             // enable Wifi tethering
     97             assertTrue(mAct.mWifiManager.setWifiApEnabled(config, true));
     98             // Wait for wifi ap state to be ENABLED
     99             assertTrue(mAct.waitForWifiAPState(WifiManager.WIFI_AP_STATE_ENABLED,
    100                     ConnectivityManagerTestActivity.LONG_TIMEOUT));
    101             // Wait for wifi tethering result
    102             assertEquals(ConnectivityManagerTestActivity.SUCCESS,
    103                     mAct.waitForTetherStateChange(2*ConnectivityManagerTestActivity.SHORT_TIMEOUT));
    104             // Allow the wifi tethering to be enabled for 10 seconds
    105             try {
    106                 Thread.sleep(2 * ConnectivityManagerTestActivity.SHORT_TIMEOUT);
    107             } catch (Exception e) {
    108                 fail("thread in sleep is interrupted");
    109             }
    110             assertTrue("no uplink data connection after Wi-Fi tethering", mAct.pingTest(null));
    111             // Disable soft AP
    112             assertTrue(mAct.mWifiManager.setWifiApEnabled(config, false));
    113             // Wait for 30 seconds until Wi-Fi tethering is stopped
    114             try {
    115                 Thread.sleep(30 * 1000);
    116                 Log.v(TAG, "wait for Wi-Fi tethering to be disabled.");
    117             } catch (Exception e) {
    118                 fail("thread in sleep is interrupted");
    119             }
    120             assertFalse("Wi-Fi AP disable failed", mAct.mWifiManager.isWifiApEnabled());
    121         }
    122         if (i == iterations) {
    123             mLastIteration = iterations;
    124         }
    125     }
    126 
    127 }
    128