WifiAssociationTestRunner.java revision d21f1c1e4d34306ab7aa99f5be1b886f6b4e07a8
1/* 2 * Copyright (C) 2013, 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; 18 19import android.content.Context; 20import android.net.wifi.WifiManager; 21import android.os.Bundle; 22import android.test.InstrumentationTestRunner; 23import android.test.InstrumentationTestSuite; 24import android.util.Log; 25 26import com.android.connectivitymanagertest.functional.WifiAssociationTest; 27 28import junit.framework.TestSuite; 29import junit.framework.Assert; 30 31/** 32 * Instrumentation Test Runner for wifi association test. 33 * The instrument will set frequency band if it is necessary 34 * 35 * To run the association tests: 36 * 37 * adb shell am instrument -e ssid <ssid> -e password <password> \ 38 * -e security-type [OPEN|WEP64|WEP128|WPA_TKIP|WPA2_AES] -e frequency-band [2.4|5.0|auto] 39 * -w com.android.connectivitymanagertest/.WifiAssociationTestRunner" 40 */ 41public class WifiAssociationTestRunner extends InstrumentationTestRunner { 42 private static final String TAG = "WifiAssociationTestRunner"; 43 public int mBand; 44 45 @Override 46 public TestSuite getAllTests() { 47 TestSuite suite = new InstrumentationTestSuite(this); 48 suite.addTestSuite(WifiAssociationTest.class); 49 return suite; 50 } 51 52 @Override 53 public ClassLoader getLoader() { 54 return WifiAssociationTestRunner.class.getClassLoader(); 55 } 56 57 @Override 58 public void onCreate(Bundle icicle) { 59 super.onCreate(icicle); 60 String mFrequencyBand = icicle.getString("frequency-band"); 61 if (mFrequencyBand != null) { 62 setFrequencyBand(mFrequencyBand); 63 } 64 } 65 66 private void setFrequencyBand(String band) { 67 WifiManager mWifiManager = (WifiManager)getContext().getSystemService(Context.WIFI_SERVICE); 68 if (band.equals("2.4")) { 69 Log.v(TAG, "set frequency band to 2.4"); 70 mBand = WifiManager.WIFI_FREQUENCY_BAND_2GHZ; 71 } else if (band.equals("5.0")) { 72 Log.v(TAG, "set frequency band to 5.0"); 73 mBand = WifiManager.WIFI_FREQUENCY_BAND_5GHZ; 74 } else if (band.equals("auto")) { 75 Log.v(TAG, "set frequency band to auto"); 76 mBand = WifiManager.WIFI_FREQUENCY_BAND_AUTO; 77 } else { 78 Assert.fail("invalid frequency band"); 79 } 80 int currentFreq = mWifiManager.getFrequencyBand(); 81 if (mBand == currentFreq) { 82 Log.v(TAG, "frequency band has been set"); 83 return; 84 } 85 mWifiManager.setFrequencyBand(mBand, true); 86 } 87} 88