Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 1 | |
| 2 | package com.android.systemui.statusbar.policy; |
| 3 | |
| 4 | import static org.mockito.Mockito.mock; |
| 5 | import static org.mockito.Mockito.when; |
| 6 | |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 7 | import android.content.Intent; |
| 8 | import android.net.ConnectivityManager; |
| 9 | import android.net.NetworkInfo; |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 10 | import android.net.wifi.WifiManager; |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 11 | import android.telephony.PhoneStateListener; |
| 12 | import android.telephony.ServiceState; |
| 13 | import android.telephony.SignalStrength; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 14 | import android.telephony.SubscriptionInfo; |
| 15 | import android.telephony.SubscriptionManager; |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 16 | import android.telephony.TelephonyManager; |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 17 | import android.test.AndroidTestCase; |
| 18 | import android.util.Log; |
| 19 | |
Leigh Anne | d0a6279 | 2014-11-13 14:27:10 -0800 | [diff] [blame^] | 20 | import com.android.internal.telephony.IccCardConstants; |
| 21 | import com.android.internal.telephony.TelephonyIntents; |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 22 | import com.android.internal.telephony.cdma.EriInfo; |
| 23 | import com.android.systemui.statusbar.policy.NetworkController.NetworkSignalChangedCallback; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 24 | import com.android.systemui.statusbar.policy.NetworkControllerImpl.Config; |
Leigh Anne | d0a6279 | 2014-11-13 14:27:10 -0800 | [diff] [blame^] | 25 | import com.android.systemui.statusbar.policy.NetworkControllerImpl.MobileSignalController; |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 26 | import com.android.systemui.statusbar.policy.NetworkControllerImpl.SignalCluster; |
| 27 | |
| 28 | import org.mockito.ArgumentCaptor; |
| 29 | import org.mockito.Mockito; |
| 30 | |
| 31 | import java.io.PrintWriter; |
| 32 | import java.io.StringWriter; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 33 | import java.util.ArrayList; |
| 34 | import java.util.List; |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 35 | |
| 36 | public class NetworkControllerBaseTest extends AndroidTestCase { |
| 37 | private static final String TAG = "NetworkControllerBaseTest"; |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 38 | protected static final int DEFAULT_LEVEL = 2; |
| 39 | protected static final int DEFAULT_SIGNAL_STRENGTH = |
| 40 | TelephonyIcons.TELEPHONY_SIGNAL_STRENGTH[1][DEFAULT_LEVEL]; |
| 41 | protected static final int DEFAULT_QS_SIGNAL_STRENGTH = |
| 42 | TelephonyIcons.QS_TELEPHONY_SIGNAL_STRENGTH[1][DEFAULT_LEVEL]; |
| 43 | protected static final int DEFAULT_ICON = TelephonyIcons.ICON_3G; |
| 44 | protected static final int DEFAULT_QS_ICON = TelephonyIcons.QS_ICON_3G; |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 45 | |
| 46 | protected NetworkControllerImpl mNetworkController; |
Leigh Anne | d0a6279 | 2014-11-13 14:27:10 -0800 | [diff] [blame^] | 47 | protected MobileSignalController mMobileSignalController; |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 48 | protected PhoneStateListener mPhoneStateListener; |
| 49 | protected SignalCluster mSignalCluster; |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 50 | protected NetworkSignalChangedCallback mNetworkSignalChangedCallback; |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 51 | private SignalStrength mSignalStrength; |
| 52 | private ServiceState mServiceState; |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 53 | protected ConnectivityManager mMockCm; |
| 54 | protected WifiManager mMockWm; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 55 | protected SubscriptionManager mMockSm; |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 56 | protected TelephonyManager mMockTm; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 57 | protected Config mConfig; |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 58 | |
| 59 | @Override |
| 60 | protected void setUp() throws Exception { |
| 61 | super.setUp(); |
| 62 | // Mockito stuff. |
| 63 | System.setProperty("dexmaker.dexcache", mContext.getCacheDir().getPath()); |
| 64 | Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); |
| 65 | |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 66 | mMockWm = mock(WifiManager.class); |
| 67 | mMockTm = mock(TelephonyManager.class); |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 68 | mMockSm = mock(SubscriptionManager.class); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 69 | mMockCm = mock(ConnectivityManager.class); |
| 70 | when(mMockCm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)).thenReturn(true); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 71 | |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 72 | mSignalStrength = mock(SignalStrength.class); |
| 73 | mServiceState = mock(ServiceState.class); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 74 | |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 75 | mConfig = new Config(); |
| 76 | mConfig.hspaDataDistinguishable = true; |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 77 | mNetworkController = new NetworkControllerImpl(mContext, mMockCm, mMockTm, mMockWm, mMockSm, |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 78 | mConfig, mock(AccessPointControllerImpl.class), |
| 79 | mock(MobileDataControllerImpl.class)); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 80 | setupNetworkController(); |
| 81 | } |
| 82 | |
| 83 | protected void setupNetworkController() { |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 84 | // For now just pretend to be the data sim, so we can test that too. |
| 85 | final int subId = SubscriptionManager.getDefaultDataSubId(); |
| 86 | SubscriptionInfo subscription = mock(SubscriptionInfo.class); |
| 87 | List<SubscriptionInfo> subs = new ArrayList<SubscriptionInfo>(); |
| 88 | when(subscription.getSubscriptionId()).thenReturn(subId); |
| 89 | subs.add(subscription); |
| 90 | mNetworkController.setCurrentSubscriptions(subs); |
Leigh Anne | d0a6279 | 2014-11-13 14:27:10 -0800 | [diff] [blame^] | 91 | mMobileSignalController = mNetworkController.mMobileSignalControllers.get(subId); |
| 92 | mPhoneStateListener = mMobileSignalController.mPhoneStateListener; |
Jason Monk | d2263cd | 2014-11-10 14:22:56 -0500 | [diff] [blame] | 93 | mSignalCluster = mock(SignalCluster.class); |
| 94 | mNetworkSignalChangedCallback = mock(NetworkSignalChangedCallback.class); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 95 | mNetworkController.addSignalCluster(mSignalCluster); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 96 | mNetworkController.addNetworkSignalChangedCallback(mNetworkSignalChangedCallback); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 97 | } |
| 98 | |
Leigh Anne | d0a6279 | 2014-11-13 14:27:10 -0800 | [diff] [blame^] | 99 | protected NetworkControllerImpl setUpNoMobileData() { |
| 100 | when(mMockCm.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)).thenReturn(false); |
| 101 | NetworkControllerImpl networkControllerNoMobile |
| 102 | = new NetworkControllerImpl(mContext, mMockCm, mMockTm, mMockWm, mMockSm, |
| 103 | mConfig, mock(AccessPointControllerImpl.class), |
| 104 | mock(MobileDataControllerImpl.class)); |
| 105 | |
| 106 | setupNetworkController(); |
| 107 | |
| 108 | return networkControllerNoMobile; |
| 109 | |
| 110 | } |
| 111 | |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 112 | @Override |
| 113 | protected void tearDown() throws Exception { |
| 114 | StringWriter sw = new StringWriter(); |
| 115 | PrintWriter pw = new PrintWriter(sw); |
| 116 | mNetworkController.dump(null, pw, null); |
| 117 | pw.flush(); |
| 118 | Log.d(TAG, sw.toString()); |
| 119 | super.tearDown(); |
| 120 | } |
| 121 | |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 122 | // 2 Bars 3G GSM. |
| 123 | public void setupDefaultSignal() { |
| 124 | setIsGsm(true); |
| 125 | setVoiceRegState(ServiceState.STATE_IN_SERVICE); |
| 126 | setGsmRoaming(false); |
| 127 | setLevel(DEFAULT_LEVEL); |
| 128 | updateDataConnectionState(TelephonyManager.DATA_CONNECTED, |
| 129 | TelephonyManager.NETWORK_TYPE_UMTS); |
| 130 | setConnectivity(100, ConnectivityManager.TYPE_MOBILE, true); |
| 131 | } |
| 132 | |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 133 | public void setConnectivity(int inetCondition, int networkType, boolean isConnected) { |
| 134 | Intent i = new Intent(ConnectivityManager.INET_CONDITION_ACTION); |
| 135 | NetworkInfo networkInfo = mock(NetworkInfo.class); |
| 136 | when(networkInfo.isConnected()).thenReturn(isConnected); |
| 137 | when(networkInfo.getType()).thenReturn(networkType); |
| 138 | when(networkInfo.getTypeName()).thenReturn(""); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 139 | when(mMockCm.getActiveNetworkInfo()).thenReturn(networkInfo); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 140 | |
| 141 | i.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, inetCondition); |
| 142 | mNetworkController.onReceive(mContext, i); |
| 143 | } |
| 144 | |
| 145 | public void setGsmRoaming(boolean isRoaming) { |
| 146 | when(mServiceState.getRoaming()).thenReturn(isRoaming); |
| 147 | updateServiceState(); |
| 148 | } |
| 149 | |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 150 | public void setCdmaRoaming(boolean isRoaming) { |
| 151 | when(mServiceState.getCdmaEriIconIndex()).thenReturn(isRoaming ? |
| 152 | EriInfo.ROAMING_INDICATOR_ON : EriInfo.ROAMING_INDICATOR_OFF); |
| 153 | when(mServiceState.getCdmaEriIconMode()).thenReturn(isRoaming ? |
| 154 | EriInfo.ROAMING_ICON_MODE_NORMAL : -1); |
| 155 | updateServiceState(); |
| 156 | } |
| 157 | |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 158 | public void setVoiceRegState(int voiceRegState) { |
| 159 | when(mServiceState.getVoiceRegState()).thenReturn(voiceRegState); |
| 160 | updateServiceState(); |
| 161 | } |
| 162 | |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 163 | public void setDataRegState(int dataRegState) { |
| 164 | when(mServiceState.getDataRegState()).thenReturn(dataRegState); |
| 165 | updateServiceState(); |
| 166 | } |
| 167 | |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 168 | public void setIsEmergencyOnly(boolean isEmergency) { |
| 169 | when(mServiceState.isEmergencyOnly()).thenReturn(isEmergency); |
| 170 | updateServiceState(); |
| 171 | } |
| 172 | |
| 173 | public void setCdmaLevel(int level) { |
| 174 | when(mSignalStrength.getCdmaLevel()).thenReturn(level); |
| 175 | updateSignalStrength(); |
| 176 | } |
| 177 | |
| 178 | public void setLevel(int level) { |
| 179 | when(mSignalStrength.getLevel()).thenReturn(level); |
| 180 | updateSignalStrength(); |
| 181 | } |
| 182 | |
| 183 | public void setIsGsm(boolean gsm) { |
| 184 | when(mSignalStrength.isGsm()).thenReturn(gsm); |
| 185 | updateSignalStrength(); |
| 186 | } |
| 187 | |
| 188 | public void setCdmaEri(int index, int mode) { |
| 189 | // TODO: Figure this out. |
| 190 | } |
| 191 | |
| 192 | private void updateSignalStrength() { |
| 193 | Log.d(TAG, "Sending Signal Strength: " + mSignalStrength); |
| 194 | mPhoneStateListener.onSignalStrengthsChanged(mSignalStrength); |
| 195 | } |
| 196 | |
| 197 | private void updateServiceState() { |
| 198 | Log.d(TAG, "Sending Service State: " + mServiceState); |
| 199 | mPhoneStateListener.onServiceStateChanged(mServiceState); |
| 200 | } |
| 201 | |
| 202 | public void updateCallState(int state) { |
| 203 | // Inputs not currently used in NetworkControllerImpl. |
| 204 | mPhoneStateListener.onCallStateChanged(state, "0123456789"); |
| 205 | } |
| 206 | |
| 207 | public void updateDataConnectionState(int dataState, int dataNetType) { |
| 208 | mPhoneStateListener.onDataConnectionStateChanged(dataState, dataNetType); |
| 209 | } |
| 210 | |
| 211 | public void updateDataActivity(int dataActivity) { |
| 212 | mPhoneStateListener.onDataActivity(dataActivity); |
| 213 | } |
| 214 | |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 215 | protected void verifyLastQsMobileDataIndicators(boolean visible, int icon, int typeIcon, |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 216 | boolean dataIn, boolean dataOut) { |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 217 | ArgumentCaptor<Integer> iconArg = ArgumentCaptor.forClass(Integer.class); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 218 | ArgumentCaptor<Integer> typeIconArg = ArgumentCaptor.forClass(Integer.class); |
| 219 | ArgumentCaptor<Boolean> visibleArg = ArgumentCaptor.forClass(Boolean.class); |
| 220 | ArgumentCaptor<Boolean> dataInArg = ArgumentCaptor.forClass(Boolean.class); |
| 221 | ArgumentCaptor<Boolean> dataOutArg = ArgumentCaptor.forClass(Boolean.class); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 222 | |
| 223 | Mockito.verify(mNetworkSignalChangedCallback, Mockito.atLeastOnce()) |
| 224 | .onMobileDataSignalChanged(visibleArg.capture(), iconArg.capture(), |
| 225 | ArgumentCaptor.forClass(String.class).capture(), |
| 226 | typeIconArg.capture(), |
| 227 | dataInArg.capture(), |
| 228 | dataOutArg.capture(), |
| 229 | ArgumentCaptor.forClass(String.class).capture(), |
| 230 | ArgumentCaptor.forClass(String.class).capture(), |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 231 | ArgumentCaptor.forClass(Boolean.class).capture()); |
| 232 | assertEquals("Visibility in, quick settings", visible, (boolean) visibleArg.getValue()); |
| 233 | assertEquals("Signal icon in, quick settings", icon, (int) iconArg.getValue()); |
| 234 | assertEquals("Data icon in, quick settings", typeIcon, (int) typeIconArg.getValue()); |
| 235 | assertEquals("Data direction in, in quick settings", dataIn, |
| 236 | (boolean) dataInArg.getValue()); |
| 237 | assertEquals("Data direction out, in quick settings", dataOut, |
| 238 | (boolean) dataOutArg.getValue()); |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 239 | } |
| 240 | |
| 241 | protected void verifyLastMobileDataIndicators(boolean visible, int icon, int typeIcon) { |
| 242 | ArgumentCaptor<Integer> iconArg = ArgumentCaptor.forClass(Integer.class); |
| 243 | ArgumentCaptor<Integer> typeIconArg = ArgumentCaptor.forClass(Integer.class); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 244 | ArgumentCaptor<Boolean> visibleArg = ArgumentCaptor.forClass(Boolean.class); |
| 245 | |
| 246 | // TODO: Verify all fields. |
| 247 | Mockito.verify(mSignalCluster, Mockito.atLeastOnce()).setMobileDataIndicators( |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 248 | visibleArg.capture(), iconArg.capture(), typeIconArg.capture(), |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 249 | ArgumentCaptor.forClass(String.class).capture(), |
| 250 | ArgumentCaptor.forClass(String.class).capture(), |
Jason Monk | b574627 | 2014-11-12 16:50:31 -0500 | [diff] [blame] | 251 | ArgumentCaptor.forClass(Boolean.class).capture(), |
| 252 | ArgumentCaptor.forClass(Integer.class).capture()); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 253 | |
Jason Monk | f13b4b3 | 2014-11-07 16:39:34 -0500 | [diff] [blame] | 254 | assertEquals("Signal icon in status bar", icon, (int) iconArg.getValue()); |
| 255 | assertEquals("Data icon in status bar", typeIcon, (int) typeIconArg.getValue()); |
| 256 | assertEquals("Visibility in status bar", visible, (boolean) visibleArg.getValue()); |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 257 | } |
Leigh Anne | d0a6279 | 2014-11-13 14:27:10 -0800 | [diff] [blame^] | 258 | |
| 259 | protected void assertSimStateEquals(IccCardConstants.State expected) { |
| 260 | assertEquals("Sim state", expected, mMobileSignalController.getSimState()); |
| 261 | } |
| 262 | |
| 263 | protected void assertNetworkNameEquals(String expected) { |
| 264 | assertEquals("Network name", expected, mNetworkController.getMobileNetworkName()); |
| 265 | } |
Jason Monk | e0cdb60 | 2014-11-05 12:39:45 -0500 | [diff] [blame] | 266 | } |