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