Remove old @Postsubmit annotation
[android/platform/frameworks/opt/telephony.git] / tests / telephonytests / src / com / android / internal / telephony / ServiceStateTrackerTest.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 com.android.internal.telephony.TelephonyTestUtils.waitForMs;
20
21 import static org.junit.Assert.assertEquals;
22 import static org.junit.Assert.assertFalse;
23 import static org.junit.Assert.assertTrue;
24 import static org.mockito.Matchers.any;
25 import static org.mockito.Matchers.anyLong;
26 import static org.mockito.Matchers.anyString;
27 import static org.mockito.Mockito.anyInt;
28 import static org.mockito.Mockito.atLeast;
29 import static org.mockito.Mockito.doReturn;
30 import static org.mockito.Mockito.eq;
31 import static org.mockito.Mockito.spy;
32 import static org.mockito.Mockito.times;
33 import static org.mockito.Mockito.verify;
34
35 import android.app.IAlarmManager;
36 import android.content.Context;
37 import android.content.Intent;
38 import android.os.AsyncResult;
39 import android.os.Bundle;
40 import android.os.Handler;
41 import android.os.HandlerThread;
42 import android.os.Message;
43 import android.os.Parcel;
44 import android.os.Process;
45 import android.os.UserHandle;
46 import android.os.WorkSource;
47 import android.support.test.filters.FlakyTest;
48 import android.telephony.CellInfo;
49 import android.telephony.CellInfoGsm;
50 import android.telephony.ServiceState;
51 import android.telephony.SignalStrength;
52 import android.telephony.SubscriptionManager;
53 import android.telephony.gsm.GsmCellLocation;
54 import android.test.suitebuilder.annotation.MediumTest;
55 import android.test.suitebuilder.annotation.SmallTest;
56 import android.util.Pair;
57
58 import com.android.internal.telephony.cdma.CdmaSubscriptionSourceManager;
59 import com.android.internal.telephony.dataconnection.DcTracker;
60 import com.android.internal.telephony.test.SimulatedCommands;
61 import com.android.internal.telephony.uicc.IccCardApplicationStatus;
62
63 import org.junit.After;
64 import org.junit.Before;
65 import org.junit.Test;
66 import org.mockito.ArgumentCaptor;
67 import org.mockito.Mock;
68
69 import java.util.ArrayList;
70
71 public class ServiceStateTrackerTest extends TelephonyTest {
72
73     @Mock
74     private DcTracker mDct;
75     @Mock
76     private ProxyController mProxyController;
77     @Mock
78     private Handler mTestHandler;
79     @Mock
80     protected IAlarmManager mAlarmManager;
81
82     private ServiceStateTracker sst;
83     private ServiceStateTrackerTestHandler mSSTTestHandler;
84
85     private static final int EVENT_REGISTERED_TO_NETWORK = 1;
86     private static final int EVENT_SUBSCRIPTION_INFO_READY = 2;
87     private static final int EVENT_ROAMING_ON = 3;
88     private static final int EVENT_ROAMING_OFF = 4;
89     private static final int EVENT_DATA_CONNECTION_ATTACHED = 5;
90     private static final int EVENT_DATA_CONNECTION_DETACHED = 6;
91     private static final int EVENT_DATA_RAT_CHANGED = 7;
92     private static final int EVENT_PS_RESTRICT_ENABLED = 8;
93     private static final int EVENT_PS_RESTRICT_DISABLED = 9;
94
95     private class ServiceStateTrackerTestHandler extends HandlerThread {
96
97         private ServiceStateTrackerTestHandler(String name) {
98             super(name);
99         }
100
101         @Override
102         public void onLooperPrepared() {
103             sst = new ServiceStateTracker(mPhone, mSimulatedCommands);
104             setReady(true);
105         }
106     }
107
108     @Before
109     public void setUp() throws Exception {
110
111         logd("ServiceStateTrackerTest +Setup!");
112         super.setUp("ServiceStateTrackerTest");
113
114         doReturn(true).when(mDct).isDisconnected();
115         mPhone.mDcTracker = mDct;
116
117         replaceInstance(ProxyController.class, "sProxyController", null, mProxyController);
118
119         mContextFixture.putStringArrayResource(
120                 com.android.internal.R.array.config_sameNamedOperatorConsideredRoaming,
121                 new String[]{"123456"});
122
123         mContextFixture.putStringArrayResource(
124                 com.android.internal.R.array.config_operatorConsideredNonRoaming,
125                 new String[]{"123456"});
126
127         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_HOME);
128         mSimulatedCommands.setVoiceRadioTech(ServiceState.RIL_RADIO_TECHNOLOGY_HSPA);
129         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_HOME);
130         mSimulatedCommands.setDataRadioTech(ServiceState.RIL_RADIO_TECHNOLOGY_HSPA);
131
132         int dds = SubscriptionManager.getDefaultDataSubscriptionId();
133         doReturn(dds).when(mPhone).getSubId();
134
135         mSSTTestHandler = new ServiceStateTrackerTestHandler(getClass().getSimpleName());
136         mSSTTestHandler.start();
137         waitUntilReady();
138         waitForMs(600);
139         logd("ServiceStateTrackerTest -Setup!");
140     }
141
142     @After
143     public void tearDown() throws Exception {
144         sst = null;
145         mSSTTestHandler.quitSafely();
146         super.tearDown();
147     }
148
149     @Test
150     @MediumTest
151     public void testSetRadioPower() {
152         boolean oldState = mSimulatedCommands.getRadioState().isOn();
153         sst.setRadioPower(!oldState);
154         waitForMs(100);
155         assertTrue(oldState != mSimulatedCommands.getRadioState().isOn());
156     }
157
158     @Test
159     @MediumTest
160     public void testSetRadioPowerFromCarrier() {
161         // Carrier disable radio power
162         sst.setRadioPowerFromCarrier(false);
163         waitForMs(100);
164         assertFalse(mSimulatedCommands.getRadioState().isOn());
165         assertTrue(sst.getDesiredPowerState());
166         assertFalse(sst.getPowerStateFromCarrier());
167
168         // User toggle radio power will not overrides carrier settings
169         sst.setRadioPower(true);
170         waitForMs(100);
171         assertFalse(mSimulatedCommands.getRadioState().isOn());
172         assertTrue(sst.getDesiredPowerState());
173         assertFalse(sst.getPowerStateFromCarrier());
174
175         // Carrier re-enable radio power
176         sst.setRadioPowerFromCarrier(true);
177         waitForMs(100);
178         assertTrue(mSimulatedCommands.getRadioState().isOn());
179         assertTrue(sst.getDesiredPowerState());
180         assertTrue(sst.getPowerStateFromCarrier());
181
182         // User toggle radio power off (airplane mode) and set carrier on
183         sst.setRadioPower(false);
184         sst.setRadioPowerFromCarrier(true);
185         waitForMs(100);
186         assertFalse(mSimulatedCommands.getRadioState().isOn());
187         assertFalse(sst.getDesiredPowerState());
188         assertTrue(sst.getPowerStateFromCarrier());
189     }
190
191     @Test
192     @MediumTest
193     public void testRilTrafficAfterSetRadioPower() {
194         sst.setRadioPower(true);
195         final int getOperatorCallCount = mSimulatedCommands.getGetOperatorCallCount();
196         final int getDataRegistrationStateCallCount =
197                 mSimulatedCommands.getGetDataRegistrationStateCallCount();
198         final int getVoiceRegistrationStateCallCount =
199                 mSimulatedCommands.getGetVoiceRegistrationStateCallCount();
200         final int getNetworkSelectionModeCallCount =
201                 mSimulatedCommands.getGetNetworkSelectionModeCallCount();
202         sst.setRadioPower(false);
203
204         waitForMs(500);
205         sst.pollState();
206         waitForMs(250);
207
208         // This test was meant to be for *no* ril traffic. However, RADIO_STATE_CHANGED is
209         // considered a modem triggered action and that causes a pollState() to be done
210         assertEquals(getOperatorCallCount + 1, mSimulatedCommands.getGetOperatorCallCount());
211         assertEquals(getDataRegistrationStateCallCount + 1,
212                 mSimulatedCommands.getGetDataRegistrationStateCallCount());
213         assertEquals(getVoiceRegistrationStateCallCount + 1,
214                 mSimulatedCommands.getGetVoiceRegistrationStateCallCount());
215         assertEquals(getNetworkSelectionModeCallCount + 1,
216                 mSimulatedCommands.getGetNetworkSelectionModeCallCount());
217
218         // Note that if the poll is triggered by a network change notification
219         // and the modem is supposed to be off, we should still do the poll
220         mSimulatedCommands.notifyNetworkStateChanged();
221         waitForMs(250);
222
223         assertEquals(getOperatorCallCount + 2 , mSimulatedCommands.getGetOperatorCallCount());
224         assertEquals(getDataRegistrationStateCallCount + 2,
225                 mSimulatedCommands.getGetDataRegistrationStateCallCount());
226         assertEquals(getVoiceRegistrationStateCallCount + 2,
227                 mSimulatedCommands.getGetVoiceRegistrationStateCallCount());
228         assertEquals(getNetworkSelectionModeCallCount + 2,
229                 mSimulatedCommands.getGetNetworkSelectionModeCallCount());
230     }
231
232     @Test
233     @MediumTest
234     public void testSpnUpdateShowPlmnOnly() {
235         doReturn(0x02).when(mSimRecords).getDisplayRule(anyString());
236         doReturn(IccCardApplicationStatus.AppState.APPSTATE_UNKNOWN).
237                 when(mUiccCardApplication3gpp).getState();
238
239         sst.sendMessage(sst.obtainMessage(ServiceStateTracker.EVENT_NETWORK_STATE_CHANGED, null));
240
241         waitForMs(750);
242
243         ArgumentCaptor<Intent> intentArgumentCaptor = ArgumentCaptor.forClass(Intent.class);
244         verify(mContextFixture.getTestDouble(), atLeast(2)).
245                 sendStickyBroadcastAsUser(intentArgumentCaptor.capture(), eq(UserHandle.ALL));
246
247         // We only want to verify the intent SPN_STRINGS_UPDATED_ACTION.
248         Intent intent = intentArgumentCaptor.getValue();
249         assertEquals(TelephonyIntents.SPN_STRINGS_UPDATED_ACTION, intent.getAction());
250
251         Bundle b = intent.getExtras();
252
253         // For boolean we need to make sure the key exists first
254         assertTrue(b.containsKey(TelephonyIntents.EXTRA_SHOW_SPN));
255         assertFalse(b.getBoolean(TelephonyIntents.EXTRA_SHOW_SPN));
256
257         assertEquals(null, b.getString(TelephonyIntents.EXTRA_SPN));
258         assertEquals(null, b.getString(TelephonyIntents.EXTRA_DATA_SPN));
259
260         // For boolean we need to make sure the key exists first
261         assertTrue(b.containsKey(TelephonyIntents.EXTRA_SHOW_PLMN));
262         assertTrue(b.getBoolean(TelephonyIntents.EXTRA_SHOW_PLMN));
263
264         assertEquals(SimulatedCommands.FAKE_LONG_NAME, b.getString(TelephonyIntents.EXTRA_PLMN));
265
266         ArgumentCaptor<Integer> intArgumentCaptor = ArgumentCaptor.forClass(Integer.class);
267         verify(mTelephonyManager).setDataNetworkTypeForPhone(anyInt(), intArgumentCaptor.capture());
268         assertEquals(ServiceState.RIL_RADIO_TECHNOLOGY_HSPA,
269                 intArgumentCaptor.getValue().intValue());
270     }
271
272     @Test
273     @MediumTest
274     public void testCellInfoList() {
275         Parcel p = Parcel.obtain();
276         p.writeInt(1);
277         p.writeInt(1);
278         p.writeInt(2);
279         p.writeLong(1453510289108L);
280         p.writeInt(310);
281         p.writeInt(260);
282         p.writeInt(123);
283         p.writeInt(456);
284         p.writeInt(99);
285         p.writeInt(3);
286         p.setDataPosition(0);
287
288         CellInfoGsm cellInfo = CellInfoGsm.CREATOR.createFromParcel(p);
289
290         ArrayList<CellInfo> list = new ArrayList();
291         list.add(cellInfo);
292         mSimulatedCommands.setCellInfoList(list);
293
294         WorkSource workSource = new WorkSource(Process.myUid(),
295                 mContext.getPackageName());
296         assertEquals(sst.getAllCellInfo(workSource), list);
297     }
298
299     @Test
300     @MediumTest
301     public void testImsRegState() {
302         // Simulate IMS registered
303         mSimulatedCommands.setImsRegistrationState(new int[]{1, PhoneConstants.PHONE_TYPE_GSM});
304
305         sst.sendMessage(sst.obtainMessage(ServiceStateTracker.EVENT_IMS_STATE_CHANGED, null));
306         waitForMs(200);
307
308         assertTrue(sst.isImsRegistered());
309
310         // Simulate IMS unregistered
311         mSimulatedCommands.setImsRegistrationState(new int[]{0, PhoneConstants.PHONE_TYPE_GSM});
312
313         sst.sendMessage(sst.obtainMessage(ServiceStateTracker.EVENT_IMS_STATE_CHANGED, null));
314         waitForMs(200);
315
316         assertFalse(sst.isImsRegistered());
317     }
318
319     @FlakyTest
320     @Test
321     @MediumTest
322     public void testSignalStrength() {
323         SignalStrength ss = new SignalStrength(
324                 30, // gsmSignalStrength
325                 0,  // gsmBitErrorRate
326                 -1, // cdmaDbm
327                 -1, // cdmaEcio
328                 -1, // evdoDbm
329                 -1, // evdoEcio
330                 -1, // evdoSnr
331                 99, // lteSignalStrength
332                 SignalStrength.INVALID,     // lteRsrp
333                 SignalStrength.INVALID,     // lteRsrq
334                 SignalStrength.INVALID,     // lteRssnr
335                 SignalStrength.INVALID,     // lteCqi
336                 SignalStrength.INVALID,     // tdScdmaRscp
337                 true                        // gsmFlag
338         );
339
340         mSimulatedCommands.setSignalStrength(ss);
341         mSimulatedCommands.notifySignalStrength();
342         waitForMs(300);
343         assertEquals(sst.getSignalStrength(), ss);
344         assertEquals(sst.getSignalStrength().isGsm(), true);
345
346         // switch to CDMA
347         doReturn(false).when(mPhone).isPhoneTypeGsm();
348         doReturn(true).when(mPhone).isPhoneTypeCdmaLte();
349         sst.updatePhoneType();
350         sst.mSS.setRilDataRadioTechnology(ServiceState.RIL_RADIO_TECHNOLOGY_LTE);
351
352         mSimulatedCommands.notifySignalStrength();
353         waitForMs(200);
354         assertEquals(sst.getSignalStrength(), ss);
355         assertEquals(sst.getSignalStrength().isGsm(), true);
356
357         // notify signal strength again, but this time data RAT is not LTE
358         sst.mSS.setRilDataRadioTechnology(ServiceState.RIL_RADIO_TECHNOLOGY_EHRPD);
359         mSimulatedCommands.notifySignalStrength();
360         waitForMs(200);
361         assertEquals(sst.getSignalStrength(), ss);
362         assertEquals(sst.getSignalStrength().isGsm(), false);
363     }
364
365     @Test
366     @MediumTest
367     public void testGsmCellLocation() {
368
369         sst.sendMessage(sst.obtainMessage(ServiceStateTracker.EVENT_GET_LOC_DONE,
370                 new AsyncResult(null, new String[]{"1", "2", "3", "4", "5", "6", "7", "8", "9",
371                         "10", "11", "12", "13", "14", "15"}, null)));
372
373         waitForMs(200);
374         WorkSource workSource = new WorkSource(Process.myUid(),
375                 mContext.getPackageName());
376         GsmCellLocation cl = (GsmCellLocation) sst.getCellLocation(workSource);
377         assertEquals(2, cl.getLac());
378         assertEquals(3, cl.getCid());
379     }
380
381     @Test
382     @MediumTest
383     public void testUpdatePhoneType() {
384         doReturn(false).when(mPhone).isPhoneTypeGsm();
385         doReturn(true).when(mPhone).isPhoneTypeCdmaLte();
386         doReturn(CdmaSubscriptionSourceManager.SUBSCRIPTION_FROM_RUIM).when(mCdmaSSM).
387                 getCdmaSubscriptionSource();
388
389         logd("Calling updatePhoneType");
390         // switch to CDMA
391         sst.updatePhoneType();
392
393         ArgumentCaptor<Integer> integerArgumentCaptor = ArgumentCaptor.forClass(Integer.class);
394         verify(mRuimRecords).registerForRecordsLoaded(eq(sst), integerArgumentCaptor.capture(),
395                 any(Object.class));
396
397         // response for mRuimRecords.registerForRecordsLoaded()
398         Message msg = Message.obtain();
399         msg.what = integerArgumentCaptor.getValue();
400         msg.obj = new AsyncResult(null, null, null);
401         sst.sendMessage(msg);
402         waitForMs(100);
403
404         // on RUIM_RECORDS_LOADED, sst is expected to call following apis
405         verify(mRuimRecords, times(1)).isProvisioned();
406
407         // switch back to GSM
408         doReturn(true).when(mPhone).isPhoneTypeGsm();
409         doReturn(false).when(mPhone).isPhoneTypeCdmaLte();
410
411         // response for mRuimRecords.registerForRecordsLoaded() can be sent after switching to GSM
412         msg = Message.obtain();
413         msg.what = integerArgumentCaptor.getValue();
414         msg.obj = new AsyncResult(null, null, null);
415         sst.sendMessage(msg);
416
417         // There's no easy way to check if the msg was handled or discarded. Wait to make sure sst
418         // did not crash, and then verify that the functions called records loaded are not called
419         // again
420         waitForMs(200);
421
422         verify(mRuimRecords, times(1)).isProvisioned();
423     }
424
425     @Test
426     @MediumTest
427     public void testRegAndUnregForVoiceRoamingOn() throws Exception {
428         sst.registerForVoiceRoamingOn(mTestHandler, EVENT_ROAMING_ON, null);
429
430         // Enable roaming and trigger events to notify handler registered
431         doReturn(true).when(mPhone).isPhoneTypeGsm();
432         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_ROAMING);
433         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_ROAMING);
434         mSimulatedCommands.notifyNetworkStateChanged();
435
436         waitForMs(200);
437
438         // verify if registered handler has message posted to it
439         ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
440         verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong());
441         assertEquals(EVENT_ROAMING_ON, messageArgumentCaptor.getValue().what);
442
443         // Disable roaming
444         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_HOME);
445         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_HOME);
446         mSimulatedCommands.notifyNetworkStateChanged();
447
448         waitForMs(100);
449
450         // Unregister registrant
451         sst.unregisterForVoiceRoamingOn(mTestHandler);
452
453         // Enable roaming
454         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_ROAMING);
455         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_ROAMING);
456         mSimulatedCommands.notifyNetworkStateChanged();
457
458         waitForMs(200);
459
460         // verify that no new message posted to handler
461         verify(mTestHandler, times(1)).sendMessageAtTime(any(Message.class), anyLong());
462     }
463
464     @Test
465     @MediumTest
466     public void testRegAndUnregForVoiceRoamingOff() throws Exception {
467         // Enable roaming
468         doReturn(true).when(mPhone).isPhoneTypeGsm();
469         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_ROAMING);
470         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_ROAMING);
471         mSimulatedCommands.notifyNetworkStateChanged();
472
473         waitForMs(100);
474
475         sst.registerForVoiceRoamingOff(mTestHandler, EVENT_ROAMING_OFF, null);
476
477         // Disable roaming
478         doReturn(true).when(mPhone).isPhoneTypeGsm();
479         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_HOME);
480         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_HOME);
481         mSimulatedCommands.notifyNetworkStateChanged();
482
483         waitForMs(200);
484
485         // verify if registered handler has message posted to it
486         ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
487         verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong());
488         assertEquals(EVENT_ROAMING_OFF, messageArgumentCaptor.getValue().what);
489
490         // Enable roaming
491         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_ROAMING);
492         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_ROAMING);
493         mSimulatedCommands.notifyNetworkStateChanged();
494
495         waitForMs(100);
496
497         // Unregister registrant
498         sst.unregisterForVoiceRoamingOff(mTestHandler);
499
500         // Disable roaming
501         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_HOME);
502         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_HOME);
503         mSimulatedCommands.notifyNetworkStateChanged();
504
505         waitForMs(100);
506
507         // verify that no new message posted to handler
508         verify(mTestHandler, times(1)).sendMessageAtTime(any(Message.class), anyLong());
509     }
510
511     @Test
512     @MediumTest
513     public void testRegAndUnregForDataRoamingOn() throws Exception {
514         sst.registerForDataRoamingOn(mTestHandler, EVENT_ROAMING_ON, null);
515
516         // Enable roaming and trigger events to notify handler registered
517         doReturn(true).when(mPhone).isPhoneTypeGsm();
518         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_ROAMING);
519         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_ROAMING);
520         mSimulatedCommands.notifyNetworkStateChanged();
521
522         waitForMs(200);
523
524         // verify if registered handler has message posted to it
525         ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
526         verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong());
527         assertEquals(EVENT_ROAMING_ON, messageArgumentCaptor.getValue().what);
528
529         // Disable roaming
530         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_HOME);
531         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_HOME);
532         mSimulatedCommands.notifyNetworkStateChanged();
533
534         waitForMs(100);
535
536         // Unregister registrant
537         sst.unregisterForDataRoamingOn(mTestHandler);
538
539         // Enable roaming
540         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_ROAMING);
541         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_ROAMING);
542         mSimulatedCommands.notifyNetworkStateChanged();
543
544         waitForMs(200);
545
546         // verify that no new message posted to handler
547         verify(mTestHandler, times(1)).sendMessageAtTime(any(Message.class), anyLong());
548     }
549
550     @Test
551     @MediumTest
552     public void testRegAndUnregForDataRoamingOff() throws Exception {
553         // Enable roaming
554         doReturn(true).when(mPhone).isPhoneTypeGsm();
555         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_ROAMING);
556         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_ROAMING);
557         mSimulatedCommands.notifyNetworkStateChanged();
558
559         waitForMs(100);
560
561         sst.registerForDataRoamingOff(mTestHandler, EVENT_ROAMING_OFF, null);
562
563         // Disable roaming
564         doReturn(true).when(mPhone).isPhoneTypeGsm();
565         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_HOME);
566         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_HOME);
567         mSimulatedCommands.notifyNetworkStateChanged();
568
569         waitForMs(100);
570
571         // verify if registered handler has message posted to it
572         ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
573         verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong());
574         assertEquals(EVENT_ROAMING_OFF, messageArgumentCaptor.getValue().what);
575
576         // Enable roaming
577         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_ROAMING);
578         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_ROAMING);
579         mSimulatedCommands.notifyNetworkStateChanged();
580
581         waitForMs(100);
582
583         // Unregister registrant
584         sst.unregisterForDataRoamingOff(mTestHandler);
585
586         // Disable roaming
587         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_HOME);
588         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_HOME);
589         mSimulatedCommands.notifyNetworkStateChanged();
590
591         waitForMs(100);
592
593         // verify that no new message posted to handler
594         verify(mTestHandler, times(1)).sendMessageAtTime(any(Message.class), anyLong());
595     }
596
597     @Test
598     @MediumTest
599     public void testRegAndUnregForDataConnAttach() throws Exception {
600         // Initially set service state out of service
601         doReturn(true).when(mPhone).isPhoneTypeGsm();
602         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_UNKNOWN);
603         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_UNKNOWN);
604         mSimulatedCommands.notifyNetworkStateChanged();
605
606         waitForMs(100);
607
608         sst.registerForDataConnectionAttached(mTestHandler, EVENT_DATA_CONNECTION_ATTACHED, null);
609
610         // set service state in service and trigger events to post message on handler
611         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_ROAMING);
612         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_ROAMING);
613         mSimulatedCommands.notifyNetworkStateChanged();
614
615         waitForMs(200);
616
617         // verify if registered handler has message posted to it
618         ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
619         verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong());
620         assertEquals(EVENT_DATA_CONNECTION_ATTACHED, messageArgumentCaptor.getValue().what);
621
622         // set service state out of service
623         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_UNKNOWN);
624         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_UNKNOWN);
625         mSimulatedCommands.notifyNetworkStateChanged();
626
627         waitForMs(100);
628
629         // Unregister registrant
630         sst.unregisterForDataConnectionAttached(mTestHandler);
631
632         // set service state in service
633         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_ROAMING);
634         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_ROAMING);
635         mSimulatedCommands.notifyNetworkStateChanged();
636
637         waitForMs(100);
638
639         // verify that no new message posted to handler
640         verify(mTestHandler, times(1)).sendMessageAtTime(any(Message.class), anyLong());
641     }
642
643     @Test
644     @MediumTest
645     public void testRegAndUnregForDataConnDetach() throws Exception {
646         // Initially set service state in service
647         doReturn(true).when(mPhone).isPhoneTypeGsm();
648         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_ROAMING);
649         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_ROAMING);
650         mSimulatedCommands.notifyNetworkStateChanged();
651
652         sst.registerForDataConnectionDetached(mTestHandler, EVENT_DATA_CONNECTION_DETACHED, null);
653
654         // set service state out of service and trigger events to post message on handler
655         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_UNKNOWN);
656         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_UNKNOWN);
657         mSimulatedCommands.notifyNetworkStateChanged();
658
659         waitForMs(200);
660
661         // verify if registered handler has message posted to it
662         ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
663         verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong());
664         assertEquals(EVENT_DATA_CONNECTION_DETACHED, messageArgumentCaptor.getValue().what);
665
666         // set service state in service
667         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_ROAMING);
668         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_ROAMING);
669         mSimulatedCommands.notifyNetworkStateChanged();
670
671         waitForMs(100);
672
673         // Unregister registrant
674         sst.unregisterForDataConnectionDetached(mTestHandler);
675
676         // set service state out of service
677         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_UNKNOWN);
678         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_UNKNOWN);
679         mSimulatedCommands.notifyNetworkStateChanged();
680
681         waitForMs(100);
682
683         // verify that no new message posted to handler
684         verify(mTestHandler, times(1)).sendMessageAtTime(any(Message.class), anyLong());
685     }
686
687     @Test
688     @MediumTest
689     public void testRegisterForDataRegStateOrRatChange() {
690         int drs = sst.mSS.RIL_REG_STATE_HOME;
691         int rat = sst.mSS.RIL_RADIO_TECHNOLOGY_LTE;
692         sst.mSS.setRilDataRadioTechnology(rat);
693         sst.mSS.setDataRegState(drs);
694         sst.registerForDataRegStateOrRatChanged(mTestHandler, EVENT_DATA_RAT_CHANGED, null);
695
696         waitForMs(100);
697
698         // Verify if message was posted to handler and value of result
699         ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
700         verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong());
701         assertEquals(EVENT_DATA_RAT_CHANGED, messageArgumentCaptor.getValue().what);
702         assertEquals(new Pair<Integer, Integer>(drs, rat),
703                 ((AsyncResult)messageArgumentCaptor.getValue().obj).result);
704     }
705
706     @Test
707     @MediumTest
708     public void testRegAndUnregForNetworkAttached() throws Exception {
709         // Initially set service state out of service
710         doReturn(true).when(mPhone).isPhoneTypeGsm();
711         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_UNKNOWN);
712         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_UNKNOWN);
713         mSimulatedCommands.notifyNetworkStateChanged();
714
715         waitForMs(100);
716
717         sst.registerForNetworkAttached(mTestHandler, EVENT_REGISTERED_TO_NETWORK, null);
718
719         // set service state in service and trigger events to post message on handler
720         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_ROAMING);
721         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_ROAMING);
722         mSimulatedCommands.notifyNetworkStateChanged();
723
724         waitForMs(100);
725
726         // verify if registered handler has message posted to it
727         ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
728         verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong());
729         assertEquals(EVENT_REGISTERED_TO_NETWORK, messageArgumentCaptor.getValue().what);
730
731         // set service state out of service
732         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_UNKNOWN);
733         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_UNKNOWN);
734         mSimulatedCommands.notifyNetworkStateChanged();
735
736         waitForMs(100);
737
738         // Unregister registrant
739         sst.unregisterForNetworkAttached(mTestHandler);
740
741         // set service state in service
742         mSimulatedCommands.setVoiceRegState(ServiceState.RIL_REG_STATE_ROAMING);
743         mSimulatedCommands.setDataRegState(ServiceState.RIL_REG_STATE_ROAMING);
744         mSimulatedCommands.notifyNetworkStateChanged();
745
746         waitForMs(100);
747
748         // verify that no new message posted to handler
749         verify(mTestHandler, times(1)).sendMessageAtTime(any(Message.class), anyLong());
750     }
751
752     @Test
753     @MediumTest
754     public void testRegisterForPsRestrictedEnabled() throws Exception {
755         sst.mRestrictedState.setPsRestricted(true);
756         // Since PsRestricted is set to true, registerForPsRestrictedEnabled will
757         // also post message to handler
758         sst.registerForPsRestrictedEnabled(mTestHandler, EVENT_PS_RESTRICT_ENABLED, null);
759
760         waitForMs(100);
761
762         // verify posted message
763         ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
764         verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong());
765         assertEquals(EVENT_PS_RESTRICT_ENABLED, messageArgumentCaptor.getValue().what);
766     }
767
768     @Test
769     @MediumTest
770     public void testRegisterForPsRestrictedDisabled() throws Exception {
771         sst.mRestrictedState.setPsRestricted(true);
772         // Since PsRestricted is set to true, registerForPsRestrictedDisabled will
773         // also post message to handler
774         sst.registerForPsRestrictedDisabled(mTestHandler, EVENT_PS_RESTRICT_DISABLED, null);
775
776         waitForMs(100);
777
778         // verify posted message
779         ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
780         verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong());
781         assertEquals(EVENT_PS_RESTRICT_DISABLED, messageArgumentCaptor.getValue().what);
782     }
783
784     @Test
785     @MediumTest
786     public void testOnRestrictedStateChanged() throws Exception {
787         ServiceStateTracker spySst = spy(sst);
788         doReturn(true).when(mPhone).isPhoneTypeGsm();
789         doReturn(IccCardApplicationStatus.AppState.APPSTATE_READY).when(
790                 mUiccCardApplication3gpp).getState();
791         spySst.updatePhoneType();
792
793         // Combination of restricted state and expected notification type.
794         final int CS_ALL[] = {RILConstants.RIL_RESTRICTED_STATE_CS_ALL,
795                 ServiceStateTracker.CS_ENABLED};
796         final int CS_NOR[] = {RILConstants.RIL_RESTRICTED_STATE_CS_NORMAL,
797                 ServiceStateTracker.CS_NORMAL_ENABLED};
798         final int CS_EME[] = {RILConstants.RIL_RESTRICTED_STATE_CS_EMERGENCY,
799                 ServiceStateTracker.CS_EMERGENCY_ENABLED};
800         final int CS_NON[] = {RILConstants.RIL_RESTRICTED_STATE_NONE,
801                 ServiceStateTracker.CS_DISABLED};
802         final int PS_ALL[] = {RILConstants.RIL_RESTRICTED_STATE_PS_ALL,
803                 ServiceStateTracker.PS_ENABLED};
804         final int PS_NON[] = {RILConstants.RIL_RESTRICTED_STATE_NONE,
805                 ServiceStateTracker.PS_DISABLED};
806
807         int notifyCount = 0;
808         // cs not restricted -> cs emergency/normal restricted
809         internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_ALL);
810         // cs emergency/normal restricted -> cs normal restricted
811         internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_NOR);
812         // cs normal restricted -> cs emergency restricted
813         internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_EME);
814         // cs emergency restricted -> cs not restricted
815         internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_NON);
816         // cs not restricted -> cs normal restricted
817         internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_NOR);
818         // cs normal restricted -> cs emergency/normal restricted
819         internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_ALL);
820         // cs emergency/normal restricted -> cs emergency restricted
821         internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_EME);
822         // cs emergency restricted -> cs emergency/normal restricted
823         internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_ALL);
824         // cs emergency/normal restricted -> cs not restricted
825         internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_NON);
826         // cs not restricted -> cs emergency restricted
827         internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_EME);
828         // cs emergency restricted -> cs normal restricted
829         internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_NOR);
830         // cs normal restricted -> cs not restricted
831         internalCheckForRestrictedStateChange(spySst, ++notifyCount, CS_NON);
832
833         // ps not restricted -> ps restricted
834         internalCheckForRestrictedStateChange(spySst, ++notifyCount, PS_ALL);
835         // ps restricted -> ps not restricted
836         internalCheckForRestrictedStateChange(spySst, ++notifyCount, PS_NON);
837     }
838
839     private void internalCheckForRestrictedStateChange(ServiceStateTracker serviceStateTracker,
840                 int times, int[] restrictedState) {
841         mSimulatedCommands.triggerRestrictedStateChanged(restrictedState[0]);
842         waitForMs(100);
843         ArgumentCaptor<Integer> intArgumentCaptor = ArgumentCaptor.forClass(Integer.class);
844         verify(serviceStateTracker, times(times)).setNotification(intArgumentCaptor.capture());
845         assertEquals(intArgumentCaptor.getValue().intValue(), restrictedState[1]);
846     }
847
848     @Test
849     @MediumTest
850     public void testRegisterForSubscriptionInfoReady() {
851         sst.registerForSubscriptionInfoReady(mTestHandler, EVENT_SUBSCRIPTION_INFO_READY, null);
852
853         // Call functions which would trigger posting of message on test handler
854         doReturn(false).when(mPhone).isPhoneTypeGsm();
855         sst.updatePhoneType();
856         mSimulatedCommands.notifyOtaProvisionStatusChanged();
857
858         waitForMs(200);
859
860         // verify posted message
861         ArgumentCaptor<Message> messageArgumentCaptor = ArgumentCaptor.forClass(Message.class);
862         verify(mTestHandler).sendMessageAtTime(messageArgumentCaptor.capture(), anyLong());
863         assertEquals(EVENT_SUBSCRIPTION_INFO_READY, messageArgumentCaptor.getValue().what);
864     }
865
866     @Test
867     @SmallTest
868     public void testGetDesiredPowerState() {
869         sst.setRadioPower(true);
870         assertEquals(sst.getDesiredPowerState(), true);
871     }
872
873     @Test
874     @MediumTest
875     public void testEnableLocationUpdates() throws Exception {
876         sst.enableLocationUpdates();
877         verify(mSimulatedCommandsVerifier, times(1)).setLocationUpdates(eq(true),
878                 any(Message.class));
879     }
880
881     @Test
882     @SmallTest
883     public void testDisableLocationUpdates() throws Exception {
884         sst.disableLocationUpdates();
885         verify(mSimulatedCommandsVerifier, times(1)).setLocationUpdates(eq(false),
886                 any(Message.class));
887     }
888
889     @Test
890     @SmallTest
891     public void testGetCurrentDataRegState() throws Exception {
892         sst.mSS.setDataRegState(ServiceState.STATE_OUT_OF_SERVICE);
893         assertEquals(sst.getCurrentDataConnectionState(), ServiceState.STATE_OUT_OF_SERVICE);
894     }
895
896     @Test
897     @SmallTest
898     public void testIsConcurrentVoiceAndDataAllowed() {
899         // Verify all 3 branches in the function isConcurrentVoiceAndDataAllowed
900         doReturn(true).when(mPhone).isPhoneTypeGsm();
901         sst.mSS.setRilVoiceRadioTechnology(sst.mSS.RIL_RADIO_TECHNOLOGY_HSPA);
902         assertEquals(true, sst.isConcurrentVoiceAndDataAllowed());
903
904         doReturn(false).when(mPhone).isPhoneTypeGsm();
905         doReturn(true).when(mPhone).isPhoneTypeCdma();
906         assertEquals(false, sst.isConcurrentVoiceAndDataAllowed());
907
908         doReturn(false).when(mPhone).isPhoneTypeGsm();
909         doReturn(false).when(mPhone).isPhoneTypeCdma();
910         sst.mSS.setCssIndicator(1);
911         assertEquals(true, sst.isConcurrentVoiceAndDataAllowed());
912         sst.mSS.setCssIndicator(0);
913         assertEquals(false, sst.isConcurrentVoiceAndDataAllowed());
914     }
915
916     @Test
917     @MediumTest
918     public void testIsImsRegistered() throws Exception {
919         mSimulatedCommands.setImsRegistrationState(new int[]{1, PhoneConstants.PHONE_TYPE_GSM});
920         mSimulatedCommands.notifyImsNetworkStateChanged();
921         waitForMs(200);
922         assertEquals(sst.isImsRegistered(), true);
923     }
924
925     @Test
926     @SmallTest
927     public void testIsDeviceShuttingDown() throws Exception {
928         sst.requestShutdown();
929         assertEquals(true, sst.isDeviceShuttingDown());
930     }
931
932     @Test
933     @SmallTest
934     public void testSetTimeFromNITZStr() throws Exception {
935         doReturn(mAlarmManager).when(mIBinder).queryLocalInterface(anyString());
936         mServiceManagerMockedServices.put(Context.ALARM_SERVICE, mIBinder);
937
938         // Mock sending incorrect nitz str from RIL
939         mSimulatedCommands.triggerNITZupdate("38/06/20,00:00:00+0");
940         waitForMs(200);
941         // AlarmManger.setTime is triggered by SystemClock.setCurrentTimeMillis().
942         // Verify system time is not set to incorrect NITZ time
943         verify(mAlarmManager, times(0)).setTime(anyLong());
944
945         // Mock sending correct nitz str from RIL
946         mSimulatedCommands.triggerNITZupdate("15/06/20,00:00:00+0");
947         waitForMs(200);
948         verify(mAlarmManager, times(1)).setTime(anyLong());
949     }
950 }