GsmCdmaPhoneTest.java revision 01d7b95381a5247520e7fe4ac40d10bcced525cf
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.Intent;
20import android.content.SharedPreferences;
21import android.os.AsyncResult;
22import android.os.HandlerThread;
23import android.os.Message;
24import android.provider.Settings;
25import android.telephony.CarrierConfigManager;
26import android.telephony.CellLocation;
27import android.telephony.ServiceState;
28import android.telephony.cdma.CdmaCellLocation;
29import android.telephony.gsm.GsmCellLocation;
30import android.test.suitebuilder.annotation.SmallTest;
31
32import com.android.internal.telephony.uicc.IccException;
33
34import static com.android.internal.telephony.TelephonyTestUtils.waitForMs;
35import static org.junit.Assert.*;
36import static org.mockito.Mockito.*;
37
38import org.junit.After;
39import org.junit.Before;
40import org.junit.Test;
41import org.mockito.ArgumentCaptor;
42
43public class GsmCdmaPhoneTest extends TelephonyTest {
44    //mPhoneUnderTest
45    private GsmCdmaPhone mPhoneUT;
46
47    private class GsmCdmaPhoneTestHandler extends HandlerThread {
48
49        private GsmCdmaPhoneTestHandler(String name) {
50            super(name);
51        }
52
53        @Override
54        public void onLooperPrepared() {
55            mPhoneUT = new GsmCdmaPhone(mContext, mSimulatedCommands, mNotifier, true, 0,
56                    PhoneConstants.PHONE_TYPE_GSM, mTelephonyComponentFactory);
57            setReady(true);
58        }
59    }
60
61    private void switchToGsm() {
62        mSimulatedCommands.setVoiceRadioTech(ServiceState.RIL_RADIO_TECHNOLOGY_GSM);
63        mPhoneUT.sendMessage(mPhoneUT.obtainMessage(GsmCdmaPhone.EVENT_VOICE_RADIO_TECH_CHANGED,
64                new AsyncResult(null, new int[]{ServiceState.RIL_RADIO_TECHNOLOGY_GSM}, null)));
65        //wait for voice RAT to be updated
66        waitForMs(50);
67        assertEquals(PhoneConstants.PHONE_TYPE_GSM, mPhoneUT.getPhoneType());
68    }
69
70    private void switchToCdma() {
71        mSimulatedCommands.setVoiceRadioTech(ServiceState.RIL_RADIO_TECHNOLOGY_IS95A);
72        mPhoneUT.sendMessage(mPhoneUT.obtainMessage(GsmCdmaPhone.EVENT_VOICE_RADIO_TECH_CHANGED,
73                new AsyncResult(null, new int[]{ServiceState.RIL_RADIO_TECHNOLOGY_IS95A}, null)));
74        //wait for voice RAT to be updated
75        waitForMs(50);
76        assertEquals(PhoneConstants.PHONE_TYPE_CDMA, mPhoneUT.getPhoneType());
77    }
78
79    @Before
80    public void setUp() throws Exception {
81        super.setUp("GsmCdmaPhoneTest");
82
83        doReturn(false).when(mSST).isDeviceShuttingDown();
84
85        new GsmCdmaPhoneTestHandler(TAG).start();
86        waitUntilReady();
87        ArgumentCaptor<Integer> integerArgumentCaptor = ArgumentCaptor.forClass(Integer.class);
88        verify(mUiccController).registerForIccChanged(eq(mPhoneUT), integerArgumentCaptor.capture(),
89                anyObject());
90        Message msg = Message.obtain();
91        msg.what = integerArgumentCaptor.getValue();
92        mPhoneUT.sendMessage(msg);
93        waitForMs(50);
94    }
95
96    @After
97    public void tearDown() throws Exception {
98        mPhoneUT.removeCallbacksAndMessages(null);
99        mPhoneUT = null;
100        super.tearDown();
101    }
102
103    @Test
104    @SmallTest
105    public void testPhoneTypeSwitch() {
106        assertTrue(mPhoneUT.isPhoneTypeGsm());
107        switchToCdma();
108        assertTrue(mPhoneUT.isPhoneTypeCdmaLte());
109    }
110
111    @Test
112    @SmallTest
113    public void testHandleActionCarrierConfigChanged() {
114        // set voice radio tech in RIL to 1xRTT. ACTION_CARRIER_CONFIG_CHANGED should trigger a
115        // query and change phone type
116        mSimulatedCommands.setVoiceRadioTech(ServiceState.RIL_RADIO_TECHNOLOGY_1xRTT);
117        assertTrue(mPhoneUT.isPhoneTypeGsm());
118        Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
119        mContext.sendBroadcast(intent);
120        waitForMs(50);
121        assertTrue(mPhoneUT.isPhoneTypeCdmaLte());
122    }
123
124    @Test
125    @SmallTest
126    public void testGetServiceState() {
127        ServiceState serviceState = new ServiceState();
128        mSST.mSS = serviceState;
129        assertEquals(serviceState, mPhoneUT.getServiceState());
130    }
131
132    @Test
133    @SmallTest
134    public void testGetCellLocation() {
135        // GSM
136        CellLocation cellLocation = new GsmCellLocation();
137        doReturn(cellLocation).when(mSST).getCellLocation();
138        assertEquals(cellLocation, mPhoneUT.getCellLocation());
139
140        // Switch to CDMA
141        switchToCdma();
142
143        CdmaCellLocation cdmaCellLocation = new CdmaCellLocation();
144        cdmaCellLocation.setCellLocationData(0, 0, 0, 0, 0);
145        mSST.mCellLoc = cdmaCellLocation;
146
147        int origValue = Settings.Secure.getInt(TestApplication.getAppContext().getContentResolver(),
148                Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
149
150        // LOCATION_MODE_ON
151        Settings.Secure.putInt(TestApplication.getAppContext().getContentResolver(),
152                Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_HIGH_ACCURACY);
153        CdmaCellLocation actualCellLocation = (CdmaCellLocation) mPhoneUT.getCellLocation();
154        assertEquals(0, actualCellLocation.getBaseStationLatitude());
155        assertEquals(0, actualCellLocation.getBaseStationLongitude());
156
157        // LOCATION_MODE_OFF
158        Settings.Secure.putInt(TestApplication.getAppContext().getContentResolver(),
159                Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
160        actualCellLocation = (CdmaCellLocation) mPhoneUT.getCellLocation();
161        assertEquals(CdmaCellLocation.INVALID_LAT_LONG,
162                actualCellLocation.getBaseStationLatitude());
163        assertEquals(CdmaCellLocation.INVALID_LAT_LONG,
164                actualCellLocation.getBaseStationLongitude());
165
166        // reset to origValue
167        Settings.Secure.putInt(TestApplication.getAppContext().getContentResolver(),
168                Settings.Secure.LOCATION_MODE, origValue);
169    }
170
171    @Test
172    @SmallTest
173    public void testGetPhoneType() {
174        assertEquals(PhoneConstants.PHONE_TYPE_GSM, mPhoneUT.getPhoneType());
175
176        // Switch to CDMA
177        switchToCdma();
178
179        assertEquals(PhoneConstants.PHONE_TYPE_CDMA, mPhoneUT.getPhoneType());
180    }
181
182    @Test
183    @SmallTest
184    public void testGetDataConnectionState() {
185        // There are several cases possible. Testing few of them for now.
186        // 1. GSM, getCurrentDataConnectionState != STATE_IN_SERVICE, apn != APN_TYPE_EMERGENCY
187        doReturn(ServiceState.STATE_OUT_OF_SERVICE).when(mSST).getCurrentDataConnectionState();
188        assertEquals(PhoneConstants.DataState.DISCONNECTED, mPhoneUT.getDataConnectionState(
189                PhoneConstants.APN_TYPE_ALL));
190
191        // 2. GSM, getCurrentDataConnectionState != STATE_IN_SERVICE, apn = APN_TYPE_EMERGENCY, apn
192        // not enabled and not active
193        assertEquals(PhoneConstants.DataState.DISCONNECTED, mPhoneUT.getDataConnectionState(
194                PhoneConstants.APN_TYPE_EMERGENCY));
195
196        // 3. GSM, getCurrentDataConnectionState != STATE_IN_SERVICE, apn = APN_TYPE_EMERGENCY,
197        // APN enabled, active and CONNECTED, callTracker state = idle
198        doReturn(true).when(mDcTracker).isApnTypeEnabled(PhoneConstants.APN_TYPE_EMERGENCY);
199        doReturn(true).when(mDcTracker).isApnTypeActive(PhoneConstants.APN_TYPE_EMERGENCY);
200        doReturn(DctConstants.State.CONNECTED).when(mDcTracker).getState(
201                PhoneConstants.APN_TYPE_EMERGENCY);
202        mCT.mState = PhoneConstants.State.IDLE;
203        assertEquals(PhoneConstants.DataState.CONNECTED, mPhoneUT.getDataConnectionState(
204                PhoneConstants.APN_TYPE_EMERGENCY));
205
206        // 3. GSM, getCurrentDataConnectionState != STATE_IN_SERVICE, apn = APN_TYPE_EMERGENCY,
207        // APN enabled and CONNECTED, callTracker state != idle, !isConcurrentVoiceAndDataAllowed
208        mCT.mState = PhoneConstants.State.RINGING;
209        doReturn(false).when(mSST).isConcurrentVoiceAndDataAllowed();
210        assertEquals(PhoneConstants.DataState.SUSPENDED, mPhoneUT.getDataConnectionState(
211                PhoneConstants.APN_TYPE_EMERGENCY));
212    }
213
214    @Test
215    @SmallTest
216    public void testHandleInCallMmiCommands() {
217        try {
218            // Switch to CDMA
219            switchToCdma();
220
221            assertFalse(mPhoneUT.handleInCallMmiCommands("0"));
222
223            // Switch to GSM
224            switchToGsm();
225
226            mCT.mForegroundCall = mGsmCdmaCall;
227            mCT.mBackgroundCall = mGsmCdmaCall;
228            mCT.mRingingCall = mGsmCdmaCall;
229            doReturn(GsmCdmaCall.State.IDLE).when(mGsmCdmaCall).getState();
230
231            // !isInCall
232            assertFalse(mPhoneUT.handleInCallMmiCommands("0"));
233
234            // isInCall
235            doReturn(GsmCdmaCall.State.ACTIVE).when(mGsmCdmaCall).getState();
236            assertTrue(mPhoneUT.handleInCallMmiCommands("0"));
237
238            // empty dialString
239            assertFalse(mPhoneUT.handleInCallMmiCommands(""));
240            assertFalse(mPhoneUT.handleInCallMmiCommands(null));
241
242        } catch (Exception e) {
243            fail(e.toString());
244        }
245    }
246
247    @Test
248    @SmallTest
249    public void testDial() {
250        try {
251            mSST.mSS = mServiceState;
252            doReturn(ServiceState.STATE_IN_SERVICE).when(mServiceState).getState();
253
254            mCT.mForegroundCall = mGsmCdmaCall;
255            mCT.mBackgroundCall = mGsmCdmaCall;
256            mCT.mRingingCall = mGsmCdmaCall;
257            doReturn(GsmCdmaCall.State.IDLE).when(mGsmCdmaCall).getState();
258
259            Connection connection = mPhoneUT.dial("1234567890", 0);
260            verify(mCT).dial("1234567890", null, null);
261        } catch (CallStateException e) {
262            fail();
263        }
264    }
265
266    @Test
267    @SmallTest
268    public void testHandlePinMmi() {
269        assertFalse(mPhoneUT.handlePinMmi("1234567890"));
270    }
271
272    @Test
273    @SmallTest
274    public void testSendBurstDtmf() {
275        //Should do nothing for GSM
276        mPhoneUT.sendBurstDtmf("1234567890", 0, 0, null);
277        verify(mSimulatedCommandsVerifier, times(0)).sendBurstDtmf(anyString(), anyInt(), anyInt(),
278                any(Message.class));
279
280        switchToCdma();
281        //invalid character
282        mPhoneUT.sendBurstDtmf("12345a67890", 0, 0, null);
283        verify(mSimulatedCommandsVerifier, times(0)).sendBurstDtmf(anyString(), anyInt(), anyInt(),
284                any(Message.class));
285
286        //state IDLE
287        mCT.mState = PhoneConstants.State.IDLE;
288        mPhoneUT.sendBurstDtmf("1234567890", 0, 0, null);
289        verify(mSimulatedCommandsVerifier, times(0)).sendBurstDtmf(anyString(), anyInt(), anyInt(),
290                any(Message.class));
291
292        //state RINGING
293        mCT.mState = PhoneConstants.State.RINGING;
294        mPhoneUT.sendBurstDtmf("1234567890", 0, 0, null);
295        verify(mSimulatedCommandsVerifier, times(0)).sendBurstDtmf(anyString(), anyInt(), anyInt(),
296                any(Message.class));
297
298        mCT.mState = PhoneConstants.State.OFFHOOK;
299        mPhoneUT.sendBurstDtmf("1234567890", 0, 0, null);
300        verify(mSimulatedCommandsVerifier).sendBurstDtmf("1234567890", 0, 0, null);
301    }
302
303    @Test
304    @SmallTest
305    public void testVoiceMailNumberGsm() {
306        String voiceMailNumber = "1234567890";
307        // first test for GSM
308        assertEquals(PhoneConstants.PHONE_TYPE_GSM, mPhoneUT.getPhoneType());
309
310        // no resource or sharedPreference set -- should be null
311        assertEquals(null, mPhoneUT.getVoiceMailNumber());
312
313        // voicemail number from config
314        mContextFixture.putStringArrayResource(
315                com.android.internal.R.array.config_default_vm_number,
316                new String[]{voiceMailNumber});
317        assertEquals(voiceMailNumber, mPhoneUT.getVoiceMailNumber());
318
319        // voicemail number that is explicitly set
320        voiceMailNumber = "1234567891";
321        mPhoneUT.setVoiceMailNumber("alphaTag", voiceMailNumber, null);
322        verify(mSimRecords).setVoiceMailNumber(eq("alphaTag"), eq(voiceMailNumber),
323                any(Message.class));
324
325        doReturn(voiceMailNumber).when(mSimRecords).getVoiceMailNumber();
326        assertEquals(voiceMailNumber, mPhoneUT.getVoiceMailNumber());
327    }
328
329    @Test
330    @SmallTest
331    public void testVoiceMailNumberCdma() {
332        switchToCdma();
333        String voiceMailNumber = "1234567890";
334
335        // no resource or sharedPreference set -- should be *86
336        assertEquals("*86", mPhoneUT.getVoiceMailNumber());
337
338        // config_telephony_use_own_number_for_voicemail
339        mContextFixture.putBooleanResource(
340                com.android.internal.R.bool.config_telephony_use_own_number_for_voicemail, true);
341        doReturn(voiceMailNumber).when(mSimRecords).getMsisdnNumber();
342        assertEquals(voiceMailNumber, mPhoneUT.getVoiceMailNumber());
343
344        // voicemail number from config
345        voiceMailNumber = "1234567891";
346        mContextFixture.putStringArrayResource(
347                com.android.internal.R.array.config_default_vm_number,
348                new String[]{voiceMailNumber});
349        assertEquals(voiceMailNumber, mPhoneUT.getVoiceMailNumber());
350
351        // voicemail number from sharedPreference
352        mPhoneUT.setVoiceMailNumber("alphaTag", voiceMailNumber, null);
353        ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
354        verify(mRuimRecords).setVoiceMailNumber(eq("alphaTag"), eq(voiceMailNumber),
355                messageArgumentCaptor.capture());
356
357        Message msg = messageArgumentCaptor.getValue();
358        AsyncResult.forMessage(msg).exception =
359                new IccException("setVoiceMailNumber not implemented");
360        msg.sendToTarget();
361        waitForMs(50);
362
363        assertEquals(voiceMailNumber, mPhoneUT.getVoiceMailNumber());
364    }
365}