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