resolve merge conflicts of 3ed74f4b6 to stage-aosp-master
[android/platform/frameworks/opt/telephony.git] / tests / telephonytests / src / com / android / internal / telephony / TelephonyTest.java
1 /*
2  * Copyright (C) 2016 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package com.android.internal.telephony;
18
19 import static org.junit.Assert.fail;
20 import static org.mockito.Matchers.anyBoolean;
21 import static org.mockito.Matchers.anyLong;
22 import static org.mockito.Matchers.nullable;
23 import static org.mockito.Mockito.anyInt;
24 import static org.mockito.Mockito.doAnswer;
25 import static org.mockito.Mockito.doReturn;
26 import static org.mockito.Mockito.eq;
27
28 import android.app.ActivityManagerNative;
29 import android.app.IActivityManager;
30 import android.content.Context;
31 import android.content.IIntentSender;
32 import android.content.Intent;
33 import android.content.SharedPreferences;
34 import android.content.pm.PackageInfo;
35 import android.content.pm.PackageManager;
36 import android.database.Cursor;
37 import android.os.Bundle;
38 import android.os.Handler;
39 import android.os.IBinder;
40 import android.os.IDeviceIdleController;
41 import android.os.RegistrantList;
42 import android.os.ServiceManager;
43 import android.provider.BlockedNumberContract;
44 import android.telephony.ServiceState;
45 import android.telephony.SubscriptionManager;
46 import android.telephony.TelephonyManager;
47 import android.test.mock.MockContentProvider;
48 import android.test.mock.MockContentResolver;
49 import android.util.Log;
50 import android.util.Singleton;
51
52 import com.android.ims.ImsCall;
53 import com.android.ims.ImsCallProfile;
54 import com.android.ims.ImsEcbm;
55 import com.android.ims.ImsManager;
56 import com.android.internal.telephony.cdma.CdmaSubscriptionSourceManager;
57 import com.android.internal.telephony.cdma.EriManager;
58 import com.android.internal.telephony.dataconnection.DcTracker;
59 import com.android.internal.telephony.imsphone.ImsExternalCallTracker;
60 import com.android.internal.telephony.imsphone.ImsPhone;
61 import com.android.internal.telephony.imsphone.ImsPhoneCallTracker;
62 import com.android.internal.telephony.test.SimulatedCommands;
63 import com.android.internal.telephony.test.SimulatedCommandsVerifier;
64 import com.android.internal.telephony.uicc.IccCardProxy;
65 import com.android.internal.telephony.uicc.IccRecords;
66 import com.android.internal.telephony.uicc.IsimUiccRecords;
67 import com.android.internal.telephony.uicc.RuimRecords;
68 import com.android.internal.telephony.uicc.SIMRecords;
69 import com.android.internal.telephony.uicc.UiccCardApplication;
70 import com.android.internal.telephony.uicc.UiccController;
71
72 import org.mockito.Mock;
73 import org.mockito.MockitoAnnotations;
74 import org.mockito.invocation.InvocationOnMock;
75 import org.mockito.stubbing.Answer;
76
77 import java.lang.reflect.Field;
78 import java.util.HashMap;
79 import java.util.HashSet;
80 import java.util.Iterator;
81 import java.util.LinkedList;
82 import java.util.Set;
83
84 public abstract class TelephonyTest {
85     protected static String TAG;
86
87     @Mock
88     protected GsmCdmaPhone mPhone;
89     @Mock
90     protected ImsPhone mImsPhone;
91     @Mock
92     protected ServiceStateTracker mSST;
93     @Mock
94     protected GsmCdmaCallTracker mCT;
95     @Mock
96     protected ImsPhoneCallTracker mImsCT;
97     @Mock
98     protected UiccController mUiccController;
99     @Mock
100     protected IccCardProxy mIccCardProxy;
101     @Mock
102     protected CallManager mCallManager;
103     @Mock
104     protected PhoneNotifier mNotifier;
105     @Mock
106     protected TelephonyComponentFactory mTelephonyComponentFactory;
107     @Mock
108     protected CdmaSubscriptionSourceManager mCdmaSSM;
109     @Mock
110     protected RegistrantList mRegistrantList;
111     @Mock
112     protected IccPhoneBookInterfaceManager mIccPhoneBookIntManager;
113     @Mock
114     protected ImsManager mImsManager;
115     @Mock
116     protected DcTracker mDcTracker;
117     @Mock
118     protected GsmCdmaCall mGsmCdmaCall;
119     @Mock
120     protected ImsCall mImsCall;
121     @Mock
122     protected ImsCallProfile mImsCallProfile;
123     @Mock
124     protected ImsEcbm mImsEcbm;
125     @Mock
126     protected SubscriptionController mSubscriptionController;
127     @Mock
128     protected ServiceState mServiceState;
129     @Mock
130     protected SimulatedCommandsVerifier mSimulatedCommandsVerifier;
131     @Mock
132     protected IDeviceIdleController mIDeviceIdleController;
133     @Mock
134     protected InboundSmsHandler mInboundSmsHandler;
135     @Mock
136     protected WspTypeDecoder mWspTypeDecoder;
137     @Mock
138     protected UiccCardApplication mUiccCardApplication3gpp;
139     @Mock
140     protected UiccCardApplication mUiccCardApplication3gpp2;
141     @Mock
142     protected UiccCardApplication mUiccCardApplicationIms;
143     @Mock
144     protected SIMRecords mSimRecords;
145     @Mock
146     protected RuimRecords mRuimRecords;
147     @Mock
148     protected IsimUiccRecords mIsimUiccRecords;
149     @Mock
150     protected ProxyController mProxyController;
151     @Mock
152     protected Singleton<IActivityManager> mIActivityManagerSingleton;
153     @Mock
154     protected IActivityManager mIActivityManager;
155     @Mock
156     protected InboundSmsTracker mInboundSmsTracker;
157     @Mock
158     protected IIntentSender mIIntentSender;
159     @Mock
160     protected IBinder mIBinder;
161     @Mock
162     protected SmsStorageMonitor mSmsStorageMonitor;
163     @Mock
164     protected SmsUsageMonitor mSmsUsageMonitor;
165     @Mock
166     protected PackageInfo mPackageInfo;
167     @Mock
168     protected EriManager mEriManager;
169     @Mock
170     protected IBinder mConnMetLoggerBinder;
171     @Mock
172     protected CarrierSignalAgent mCarrierSignalAgent;
173     @Mock
174     protected CarrierActionAgent mCarrierActionAgent;
175     @Mock
176     protected ImsExternalCallTracker mImsExternalCallTracker;
177     @Mock
178     protected DeviceStateMonitor mDeviceStateMonitor;
179
180     protected TelephonyManager mTelephonyManager;
181     protected SubscriptionManager mSubscriptionManager;
182     protected PackageManager mPackageManager;
183     protected SimulatedCommands mSimulatedCommands;
184     protected ContextFixture mContextFixture;
185     protected Context mContext;
186     protected FakeBlockedNumberContentProvider mFakeBlockedNumberContentProvider;
187     private Object mLock = new Object();
188     private boolean mReady;
189     protected HashMap<String, IBinder> mServiceManagerMockedServices = new HashMap<>();
190
191
192     protected HashMap<Integer, ImsManager> mImsManagerInstances = new HashMap<>();
193     private HashMap<InstanceKey, Object> mOldInstances = new HashMap<InstanceKey, Object>();
194
195     private LinkedList<InstanceKey> mInstanceKeys = new LinkedList<InstanceKey>();
196
197     private class InstanceKey {
198         public final Class mClass;
199         public final String mInstName;
200         public final Object mObj;
201         InstanceKey(final Class c, final String instName, final Object obj) {
202             mClass = c;
203             mInstName = instName;
204             mObj = obj;
205         }
206
207         @Override
208         public int hashCode() {
209             return (mClass.getName().hashCode() * 31 + mInstName.hashCode()) * 31;
210         }
211
212         @Override
213         public boolean equals(Object obj) {
214             if (obj == null || obj.getClass() != getClass()) {
215                 return false;
216             }
217
218             InstanceKey other = (InstanceKey) obj;
219             return (other.mClass == mClass && other.mInstName.equals(mInstName)
220                     && other.mObj == mObj);
221         }
222     }
223
224     protected void waitUntilReady() {
225         while (true) {
226             synchronized (mLock) {
227                 if (mReady) {
228                     break;
229                 }
230             }
231         }
232     }
233
234     protected void setReady(boolean ready) {
235         synchronized (mLock) {
236             mReady = ready;
237         }
238     }
239
240     protected synchronized void replaceInstance(final Class c, final String instanceName,
241                                                 final Object obj, final Object newValue)
242             throws Exception {
243         Field field = c.getDeclaredField(instanceName);
244         field.setAccessible(true);
245
246         InstanceKey key = new InstanceKey(c, instanceName, obj);
247         if (!mOldInstances.containsKey(key)) {
248             mOldInstances.put(key, field.get(obj));
249             mInstanceKeys.add(key);
250         }
251         field.set(obj, newValue);
252     }
253
254     protected synchronized void restoreInstance(final Class c, final String instanceName,
255                                                 final Object obj) throws Exception {
256         InstanceKey key = new InstanceKey(c, instanceName, obj);
257         if (mOldInstances.containsKey(key)) {
258             Field field = c.getDeclaredField(instanceName);
259             field.setAccessible(true);
260             field.set(obj, mOldInstances.get(key));
261             mOldInstances.remove(key);
262             mInstanceKeys.remove(key);
263         }
264     }
265
266     protected synchronized void restoreInstances() throws Exception {
267         Iterator<InstanceKey> it = mInstanceKeys.descendingIterator();
268
269         while (it.hasNext()) {
270             InstanceKey key = it.next();
271             Field field = key.mClass.getDeclaredField(key.mInstName);
272             field.setAccessible(true);
273             field.set(key.mObj, mOldInstances.get(key));
274         }
275
276         mInstanceKeys.clear();
277         mOldInstances.clear();
278     }
279
280     protected void setUp(String tag) throws Exception {
281         TAG = tag;
282         MockitoAnnotations.initMocks(this);
283
284         //Use reflection to mock singletons
285         replaceInstance(CallManager.class, "INSTANCE", null, mCallManager);
286         replaceInstance(TelephonyComponentFactory.class, "sInstance", null,
287                 mTelephonyComponentFactory);
288         replaceInstance(UiccController.class, "mInstance", null, mUiccController);
289         replaceInstance(CdmaSubscriptionSourceManager.class, "sInstance", null, mCdmaSSM);
290         replaceInstance(ImsManager.class, "sImsManagerInstances", null, mImsManagerInstances);
291         replaceInstance(SubscriptionController.class, "sInstance", null, mSubscriptionController);
292         replaceInstance(ProxyController.class, "sProxyController", null, mProxyController);
293         replaceInstance(ActivityManagerNative.class, "gDefault", null, mIActivityManagerSingleton);
294         replaceInstance(CdmaSubscriptionSourceManager.class,
295                 "mCdmaSubscriptionSourceChangedRegistrants", mCdmaSSM, mRegistrantList);
296         replaceInstance(SimulatedCommandsVerifier.class, "sInstance", null,
297                 mSimulatedCommandsVerifier);
298         replaceInstance(Singleton.class, "mInstance", mIActivityManagerSingleton,
299                 mIActivityManager);
300         replaceInstance(ServiceManager.class, "sCache", null, mServiceManagerMockedServices);
301
302         mSimulatedCommands = new SimulatedCommands();
303         mContextFixture = new ContextFixture();
304         mContext = mContextFixture.getTestDouble();
305         mFakeBlockedNumberContentProvider = new FakeBlockedNumberContentProvider();
306         ((MockContentResolver)mContext.getContentResolver()).addProvider(
307                 BlockedNumberContract.AUTHORITY, mFakeBlockedNumberContentProvider);
308         mPhone.mCi = mSimulatedCommands;
309         mCT.mCi = mSimulatedCommands;
310         mTelephonyManager = (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE);
311         mSubscriptionManager = (SubscriptionManager) mContext.getSystemService(
312                 Context.TELEPHONY_SUBSCRIPTION_SERVICE);
313         mPackageManager = mContext.getPackageManager();
314
315         replaceInstance(TelephonyManager.class, "sInstance", null,
316                 mContext.getSystemService(Context.TELEPHONY_SERVICE));
317
318         //mTelephonyComponentFactory
319         doReturn(mSST).when(mTelephonyComponentFactory)
320                 .makeServiceStateTracker(nullable(GsmCdmaPhone.class),
321                         nullable(CommandsInterface.class));
322         doReturn(mIccCardProxy).when(mTelephonyComponentFactory)
323                 .makeIccCardProxy(nullable(Context.class), nullable(CommandsInterface.class),
324                         anyInt());
325         doReturn(mCT).when(mTelephonyComponentFactory)
326                 .makeGsmCdmaCallTracker(nullable(GsmCdmaPhone.class));
327         doReturn(mIccPhoneBookIntManager).when(mTelephonyComponentFactory)
328                 .makeIccPhoneBookInterfaceManager(nullable(Phone.class));
329         doReturn(mDcTracker).when(mTelephonyComponentFactory)
330                 .makeDcTracker(nullable(Phone.class));
331         doReturn(mWspTypeDecoder).when(mTelephonyComponentFactory)
332                 .makeWspTypeDecoder(nullable(byte[].class));
333         doReturn(mInboundSmsTracker).when(mTelephonyComponentFactory)
334                 .makeInboundSmsTracker(nullable(byte[].class), anyLong(), anyInt(), anyBoolean(),
335                         anyBoolean(), nullable(String.class), nullable(String.class),
336                         nullable(String.class));
337         doReturn(mInboundSmsTracker).when(mTelephonyComponentFactory)
338                 .makeInboundSmsTracker(nullable(byte[].class), anyLong(), anyInt(), anyBoolean(),
339                         nullable(String.class), nullable(String.class), anyInt(), anyInt(),
340                         anyInt(), anyBoolean(), nullable(String.class));
341         doReturn(mInboundSmsTracker).when(mTelephonyComponentFactory)
342                 .makeInboundSmsTracker(nullable(Cursor.class), anyBoolean());
343         doReturn(mImsCT).when(mTelephonyComponentFactory)
344                 .makeImsPhoneCallTracker(nullable(ImsPhone.class));
345         doReturn(mCdmaSSM).when(mTelephonyComponentFactory)
346                 .getCdmaSubscriptionSourceManagerInstance(nullable(Context.class),
347                         nullable(CommandsInterface.class), nullable(Handler.class),
348                         anyInt(), nullable(Object.class));
349         doReturn(mIDeviceIdleController).when(mTelephonyComponentFactory)
350                 .getIDeviceIdleController();
351         doReturn(mImsExternalCallTracker).when(mTelephonyComponentFactory)
352                 .makeImsExternalCallTracker(nullable(ImsPhone.class));
353         doReturn(mCarrierSignalAgent).when(mTelephonyComponentFactory)
354                 .makeCarrierActionAgent(nullable(Phone.class));
355         doReturn(mCarrierActionAgent).when(mTelephonyComponentFactory)
356                 .makeCarrierActionAgent(nullable(Phone.class));
357         doReturn(mDeviceStateMonitor).when(mTelephonyComponentFactory)
358                 .makeDeviceStateMonitor(nullable(Phone.class));
359
360         //mPhone
361         doReturn(mContext).when(mPhone).getContext();
362         doReturn(mContext).when(mImsPhone).getContext();
363         doReturn(true).when(mPhone).getUnitTestMode();
364         doReturn(mIccCardProxy).when(mPhone).getIccCard();
365         doReturn(mServiceState).when(mPhone).getServiceState();
366         doReturn(mServiceState).when(mImsPhone).getServiceState();
367         doReturn(mPhone).when(mImsPhone).getDefaultPhone();
368         doReturn(true).when(mPhone).isPhoneTypeGsm();
369         doReturn(PhoneConstants.PHONE_TYPE_GSM).when(mPhone).getPhoneType();
370         doReturn(mCT).when(mPhone).getCallTracker();
371         doReturn(mSST).when(mPhone).getServiceStateTracker();
372         doReturn(mCarrierSignalAgent).when(mPhone).getCarrierSignalAgent();
373         doReturn(mCarrierActionAgent).when(mPhone).getCarrierActionAgent();
374         mPhone.mEriManager = mEriManager;
375
376         //mUiccController
377         doReturn(mUiccCardApplication3gpp).when(mUiccController).getUiccCardApplication(anyInt(),
378                 eq(UiccController.APP_FAM_3GPP));
379         doReturn(mUiccCardApplication3gpp2).when(mUiccController).getUiccCardApplication(anyInt(),
380                 eq(UiccController.APP_FAM_3GPP2));
381         doReturn(mUiccCardApplicationIms).when(mUiccController).getUiccCardApplication(anyInt(),
382                 eq(UiccController.APP_FAM_IMS));
383
384         doAnswer(new Answer<IccRecords>() {
385             public IccRecords answer(InvocationOnMock invocation) {
386                 switch ((Integer) invocation.getArguments()[1]) {
387                     case UiccController.APP_FAM_3GPP:
388                         return mSimRecords;
389                     case UiccController.APP_FAM_3GPP2:
390                         return mRuimRecords;
391                     case UiccController.APP_FAM_IMS:
392                         return mIsimUiccRecords;
393                     default:
394                         logd("Unrecognized family " + invocation.getArguments()[1]);
395                         return null;
396                 }
397             }
398         }).when(mUiccController).getIccRecords(anyInt(), anyInt());
399
400         //UiccCardApplication
401         doReturn(mSimRecords).when(mUiccCardApplication3gpp).getIccRecords();
402         doReturn(mRuimRecords).when(mUiccCardApplication3gpp2).getIccRecords();
403         doReturn(mIsimUiccRecords).when(mUiccCardApplicationIms).getIccRecords();
404
405         //mIccCardProxy
406         doReturn(mSimRecords).when(mIccCardProxy).getIccRecords();
407         doAnswer(new Answer<IccRecords>() {
408             public IccRecords answer(InvocationOnMock invocation) {
409                 return (mPhone.isPhoneTypeGsm()) ? mSimRecords : mRuimRecords;
410             }
411         }).when(mIccCardProxy).getIccRecords();
412
413         //SMS
414         doReturn(true).when(mSmsStorageMonitor).isStorageAvailable();
415         doReturn(true).when(mSmsUsageMonitor).check(nullable(String.class), anyInt());
416         doReturn(true).when(mTelephonyManager).getSmsReceiveCapableForPhone(anyInt(), anyBoolean());
417         doReturn(true).when(mTelephonyManager).getSmsSendCapableForPhone(
418                 anyInt(), anyBoolean());
419
420         //Misc
421         doReturn(ServiceState.RIL_RADIO_TECHNOLOGY_UMTS).when(mServiceState).
422                 getRilDataRadioTechnology();
423         doReturn(mPhone).when(mCT).getPhone();
424         mImsManagerInstances.put(mPhone.getPhoneId(), null);
425         doReturn(mImsEcbm).when(mImsManager).getEcbmInterface(anyInt());
426         doReturn(mPhone).when(mInboundSmsHandler).getPhone();
427         doReturn(mImsCallProfile).when(mImsCall).getCallProfile();
428         doReturn(mIBinder).when(mIIntentSender).asBinder();
429         doReturn(mIIntentSender).when(mIActivityManager).getIntentSender(anyInt(),
430                 nullable(String.class), nullable(IBinder.class), nullable(String.class), anyInt(),
431                 nullable(Intent[].class), nullable(String[].class), anyInt(),
432                 nullable(Bundle.class), anyInt());
433         mSST.mSS = mServiceState;
434         mServiceManagerMockedServices.put("connectivity_metrics_logger", mConnMetLoggerBinder);
435
436         setReady(false);
437     }
438
439     protected void tearDown() throws Exception {
440
441         SharedPreferences sharedPreferences = mContext.getSharedPreferences((String) null, 0);
442         sharedPreferences.edit().clear().commit();
443
444         restoreInstances();
445     }
446
447     protected static void logd(String s) {
448         Log.d(TAG, s);
449     }
450
451     public static class FakeBlockedNumberContentProvider extends MockContentProvider {
452         public Set<String> mBlockedNumbers = new HashSet<>();
453         public int mNumEmergencyContactNotifications = 0;
454
455         @Override
456         public Bundle call(String method, String arg, Bundle extras) {
457             switch (method) {
458                 case BlockedNumberContract.SystemContract.METHOD_SHOULD_SYSTEM_BLOCK_NUMBER:
459                     Bundle bundle = new Bundle();
460                     bundle.putBoolean(BlockedNumberContract.RES_NUMBER_IS_BLOCKED,
461                             mBlockedNumbers.contains(arg));
462                     return bundle;
463                 case BlockedNumberContract.SystemContract.METHOD_NOTIFY_EMERGENCY_CONTACT:
464                     mNumEmergencyContactNotifications++;
465                     return new Bundle();
466                 default:
467                     fail("Method not expected: " + method);
468             }
469             return null;
470         }
471     }
472
473     protected void setupMockPackagePermissionChecks() throws Exception {
474         doReturn(new String[]{TAG}).when(mPackageManager).getPackagesForUid(anyInt());
475         doReturn(mPackageInfo).when(mPackageManager).getPackageInfo(eq(TAG), anyInt());
476         doReturn(mPackageInfo).when(mPackageManager).getPackageInfoAsUser(
477                 eq(TAG), anyInt(), anyInt());
478     }
479 }