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 | |
| 7 | import android.content.Context; |
| 8 | import android.content.Intent; |
| 9 | import android.net.ConnectivityManager; |
| 10 | import android.net.NetworkInfo; |
| 11 | import android.telephony.PhoneStateListener; |
| 12 | import android.telephony.ServiceState; |
| 13 | import android.telephony.SignalStrength; |
| 14 | import android.test.AndroidTestCase; |
| 15 | import android.util.Log; |
| 16 | |
| 17 | import com.android.systemui.statusbar.policy.NetworkControllerImpl.SignalCluster; |
| 18 | |
| 19 | import org.mockito.ArgumentCaptor; |
| 20 | import org.mockito.Mockito; |
| 21 | |
| 22 | import java.io.PrintWriter; |
| 23 | import java.io.StringWriter; |
| 24 | |
| 25 | public class NetworkControllerBaseTest extends AndroidTestCase { |
| 26 | private static final String TAG = "NetworkControllerBaseTest"; |
| 27 | |
| 28 | protected NetworkControllerImpl mNetworkController; |
| 29 | protected PhoneStateListener mPhoneStateListener; |
| 30 | protected SignalCluster mSignalCluster; |
| 31 | private SignalStrength mSignalStrength; |
| 32 | private ServiceState mServiceState; |
| 33 | private ConnectivityManager mMockCM; |
| 34 | |
| 35 | @Override |
| 36 | protected void setUp() throws Exception { |
| 37 | super.setUp(); |
| 38 | // Mockito stuff. |
| 39 | System.setProperty("dexmaker.dexcache", mContext.getCacheDir().getPath()); |
| 40 | Thread.currentThread().setContextClassLoader(getClass().getClassLoader()); |
| 41 | |
| 42 | mMockCM = mock(ConnectivityManager.class); |
| 43 | when(mMockCM.isNetworkSupported(ConnectivityManager.TYPE_MOBILE)).thenReturn(true); |
| 44 | |
| 45 | // TODO: Move away from fake, use spy if possible after MSIM refactor. |
| 46 | mNetworkController = new FakeNetworkControllerImpl(mContext); |
| 47 | |
| 48 | mPhoneStateListener = mNetworkController.mPhoneStateListener; |
| 49 | mSignalStrength = mock(SignalStrength.class); |
| 50 | mServiceState = mock(ServiceState.class); |
| 51 | mSignalCluster = mock(SignalCluster.class); |
| 52 | mNetworkController.addSignalCluster(mSignalCluster); |
| 53 | } |
| 54 | |
| 55 | @Override |
| 56 | protected void tearDown() throws Exception { |
| 57 | StringWriter sw = new StringWriter(); |
| 58 | PrintWriter pw = new PrintWriter(sw); |
| 59 | mNetworkController.dump(null, pw, null); |
| 60 | pw.flush(); |
| 61 | Log.d(TAG, sw.toString()); |
| 62 | super.tearDown(); |
| 63 | } |
| 64 | |
| 65 | public void setConnectivity(int inetCondition, int networkType, boolean isConnected) { |
| 66 | Intent i = new Intent(ConnectivityManager.INET_CONDITION_ACTION); |
| 67 | NetworkInfo networkInfo = mock(NetworkInfo.class); |
| 68 | when(networkInfo.isConnected()).thenReturn(isConnected); |
| 69 | when(networkInfo.getType()).thenReturn(networkType); |
| 70 | when(networkInfo.getTypeName()).thenReturn(""); |
| 71 | when(mMockCM.getActiveNetworkInfo()).thenReturn(networkInfo); |
| 72 | |
| 73 | i.putExtra(ConnectivityManager.EXTRA_INET_CONDITION, inetCondition); |
| 74 | mNetworkController.onReceive(mContext, i); |
| 75 | } |
| 76 | |
| 77 | public void setGsmRoaming(boolean isRoaming) { |
| 78 | when(mServiceState.getRoaming()).thenReturn(isRoaming); |
| 79 | updateServiceState(); |
| 80 | } |
| 81 | |
| 82 | public void setVoiceRegState(int voiceRegState) { |
| 83 | when(mServiceState.getVoiceRegState()).thenReturn(voiceRegState); |
| 84 | updateServiceState(); |
| 85 | } |
| 86 | |
| 87 | public void setIsEmergencyOnly(boolean isEmergency) { |
| 88 | when(mServiceState.isEmergencyOnly()).thenReturn(isEmergency); |
| 89 | updateServiceState(); |
| 90 | } |
| 91 | |
| 92 | public void setCdmaLevel(int level) { |
| 93 | when(mSignalStrength.getCdmaLevel()).thenReturn(level); |
| 94 | updateSignalStrength(); |
| 95 | } |
| 96 | |
| 97 | public void setLevel(int level) { |
| 98 | when(mSignalStrength.getLevel()).thenReturn(level); |
| 99 | updateSignalStrength(); |
| 100 | } |
| 101 | |
| 102 | public void setIsGsm(boolean gsm) { |
| 103 | when(mSignalStrength.isGsm()).thenReturn(gsm); |
| 104 | updateSignalStrength(); |
| 105 | } |
| 106 | |
| 107 | public void setCdmaEri(int index, int mode) { |
| 108 | // TODO: Figure this out. |
| 109 | } |
| 110 | |
| 111 | private void updateSignalStrength() { |
| 112 | Log.d(TAG, "Sending Signal Strength: " + mSignalStrength); |
| 113 | mPhoneStateListener.onSignalStrengthsChanged(mSignalStrength); |
| 114 | } |
| 115 | |
| 116 | private void updateServiceState() { |
| 117 | Log.d(TAG, "Sending Service State: " + mServiceState); |
| 118 | mPhoneStateListener.onServiceStateChanged(mServiceState); |
| 119 | } |
| 120 | |
| 121 | public void updateCallState(int state) { |
| 122 | // Inputs not currently used in NetworkControllerImpl. |
| 123 | mPhoneStateListener.onCallStateChanged(state, "0123456789"); |
| 124 | } |
| 125 | |
| 126 | public void updateDataConnectionState(int dataState, int dataNetType) { |
| 127 | mPhoneStateListener.onDataConnectionStateChanged(dataState, dataNetType); |
| 128 | } |
| 129 | |
| 130 | public void updateDataActivity(int dataActivity) { |
| 131 | mPhoneStateListener.onDataActivity(dataActivity); |
| 132 | } |
| 133 | |
| 134 | protected void verifyLastMobileDataIndicators(boolean visible, int icon) { |
| 135 | ArgumentCaptor<Integer> iconArg = ArgumentCaptor.forClass(Integer.class); |
| 136 | ArgumentCaptor<Boolean> visibleArg = ArgumentCaptor.forClass(Boolean.class); |
| 137 | |
| 138 | // TODO: Verify all fields. |
| 139 | Mockito.verify(mSignalCluster, Mockito.atLeastOnce()).setMobileDataIndicators( |
| 140 | visibleArg.capture(), iconArg.capture(), |
| 141 | ArgumentCaptor.forClass(Integer.class).capture(), |
| 142 | ArgumentCaptor.forClass(String.class).capture(), |
| 143 | ArgumentCaptor.forClass(String.class).capture(), |
| 144 | ArgumentCaptor.forClass(Boolean.class).capture()); |
| 145 | |
| 146 | assertEquals(icon, (int) iconArg.getValue()); |
| 147 | assertEquals(visible, (boolean) visibleArg.getValue()); |
| 148 | } |
| 149 | |
| 150 | private class FakeNetworkControllerImpl extends NetworkControllerImpl { |
| 151 | public FakeNetworkControllerImpl(Context context) { |
| 152 | super(context); |
| 153 | } |
| 154 | |
| 155 | @Override |
| 156 | public ConnectivityManager getCM() { |
| 157 | return mMockCM; |
| 158 | } |
| 159 | |
| 160 | public void registerListeners() {}; |
| 161 | } |
| 162 | } |