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