1 /* 2 * Copyright (C) 2008 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 android.net.wifi.cts; 18 19 20 import android.content.BroadcastReceiver; 21 import android.content.Context; 22 import android.content.Intent; 23 import android.content.IntentFilter; 24 import android.net.wifi.SupplicantState; 25 import android.net.wifi.WifiInfo; 26 import android.net.wifi.WifiManager; 27 import android.net.wifi.WifiManager.WifiLock; 28 import android.net.wifi.WifiSsid; 29 import android.platform.test.annotations.AppModeFull; 30 import android.test.AndroidTestCase; 31 32 import com.android.compatibility.common.util.PollingCheck; 33 import com.android.compatibility.common.util.SystemUtil; 34 35 import java.util.concurrent.Callable; 36 37 @AppModeFull(reason = "Cannot get WifiManager in instant app mode") 38 public class WifiInfoTest extends AndroidTestCase { 39 private static class MySync { 40 int expectedState = STATE_NULL; 41 } 42 43 private WifiManager mWifiManager; 44 private WifiLock mWifiLock; 45 private static MySync mMySync; 46 47 private static final int STATE_NULL = 0; 48 private static final int STATE_WIFI_CHANGING = 1; 49 private static final int STATE_WIFI_CHANGED = 2; 50 51 private static final String TAG = "WifiInfoTest"; 52 private static final int TIMEOUT_MSEC = 6000; 53 private static final int WAIT_MSEC = 60; 54 private static final int DURATION = 10000; 55 private IntentFilter mIntentFilter; 56 private final BroadcastReceiver mReceiver = new BroadcastReceiver() { 57 @Override 58 public void onReceive(Context context, Intent intent) { 59 final String action = intent.getAction(); 60 if (action.equals(WifiManager.WIFI_STATE_CHANGED_ACTION)) { 61 synchronized (mMySync) { 62 mMySync.expectedState = STATE_WIFI_CHANGED; 63 mMySync.notify(); 64 } 65 } 66 } 67 }; 68 69 @Override 70 protected void setUp() throws Exception { 71 super.setUp(); 72 if (!WifiFeature.isWifiSupported(getContext())) { 73 // skip the test if WiFi is not supported 74 return; 75 } 76 mMySync = new MySync(); 77 mIntentFilter = new IntentFilter(); 78 mIntentFilter.addAction(WifiManager.WIFI_STATE_CHANGED_ACTION); 79 80 mContext.registerReceiver(mReceiver, mIntentFilter); 81 mWifiManager = (WifiManager) getContext().getSystemService(Context.WIFI_SERVICE); 82 assertNotNull(mWifiManager); 83 mWifiLock = mWifiManager.createWifiLock(TAG); 84 mWifiLock.acquire(); 85 if (!mWifiManager.isWifiEnabled()) 86 setWifiEnabled(true); 87 Thread.sleep(DURATION); 88 assertTrue(mWifiManager.isWifiEnabled()); 89 mMySync.expectedState = STATE_NULL; 90 } 91 92 @Override 93 protected void tearDown() throws Exception { 94 if (!WifiFeature.isWifiSupported(getContext())) { 95 // skip the test if WiFi is not supported 96 super.tearDown(); 97 return; 98 } 99 mWifiLock.release(); 100 mContext.unregisterReceiver(mReceiver); 101 if (!mWifiManager.isWifiEnabled()) 102 setWifiEnabled(true); 103 Thread.sleep(DURATION); 104 super.tearDown(); 105 } 106 107 private void setWifiEnabled(boolean enable) throws Exception { 108 synchronized (mMySync) { 109 mMySync.expectedState = STATE_WIFI_CHANGING; 110 if (enable) { 111 SystemUtil.runShellCommand("svc wifi enable"); 112 } else { 113 SystemUtil.runShellCommand("svc wifi disable"); 114 } 115 long timeout = System.currentTimeMillis() + TIMEOUT_MSEC; 116 while (System.currentTimeMillis() < timeout 117 && mMySync.expectedState == STATE_WIFI_CHANGING) 118 mMySync.wait(WAIT_MSEC); 119 } 120 } 121 122 public void testWifiInfoProperties() throws Exception { 123 if (!WifiFeature.isWifiSupported(getContext())) { 124 // skip the test if WiFi is not supported 125 return; 126 } 127 // this test case should in Wifi environment 128 WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); 129 130 assertNotNull(wifiInfo); 131 assertNotNull(wifiInfo.toString()); 132 SupplicantState.isValidState(wifiInfo.getSupplicantState()); 133 WifiInfo.getDetailedStateOf(SupplicantState.DISCONNECTED); 134 String ssid = wifiInfo.getSSID(); 135 if (!ssid.startsWith("0x") && !ssid.equals(WifiSsid.NONE)) { 136 // Non-hex string should be quoted 137 assertTrue(ssid.charAt(0) == '"'); 138 assertTrue(ssid.charAt(ssid.length() - 1) == '"'); 139 } 140 141 wifiInfo.getBSSID(); 142 wifiInfo.getIpAddress(); 143 wifiInfo.getLinkSpeed(); 144 wifiInfo.getTxLinkSpeedMbps(); 145 wifiInfo.getRxLinkSpeedMbps(); 146 wifiInfo.getRssi(); 147 wifiInfo.getHiddenSSID(); 148 wifiInfo.getMacAddress(); 149 setWifiEnabled(false); 150 151 PollingCheck.check("getNetworkId not -1", 20000, new Callable<Boolean>() { 152 @Override 153 public Boolean call() throws Exception { 154 WifiInfo wifiInfo = mWifiManager.getConnectionInfo(); 155 return wifiInfo.getNetworkId() == -1; 156 } 157 }); 158 159 PollingCheck.check("getWifiState not disabled", 20000, new Callable<Boolean>() { 160 @Override 161 public Boolean call() throws Exception { 162 return mWifiManager.getWifiState() == WifiManager.WIFI_STATE_DISABLED; 163 } 164 }); 165 } 166 167 } 168