WifiApStress.java revision d21f1c1e4d34306ab7aa99f5be1b886f6b4e07a8
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 test setting up device as wifi hotspot
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 mTotalIterations;
45    private BufferedWriter mOutputWriter = null;
46    private int mLastIteration = 0;
47    private boolean mWifiOnlyFlag;
48
49    @Override
50    protected void setUp() throws Exception {
51        super.setUp();
52        ConnectivityManagerStressTestRunner mRunner =
53            (ConnectivityManagerStressTestRunner)getInstrumentation();
54        mTotalIterations = mRunner.getSoftApInterations();
55        mWifiOnlyFlag = mRunner.isWifiOnly();
56        turnScreenOn();
57    }
58
59    @Override
60    protected 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",
65                mLastIteration + 1, mTotalIterations));
66        mOutputWriter.flush();
67        mOutputWriter.close();
68        super.tearDown();
69    }
70
71    @LargeTest
72    public void testWifiHotSpot() {
73        if (mWifiOnlyFlag) {
74            Log.v(TAG, this.getName() + " is excluded for wi-fi only test");
75            return;
76        }
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 wifiap enabled, disable it
84        assertTrue("failed to disable wifi hotspot",
85                mWifiManager.setWifiApEnabled(config, false));
86        assertTrue("wifi hotspot not enabled", waitForWifiApState(
87                WifiManager.WIFI_AP_STATE_DISABLED, 2 * LONG_TIMEOUT));
88
89        // if Wifi is enabled, disable it
90        if (mWifiManager.isWifiEnabled()) {
91            assertTrue("failed to disable wifi", disableWifi());
92            // wait for the wifi state to be DISABLED
93            assertTrue("wifi state not disabled", waitForWifiState(
94                    WifiManager.WIFI_STATE_DISABLED, LONG_TIMEOUT));
95        }
96        int i;
97        for (i = 0; i < mTotalIterations; i++) {
98            Log.v(TAG, "iteration: " + i);
99            mLastIteration = i;
100            // enable Wifi tethering
101            assertTrue("failed to enable wifi hotspot",
102                    mWifiManager.setWifiApEnabled(config, true));
103            // wait for wifi ap state to be ENABLED
104            assertTrue("wifi hotspot not enabled", waitForWifiApState(
105                    WifiManager.WIFI_AP_STATE_ENABLED, 2 * LONG_TIMEOUT));
106            // wait for wifi tethering result
107            assertTrue("tether state not changed", waitForTetherStateChange(LONG_TIMEOUT));
108            // allow the wifi tethering to be enabled for 10 seconds
109            try {
110                Thread.sleep(2 * SHORT_TIMEOUT);
111            } catch (Exception e) {
112                // ignore
113            }
114            assertTrue("no uplink data connection after Wi-Fi tethering", pingTest(null));
115            // disable wifi hotspot
116            assertTrue("failed to disable wifi hotspot",
117                    mWifiManager.setWifiApEnabled(config, false));
118            assertTrue("wifi hotspot not enabled", waitForWifiApState(
119                    WifiManager.WIFI_AP_STATE_DISABLED, 2 * LONG_TIMEOUT));
120            assertFalse("wifi hotspot still enabled", mWifiManager.isWifiApEnabled());
121        }
122    }
123
124}
125