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