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