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