TelephonyTest.java revision 4a9f066fbcbd20e2ea3ca3cbc914f81b230e3825
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
17package com.android.internal.telephony;
18
19import android.content.Context;
20import android.content.Intent;
21import android.os.AsyncResult;
22import android.os.HandlerThread;
23import android.os.IDeviceIdleController;
24import android.os.Message;
25import android.os.RegistrantList;
26import android.provider.Settings;
27import android.telephony.CarrierConfigManager;
28import android.telephony.CellLocation;
29import android.telephony.ServiceState;
30import android.telephony.TelephonyManager;
31import android.telephony.cdma.CdmaCellLocation;
32import android.telephony.gsm.GsmCellLocation;
33import android.test.suitebuilder.annotation.SmallTest;
34import android.util.Log;
35
36import com.android.ims.ImsManager;
37import com.android.internal.telephony.cdma.CdmaSubscriptionSourceManager;
38import com.android.internal.telephony.dataconnection.DcTracker;
39import com.android.internal.telephony.test.SimulatedCommands;
40import com.android.internal.telephony.test.SimulatedCommandsVerifier;
41import com.android.internal.telephony.uicc.IccCardProxy;
42import com.android.internal.telephony.uicc.UiccController;
43
44import static org.junit.Assert.*;
45import static org.mockito.Mockito.*;
46
47import org.junit.After;
48import org.junit.Before;
49import org.junit.Test;
50import org.mockito.Mock;
51import org.mockito.MockitoAnnotations;
52
53import java.lang.reflect.Field;
54import java.util.HashMap;
55
56public abstract class TelephonyTest {
57    protected static String TAG;
58
59    @Mock
60    protected Phone mPhone;
61    @Mock
62    protected ServiceStateTracker mSST;
63    @Mock
64    protected GsmCdmaCallTracker mCT;
65    @Mock
66    protected UiccController mUiccController;
67    @Mock
68    protected IccCardProxy mIccCardProxy;
69    @Mock
70    protected CallManager mCallManager;
71    @Mock
72    protected PhoneNotifier mNotifier;
73    @Mock
74    protected TelephonyComponentFactory mTelephonyComponentFactory;
75    @Mock
76    protected CdmaSubscriptionSourceManager mCdmaSSM;
77    @Mock
78    protected RegistrantList mRegistrantList;
79    @Mock
80    protected IccPhoneBookInterfaceManager mIccPhoneBookIntManager;
81    @Mock
82    protected HashMap<Integer, ImsManager> mImsManagerInstances;
83    @Mock
84    protected DcTracker mDcTracker;
85    @Mock
86    protected GsmCdmaCall mGsmCdmaCall;
87    @Mock
88    protected SubscriptionController mSubscriptionController;
89    @Mock
90    protected ServiceState mServiceState;
91    @Mock
92    protected SimulatedCommandsVerifier mSimulatedCommandsVerifier;
93    @Mock
94    protected IDeviceIdleController mIDeviceIdleController;
95
96    protected SimulatedCommands mSimulatedCommands;
97    protected ContextFixture mContextFixture;
98    protected Object mLock = new Object();
99    protected boolean mReady;
100
101    protected void waitUntilReady() {
102        while(true) {
103            synchronized (mLock) {
104                if (mReady) {
105                    break;
106                }
107            }
108        }
109    }
110
111    protected void setReady(boolean ready) {
112        synchronized (mLock) {
113            mReady = ready;
114        }
115    }
116
117    protected void setUp(String tag) throws Exception {
118        TAG = tag;
119        MockitoAnnotations.initMocks(this);
120        //Use reflection to mock singleton
121        Field field = CallManager.class.getDeclaredField("INSTANCE");
122        field.setAccessible(true);
123        field.set(null, mCallManager);
124
125        //Use reflection to mock singleton
126        field = UiccController.class.getDeclaredField("mInstance");
127        field.setAccessible(true);
128        field.set(null, mUiccController);
129
130        //Use reflection to mock singleton
131        field = CdmaSubscriptionSourceManager.class.getDeclaredField("sInstance");
132        field.setAccessible(true);
133        field.set(null, mCdmaSSM);
134
135        //Use reflection to mock singleton
136        field = ImsManager.class.getDeclaredField("sImsManagerInstances");
137        field.setAccessible(true);
138        field.set(null, mImsManagerInstances);
139
140        //Use reflection to mock singleton
141        field = SubscriptionController.class.getDeclaredField("sInstance");
142        field.setAccessible(true);
143        field.set(null, mSubscriptionController);
144
145        field = CdmaSubscriptionSourceManager.class.getDeclaredField(
146                "mCdmaSubscriptionSourceChangedRegistrants");
147        field.setAccessible(true);
148        field.set(mCdmaSSM, mRegistrantList);
149
150        field = SimulatedCommandsVerifier.class.getDeclaredField("sInstance");
151        field.setAccessible(true);
152        field.set(null, mSimulatedCommandsVerifier);
153
154        mSimulatedCommands = new SimulatedCommands();
155        mContextFixture = new ContextFixture();
156        mPhone.mCi = mSimulatedCommands;
157
158        field = TelephonyManager.class.getDeclaredField("sInstance");
159        field.setAccessible(true);
160        field.set(null, mContextFixture.getTestDouble().
161                getSystemService(Context.TELEPHONY_SERVICE));
162
163        doReturn(mSST).when(mTelephonyComponentFactory).
164                makeServiceStateTracker(any(GsmCdmaPhone.class), any(CommandsInterface.class));
165        doReturn(mIccCardProxy).when(mTelephonyComponentFactory).
166                makeIccCardProxy(any(Context.class), any(CommandsInterface.class), anyInt());
167        doReturn(mCT).when(mTelephonyComponentFactory).
168                makeGsmCdmaCallTracker(any(GsmCdmaPhone.class));
169        doReturn(mIccPhoneBookIntManager).when(mTelephonyComponentFactory).
170                makeIccPhoneBookInterfaceManager(any(Phone.class));
171        doReturn(mDcTracker).when(mTelephonyComponentFactory).
172                makeDcTracker(any(Phone.class));
173        doReturn(true).when(mImsManagerInstances).containsKey(anyInt());
174        doReturn(mIDeviceIdleController).when(mTelephonyComponentFactory).
175                getIDeviceIdleController();
176        doReturn(mContextFixture.getTestDouble()).when(mPhone).getContext();
177
178        setReady(false);
179    }
180
181    protected static void logd(String s) {
182        Log.d(TAG, s);
183    }
184}