1 /* 2 * Copyright (C) 2015 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 com.android.systemui.statusbar.policy; 18 19 import android.content.Intent; 20 import android.net.ConnectivityManager; 21 import android.net.NetworkCapabilities; 22 import android.net.wifi.WifiManager; 23 import android.os.Looper; 24 import android.telephony.PhoneStateListener; 25 import android.telephony.ServiceState; 26 import android.telephony.SignalStrength; 27 import android.telephony.SubscriptionInfo; 28 import android.telephony.SubscriptionManager; 29 import android.telephony.TelephonyManager; 30 import android.util.Log; 31 import com.android.internal.telephony.cdma.EriInfo; 32 import com.android.settingslib.net.DataUsageController; 33 import com.android.systemui.SysuiTestCase; 34 import com.android.systemui.statusbar.policy.NetworkController.IconState; 35 import com.android.systemui.statusbar.policy.NetworkController.SignalCallback; 36 import com.android.systemui.statusbar.policy.NetworkControllerImpl.Config; 37 import com.android.systemui.statusbar.policy.NetworkControllerImpl.SubscriptionDefaults; 38 import org.mockito.ArgumentCaptor; 39 import org.mockito.Mockito; 40 41 import java.io.PrintWriter; 42 import java.io.StringWriter; 43 import java.util.ArrayList; 44 import java.util.List; 45 46 import static org.mockito.Mockito.mock; 47 import static org.mockito.Mockito.when; 48 49 public class NetworkControllerBaseTest extends SysuiTestCase { 50 private static final String TAG = "NetworkControllerBaseTest"; 51 protected static final int DEFAULT_LEVEL = 2; 52 protected static final int DEFAULT_SIGNAL_STRENGTH = 53 TelephonyIcons.TELEPHONY_SIGNAL_STRENGTH[1][DEFAULT_LEVEL]; 54 protected static final int DEFAULT_QS_SIGNAL_STRENGTH = 55 TelephonyIcons.QS_TELEPHONY_SIGNAL_STRENGTH[1][DEFAULT_LEVEL]; 56 protected static final int DEFAULT_ICON = TelephonyIcons.ICON_3G; 57 protected static final int DEFAULT_QS_ICON = TelephonyIcons.QS_ICON_3G; 58 59 protected NetworkControllerImpl mNetworkController; 60 protected MobileSignalController mMobileSignalController; 61 protected PhoneStateListener mPhoneStateListener; 62 protected SignalStrength mSignalStrength; 63 protected ServiceState mServiceState; 64 protected ConnectivityManager mMockCm; 65 protected WifiManager mMockWm; 66 protected SubscriptionManager mMockSm; 67 protected TelephonyManager mMockTm; 68 protected Config mConfig; 69 protected CallbackHandler mCallbackHandler; 70 protected SubscriptionDefaults mMockSubDefaults; 71 72 protected int mSubId; 73 74 private NetworkCapabilities mNetCapabilities; 75 76 @Override 77 protected void setUp() throws Exception { 78 super.setUp(); 79 80 mMockWm = mock(WifiManager.class); 81 mMockTm = mock(TelephonyManager.class); 82 mMockSm = mock(SubscriptionManager.class); 83 mMockCm = mock(ConnectivityManager.class); 84 mMockSubDefaults = mock(SubscriptionDefaults.class); 85 mNetCapabilities = new NetworkCapabilities(); 86 when(mMockCm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)).thenReturn(true); 87 when(mMockCm.getDefaultNetworkCapabilitiesForUser(0)).thenReturn( 88 new NetworkCapabilities[] { mNetCapabilities }); 89 90 mSignalStrength = mock(SignalStrength.class); 91 mServiceState = mock(ServiceState.class); 92 93 mConfig = new Config(); 94 mConfig.hspaDataDistinguishable = true; 95 mCallbackHandler = mock(CallbackHandler.class); 96 mNetworkController = new NetworkControllerImpl(mContext, mMockCm, mMockTm, mMockWm, mMockSm, 97 mConfig, Looper.getMainLooper(), mCallbackHandler, 98 mock(AccessPointControllerImpl.class), mock(DataUsageController.class), 99 mMockSubDefaults); 100 setupNetworkController(); 101 102 // Trigger blank callbacks to always get the current state (some tests don't trigger 103 // changes from default state). 104 mNetworkController.addSignalCallback(mock(SignalCallback.class)); 105 mNetworkController.addEmergencyListener(null); 106 } 107 108 protected void setupNetworkController() { 109 // For now just pretend to be the data sim, so we can test that too. 110 mSubId = SubscriptionManager.DEFAULT_SUBSCRIPTION_ID; 111 when(mMockTm.getDataEnabled(mSubId)).thenReturn(true); 112 setDefaultSubId(mSubId); 113 setSubscriptions(mSubId); 114 mNetworkController.handleSetUserSetupComplete(true); 115 mMobileSignalController = mNetworkController.mMobileSignalControllers.get(mSubId); 116 mPhoneStateListener = mMobileSignalController.mPhoneStateListener; 117 } 118 119 protected void setDefaultSubId(int subId) { 120 when(mMockSubDefaults.getDefaultDataSubId()).thenReturn(subId); 121 when(mMockSubDefaults.getDefaultVoiceSubId()).thenReturn(subId); 122 } 123 124 protected void setSubscriptions(int... subIds) { 125 List<SubscriptionInfo> subs = new ArrayList<SubscriptionInfo>(); 126 for (int subId : subIds) { 127 SubscriptionInfo subscription = mock(SubscriptionInfo.class); 128 when(subscription.getSubscriptionId()).thenReturn(subId); 129 subs.add(subscription); 130 } 131 when(mMockSm.getActiveSubscriptionInfoList()).thenReturn(subs); 132 mNetworkController.doUpdateMobileControllers(); 133 } 134 135 protected NetworkControllerImpl setUpNoMobileData() { 136 when(mMockCm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)).thenReturn(false); 137 NetworkControllerImpl networkControllerNoMobile 138 = new NetworkControllerImpl(mContext, mMockCm, mMockTm, mMockWm, mMockSm, 139 mConfig, Looper.getMainLooper(), mCallbackHandler, 140 mock(AccessPointControllerImpl.class), 141 mock(DataUsageController.class), mMockSubDefaults); 142 143 setupNetworkController(); 144 145 return networkControllerNoMobile; 146 147 } 148 149 @Override 150 protected void tearDown() throws Exception { 151 StringWriter sw = new StringWriter(); 152 PrintWriter pw = new PrintWriter(sw); 153 mNetworkController.dump(null, pw, null); 154 pw.flush(); 155 Log.d(TAG, sw.toString()); 156 super.tearDown(); 157 } 158 159 // 2 Bars 3G GSM. 160 public void setupDefaultSignal() { 161 setIsGsm(true); 162 setVoiceRegState(ServiceState.STATE_IN_SERVICE); 163 setGsmRoaming(false); 164 setLevel(DEFAULT_LEVEL); 165 updateDataConnectionState(TelephonyManager.DATA_CONNECTED, 166 TelephonyManager.NETWORK_TYPE_UMTS); 167 setConnectivity(NetworkCapabilities.TRANSPORT_CELLULAR, true, true); 168 } 169 170 public void setConnectivity(int networkType, boolean inetCondition, boolean isConnected) { 171 Intent i = new Intent(ConnectivityManager.INET_CONDITION_ACTION); 172 // TODO: Separate out into several NetworkCapabilities. 173 if (isConnected) { 174 mNetCapabilities.addTransportType(networkType); 175 } else { 176 mNetCapabilities.removeTransportType(networkType); 177 } 178 if (inetCondition) { 179 mNetCapabilities.addCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED); 180 } else { 181 mNetCapabilities.removeCapability(NetworkCapabilities.NET_CAPABILITY_VALIDATED); 182 } 183 184 mNetworkController.onReceive(mContext, i); 185 } 186 187 public void setGsmRoaming(boolean isRoaming) { 188 when(mServiceState.getRoaming()).thenReturn(isRoaming); 189 updateServiceState(); 190 } 191 192 public void setCdmaRoaming(boolean isRoaming) { 193 when(mServiceState.getCdmaEriIconIndex()).thenReturn(isRoaming ? 194 EriInfo.ROAMING_INDICATOR_ON : EriInfo.ROAMING_INDICATOR_OFF); 195 when(mServiceState.getCdmaEriIconMode()).thenReturn(isRoaming ? 196 EriInfo.ROAMING_ICON_MODE_NORMAL : -1); 197 updateServiceState(); 198 } 199 200 public void setVoiceRegState(int voiceRegState) { 201 when(mServiceState.getVoiceRegState()).thenReturn(voiceRegState); 202 updateServiceState(); 203 } 204 205 public void setDataRegState(int dataRegState) { 206 when(mServiceState.getDataRegState()).thenReturn(dataRegState); 207 updateServiceState(); 208 } 209 210 public void setIsEmergencyOnly(boolean isEmergency) { 211 when(mServiceState.isEmergencyOnly()).thenReturn(isEmergency); 212 updateServiceState(); 213 } 214 215 public void setCdmaLevel(int level) { 216 when(mSignalStrength.getCdmaLevel()).thenReturn(level); 217 updateSignalStrength(); 218 } 219 220 public void setLevel(int level) { 221 when(mSignalStrength.getLevel()).thenReturn(level); 222 updateSignalStrength(); 223 } 224 225 public void setIsGsm(boolean gsm) { 226 when(mSignalStrength.isGsm()).thenReturn(gsm); 227 updateSignalStrength(); 228 } 229 230 public void setCdmaEri(int index, int mode) { 231 // TODO: Figure this out. 232 } 233 234 private void updateSignalStrength() { 235 Log.d(TAG, "Sending Signal Strength: " + mSignalStrength); 236 mPhoneStateListener.onSignalStrengthsChanged(mSignalStrength); 237 } 238 239 protected void updateServiceState() { 240 Log.d(TAG, "Sending Service State: " + mServiceState); 241 mPhoneStateListener.onServiceStateChanged(mServiceState); 242 } 243 244 public void updateCallState(int state) { 245 // Inputs not currently used in NetworkControllerImpl. 246 mPhoneStateListener.onCallStateChanged(state, "0123456789"); 247 } 248 249 public void updateDataConnectionState(int dataState, int dataNetType) { 250 when(mServiceState.getDataNetworkType()).thenReturn(dataNetType); 251 mPhoneStateListener.onDataConnectionStateChanged(dataState, dataNetType); 252 } 253 254 public void updateDataActivity(int dataActivity) { 255 mPhoneStateListener.onDataActivity(dataActivity); 256 } 257 258 public void setCarrierNetworkChange(boolean enable) { 259 Log.d(TAG, "setCarrierNetworkChange(" + enable + ")"); 260 mPhoneStateListener.onCarrierNetworkChange(enable); 261 } 262 263 protected void verifyHasNoSims(boolean hasNoSimsVisible) { 264 ArgumentCaptor<Boolean> hasNoSimsArg = ArgumentCaptor.forClass(Boolean.class); 265 266 Mockito.verify(mCallbackHandler, Mockito.atLeastOnce()).setNoSims(hasNoSimsArg.capture()); 267 assertEquals("No sims", hasNoSimsVisible, (boolean) hasNoSimsArg.getValue()); 268 } 269 270 protected void verifyLastQsMobileDataIndicators(boolean visible, int icon, int typeIcon, 271 boolean dataIn, boolean dataOut) { 272 ArgumentCaptor<IconState> iconArg = ArgumentCaptor.forClass(IconState.class); 273 ArgumentCaptor<Integer> typeIconArg = ArgumentCaptor.forClass(Integer.class); 274 ArgumentCaptor<Boolean> dataInArg = ArgumentCaptor.forClass(Boolean.class); 275 ArgumentCaptor<Boolean> dataOutArg = ArgumentCaptor.forClass(Boolean.class); 276 277 Mockito.verify(mCallbackHandler, Mockito.atLeastOnce()).setMobileDataIndicators( 278 ArgumentCaptor.forClass(IconState.class).capture(), 279 iconArg.capture(), 280 ArgumentCaptor.forClass(Integer.class).capture(), 281 typeIconArg.capture(), dataInArg.capture(), dataOutArg.capture(), 282 ArgumentCaptor.forClass(String.class).capture(), 283 ArgumentCaptor.forClass(String.class).capture(), 284 ArgumentCaptor.forClass(Boolean.class).capture(), 285 ArgumentCaptor.forClass(Integer.class).capture()); 286 IconState iconState = iconArg.getValue(); 287 assertEquals("Visibility in, quick settings", visible, iconState.visible); 288 assertEquals("Signal icon in, quick settings", icon, iconState.icon); 289 assertEquals("Data icon in, quick settings", typeIcon, (int) typeIconArg.getValue()); 290 assertEquals("Data direction in, in quick settings", dataIn, 291 (boolean) dataInArg.getValue()); 292 assertEquals("Data direction out, in quick settings", dataOut, 293 (boolean) dataOutArg.getValue()); 294 } 295 296 protected void verifyLastMobileDataIndicators(boolean visible, int icon, int typeIcon) { 297 ArgumentCaptor<IconState> iconArg = ArgumentCaptor.forClass(IconState.class); 298 ArgumentCaptor<Integer> typeIconArg = ArgumentCaptor.forClass(Integer.class); 299 300 // TODO: Verify all fields. 301 Mockito.verify(mCallbackHandler, Mockito.atLeastOnce()).setMobileDataIndicators( 302 iconArg.capture(), 303 ArgumentCaptor.forClass(IconState.class).capture(), 304 typeIconArg.capture(), 305 ArgumentCaptor.forClass(Integer.class).capture(), 306 ArgumentCaptor.forClass(Boolean.class).capture(), 307 ArgumentCaptor.forClass(Boolean.class).capture(), 308 ArgumentCaptor.forClass(String.class).capture(), 309 ArgumentCaptor.forClass(String.class).capture(), 310 ArgumentCaptor.forClass(Boolean.class).capture(), 311 ArgumentCaptor.forClass(Integer.class).capture()); 312 IconState iconState = iconArg.getValue(); 313 314 assertEquals("Data icon in status bar", typeIcon, (int) typeIconArg.getValue()); 315 assertEquals("Visibility in status bar", visible, iconState.visible); 316 } 317 318 protected void assertNetworkNameEquals(String expected) { 319 assertEquals("Network name", expected, mMobileSignalController.getState().networkName); 320 } 321 } 322