GsmCdmaPhoneTest.java revision 8b38cd985149b459969dd2ac9e8013d852dc6760
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.app.Activity;
20import android.app.IApplicationThread;
21import android.content.IIntentReceiver;
22import android.content.Intent;
23import android.content.SharedPreferences;
24import android.os.AsyncResult;
25import android.os.Bundle;
26import android.os.Handler;
27import android.os.HandlerThread;
28import android.os.Message;
29import android.os.Process;
30import android.os.WorkSource;
31import android.preference.PreferenceManager;
32import android.telephony.CarrierConfigManager;
33import android.telephony.CellLocation;
34import android.telephony.ServiceState;
35import android.telephony.SubscriptionManager;
36import android.telephony.cdma.CdmaCellLocation;
37import android.telephony.gsm.GsmCellLocation;
38import android.test.suitebuilder.annotation.SmallTest;
39
40import com.android.internal.telephony.test.SimulatedCommands;
41import com.android.internal.telephony.uicc.IccException;
42import com.android.internal.telephony.uicc.IccRecords;
43
44import org.junit.After;
45import org.junit.Before;
46import org.junit.Test;
47import org.mockito.ArgumentCaptor;
48import org.mockito.Mock;
49
50import java.util.List;
51
52import static com.android.internal.telephony.CommandsInterface.CF_ACTION_ENABLE;
53import static com.android.internal.telephony.CommandsInterface.CF_REASON_UNCONDITIONAL;
54import static com.android.internal.telephony.TelephonyTestUtils.waitForMs;
55import static org.junit.Assert.assertEquals;
56import static org.junit.Assert.assertFalse;
57import static org.junit.Assert.assertTrue;
58import static org.junit.Assert.fail;
59import static org.mockito.Matchers.anyLong;
60import static org.mockito.Mockito.any;
61import static org.mockito.Mockito.anyBoolean;
62import static org.mockito.Mockito.anyInt;
63import static org.mockito.Mockito.anyObject;
64import static org.mockito.Mockito.anyString;
65import static org.mockito.Mockito.atLeast;
66import static org.mockito.Mockito.doReturn;
67import static org.mockito.Mockito.eq;
68import static org.mockito.Mockito.times;
69import static org.mockito.Mockito.verify;
70
71public class GsmCdmaPhoneTest extends TelephonyTest {
72    @Mock
73    private Handler mTestHandler;
74
75    //mPhoneUnderTest
76    private GsmCdmaPhone mPhoneUT;
77    private GsmCdmaPhoneTestHandler mGsmCdmaPhoneTestHandler;
78
79    private static final int EVENT_EMERGENCY_CALLBACK_MODE_EXIT = 1;
80    private static final int EVENT_EMERGENCY_CALL_TOGGLE = 2;
81
82    private class GsmCdmaPhoneTestHandler extends HandlerThread {
83
84        private GsmCdmaPhoneTestHandler(String name) {
85            super(name);
86        }
87
88        @Override
89        public void onLooperPrepared() {
90            mPhoneUT = new GsmCdmaPhone(mContext, mSimulatedCommands, mNotifier, true, 0,
91                    PhoneConstants.PHONE_TYPE_GSM, mTelephonyComponentFactory);
92            setReady(true);
93        }
94    }
95
96    private void switchToGsm() {
97        mSimulatedCommands.setVoiceRadioTech(ServiceState.RIL_RADIO_TECHNOLOGY_GSM);
98        mPhoneUT.sendMessage(mPhoneUT.obtainMessage(GsmCdmaPhone.EVENT_VOICE_RADIO_TECH_CHANGED,
99                new AsyncResult(null, new int[]{ServiceState.RIL_RADIO_TECHNOLOGY_GSM}, null)));
100        //wait for voice RAT to be updated
101        waitForMs(50);
102        assertEquals(PhoneConstants.PHONE_TYPE_GSM, mPhoneUT.getPhoneType());
103    }
104
105    private void switchToCdma() {
106        mSimulatedCommands.setVoiceRadioTech(ServiceState.RIL_RADIO_TECHNOLOGY_IS95A);
107        mPhoneUT.sendMessage(mPhoneUT.obtainMessage(GsmCdmaPhone.EVENT_VOICE_RADIO_TECH_CHANGED,
108                new AsyncResult(null, new int[]{ServiceState.RIL_RADIO_TECHNOLOGY_IS95A}, null)));
109        //wait for voice RAT to be updated
110        waitForMs(100);
111        assertEquals(PhoneConstants.PHONE_TYPE_CDMA, mPhoneUT.getPhoneType());
112    }
113
114    @Before
115    public void setUp() throws Exception {
116        super.setUp(getClass().getSimpleName());
117
118        doReturn(false).when(mSST).isDeviceShuttingDown();
119
120        mGsmCdmaPhoneTestHandler = new GsmCdmaPhoneTestHandler(TAG);
121        mGsmCdmaPhoneTestHandler.start();
122        waitUntilReady();
123        ArgumentCaptor<Integer> integerArgumentCaptor = ArgumentCaptor.forClass(Integer.class);
124        verify(mUiccController).registerForIccChanged(eq(mPhoneUT), integerArgumentCaptor.capture(),
125                anyObject());
126        Message msg = Message.obtain();
127        msg.what = integerArgumentCaptor.getValue();
128        mPhoneUT.sendMessage(msg);
129        waitForMs(50);
130    }
131
132    @After
133    public void tearDown() throws Exception {
134        mPhoneUT.removeCallbacksAndMessages(null);
135        mPhoneUT = null;
136        mGsmCdmaPhoneTestHandler.quitSafely();
137        super.tearDown();
138    }
139
140    @Test
141    @SmallTest
142    public void testPhoneTypeSwitch() {
143        assertTrue(mPhoneUT.isPhoneTypeGsm());
144        switchToCdma();
145        assertTrue(mPhoneUT.isPhoneTypeCdmaLte());
146    }
147
148    @Test
149    @SmallTest
150    public void testHandleActionCarrierConfigChanged() {
151        // set voice radio tech in RIL to 1xRTT. ACTION_CARRIER_CONFIG_CHANGED should trigger a
152        // query and change phone type
153        mSimulatedCommands.setVoiceRadioTech(ServiceState.RIL_RADIO_TECHNOLOGY_1xRTT);
154        assertTrue(mPhoneUT.isPhoneTypeGsm());
155        Intent intent = new Intent(CarrierConfigManager.ACTION_CARRIER_CONFIG_CHANGED);
156        mContext.sendBroadcast(intent);
157        waitForMs(50);
158        assertTrue(mPhoneUT.isPhoneTypeCdmaLte());
159    }
160
161    @Test
162    @SmallTest
163    public void testGetServiceState() {
164        ServiceState serviceState = new ServiceState();
165        mSST.mSS = serviceState;
166        assertEquals(serviceState, mPhoneUT.getServiceState());
167    }
168
169    @Test
170    @SmallTest
171    public void testGetCellLocation() {
172        // GSM
173        CellLocation cellLocation = new GsmCellLocation();
174        WorkSource workSource = new WorkSource(Process.myUid(),
175            mContext.getPackageName());
176        doReturn(cellLocation).when(mSST).getCellLocation(workSource);
177        assertEquals(cellLocation, mPhoneUT.getCellLocation(workSource));
178
179        // Switch to CDMA
180        switchToCdma();
181
182        CdmaCellLocation cdmaCellLocation = new CdmaCellLocation();
183        cdmaCellLocation.setCellLocationData(0, 0, 0, 0, 0);
184        mSST.mCellLoc = cdmaCellLocation;
185
186        /*
187        LOCATION_MODE is a special case in SettingsProvider. Adding the special handling in mock
188        content provider is probably not worth the effort; it will also tightly couple tests with
189        SettingsProvider implementation.
190        // LOCATION_MODE_ON
191        Settings.Secure.putInt(mContext.getContentResolver(),
192                Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_HIGH_ACCURACY);
193        waitForMs(50);
194        CdmaCellLocation actualCellLocation = (CdmaCellLocation) mPhoneUT.getCellLocation();
195        assertEquals(0, actualCellLocation.getBaseStationLatitude());
196        assertEquals(0, actualCellLocation.getBaseStationLongitude());
197
198        // LOCATION_MODE_OFF
199        Settings.Secure.putInt(TestApplication.getAppContext().getContentResolver(),
200                Settings.Secure.LOCATION_MODE, Settings.Secure.LOCATION_MODE_OFF);
201        waitForMs(50);
202        */
203
204        CdmaCellLocation actualCellLocation = (CdmaCellLocation) mPhoneUT.getCellLocation(workSource);
205        assertEquals(CdmaCellLocation.INVALID_LAT_LONG,
206                actualCellLocation.getBaseStationLatitude());
207        assertEquals(CdmaCellLocation.INVALID_LAT_LONG,
208                actualCellLocation.getBaseStationLongitude());
209    }
210
211    @Test
212    @SmallTest
213    public void testGetPhoneType() {
214        assertEquals(PhoneConstants.PHONE_TYPE_GSM, mPhoneUT.getPhoneType());
215
216        // Switch to CDMA
217        switchToCdma();
218
219        assertEquals(PhoneConstants.PHONE_TYPE_CDMA, mPhoneUT.getPhoneType());
220    }
221
222    @Test
223    @SmallTest
224    public void testGetDataConnectionState() {
225        // There are several cases possible. Testing few of them for now.
226        // 1. GSM, getCurrentDataConnectionState != STATE_IN_SERVICE, apn != APN_TYPE_EMERGENCY
227        doReturn(ServiceState.STATE_OUT_OF_SERVICE).when(mSST).getCurrentDataConnectionState();
228        assertEquals(PhoneConstants.DataState.DISCONNECTED, mPhoneUT.getDataConnectionState(
229                PhoneConstants.APN_TYPE_ALL));
230
231        // 2. GSM, getCurrentDataConnectionState != STATE_IN_SERVICE, apn = APN_TYPE_EMERGENCY, apn
232        // not connected
233        doReturn(DctConstants.State.IDLE).when(mDcTracker).getState(
234                PhoneConstants.APN_TYPE_EMERGENCY);
235        assertEquals(PhoneConstants.DataState.DISCONNECTED, mPhoneUT.getDataConnectionState(
236                PhoneConstants.APN_TYPE_EMERGENCY));
237
238        // 3. GSM, getCurrentDataConnectionState != STATE_IN_SERVICE, apn = APN_TYPE_EMERGENCY,
239        // APN is connected, callTracker state = idle
240        doReturn(DctConstants.State.CONNECTED).when(mDcTracker).getState(
241                PhoneConstants.APN_TYPE_EMERGENCY);
242        mCT.mState = PhoneConstants.State.IDLE;
243        assertEquals(PhoneConstants.DataState.CONNECTED, mPhoneUT.getDataConnectionState(
244                PhoneConstants.APN_TYPE_EMERGENCY));
245
246        // 3. GSM, getCurrentDataConnectionState != STATE_IN_SERVICE, apn = APN_TYPE_EMERGENCY,
247        // APN enabled and CONNECTED, callTracker state != idle, !isConcurrentVoiceAndDataAllowed
248        mCT.mState = PhoneConstants.State.RINGING;
249        doReturn(false).when(mSST).isConcurrentVoiceAndDataAllowed();
250        assertEquals(PhoneConstants.DataState.SUSPENDED, mPhoneUT.getDataConnectionState(
251                PhoneConstants.APN_TYPE_EMERGENCY));
252    }
253
254    @Test
255    @SmallTest
256    public void testHandleInCallMmiCommands() {
257        try {
258            // Switch to CDMA
259            switchToCdma();
260
261            assertFalse(mPhoneUT.handleInCallMmiCommands("0"));
262
263            // Switch to GSM
264            switchToGsm();
265
266            mCT.mForegroundCall = mGsmCdmaCall;
267            mCT.mBackgroundCall = mGsmCdmaCall;
268            mCT.mRingingCall = mGsmCdmaCall;
269            doReturn(GsmCdmaCall.State.IDLE).when(mGsmCdmaCall).getState();
270
271            // !isInCall
272            assertFalse(mPhoneUT.handleInCallMmiCommands("0"));
273
274            // isInCall
275            doReturn(GsmCdmaCall.State.ACTIVE).when(mGsmCdmaCall).getState();
276            assertTrue(mPhoneUT.handleInCallMmiCommands("0"));
277
278            // empty dialString
279            assertFalse(mPhoneUT.handleInCallMmiCommands(""));
280            assertFalse(mPhoneUT.handleInCallMmiCommands(null));
281
282        } catch (Exception e) {
283            fail(e.toString());
284        }
285    }
286
287    @Test
288    @SmallTest
289    public void testDial() {
290        try {
291            mSST.mSS = mServiceState;
292            doReturn(ServiceState.STATE_IN_SERVICE).when(mServiceState).getState();
293
294            mCT.mForegroundCall = mGsmCdmaCall;
295            mCT.mBackgroundCall = mGsmCdmaCall;
296            mCT.mRingingCall = mGsmCdmaCall;
297            doReturn(GsmCdmaCall.State.IDLE).when(mGsmCdmaCall).getState();
298
299            Connection connection = mPhoneUT.dial("1234567890", 0);
300            verify(mCT).dial("1234567890", null, null);
301        } catch (CallStateException e) {
302            fail();
303        }
304    }
305
306    @Test
307    @SmallTest
308    public void testHandlePinMmi() {
309        assertFalse(mPhoneUT.handlePinMmi("1234567890"));
310    }
311
312    @Test
313    @SmallTest
314    public void testSendBurstDtmf() {
315        //Should do nothing for GSM
316        mPhoneUT.sendBurstDtmf("1234567890", 0, 0, null);
317        verify(mSimulatedCommandsVerifier, times(0)).sendBurstDtmf(anyString(), anyInt(), anyInt(),
318                any(Message.class));
319
320        switchToCdma();
321        //invalid character
322        mPhoneUT.sendBurstDtmf("12345a67890", 0, 0, null);
323        verify(mSimulatedCommandsVerifier, times(0)).sendBurstDtmf(anyString(), anyInt(), anyInt(),
324                any(Message.class));
325
326        //state IDLE
327        mCT.mState = PhoneConstants.State.IDLE;
328        mPhoneUT.sendBurstDtmf("1234567890", 0, 0, null);
329        verify(mSimulatedCommandsVerifier, times(0)).sendBurstDtmf(anyString(), anyInt(), anyInt(),
330                any(Message.class));
331
332        //state RINGING
333        mCT.mState = PhoneConstants.State.RINGING;
334        mPhoneUT.sendBurstDtmf("1234567890", 0, 0, null);
335        verify(mSimulatedCommandsVerifier, times(0)).sendBurstDtmf(anyString(), anyInt(), anyInt(),
336                any(Message.class));
337
338        mCT.mState = PhoneConstants.State.OFFHOOK;
339        mPhoneUT.sendBurstDtmf("1234567890", 0, 0, null);
340        verify(mSimulatedCommandsVerifier).sendBurstDtmf("1234567890", 0, 0, null);
341    }
342
343    @Test
344    @SmallTest
345    public void testVoiceMailNumberGsm() {
346        String voiceMailNumber = "1234567890";
347        // first test for GSM
348        assertEquals(PhoneConstants.PHONE_TYPE_GSM, mPhoneUT.getPhoneType());
349
350        // no resource or sharedPreference set -- should be null
351        assertEquals(null, mPhoneUT.getVoiceMailNumber());
352
353        // voicemail number from config
354        mContextFixture.getCarrierConfigBundle().
355                putString(CarrierConfigManager.KEY_DEFAULT_VM_NUMBER_STRING, voiceMailNumber);
356        assertEquals(voiceMailNumber, mPhoneUT.getVoiceMailNumber());
357
358        // voicemail number that is explicitly set
359        voiceMailNumber = "1234567891";
360        mPhoneUT.setVoiceMailNumber("alphaTag", voiceMailNumber, null);
361        verify(mSimRecords).setVoiceMailNumber(eq("alphaTag"), eq(voiceMailNumber),
362                any(Message.class));
363
364        doReturn(voiceMailNumber).when(mSimRecords).getVoiceMailNumber();
365        assertEquals(voiceMailNumber, mPhoneUT.getVoiceMailNumber());
366    }
367
368    @Test
369    @SmallTest
370    public void testVoiceMailNumberCdma() {
371        switchToCdma();
372        String voiceMailNumber = "1234567890";
373
374        // no resource or sharedPreference set -- should be *86
375        assertEquals("*86", mPhoneUT.getVoiceMailNumber());
376
377        // config_telephony_use_own_number_for_voicemail
378        mContextFixture.putBooleanResource(
379                com.android.internal.R.bool.config_telephony_use_own_number_for_voicemail, true);
380        doReturn(voiceMailNumber).when(mSST).getMdnNumber();
381        assertEquals(voiceMailNumber, mPhoneUT.getVoiceMailNumber());
382
383        // voicemail number from config
384        voiceMailNumber = "1234567891";
385        mContextFixture.getCarrierConfigBundle().
386                putString(CarrierConfigManager.KEY_DEFAULT_VM_NUMBER_STRING, voiceMailNumber);
387        assertEquals(voiceMailNumber, mPhoneUT.getVoiceMailNumber());
388
389        // voicemail number from sharedPreference
390        mPhoneUT.setVoiceMailNumber("alphaTag", voiceMailNumber, null);
391        ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
392        verify(mRuimRecords).setVoiceMailNumber(eq("alphaTag"), eq(voiceMailNumber),
393                messageArgumentCaptor.capture());
394
395        Message msg = messageArgumentCaptor.getValue();
396        AsyncResult.forMessage(msg).exception =
397                new IccException("setVoiceMailNumber not implemented");
398        msg.sendToTarget();
399        waitForMs(50);
400
401        assertEquals(voiceMailNumber, mPhoneUT.getVoiceMailNumber());
402    }
403
404    @Test
405    @SmallTest
406    public void testVoiceMailCount() {
407        // initial value
408        assertEquals(0, mPhoneUT.getVoiceMessageCount());
409
410        // old sharedPreference set (testing upgrade from M to N scenario)
411        SharedPreferences sharedPreferences =
412                PreferenceManager.getDefaultSharedPreferences(mContext);
413        SharedPreferences.Editor editor = sharedPreferences.edit();
414        String imsi = "1234567890";
415        editor.putString("vm_id_key", imsi);
416        editor.putInt("vm_count_key", 5);
417        editor.apply();
418        doReturn(imsi).when(mSimRecords).getIMSI();
419
420        // updateVoiceMail should read old shared pref and delete it and new sharedPref should be
421        // updated now
422        doReturn(-1).when(mSimRecords).getVoiceMessageCount();
423        mPhoneUT.updateVoiceMail();
424        assertEquals(5, mPhoneUT.getVoiceMessageCount());
425        assertEquals(null, sharedPreferences.getString("vm_id_key", null));
426        assertEquals(5, sharedPreferences.getInt("vm_count_key" + mPhoneUT.getSubId(), 0));
427
428        // sim records return count as 0, that overrides shared preference
429        doReturn(0).when(mSimRecords).getVoiceMessageCount();
430        mPhoneUT.updateVoiceMail();
431        assertEquals(0, mPhoneUT.getVoiceMessageCount());
432
433        // sim records return count as -1
434        doReturn(-1).when(mSimRecords).getVoiceMessageCount();
435        mPhoneUT.updateVoiceMail();
436        assertEquals(-1, mPhoneUT.getVoiceMessageCount());
437
438        // sim records return count as -1 and sharedPreference says 0
439        mPhoneUT.setVoiceMessageCount(0);
440        mPhoneUT.updateVoiceMail();
441        assertEquals(-1, mPhoneUT.getVoiceMessageCount());
442
443        // sim records return count as -1 and sharedPreference says 2
444        mPhoneUT.setVoiceMessageCount(2);
445        mPhoneUT.updateVoiceMail();
446        assertEquals(2, mPhoneUT.getVoiceMessageCount());
447
448        // sim records return count as 0 and sharedPreference says 2
449        doReturn(0).when(mSimRecords).getVoiceMessageCount();
450        mPhoneUT.setVoiceMessageCount(2);
451        mPhoneUT.updateVoiceMail();
452        assertEquals(0, mPhoneUT.getVoiceMessageCount());
453    }
454
455    @Test
456    @SmallTest
457    public void testGetCallForwardingOption() {
458        // invalid reason (-1)
459        mPhoneUT.getCallForwardingOption(-1, null);
460        verify(mSimulatedCommandsVerifier, times(0)).queryCallForwardStatus(
461                anyInt(), anyInt(), anyString(), any(Message.class));
462
463        // valid reason
464        String imsi = "1234567890";
465        doReturn(imsi).when(mSimRecords).getIMSI();
466        mPhoneUT.getCallForwardingOption(CF_REASON_UNCONDITIONAL, null);
467        verify(mSimulatedCommandsVerifier).queryCallForwardStatus(
468                eq(CF_REASON_UNCONDITIONAL), anyInt(), anyString(), any(Message.class));
469        waitForMs(50);
470        verify(mSimRecords).setVoiceCallForwardingFlag(anyInt(), anyBoolean(), anyString());
471
472        // should have updated shared preferences
473        SharedPreferences sharedPreferences = PreferenceManager.
474                getDefaultSharedPreferences(mContext);
475        assertEquals(IccRecords.CALL_FORWARDING_STATUS_DISABLED,
476                sharedPreferences.getInt(Phone.CF_STATUS + mPhoneUT.getSubId(),
477                        IccRecords.CALL_FORWARDING_STATUS_ENABLED));
478
479        // clean up
480        SharedPreferences.Editor editor = sharedPreferences.edit();
481        editor.remove(Phone.CF_STATUS + mPhoneUT.getSubId());
482        editor.apply();
483    }
484
485    @Test
486    @SmallTest
487    public void testSetCallForwardingOption() {
488        String cfNumber = "1234567890";
489
490        // invalid action
491        mPhoneUT.setCallForwardingOption(-1, CF_REASON_UNCONDITIONAL,
492                cfNumber, 0, null);
493        verify(mSimulatedCommandsVerifier, times(0)).setCallForward(anyInt(), anyInt(), anyInt(),
494                anyString(), anyInt(), any(Message.class));
495
496        // valid action
497        mPhoneUT.setCallForwardingOption(CF_ACTION_ENABLE, CF_REASON_UNCONDITIONAL, cfNumber, 0,
498                null);
499        verify(mSimulatedCommandsVerifier).setCallForward(eq(CF_ACTION_ENABLE),
500                eq(CF_REASON_UNCONDITIONAL), anyInt(), eq(cfNumber), eq(0), any(Message.class));
501        waitForMs(50);
502        verify(mSimRecords).setVoiceCallForwardingFlag(anyInt(), anyBoolean(), eq(cfNumber));
503    }
504
505    /**
506     * GsmCdmaPhone handles a lot of messages. This function verifies behavior for messages that are
507     * received when obj is created and that are received on phone type switch
508     */
509    @Test
510    @SmallTest
511    public void testHandleInitialMessages() {
512        // EVENT_RADIO_AVAILABLE
513        verify(mSimulatedCommandsVerifier).getBasebandVersion(any(Message.class));
514        verify(mSimulatedCommandsVerifier).getIMEI(any(Message.class));
515        verify(mSimulatedCommandsVerifier).getIMEISV(any(Message.class));
516        verify(mSimulatedCommandsVerifier).getRadioCapability(any(Message.class));
517        // once as part of constructor, and once on radio available
518        verify(mSimulatedCommandsVerifier, times(2)).startLceService(anyInt(), anyBoolean(),
519                any(Message.class));
520
521        // EVENT_RADIO_ON
522        verify(mSimulatedCommandsVerifier).getVoiceRadioTechnology(any(Message.class));
523        verify(mSimulatedCommandsVerifier).setPreferredNetworkType(
524                eq(RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA), any(Message.class));
525
526        // verify responses for above requests:
527        // baseband version
528        verify(mTelephonyManager).setBasebandVersionForPhone(eq(mPhoneUT.getPhoneId()),
529                anyString());
530        // IMEI
531        assertEquals(SimulatedCommands.FAKE_IMEI, mPhoneUT.getImei());
532        // IMEISV
533        assertEquals(SimulatedCommands.FAKE_IMEISV, mPhoneUT.getDeviceSvn());
534        // radio capability
535        verify(mSimulatedCommandsVerifier).getNetworkSelectionMode(any(Message.class));
536
537        switchToCdma(); // this leads to eventRadioAvailable handling on cdma
538
539        // EVENT_RADIO_AVAILABLE
540        verify(mSimulatedCommandsVerifier, times(2)).getBasebandVersion(any(Message.class));
541        verify(mSimulatedCommandsVerifier).getDeviceIdentity(any(Message.class));
542        verify(mSimulatedCommandsVerifier, times(3)).startLceService(anyInt(), anyBoolean(),
543                any(Message.class));
544
545        // EVENT_RADIO_ON
546        verify(mSimulatedCommandsVerifier, times(2)).getVoiceRadioTechnology(any(Message.class));
547        // once on radio on, and once on get baseband version
548        verify(mSimulatedCommandsVerifier, times(3)).setPreferredNetworkType(
549                eq(RILConstants.NETWORK_MODE_LTE_CDMA_EVDO_GSM_WCDMA), any(Message.class));
550
551        // verify responses for above requests:
552        // baseband version
553        verify(mTelephonyManager, times(2)).setBasebandVersionForPhone(eq(mPhoneUT.getPhoneId()),
554                anyString());
555        // device identity
556        assertEquals(SimulatedCommands.FAKE_IMEI, mPhoneUT.getImei());
557        assertEquals(SimulatedCommands.FAKE_IMEISV, mPhoneUT.getDeviceSvn());
558        assertEquals(SimulatedCommands.FAKE_ESN, mPhoneUT.getEsn());
559        assertEquals(SimulatedCommands.FAKE_MEID, mPhoneUT.getMeid());
560    }
561
562    @Test
563    @SmallTest
564    public void testEmergencyCallbackMessages() {
565        verify(mSimulatedCommandsVerifier).setEmergencyCallbackMode(eq(mPhoneUT), anyInt(),
566                anyObject());
567        verify(mSimulatedCommandsVerifier).registerForExitEmergencyCallbackMode(eq(mPhoneUT),
568                anyInt(), anyObject());
569
570        // verify handling of emergency callback mode
571        mSimulatedCommands.notifyEmergencyCallbackMode();
572        waitForMs(50);
573
574        // verify ACTION_EMERGENCY_CALLBACK_MODE_CHANGED
575        ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class);
576        try {
577            verify(mIActivityManager, atLeast(1)).broadcastIntent(eq((IApplicationThread)null),
578                    intentArgumentCaptor.capture(),
579                    eq((String)null),
580                    eq((IIntentReceiver)null),
581                    eq(Activity.RESULT_OK),
582                    eq((String)null),
583                    eq((Bundle)null),
584                    eq((String[])null),
585                    anyInt(),
586                    eq((Bundle)null),
587                    eq(false),
588                    eq(true),
589                    anyInt());
590        } catch(Exception e) {
591            fail("Unexpected exception: " + e.getStackTrace());
592        }
593
594        Intent intent = intentArgumentCaptor.getValue();
595        assertEquals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED, intent.getAction());
596        assertEquals(true, intent.getBooleanExtra(PhoneConstants.PHONE_IN_ECM_STATE, false));
597
598        // verify that wakeLock is acquired in ECM
599        assertEquals(true, mPhoneUT.getWakeLock().isHeld());
600
601        mPhoneUT.setOnEcbModeExitResponse(mTestHandler, EVENT_EMERGENCY_CALLBACK_MODE_EXIT, null);
602        mPhoneUT.registerForEmergencyCallToggle(mTestHandler, EVENT_EMERGENCY_CALL_TOGGLE, null);
603
604        // verify handling of emergency callback mode exit
605        mSimulatedCommands.notifyExitEmergencyCallbackMode();
606        waitForMs(50);
607
608        // verify ACTION_EMERGENCY_CALLBACK_MODE_CHANGED
609        try {
610            verify(mIActivityManager, atLeast(2)).broadcastIntent(eq((IApplicationThread)null),
611                    intentArgumentCaptor.capture(),
612                    eq((String)null),
613                    eq((IIntentReceiver)null),
614                    eq(Activity.RESULT_OK),
615                    eq((String)null),
616                    eq((Bundle)null),
617                    eq((String[])null),
618                    anyInt(),
619                    eq((Bundle)null),
620                    eq(false),
621                    eq(true),
622                    anyInt());
623        } catch(Exception e) {
624            fail("Unexpected exception: " + e.getStackTrace());
625        }
626
627        intent = intentArgumentCaptor.getValue();
628        assertEquals(TelephonyIntents.ACTION_EMERGENCY_CALLBACK_MODE_CHANGED, intent.getAction());
629        assertEquals(false, intent.getBooleanExtra(PhoneConstants.PHONE_IN_ECM_STATE, true));
630
631        ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
632
633        // verify EcmExitRespRegistrant and mEmergencyCallToggledRegistrants are notified
634        verify(mTestHandler, times(2)).sendMessageAtTime(messageArgumentCaptor.capture(),
635                anyLong());
636        List<Message> msgList = messageArgumentCaptor.getAllValues();
637        assertEquals(EVENT_EMERGENCY_CALLBACK_MODE_EXIT, msgList.get(0).what);
638        assertEquals(EVENT_EMERGENCY_CALL_TOGGLE, msgList.get(1).what);
639
640        // verify setInternalDataEnabled
641        verify(mDcTracker).setInternalDataEnabled(true);
642
643        // verify wakeLock released
644        assertEquals(false, mPhoneUT.getWakeLock().isHeld());
645    }
646
647    @Test
648    @SmallTest
649    public void testCallForwardingIndicator() {
650        doReturn(IccRecords.CALL_FORWARDING_STATUS_UNKNOWN).when(mSimRecords).
651                getVoiceCallForwardingFlag();
652
653        // invalid subId
654        doReturn(SubscriptionManager.INVALID_SUBSCRIPTION_ID).when(mSubscriptionController).
655                getSubIdUsingPhoneId(anyInt());
656        assertEquals(false, mPhoneUT.getCallForwardingIndicator());
657
658        // valid subId, sharedPreference not present
659        int subId1 = 0;
660        int subId2 = 1;
661        doReturn(subId1).when(mSubscriptionController).getSubIdUsingPhoneId(anyInt());
662        assertEquals(false, mPhoneUT.getCallForwardingIndicator());
663
664        // old sharedPreference present
665        String imsi = "1234";
666        doReturn(imsi).when(mSimRecords).getIMSI();
667        SharedPreferences sp = PreferenceManager.getDefaultSharedPreferences(mContext);
668        SharedPreferences.Editor editor = sp.edit();
669        editor.putString(Phone.CF_ID, imsi);
670        editor.putInt(Phone.CF_STATUS, IccRecords.CALL_FORWARDING_STATUS_ENABLED);
671        editor.apply();
672        assertEquals(true, mPhoneUT.getCallForwardingIndicator());
673
674        // old sharedPreference should be removed now
675        assertEquals(null, sp.getString(Phone.CF_ID, null));
676        assertEquals(IccRecords.CALL_FORWARDING_STATUS_UNKNOWN,
677                sp.getInt(Phone.CF_ID, IccRecords.CALL_FORWARDING_STATUS_UNKNOWN));
678
679        // now verify value from new sharedPreference
680        assertEquals(true, mPhoneUT.getCallForwardingIndicator());
681
682        // check for another subId
683        doReturn(subId2).when(mSubscriptionController).getSubIdUsingPhoneId(anyInt());
684        assertEquals(false, mPhoneUT.getCallForwardingIndicator());
685
686        // set value for the new subId in sharedPreference
687        editor.putInt(Phone.CF_STATUS + subId2, IccRecords.CALL_FORWARDING_STATUS_ENABLED);
688        editor.apply();
689        assertEquals(true, mPhoneUT.getCallForwardingIndicator());
690
691        // switching back to previous subId, stored value should still be available
692        doReturn(subId1).when(mSubscriptionController).getSubIdUsingPhoneId(anyInt());
693        assertEquals(true, mPhoneUT.getCallForwardingIndicator());
694
695        // cleanup
696        editor.remove(Phone.CF_STATUS + subId1);
697        editor.remove(Phone.CF_STATUS + subId2);
698        editor.apply();
699    }
700
701    @Test
702    @SmallTest
703    public void testEriLoading() {
704        mPhoneUT.mEriManager = mEriManager;
705        mPhoneUT.sendMessage(mPhoneUT.obtainMessage(GsmCdmaPhone.EVENT_CARRIER_CONFIG_CHANGED,
706                null));
707        waitForMs(100);
708        verify(mEriManager, times(1)).loadEriFile();
709    }
710}
711