• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 package org.robolectric.shadows;
2 
3 import static android.content.Context.TELEPHONY_SERVICE;
4 import static android.os.Build.VERSION_CODES.LOLLIPOP_MR1;
5 import static android.os.Build.VERSION_CODES.M;
6 import static android.os.Build.VERSION_CODES.N;
7 import static android.os.Build.VERSION_CODES.O;
8 import static android.os.Build.VERSION_CODES.P;
9 import static android.os.Build.VERSION_CODES.Q;
10 import static android.os.Build.VERSION_CODES.R;
11 import static android.os.Build.VERSION_CODES.S;
12 import static android.os.Build.VERSION_CODES.UPSIDE_DOWN_CAKE;
13 import static android.telephony.PhoneStateListener.LISTEN_CALL_STATE;
14 import static android.telephony.PhoneStateListener.LISTEN_CELL_INFO;
15 import static android.telephony.PhoneStateListener.LISTEN_CELL_LOCATION;
16 import static android.telephony.PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED;
17 import static android.telephony.PhoneStateListener.LISTEN_SERVICE_STATE;
18 import static android.telephony.PhoneStateListener.LISTEN_SIGNAL_STRENGTHS;
19 import static android.telephony.TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA;
20 import static android.telephony.TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE;
21 import static android.telephony.TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA;
22 import static android.telephony.TelephonyManager.CALL_COMPOSER_STATUS_ON;
23 import static android.telephony.TelephonyManager.CALL_STATE_IDLE;
24 import static android.telephony.TelephonyManager.CALL_STATE_OFFHOOK;
25 import static android.telephony.TelephonyManager.CALL_STATE_RINGING;
26 import static android.telephony.TelephonyManager.NETWORK_TYPE_EVDO_0;
27 import static android.telephony.TelephonyManager.NETWORK_TYPE_LTE;
28 import static android.telephony.emergency.EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE;
29 import static android.telephony.emergency.EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_POLICE;
30 import static com.google.common.truth.Truth.assertThat;
31 import static com.google.common.util.concurrent.MoreExecutors.directExecutor;
32 import static org.junit.Assert.assertEquals;
33 import static org.junit.Assert.assertFalse;
34 import static org.junit.Assert.assertNotEquals;
35 import static org.junit.Assert.assertNotNull;
36 import static org.junit.Assert.assertThrows;
37 import static org.junit.Assert.assertTrue;
38 import static org.mockito.Mockito.mock;
39 import static org.mockito.Mockito.reset;
40 import static org.mockito.Mockito.times;
41 import static org.mockito.Mockito.verify;
42 import static org.mockito.Mockito.verifyNoMoreInteractions;
43 import static org.mockito.Mockito.withSettings;
44 import static org.robolectric.RuntimeEnvironment.getApplication;
45 import static org.robolectric.Shadows.shadowOf;
46 import static org.robolectric.shadows.ShadowTelephonyManager.createTelephonyDisplayInfo;
47 
48 import android.Manifest.permission;
49 import android.app.Activity;
50 import android.app.Application;
51 import android.content.ComponentName;
52 import android.content.Context;
53 import android.content.Intent;
54 import android.net.Uri;
55 import android.os.Build;
56 import android.os.PersistableBundle;
57 import android.telecom.PhoneAccountHandle;
58 import android.telephony.CarrierRestrictionRules;
59 import android.telephony.CellInfo;
60 import android.telephony.CellLocation;
61 import android.telephony.PhoneCapability;
62 import android.telephony.PhoneStateListener;
63 import android.telephony.ServiceState;
64 import android.telephony.SignalStrength;
65 import android.telephony.SubscriptionManager;
66 import android.telephony.TelephonyCallback;
67 import android.telephony.TelephonyCallback.CallStateListener;
68 import android.telephony.TelephonyCallback.CellInfoListener;
69 import android.telephony.TelephonyCallback.CellLocationListener;
70 import android.telephony.TelephonyCallback.DisplayInfoListener;
71 import android.telephony.TelephonyCallback.ServiceStateListener;
72 import android.telephony.TelephonyCallback.SignalStrengthsListener;
73 import android.telephony.TelephonyDisplayInfo;
74 import android.telephony.TelephonyManager;
75 import android.telephony.TelephonyManager.BootstrapAuthenticationCallback;
76 import android.telephony.TelephonyManager.CellInfoCallback;
77 import android.telephony.UiccSlotInfo;
78 import android.telephony.VisualVoicemailSmsFilterSettings;
79 import android.telephony.emergency.EmergencyNumber;
80 import android.telephony.gba.UaSecurityProtocolIdentifier;
81 import androidx.test.core.app.ApplicationProvider;
82 import androidx.test.ext.junit.runners.AndroidJUnit4;
83 import com.google.common.collect.ImmutableList;
84 import com.google.common.collect.ImmutableMap;
85 import java.util.Collections;
86 import java.util.List;
87 import java.util.Locale;
88 import java.util.concurrent.CountDownLatch;
89 import java.util.concurrent.Executor;
90 import java.util.concurrent.TimeUnit;
91 import org.junit.After;
92 import org.junit.Before;
93 import org.junit.Test;
94 import org.junit.runner.RunWith;
95 import org.robolectric.Robolectric;
96 import org.robolectric.android.controller.ActivityController;
97 import org.robolectric.annotation.Config;
98 import org.robolectric.shadow.api.Shadow;
99 import org.robolectric.shadows.ShadowSubscriptionManager.SubscriptionInfoBuilder;
100 import org.robolectric.util.ReflectionHelpers;
101 import org.robolectric.util.ReflectionHelpers.ClassParameter;
102 
103 @RunWith(AndroidJUnit4.class)
104 public class ShadowTelephonyManagerTest {
105 
106   private TelephonyManager telephonyManager;
107   private ShadowTelephonyManager shadowTelephonyManager;
108   private TelephonyManager tmForSub5;
109   private String defaultNetworkCountryIso;
110 
111   @Before
setUp()112   public void setUp() throws Exception {
113     telephonyManager = (TelephonyManager) getApplication().getSystemService(TELEPHONY_SERVICE);
114     shadowTelephonyManager = Shadow.extract(telephonyManager);
115     shadowOf((Application) ApplicationProvider.getApplicationContext())
116         .grantPermissions(permission.READ_PRIVILEGED_PHONE_STATE);
117     tmForSub5 = newTelephonyManager(5);
118     defaultNetworkCountryIso = shadowOf(telephonyManager).getNetworkCountryIso();
119   }
120 
121   @After
tearDown()122   public void tearDown() {
123     shadowOf(telephonyManager).setNetworkCountryIso(defaultNetworkCountryIso);
124   }
125 
newTelephonyManager(Integer subId)126   private TelephonyManager newTelephonyManager(Integer subId) {
127     Class<?>[] parameters;
128     Object[] arguments;
129     if (subId == null || Build.VERSION.SDK_INT < Build.VERSION_CODES.N) {
130       parameters = new Class<?>[] {Context.class};
131       arguments = new Object[] {ApplicationProvider.getApplicationContext()};
132     } else {
133       parameters = new Class<?>[] {Context.class, int.class};
134       arguments = new Object[] {ApplicationProvider.getApplicationContext(), subId};
135     }
136     TelephonyManager newInstance =
137         Shadow.newInstance(TelephonyManager.class, parameters, arguments);
138     if (subId != null) {
139       shadowTelephonyManager.setTelephonyManagerForSubscriptionId(subId, newInstance);
140     }
141     return newInstance;
142   }
143 
144   @Test
testListenInit()145   public void testListenInit() {
146     PhoneStateListener listener = mock(PhoneStateListener.class);
147     telephonyManager.listen(listener, LISTEN_CALL_STATE | LISTEN_CELL_INFO | LISTEN_CELL_LOCATION);
148 
149     verify(listener).onCallStateChanged(CALL_STATE_IDLE, null);
150     verify(listener).onCellLocationChanged(null);
151     verify(listener).onCellInfoChanged(Collections.emptyList());
152   }
153 
154   @Test
155   @Config(minSdk = S)
registerTelephonyCallback_shouldInitCallback()156   public void registerTelephonyCallback_shouldInitCallback() {
157     TelephonyCallback callback =
158         mock(
159             TelephonyCallback.class,
160             withSettings()
161                 .extraInterfaces(
162                     CallStateListener.class, CellInfoListener.class, CellLocationListener.class));
163     telephonyManager.registerTelephonyCallback(directExecutor(), callback);
164 
165     assertThat(shadowTelephonyManager.getLastTelephonyCallback()).isEqualTo(callback);
166     verify((CallStateListener) callback).onCallStateChanged(CALL_STATE_IDLE);
167     verify((CellInfoListener) callback).onCellInfoChanged(ImmutableList.of());
168     verify((CellLocationListener) callback).onCellLocationChanged(null);
169   }
170 
171   @Test
172   @Config(minSdk = S)
unregisterTelephonyCallback_shouldRemoveCallback()173   public void unregisterTelephonyCallback_shouldRemoveCallback() {
174     TelephonyCallback callback =
175         mock(TelephonyCallback.class, withSettings().extraInterfaces(CallStateListener.class));
176     telephonyManager.registerTelephonyCallback(directExecutor(), callback);
177     reset(callback);
178     telephonyManager.unregisterTelephonyCallback(callback);
179 
180     shadowOf(telephonyManager).setCallState(CALL_STATE_RINGING, "123");
181 
182     verifyNoMoreInteractions(callback);
183   }
184 
185   @Test
shouldGiveDeviceId()186   public void shouldGiveDeviceId() {
187     String testId = "TESTING123";
188     shadowOf(telephonyManager).setDeviceId(testId);
189     assertEquals(testId, telephonyManager.getDeviceId());
190   }
191 
192   @Test
193   @Config(minSdk = M)
setDeviceId_withSlot_doesNotAffectCallingInstance()194   public void setDeviceId_withSlot_doesNotAffectCallingInstance() {
195     String testId = "TESTING123";
196     shadowOf(telephonyManager).setDeviceId(123, testId);
197     assertThat(telephonyManager.getDeviceId()).isNull();
198     assertThat(telephonyManager.getDeviceId(123)).isEqualTo(testId);
199   }
200 
201   @Test
202   @Config(minSdk = M)
shouldGiveDeviceIdForSlot()203   public void shouldGiveDeviceIdForSlot() {
204     shadowOf(telephonyManager).setDeviceId(1, "device in slot 1");
205     shadowOf(telephonyManager).setDeviceId(2, "device in slot 2");
206 
207     assertEquals("device in slot 1", telephonyManager.getDeviceId(1));
208     assertEquals("device in slot 2", telephonyManager.getDeviceId(2));
209   }
210 
211   @Test
shouldGiveDeviceSoftwareVersion()212   public void shouldGiveDeviceSoftwareVersion() {
213     String testSoftwareVersion = "getDeviceSoftwareVersion";
214     shadowOf(telephonyManager).setDeviceSoftwareVersion(testSoftwareVersion);
215     assertEquals(testSoftwareVersion, telephonyManager.getDeviceSoftwareVersion());
216   }
217 
218   @Test
219   @Config(minSdk = O)
getImei()220   public void getImei() {
221     String testImei = "4test imei";
222     shadowOf(telephonyManager).setImei(testImei);
223     assertEquals(testImei, telephonyManager.getImei());
224   }
225 
226   @Test
227   @Config(minSdk = O)
getImeiForSlot()228   public void getImeiForSlot() {
229     shadowOf(telephonyManager).setImei("defaultImei");
230     shadowOf(telephonyManager).setImei(0, "imei0");
231     shadowOf(telephonyManager).setImei(1, "imei1");
232     assertEquals("imei0", telephonyManager.getImei(0));
233     assertEquals("imei1", telephonyManager.getImei(1));
234   }
235 
236   @Test
237   @Config(minSdk = O)
setImei_withSlotId_acceptsNull()238   public void setImei_withSlotId_acceptsNull() {
239     shadowOf(telephonyManager).setImei(0, "imei0");
240     shadowOf(telephonyManager).setImei(0, null);
241     assertEquals(null, telephonyManager.getImei(0));
242   }
243 
244   @Test
245   @Config(minSdk = O)
getMeid()246   public void getMeid() {
247     String testMeid = "4test meid";
248     shadowOf(telephonyManager).setMeid(testMeid);
249     assertEquals(testMeid, telephonyManager.getMeid());
250   }
251 
252   @Test
253   @Config(minSdk = O)
getMeidForSlot()254   public void getMeidForSlot() {
255     shadowOf(telephonyManager).setMeid("defaultMeid");
256     shadowOf(telephonyManager).setMeid(0, "meid0");
257     shadowOf(telephonyManager).setMeid(1, "meid1");
258     assertEquals("meid0", telephonyManager.getMeid(0));
259     assertEquals("meid1", telephonyManager.getMeid(1));
260     assertEquals("defaultMeid", telephonyManager.getMeid());
261   }
262 
263   @Test
shouldGiveNetworkOperatorName()264   public void shouldGiveNetworkOperatorName() {
265     shadowOf(telephonyManager).setNetworkOperatorName("SomeOperatorName");
266     assertEquals("SomeOperatorName", telephonyManager.getNetworkOperatorName());
267   }
268 
269   @Test
shouldGiveSimOperatorName()270   public void shouldGiveSimOperatorName() {
271     shadowOf(telephonyManager).setSimOperatorName("SomeSimOperatorName");
272     assertEquals("SomeSimOperatorName", telephonyManager.getSimOperatorName());
273   }
274 
275   @Test(expected = SecurityException.class)
276   public void
getSimSerialNumber_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted()277       getSimSerialNumber_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted()
278           throws Exception {
279     shadowOf(telephonyManager).setReadPhoneStatePermission(false);
280     telephonyManager.getSimSerialNumber();
281   }
282 
283   @Test
shouldGetSimSerialNumber()284   public void shouldGetSimSerialNumber() {
285     shadowOf(telephonyManager).setSimSerialNumber("SomeSerialNumber");
286     assertEquals("SomeSerialNumber", telephonyManager.getSimSerialNumber());
287   }
288 
289   @Test
shouldGiveNetworkType()290   public void shouldGiveNetworkType() {
291     shadowOf(telephonyManager).setNetworkType(TelephonyManager.NETWORK_TYPE_CDMA);
292     assertEquals(TelephonyManager.NETWORK_TYPE_CDMA, telephonyManager.getNetworkType());
293   }
294 
295   @Test
296   @Config(minSdk = N)
shouldGiveDataNetworkType()297   public void shouldGiveDataNetworkType() {
298     shadowOf(telephonyManager).setDataNetworkType(TelephonyManager.NETWORK_TYPE_CDMA);
299     assertEquals(TelephonyManager.NETWORK_TYPE_CDMA, telephonyManager.getDataNetworkType());
300   }
301 
302   @Test
303   @Config(minSdk = N)
shouldGiveVoiceNetworkType()304   public void shouldGiveVoiceNetworkType() {
305     shadowOf(telephonyManager).setVoiceNetworkType(TelephonyManager.NETWORK_TYPE_CDMA);
306     assertThat(telephonyManager.getVoiceNetworkType())
307         .isEqualTo(TelephonyManager.NETWORK_TYPE_CDMA);
308   }
309 
310   @Test
shouldGiveAllCellInfo()311   public void shouldGiveAllCellInfo() {
312     PhoneStateListener listener = mock(PhoneStateListener.class);
313     telephonyManager.listen(listener, LISTEN_CELL_INFO);
314 
315     List<CellInfo> allCellInfo = Collections.singletonList(mock(CellInfo.class));
316     shadowOf(telephonyManager).setAllCellInfo(allCellInfo);
317     assertEquals(allCellInfo, telephonyManager.getAllCellInfo());
318     verify(listener).onCellInfoChanged(allCellInfo);
319   }
320 
321   @Test
322   @Config(minSdk = S)
shouldGiveAllCellInfo_toCallback()323   public void shouldGiveAllCellInfo_toCallback() {
324     TelephonyCallback callback =
325         mock(TelephonyCallback.class, withSettings().extraInterfaces(CellInfoListener.class));
326     telephonyManager.registerTelephonyCallback(directExecutor(), callback);
327 
328     List<CellInfo> allCellInfo = Collections.singletonList(mock(CellInfo.class));
329     shadowOf(telephonyManager).setAllCellInfo(allCellInfo);
330     assertEquals(allCellInfo, telephonyManager.getAllCellInfo());
331     verify((CellInfoListener) callback).onCellInfoChanged(allCellInfo);
332   }
333 
334   @Test
335   @Config(minSdk = Q)
shouldGiveCellInfoUpdate()336   public void shouldGiveCellInfoUpdate() throws Exception {
337     List<CellInfo> callbackCellInfo = Collections.singletonList(mock(CellInfo.class));
338     shadowOf(telephonyManager).setCallbackCellInfos(callbackCellInfo);
339     assertNotEquals(callbackCellInfo, telephonyManager.getAllCellInfo());
340 
341     CountDownLatch callbackLatch = new CountDownLatch(1);
342     shadowOf(telephonyManager)
343         .requestCellInfoUpdate(
344             new Executor() {
345               @Override
346               public void execute(Runnable r) {
347                 r.run();
348               }
349             },
350             new CellInfoCallback() {
351               @Override
352               public void onCellInfo(List<CellInfo> list) {
353                 assertEquals(callbackCellInfo, list);
354                 callbackLatch.countDown();
355               }
356             });
357 
358     assertTrue(callbackLatch.await(5000, TimeUnit.MILLISECONDS));
359   }
360 
361   @Test
shouldGiveNetworkCountryIsoInLowercase()362   public void shouldGiveNetworkCountryIsoInLowercase() {
363     shadowOf(telephonyManager).setNetworkCountryIso("SomeIso");
364     assertEquals("someiso", telephonyManager.getNetworkCountryIso());
365   }
366 
367   @Test
shouldGiveEmptyStringForNetworkCountryIsoWhenNotSet()368   public void shouldGiveEmptyStringForNetworkCountryIsoWhenNotSet() {
369     assertThat(telephonyManager.getNetworkCountryIso()).isEmpty();
370   }
371 
372   @Test
373   @Config(minSdk = Q)
shouldGiveSimLocale()374   public void shouldGiveSimLocale() {
375     shadowOf(telephonyManager).setSimLocale(Locale.FRANCE);
376     assertEquals(Locale.FRANCE, telephonyManager.getSimLocale());
377   }
378 
379   @Test
shouldGiveNetworkOperator()380   public void shouldGiveNetworkOperator() {
381     shadowOf(telephonyManager).setNetworkOperator("SomeOperator");
382     assertEquals("SomeOperator", telephonyManager.getNetworkOperator());
383   }
384 
385   @Test
386   @Config(minSdk = O)
shouldGiveNetworkSpecifier()387   public void shouldGiveNetworkSpecifier() {
388     shadowOf(telephonyManager).setNetworkSpecifier("SomeSpecifier");
389     assertEquals("SomeSpecifier", telephonyManager.getNetworkSpecifier());
390   }
391 
392   @Test
shouldGiveLine1Number()393   public void shouldGiveLine1Number() {
394     shadowOf(telephonyManager).setLine1Number("123-244-2222");
395     assertEquals("123-244-2222", telephonyManager.getLine1Number());
396   }
397 
398   @Test
shouldGiveGroupIdLevel1()399   public void shouldGiveGroupIdLevel1() {
400     shadowOf(telephonyManager).setGroupIdLevel1("SomeGroupId");
401     assertEquals("SomeGroupId", telephonyManager.getGroupIdLevel1());
402   }
403 
404   @Test(expected = SecurityException.class)
getDeviceId_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted()405   public void getDeviceId_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted()
406       throws Exception {
407     shadowOf(telephonyManager).setReadPhoneStatePermission(false);
408     telephonyManager.getDeviceId();
409   }
410 
411   @Test
412   @Config(minSdk = M)
413   public void
getDeviceIdForSlot_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted()414       getDeviceIdForSlot_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted()
415           throws Exception {
416     shadowOf(telephonyManager).setReadPhoneStatePermission(false);
417     assertThrows(SecurityException.class, () -> telephonyManager.getDeviceId(1));
418   }
419 
420   @Test
421   public void
getDeviceSoftwareVersion_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted()422       getDeviceSoftwareVersion_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted()
423           throws Exception {
424     shadowOf(telephonyManager).setReadPhoneStatePermission(false);
425     assertThrows(SecurityException.class, () -> telephonyManager.getDeviceSoftwareVersion());
426   }
427 
428   @Test
shouldGivePhoneType()429   public void shouldGivePhoneType() {
430     shadowOf(telephonyManager).setPhoneType(TelephonyManager.PHONE_TYPE_CDMA);
431     assertEquals(TelephonyManager.PHONE_TYPE_CDMA, telephonyManager.getPhoneType());
432     shadowOf(telephonyManager).setPhoneType(TelephonyManager.PHONE_TYPE_GSM);
433     assertEquals(TelephonyManager.PHONE_TYPE_GSM, telephonyManager.getPhoneType());
434   }
435 
436   @Test
437   @Config(minSdk = M)
setCurrentPhoneType_fromPhoneId_canBeReadFromAnyInstance()438   public void setCurrentPhoneType_fromPhoneId_canBeReadFromAnyInstance() {
439     shadowOf(telephonyManager).setCurrentPhoneType(123, TelephonyManager.PHONE_TYPE_CDMA);
440 
441     assertEquals(TelephonyManager.PHONE_TYPE_CDMA, tmForSub5.getCurrentPhoneType(123));
442   }
443 
444   @Test
shouldGiveCellLocation()445   public void shouldGiveCellLocation() {
446     PhoneStateListener listener = mock(PhoneStateListener.class);
447     telephonyManager.listen(listener, LISTEN_CELL_LOCATION);
448 
449     CellLocation mockCellLocation = mock(CellLocation.class);
450     shadowOf(telephonyManager).setCellLocation(mockCellLocation);
451     assertEquals(mockCellLocation, telephonyManager.getCellLocation());
452     verify(listener).onCellLocationChanged(mockCellLocation);
453   }
454 
455   @Test
456   @Config(minSdk = S)
shouldGiveCellLocation_toCallback()457   public void shouldGiveCellLocation_toCallback() {
458     TelephonyCallback callback =
459         mock(TelephonyCallback.class, withSettings().extraInterfaces(CellLocationListener.class));
460     telephonyManager.registerTelephonyCallback(directExecutor(), callback);
461 
462     CellLocation mockCellLocation = mock(CellLocation.class);
463     shadowOf(telephonyManager).setCellLocation(mockCellLocation);
464     assertEquals(mockCellLocation, telephonyManager.getCellLocation());
465     verify((CellLocationListener) callback).onCellLocationChanged(mockCellLocation);
466   }
467 
468   @Test
469   @Config(minSdk = S)
shouldGiveCallStateForSubscription()470   public void shouldGiveCallStateForSubscription() {
471     PhoneStateListener listener = mock(PhoneStateListener.class);
472     telephonyManager.listen(listener, LISTEN_CALL_STATE);
473 
474     shadowOf(telephonyManager).setCallState(CALL_STATE_RINGING, "911");
475     assertEquals(CALL_STATE_RINGING, telephonyManager.getCallStateForSubscription());
476     verify(listener).onCallStateChanged(CALL_STATE_RINGING, "911");
477 
478     shadowOf(telephonyManager).setCallState(CALL_STATE_OFFHOOK, "911");
479     assertEquals(CALL_STATE_OFFHOOK, telephonyManager.getCallStateForSubscription());
480     verify(listener).onCallStateChanged(CALL_STATE_OFFHOOK, null);
481   }
482 
483   @Test
shouldGiveCallState()484   public void shouldGiveCallState() {
485     PhoneStateListener listener = mock(PhoneStateListener.class);
486     telephonyManager.listen(listener, LISTEN_CALL_STATE);
487 
488     shadowOf(telephonyManager).setCallState(CALL_STATE_RINGING, "911");
489     assertEquals(CALL_STATE_RINGING, telephonyManager.getCallState());
490     verify(listener).onCallStateChanged(CALL_STATE_RINGING, "911");
491 
492     shadowOf(telephonyManager).setCallState(CALL_STATE_OFFHOOK, "911");
493     assertEquals(CALL_STATE_OFFHOOK, telephonyManager.getCallState());
494     verify(listener).onCallStateChanged(CALL_STATE_OFFHOOK, null);
495   }
496 
497   @Test
498   @Config(minSdk = S)
shouldGiveCallState_toCallback()499   public void shouldGiveCallState_toCallback() {
500     TelephonyCallback callback =
501         mock(TelephonyCallback.class, withSettings().extraInterfaces(CallStateListener.class));
502     telephonyManager.registerTelephonyCallback(directExecutor(), callback);
503 
504     shadowOf(telephonyManager).setCallState(CALL_STATE_RINGING, "911");
505     assertEquals(CALL_STATE_RINGING, telephonyManager.getCallState());
506     verify((CallStateListener) callback).onCallStateChanged(CALL_STATE_RINGING);
507 
508     shadowOf(telephonyManager).setCallState(CALL_STATE_OFFHOOK, "911");
509     assertEquals(CALL_STATE_OFFHOOK, telephonyManager.getCallState());
510     verify((CallStateListener) callback).onCallStateChanged(CALL_STATE_OFFHOOK);
511   }
512 
513   @Test
isSmsCapable()514   public void isSmsCapable() {
515     assertThat(telephonyManager.isSmsCapable()).isTrue();
516     shadowOf(telephonyManager).setIsSmsCapable(false);
517     assertThat(telephonyManager.isSmsCapable()).isFalse();
518   }
519 
520   @Test
521   @Config(minSdk = R)
setSmsCapable_modifiesAllInstances()522   public void setSmsCapable_modifiesAllInstances() {
523     shadowOf(telephonyManager).setIsSmsCapable(false);
524     newTelephonyManager(123);
525     assertThat(telephonyManager.createForSubscriptionId(123).isSmsCapable()).isFalse();
526   }
527 
528   @Test
529   @Config(minSdk = O)
shouldGiveCarrierConfigIfSet()530   public void shouldGiveCarrierConfigIfSet() {
531     PersistableBundle bundle = new PersistableBundle();
532     bundle.putInt("foo", 42);
533     shadowOf(telephonyManager).setCarrierConfig(bundle);
534 
535     assertEquals(bundle, telephonyManager.getCarrierConfig());
536   }
537 
538   @Test
539   @Config(minSdk = O)
shouldGiveNonNullCarrierConfigIfNotSet()540   public void shouldGiveNonNullCarrierConfigIfNotSet() {
541     assertNotNull(telephonyManager.getCarrierConfig());
542   }
543 
544   @Test
shouldGiveVoiceMailNumber()545   public void shouldGiveVoiceMailNumber() {
546     shadowOf(telephonyManager).setVoiceMailNumber("123");
547 
548     assertEquals("123", telephonyManager.getVoiceMailNumber());
549   }
550 
551   @Test
shouldGiveVoiceMailAlphaTag()552   public void shouldGiveVoiceMailAlphaTag() {
553     shadowOf(telephonyManager).setVoiceMailAlphaTag("tag");
554 
555     assertEquals("tag", telephonyManager.getVoiceMailAlphaTag());
556   }
557 
558   @Test
559   @Config(minSdk = M)
shouldGivePhoneCount()560   public void shouldGivePhoneCount() {
561     shadowOf(telephonyManager).setPhoneCount(42);
562 
563     assertEquals(42, telephonyManager.getPhoneCount());
564   }
565 
566   @Test
567   @Config(minSdk = R)
shouldGiveDefaultActiveModemCount()568   public void shouldGiveDefaultActiveModemCount() {
569     assertThat(telephonyManager.getActiveModemCount()).isEqualTo(1);
570   }
571 
572   @Test
573   @Config(minSdk = R)
shouldGiveActiveModemCount()574   public void shouldGiveActiveModemCount() {
575     shadowOf(telephonyManager).setActiveModemCount(42);
576 
577     assertThat(telephonyManager.getActiveModemCount()).isEqualTo(42);
578   }
579 
580   @Test
581   @Config(minSdk = LOLLIPOP_MR1)
shouldGiveVoiceCapableTrue()582   public void shouldGiveVoiceCapableTrue() {
583     shadowOf(telephonyManager).setVoiceCapable(true);
584 
585     assertTrue(telephonyManager.isVoiceCapable());
586   }
587 
588   @Test
589   @Config(minSdk = LOLLIPOP_MR1)
shouldGiveVoiceCapableFalse()590   public void shouldGiveVoiceCapableFalse() {
591     shadowOf(telephonyManager).setVoiceCapable(false);
592 
593     assertFalse(telephonyManager.isVoiceCapable());
594   }
595 
596   @Test
597   @Config(minSdk = N)
shouldGiveVoiceVibrationEnabled()598   public void shouldGiveVoiceVibrationEnabled() {
599     PhoneAccountHandle phoneAccountHandle =
600         new PhoneAccountHandle(
601             new ComponentName(ApplicationProvider.getApplicationContext(), Object.class), "handle");
602 
603     shadowOf(telephonyManager).setVoicemailVibrationEnabled(phoneAccountHandle, true);
604 
605     assertTrue(telephonyManager.isVoicemailVibrationEnabled(phoneAccountHandle));
606   }
607 
608   @Test
609   @Config(minSdk = N)
setVoicemailVibrationEnabled_accessibleFromAllTelephonyManagers()610   public void setVoicemailVibrationEnabled_accessibleFromAllTelephonyManagers() {
611     PhoneAccountHandle phoneAccountHandle =
612         new PhoneAccountHandle(
613             new ComponentName(ApplicationProvider.getApplicationContext(), Object.class), "handle");
614 
615     shadowOf(telephonyManager).setVoicemailVibrationEnabled(phoneAccountHandle, true);
616 
617     assertTrue(telephonyManager.isVoicemailVibrationEnabled(phoneAccountHandle));
618   }
619 
620   @Test
621   @Config(minSdk = N)
shouldGiveVoicemailRingtoneUri()622   public void shouldGiveVoicemailRingtoneUri() {
623     PhoneAccountHandle phoneAccountHandle =
624         new PhoneAccountHandle(
625             new ComponentName(ApplicationProvider.getApplicationContext(), Object.class), "handle");
626     Uri ringtoneUri = Uri.fromParts("file", "ringtone.mp3", /* fragment= */ null);
627 
628     shadowOf(telephonyManager).setVoicemailRingtoneUri(phoneAccountHandle, ringtoneUri);
629 
630     assertEquals(ringtoneUri, telephonyManager.getVoicemailRingtoneUri(phoneAccountHandle));
631   }
632 
633   @Test
634   @Config(minSdk = O) // The setter on the real manager was added in O
shouldSetVoicemailRingtoneUri()635   public void shouldSetVoicemailRingtoneUri() {
636     PhoneAccountHandle phoneAccountHandle =
637         new PhoneAccountHandle(
638             new ComponentName(ApplicationProvider.getApplicationContext(), Object.class), "handle");
639     Uri ringtoneUri = Uri.fromParts("file", "ringtone.mp3", /* fragment= */ null);
640 
641     // Note: Using the real manager to set, instead of the shadow.
642     telephonyManager.setVoicemailRingtoneUri(phoneAccountHandle, ringtoneUri);
643 
644     assertEquals(ringtoneUri, telephonyManager.getVoicemailRingtoneUri(phoneAccountHandle));
645   }
646 
647   @Test
648   @Config(minSdk = N)
setVoicemailRingtoneUri_accessibleFromAllTelephonyManagers()649   public void setVoicemailRingtoneUri_accessibleFromAllTelephonyManagers() {
650     PhoneAccountHandle phoneAccountHandle =
651         new PhoneAccountHandle(
652             new ComponentName(ApplicationProvider.getApplicationContext(), Object.class), "handle");
653     Uri ringtoneUri = Uri.fromParts("file", "ringtone.mp3", /* fragment= */ null);
654 
655     shadowOf(telephonyManager).setVoicemailRingtoneUri(phoneAccountHandle, ringtoneUri);
656 
657     assertEquals(ringtoneUri, telephonyManager.getVoicemailRingtoneUri(phoneAccountHandle));
658   }
659 
660   @Test
661   @Config(minSdk = O)
shouldCreateForPhoneAccountHandle()662   public void shouldCreateForPhoneAccountHandle() {
663     PhoneAccountHandle phoneAccountHandle =
664         new PhoneAccountHandle(
665             new ComponentName(ApplicationProvider.getApplicationContext(), Object.class), "handle");
666     TelephonyManager mockTelephonyManager = mock(TelephonyManager.class);
667 
668     shadowOf(telephonyManager)
669         .setTelephonyManagerForHandle(phoneAccountHandle, mockTelephonyManager);
670 
671     assertEquals(
672         mockTelephonyManager, telephonyManager.createForPhoneAccountHandle(phoneAccountHandle));
673   }
674 
675   @Test
676   @Config(minSdk = O)
shouldCreateForPhoneAccountHandle_fromAllInstances()677   public void shouldCreateForPhoneAccountHandle_fromAllInstances() {
678     PhoneAccountHandle phoneAccountHandle =
679         new PhoneAccountHandle(
680             new ComponentName(ApplicationProvider.getApplicationContext(), Object.class), "handle");
681     TelephonyManager mockTelephonyManager = mock(TelephonyManager.class);
682 
683     shadowOf(telephonyManager)
684         .setTelephonyManagerForHandle(phoneAccountHandle, mockTelephonyManager);
685 
686     assertEquals(mockTelephonyManager, tmForSub5.createForPhoneAccountHandle(phoneAccountHandle));
687   }
688 
689   @Test
690   @Config(minSdk = N)
shouldCreateForSubscriptionId()691   public void shouldCreateForSubscriptionId() {
692     int subscriptionId = 42;
693     TelephonyManager mockTelephonyManager = mock(TelephonyManager.class);
694 
695     shadowOf(telephonyManager)
696         .setTelephonyManagerForSubscriptionId(subscriptionId, mockTelephonyManager);
697 
698     assertEquals(mockTelephonyManager, telephonyManager.createForSubscriptionId(subscriptionId));
699   }
700 
701   @Test
702   @Config(minSdk = N)
shouldCreateForSubscriptionId_fromAllInstances()703   public void shouldCreateForSubscriptionId_fromAllInstances() {
704     int subscriptionId = 42;
705     TelephonyManager mockTelephonyManager = mock(TelephonyManager.class);
706 
707     shadowOf(telephonyManager)
708         .setTelephonyManagerForSubscriptionId(subscriptionId, mockTelephonyManager);
709 
710     assertEquals(mockTelephonyManager, tmForSub5.createForSubscriptionId(subscriptionId));
711   }
712 
713   @Test
714   @Config(minSdk = O)
shouldSetServiceState()715   public void shouldSetServiceState() {
716     PhoneStateListener listener = mock(PhoneStateListener.class);
717     telephonyManager.listen(listener, LISTEN_SERVICE_STATE);
718     ServiceState serviceState = new ServiceState();
719     serviceState.setState(ServiceState.STATE_OUT_OF_SERVICE);
720 
721     shadowOf(telephonyManager).setServiceState(serviceState);
722 
723     assertEquals(serviceState, telephonyManager.getServiceState());
724     verify(listener).onServiceStateChanged(serviceState);
725   }
726 
727   @Test
728   @Config(minSdk = S)
shouldSetServiceState_toCallback()729   public void shouldSetServiceState_toCallback() {
730     TelephonyCallback callback =
731         mock(TelephonyCallback.class, withSettings().extraInterfaces(ServiceStateListener.class));
732     telephonyManager.registerTelephonyCallback(directExecutor(), callback);
733     ServiceState serviceState = new ServiceState();
734     serviceState.setState(ServiceState.STATE_OUT_OF_SERVICE);
735 
736     shadowOf(telephonyManager).setServiceState(serviceState);
737 
738     assertEquals(serviceState, telephonyManager.getServiceState());
739     verify((ServiceStateListener) callback).onServiceStateChanged(serviceState);
740   }
741 
742   @Test
743   @Config(minSdk = O)
listen_doesNotNotifyListenerOfCurrentServiceStateIfUninitialized()744   public void listen_doesNotNotifyListenerOfCurrentServiceStateIfUninitialized() {
745     PhoneStateListener listener = mock(PhoneStateListener.class);
746 
747     telephonyManager.listen(listener, LISTEN_SERVICE_STATE);
748 
749     verifyNoMoreInteractions(listener);
750   }
751 
752   @Test
753   @Config(minSdk = S)
register_doesNotNotifyCallbackOfCurrentServiceStateIfUninitialized()754   public void register_doesNotNotifyCallbackOfCurrentServiceStateIfUninitialized() {
755     TelephonyCallback callback =
756         mock(TelephonyCallback.class, withSettings().extraInterfaces(ServiceStateListener.class));
757 
758     telephonyManager.registerTelephonyCallback(directExecutor(), callback);
759 
760     verifyNoMoreInteractions(callback);
761   }
762 
763   @Test
764   @Config(minSdk = O)
listen_notifiesListenerOfCurrentServiceStateIfInitialized()765   public void listen_notifiesListenerOfCurrentServiceStateIfInitialized() {
766     PhoneStateListener listener = mock(PhoneStateListener.class);
767     ServiceState serviceState = new ServiceState();
768     serviceState.setState(ServiceState.STATE_OUT_OF_SERVICE);
769     shadowOf(telephonyManager).setServiceState(serviceState);
770 
771     telephonyManager.listen(listener, LISTEN_SERVICE_STATE);
772 
773     verify(listener, times(1)).onServiceStateChanged(serviceState);
774   }
775 
776   @Test
777   @Config(minSdk = S)
register_notifiesCallbackOfCurrentServiceStateIfInitialized()778   public void register_notifiesCallbackOfCurrentServiceStateIfInitialized() {
779     TelephonyCallback callback =
780         mock(TelephonyCallback.class, withSettings().extraInterfaces(ServiceStateListener.class));
781     ServiceState serviceState = new ServiceState();
782     serviceState.setState(ServiceState.STATE_OUT_OF_SERVICE);
783     shadowOf(telephonyManager).setServiceState(serviceState);
784 
785     telephonyManager.registerTelephonyCallback(directExecutor(), callback);
786 
787     verify((ServiceStateListener) callback, times(1)).onServiceStateChanged(serviceState);
788   }
789 
790   @Test
shouldSetIsNetworkRoaming()791   public void shouldSetIsNetworkRoaming() {
792     shadowOf(telephonyManager).setIsNetworkRoaming(true);
793 
794     assertTrue(telephonyManager.isNetworkRoaming());
795   }
796 
797   @Test
shouldGetSimState()798   public void shouldGetSimState() {
799     assertThat(telephonyManager.getSimState()).isEqualTo(TelephonyManager.SIM_STATE_READY);
800   }
801 
802   @Test
803   @Config(minSdk = O)
getSimState_defaultForZeroSpecial()804   public void getSimState_defaultForZeroSpecial() {
805     assertThat(telephonyManager.getSimState(1)).isEqualTo(TelephonyManager.SIM_STATE_UNKNOWN);
806     assertThat(telephonyManager.getSimState(0)).isEqualTo(TelephonyManager.SIM_STATE_READY);
807   }
808 
809   @Test
810   @Config(minSdk = O)
shouldGetSimStateUsingSlotNumber()811   public void shouldGetSimStateUsingSlotNumber() {
812     int expectedSimState = TelephonyManager.SIM_STATE_ABSENT;
813     int slotNumber = 3;
814     shadowOf(telephonyManager).setSimState(slotNumber, expectedSimState);
815 
816     assertThat(telephonyManager.getSimState(slotNumber)).isEqualTo(expectedSimState);
817   }
818 
819   @Test
820   @Config(minSdk = O)
setSimState_withSlotParameter_doesNotAffectCaller()821   public void setSimState_withSlotParameter_doesNotAffectCaller() {
822     int expectedSimState = TelephonyManager.SIM_STATE_ABSENT;
823     int slotNumber = 3;
824     shadowOf(telephonyManager).setSimState(slotNumber, expectedSimState);
825 
826     assertThat(telephonyManager.getSimState()).isEqualTo(TelephonyManager.SIM_STATE_READY);
827   }
828 
829   @Test
830   @Config(minSdk = UPSIDE_DOWN_CAKE)
getSimApplicationState_defaultUnknown()831   public void getSimApplicationState_defaultUnknown() {
832     assertThat(telephonyManager.getSimApplicationState())
833         .isEqualTo(TelephonyManager.SIM_STATE_UNKNOWN);
834   }
835 
836   @Test
837   @Config(minSdk = UPSIDE_DOWN_CAKE)
getSimApplicationState_simStateAbsent_simApplicationStateUnknown()838   public void getSimApplicationState_simStateAbsent_simApplicationStateUnknown() {
839     ShadowSubscriptionManager.setDefaultSubscriptionId(0);
840     // Don't use slot index 0 to ensure the value is remapped instead of getting default
841     // SIM_STATE_UNKNOWN from an unknown slot index.
842     new ShadowSubscriptionManager()
843         .setActiveSubscriptionInfos(
844             SubscriptionInfoBuilder.newBuilder()
845                 .setId(0)
846                 .setSimSlotIndex(3)
847                 .buildSubscriptionInfo());
848 
849     shadowOf(telephonyManager).setSimState(/* slotIndex= */ 3, TelephonyManager.SIM_STATE_ABSENT);
850 
851     assertThat(telephonyManager.getSimApplicationState())
852         .isEqualTo(TelephonyManager.SIM_STATE_UNKNOWN);
853   }
854 
855   @Test
856   @Config(minSdk = UPSIDE_DOWN_CAKE)
getSimApplicationState_simStateReady_returnsSimApplicationStateNotReady()857   public void getSimApplicationState_simStateReady_returnsSimApplicationStateNotReady() {
858     // TODO: make this the default configuration
859     ShadowSubscriptionManager.setDefaultSubscriptionId(0);
860     new ShadowSubscriptionManager()
861         .setActiveSubscriptionInfos(
862             SubscriptionInfoBuilder.newBuilder()
863                 .setId(0)
864                 .setSimSlotIndex(0)
865                 .buildSubscriptionInfo());
866 
867     shadowOf(telephonyManager).setSimState(/* slotIndex= */ 0, TelephonyManager.SIM_STATE_READY);
868 
869     assertThat(telephonyManager.getSimApplicationState())
870         .isEqualTo(TelephonyManager.SIM_STATE_NOT_READY);
871   }
872 
873   @Test
874   @Config(minSdk = UPSIDE_DOWN_CAKE)
getSimApplicationState_simStateLoaded_returnsSimApplicationStateLoaded()875   public void getSimApplicationState_simStateLoaded_returnsSimApplicationStateLoaded() {
876     // TODO: make this the default configuration
877     ShadowSubscriptionManager.setDefaultSubscriptionId(0);
878     new ShadowSubscriptionManager()
879         .setActiveSubscriptionInfos(
880             SubscriptionInfoBuilder.newBuilder()
881                 .setId(0)
882                 .setSimSlotIndex(0)
883                 .buildSubscriptionInfo());
884 
885     shadowOf(telephonyManager).setSimState(/* slotIndex= */ 0, TelephonyManager.SIM_STATE_LOADED);
886 
887     assertThat(telephonyManager.getSimApplicationState())
888         .isEqualTo(TelephonyManager.SIM_STATE_LOADED);
889   }
890 
891   @Test
shouldGetSimIso()892   public void shouldGetSimIso() {
893     assertThat(telephonyManager.getSimCountryIso()).isEmpty();
894   }
895 
896   @Test
897   @Config(minSdk = N, maxSdk = Q)
shouldGetSimIso_resetsZeroSpecial()898   public void shouldGetSimIso_resetsZeroSpecial() {
899     assertThat(callGetSimCountryIso(telephonyManager, 1)).isNull();
900     assertThat(callGetSimCountryIso(telephonyManager, 0)).isEmpty();
901   }
902 
callGetSimCountryIso(TelephonyManager telephonyManager, int subId)903   private String callGetSimCountryIso(TelephonyManager telephonyManager, int subId) {
904     return (String)
905         ReflectionHelpers.callInstanceMethod(
906             telephonyManager, "getSimCountryIso", ClassParameter.from(int.class, subId));
907   }
908 
909   @Test
910   @Config(minSdk = N, maxSdk = Q)
shouldGetSimIsoWhenSetUsingSlotNumber()911   public void shouldGetSimIsoWhenSetUsingSlotNumber() {
912     String expectedSimIso = "usa";
913     int subId = 2;
914     shadowOf(telephonyManager).setSimCountryIso(subId, expectedSimIso);
915 
916     assertThat(callGetSimCountryIso(telephonyManager, subId)).isEqualTo(expectedSimIso);
917   }
918 
919   @Test
920   @Config(minSdk = N, maxSdk = Q)
setSimIso_withSlotParameter_doesNotAffectCaller()921   public void setSimIso_withSlotParameter_doesNotAffectCaller() {
922     String expectedSimIso = "usa";
923     int subId = 2;
924     shadowOf(telephonyManager).setSimCountryIso(subId, expectedSimIso);
925 
926     assertThat(telephonyManager.getSimCountryIso()).isEqualTo("");
927   }
928 
929   @Test
930   @Config(minSdk = N, maxSdk = Q)
setSimIso_withSlotParameter_acceptsNull()931   public void setSimIso_withSlotParameter_acceptsNull() {
932     String expectedSimIso = "usa";
933     int subId = 2;
934     shadowOf(telephonyManager).setSimCountryIso(subId, expectedSimIso);
935     shadowOf(telephonyManager).setSimCountryIso(subId, null);
936 
937     assertThat(callGetSimCountryIso(telephonyManager, subId)).isEqualTo(null);
938   }
939 
940   @Test
941   @Config(minSdk = P)
shouldGetSimCarrierId()942   public void shouldGetSimCarrierId() {
943     int expectedCarrierId = 132;
944     shadowOf(telephonyManager).setSimCarrierId(expectedCarrierId);
945 
946     assertThat(telephonyManager.getSimCarrierId()).isEqualTo(expectedCarrierId);
947   }
948 
949   @Test
950   @Config(minSdk = Q)
shouldGetSimSpecificCarrierId()951   public void shouldGetSimSpecificCarrierId() {
952     int expectedCarrierId = 132;
953     shadowOf(telephonyManager).setSimSpecificCarrierId(expectedCarrierId);
954 
955     assertThat(telephonyManager.getSimSpecificCarrierId()).isEqualTo(expectedCarrierId);
956   }
957 
958   @Test
959   @Config(minSdk = P)
shouldGetSimCarrierIdName()960   public void shouldGetSimCarrierIdName() {
961     String expectedCarrierIdName = "Fi";
962     shadowOf(telephonyManager).setSimCarrierIdName(expectedCarrierIdName);
963 
964     assertThat(telephonyManager.getSimCarrierIdName().toString()).isEqualTo(expectedCarrierIdName);
965   }
966 
967   @Test
968   @Config(minSdk = Q)
shouldGetCarrierIdFromSimMccMnc()969   public void shouldGetCarrierIdFromSimMccMnc() {
970     int expectedCarrierId = 419;
971     shadowOf(telephonyManager).setCarrierIdFromSimMccMnc(expectedCarrierId);
972 
973     assertThat(telephonyManager.getCarrierIdFromSimMccMnc()).isEqualTo(expectedCarrierId);
974   }
975 
976   @Test
977   @Config(minSdk = M)
shouldGetCurrentPhoneTypeGivenSubId()978   public void shouldGetCurrentPhoneTypeGivenSubId() {
979     int subId = 1;
980     int expectedPhoneType = TelephonyManager.PHONE_TYPE_GSM;
981     shadowOf(telephonyManager).setCurrentPhoneType(subId, expectedPhoneType);
982 
983     assertThat(telephonyManager.getCurrentPhoneType(subId)).isEqualTo(expectedPhoneType);
984   }
985 
986   @Test
987   @Config(minSdk = M)
shouldGetCurrentPhoneTypeGivenSubId_fromAllInstances()988   public void shouldGetCurrentPhoneTypeGivenSubId_fromAllInstances() {
989     int subId = 1;
990     int expectedPhoneType = TelephonyManager.PHONE_TYPE_GSM;
991     shadowOf(telephonyManager).setCurrentPhoneType(subId, expectedPhoneType);
992 
993     assertThat(tmForSub5.getCurrentPhoneType(subId)).isEqualTo(expectedPhoneType);
994   }
995 
996   @Test
997   @Config(minSdk = M)
clearPhoneTypes_setsStartingState()998   public void clearPhoneTypes_setsStartingState() {
999     ShadowTelephonyManager.clearPhoneTypes();
1000     assertEquals(TelephonyManager.PHONE_TYPE_NONE, telephonyManager.getCurrentPhoneType(0));
1001     assertEquals(TelephonyManager.PHONE_TYPE_NONE, telephonyManager.getCurrentPhoneType(1));
1002   }
1003 
1004   @Test
1005   @Config(minSdk = M)
shouldGetCarrierPackageNamesForIntentAndPhone()1006   public void shouldGetCarrierPackageNamesForIntentAndPhone() {
1007     List<String> packages = Collections.singletonList("package1");
1008     int phoneId = 123;
1009     shadowOf(telephonyManager).setCarrierPackageNamesForPhone(phoneId, packages);
1010 
1011     assertThat(telephonyManager.getCarrierPackageNamesForIntentAndPhone(new Intent(), phoneId))
1012         .isEqualTo(packages);
1013   }
1014 
1015   @Test
1016   @Config(minSdk = M)
setCarrierPackageNamesForPhone_acceptsNull()1017   public void setCarrierPackageNamesForPhone_acceptsNull() {
1018     List<String> packages = Collections.singletonList("package1");
1019     int phoneId = 123;
1020     shadowOf(telephonyManager).setCarrierPackageNamesForPhone(phoneId, packages);
1021     shadowOf(telephonyManager).setCarrierPackageNamesForPhone(phoneId, null);
1022 
1023     assertThat(telephonyManager.getCarrierPackageNamesForIntentAndPhone(new Intent(), phoneId))
1024         .isEqualTo(null);
1025   }
1026 
1027   @Test
1028   @Config(minSdk = M)
shouldGetCarrierPackageNamesForIntentAndPhone_fromAllInstances()1029   public void shouldGetCarrierPackageNamesForIntentAndPhone_fromAllInstances() {
1030     List<String> packages = Collections.singletonList("package1");
1031     int phoneId = 123;
1032     shadowOf(telephonyManager).setCarrierPackageNamesForPhone(phoneId, packages);
1033 
1034     assertThat(tmForSub5.getCarrierPackageNamesForIntentAndPhone(new Intent(), phoneId))
1035         .isEqualTo(packages);
1036   }
1037 
1038   @Test
1039   @Config(minSdk = M)
shouldGetCarrierPackageNamesForIntentAndPhone_doesNotAffectCaller()1040   public void shouldGetCarrierPackageNamesForIntentAndPhone_doesNotAffectCaller() {
1041     List<String> packages = Collections.singletonList("package1");
1042     int phoneId = 123;
1043     shadowOf(telephonyManager).setCarrierPackageNamesForPhone(phoneId, packages);
1044 
1045     assertThat(telephonyManager.getCarrierPackageNamesForIntent(new Intent())).isNull();
1046   }
1047 
1048   @Test
1049   @Config(minSdk = M)
shouldGetCarrierPackageNamesForIntent()1050   public void shouldGetCarrierPackageNamesForIntent() {
1051     List<String> packages = Collections.singletonList("package1");
1052     shadowOf(telephonyManager)
1053         .setCarrierPackageNamesForPhone(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, packages);
1054 
1055     assertThat(telephonyManager.getCarrierPackageNamesForIntent(new Intent())).isEqualTo(packages);
1056   }
1057 
1058   @Test
1059   @Config(minSdk = O)
resetSimStates_shouldRetainDefaultState()1060   public void resetSimStates_shouldRetainDefaultState() {
1061     shadowOf(telephonyManager).resetSimStates();
1062 
1063     assertThat(telephonyManager.getSimState()).isEqualTo(TelephonyManager.SIM_STATE_READY);
1064     assertThat(telephonyManager.getSimState(1)).isEqualTo(TelephonyManager.SIM_STATE_UNKNOWN);
1065     assertThat(telephonyManager.getSimState(0)).isEqualTo(TelephonyManager.SIM_STATE_READY);
1066   }
1067 
1068   @Test
1069   @Config(minSdk = N)
resetSimCountryIsos_shouldRetainDefaultState()1070   public void resetSimCountryIsos_shouldRetainDefaultState() {
1071     shadowOf(telephonyManager).resetSimCountryIsos();
1072 
1073     assertThat(telephonyManager.getSimCountryIso()).isEmpty();
1074   }
1075 
1076   @Test
1077   @Config(minSdk = N, maxSdk = Q)
resetSimCountryIsos_resetZeroSpecial()1078   public void resetSimCountryIsos_resetZeroSpecial() {
1079     shadowOf(telephonyManager).resetSimCountryIsos();
1080     assertThat(callGetSimCountryIso(telephonyManager, 1)).isNull();
1081     assertThat(callGetSimCountryIso(telephonyManager, 0)).isEmpty();
1082   }
1083 
1084   @Test
shouldSetSubscriberId()1085   public void shouldSetSubscriberId() {
1086     String subscriberId = "123451234512345";
1087     shadowOf(telephonyManager).setSubscriberId(subscriberId);
1088 
1089     assertThat(telephonyManager.getSubscriberId()).isEqualTo(subscriberId);
1090   }
1091 
1092   @Test(expected = SecurityException.class)
getSubscriberId_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted()1093   public void getSubscriberId_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted() {
1094     shadowOf(telephonyManager).setReadPhoneStatePermission(false);
1095     telephonyManager.getSubscriberId();
1096   }
1097 
1098   @Test
1099   @Config(minSdk = P)
getUiccSlotsInfo()1100   public void getUiccSlotsInfo() {
1101     UiccSlotInfo slotInfo1 = new UiccSlotInfo(true, true, null, 0, 0, true);
1102     UiccSlotInfo slotInfo2 = new UiccSlotInfo(true, true, null, 0, 1, true);
1103     UiccSlotInfo[] slotInfos = new UiccSlotInfo[] {slotInfo1, slotInfo2};
1104     shadowOf(telephonyManager).setUiccSlotsInfo(slotInfos);
1105 
1106     assertThat(shadowOf(telephonyManager).getUiccSlotsInfo()).isEqualTo(slotInfos);
1107   }
1108 
1109   @Test
1110   @Config(minSdk = Q)
getUiccCardsInfo()1111   public void getUiccCardsInfo() {
1112     Object /*UiccCardInfo*/ cardsInfo1 = "new UiccCardInfo(true, true, null, 0, 0, true)";
1113     Object /*UiccCardInfo*/ cardsInfo2 = "new UiccCardInfo(true, true, null, 0, 1, true)";
1114     List<Object /*UiccCardInfo*/> cardInfos = ImmutableList.of(cardsInfo1, cardsInfo2);
1115     shadowOf(telephonyManager).setUiccCardsInfo(cardInfos);
1116 
1117     assertThat(telephonyManager.getUiccCardsInfo()).isEqualTo(cardInfos);
1118   }
1119 
1120   @Test
1121   @Config(minSdk = Q)
getUiccCardsInfo_returnsListAfterReset()1122   public void getUiccCardsInfo_returnsListAfterReset() {
1123     assertThat(telephonyManager.getUiccCardsInfo()).isEmpty();
1124   }
1125 
1126   @Test
1127   @Config(minSdk = O)
shouldSetVisualVoicemailPackage()1128   public void shouldSetVisualVoicemailPackage() {
1129     shadowOf(telephonyManager).setVisualVoicemailPackageName("org.foo");
1130 
1131     assertThat(telephonyManager.getVisualVoicemailPackageName()).isEqualTo("org.foo");
1132   }
1133 
1134   @Test
1135   @Config(minSdk = P)
canSetAndGetSignalStrength()1136   public void canSetAndGetSignalStrength() {
1137     SignalStrength ss = Shadow.newInstanceOf(SignalStrength.class);
1138     shadowOf(telephonyManager).setSignalStrength(ss);
1139     assertThat(telephonyManager.getSignalStrength()).isEqualTo(ss);
1140   }
1141 
1142   @Test
1143   @Config(minSdk = P)
shouldGiveSignalStrength()1144   public void shouldGiveSignalStrength() {
1145     PhoneStateListener listener = mock(PhoneStateListener.class);
1146     telephonyManager.listen(listener, LISTEN_SIGNAL_STRENGTHS);
1147     SignalStrength ss = Shadow.newInstanceOf(SignalStrength.class);
1148 
1149     shadowOf(telephonyManager).setSignalStrength(ss);
1150 
1151     verify(listener).onSignalStrengthsChanged(ss);
1152   }
1153 
1154   @Test
1155   @Config(minSdk = S)
shouldGiveSignalStrength_toCallback()1156   public void shouldGiveSignalStrength_toCallback() {
1157     TelephonyCallback callback =
1158         mock(
1159             TelephonyCallback.class, withSettings().extraInterfaces(SignalStrengthsListener.class));
1160     telephonyManager.registerTelephonyCallback(directExecutor(), callback);
1161     SignalStrength ss = Shadow.newInstanceOf(SignalStrength.class);
1162 
1163     shadowOf(telephonyManager).setSignalStrength(ss);
1164 
1165     verify((SignalStrengthsListener) callback).onSignalStrengthsChanged(ss);
1166   }
1167 
1168   @Test
1169   @Config(minSdk = O)
setDataEnabledChangesIsDataEnabled()1170   public void setDataEnabledChangesIsDataEnabled() {
1171     shadowOf(telephonyManager).setDataEnabled(false);
1172     assertThat(telephonyManager.isDataEnabled()).isFalse();
1173     shadowOf(telephonyManager).setDataEnabled(true);
1174     assertThat(telephonyManager.isDataEnabled()).isTrue();
1175   }
1176 
1177   @Test
1178   @Config(minSdk = S)
setDataEnabledForReasonChangesIsDataEnabledForReason()1179   public void setDataEnabledForReasonChangesIsDataEnabledForReason() {
1180     int correctReason = TelephonyManager.DATA_ENABLED_REASON_POLICY;
1181     int incorrectReason = TelephonyManager.DATA_ENABLED_REASON_USER;
1182 
1183     assertThat(telephonyManager.isDataEnabledForReason(correctReason)).isTrue();
1184     assertThat(telephonyManager.isDataEnabledForReason(incorrectReason)).isTrue();
1185 
1186     telephonyManager.setDataEnabledForReason(correctReason, false);
1187     assertThat(telephonyManager.isDataEnabledForReason(correctReason)).isFalse();
1188     assertThat(telephonyManager.isDataEnabledForReason(incorrectReason)).isTrue();
1189 
1190     telephonyManager.setDataEnabledForReason(correctReason, true);
1191     assertThat(telephonyManager.isDataEnabledForReason(correctReason)).isTrue();
1192     assertThat(telephonyManager.isDataEnabledForReason(incorrectReason)).isTrue();
1193   }
1194 
1195   @Test
setDataStateChangesDataState()1196   public void setDataStateChangesDataState() {
1197     assertThat(telephonyManager.getDataState()).isEqualTo(TelephonyManager.DATA_DISCONNECTED);
1198     shadowOf(telephonyManager).setDataState(TelephonyManager.DATA_CONNECTING);
1199     assertThat(telephonyManager.getDataState()).isEqualTo(TelephonyManager.DATA_CONNECTING);
1200     shadowOf(telephonyManager).setDataState(TelephonyManager.DATA_CONNECTED);
1201     assertThat(telephonyManager.getDataState()).isEqualTo(TelephonyManager.DATA_CONNECTED);
1202   }
1203 
1204   @Test
setDataActivityChangesDataActivity()1205   public void setDataActivityChangesDataActivity() {
1206     assertThat(telephonyManager.getDataActivity()).isEqualTo(TelephonyManager.DATA_ACTIVITY_NONE);
1207     shadowOf(telephonyManager).setDataActivity(TelephonyManager.DATA_ACTIVITY_IN);
1208     assertThat(telephonyManager.getDataActivity()).isEqualTo(TelephonyManager.DATA_ACTIVITY_IN);
1209     shadowOf(telephonyManager).setDataActivity(TelephonyManager.DATA_ACTIVITY_OUT);
1210     assertThat(telephonyManager.getDataActivity()).isEqualTo(TelephonyManager.DATA_ACTIVITY_OUT);
1211   }
1212 
1213   @Test
1214   @Config(minSdk = Q)
setRttSupportedChangesIsRttSupported()1215   public void setRttSupportedChangesIsRttSupported() {
1216     shadowOf(telephonyManager).setRttSupported(false);
1217     assertThat(telephonyManager.isRttSupported()).isFalse();
1218     shadowOf(telephonyManager).setRttSupported(true);
1219     assertThat(telephonyManager.isRttSupported()).isTrue();
1220   }
1221 
1222   @Test
1223   @Config(minSdk = M)
setTtyModeSupportedChangesIsTtyModeSupported()1224   public void setTtyModeSupportedChangesIsTtyModeSupported() {
1225     shadowOf(telephonyManager).setTtyModeSupported(false);
1226     assertThat(telephonyManager.isTtyModeSupported()).isFalse();
1227     shadowOf(telephonyManager).setTtyModeSupported(true);
1228     assertThat(telephonyManager.isTtyModeSupported()).isTrue();
1229   }
1230 
1231   @Test
1232   @Config(minSdk = M)
1233   public void
isTtyModeSupported_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted()1234       isTtyModeSupported_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted()
1235           throws Exception {
1236     shadowOf(telephonyManager).setReadPhoneStatePermission(false);
1237     assertThrows(SecurityException.class, () -> telephonyManager.isTtyModeSupported());
1238   }
1239 
1240   @Test
1241   @Config(minSdk = N)
hasCarrierPrivilegesWithSubId()1242   public void hasCarrierPrivilegesWithSubId() {
1243     int subId = 3;
1244     assertThat(telephonyManager.hasCarrierPrivileges(subId)).isFalse();
1245     shadowOf(telephonyManager).setHasCarrierPrivileges(subId, true);
1246     assertThat(telephonyManager.hasCarrierPrivileges(subId)).isTrue();
1247   }
1248 
1249   @Test
1250   @Config(minSdk = O)
sendDialerSpecialCode()1251   public void sendDialerSpecialCode() {
1252     shadowOf(telephonyManager).sendDialerSpecialCode("1234");
1253     shadowOf(telephonyManager).sendDialerSpecialCode("123456");
1254     shadowOf(telephonyManager).sendDialerSpecialCode("1234");
1255 
1256     assertThat(shadowOf(telephonyManager).getSentDialerSpecialCodes())
1257         .containsExactly("1234", "123456", "1234")
1258         .inOrder();
1259   }
1260 
1261   @Test
1262   @Config(minSdk = M)
setHearingAidCompatibilitySupportedChangesisHearingAidCompatibilitySupported()1263   public void setHearingAidCompatibilitySupportedChangesisHearingAidCompatibilitySupported() {
1264     shadowOf(telephonyManager).setHearingAidCompatibilitySupported(false);
1265     assertThat(telephonyManager.isHearingAidCompatibilitySupported()).isFalse();
1266     shadowOf(telephonyManager).setHearingAidCompatibilitySupported(true);
1267     assertThat(telephonyManager.isHearingAidCompatibilitySupported()).isTrue();
1268   }
1269 
1270   @Test
1271   @Config(minSdk = R)
createTelephonyDisplayInfo_correctlyCreatesDisplayInfo()1272   public void createTelephonyDisplayInfo_correctlyCreatesDisplayInfo() {
1273     TelephonyDisplayInfo displayInfo =
1274         (TelephonyDisplayInfo)
1275             createTelephonyDisplayInfo(NETWORK_TYPE_LTE, OVERRIDE_NETWORK_TYPE_LTE_CA);
1276 
1277     assertThat(displayInfo.getNetworkType()).isEqualTo(NETWORK_TYPE_LTE);
1278     assertThat(displayInfo.getOverrideNetworkType()).isEqualTo(OVERRIDE_NETWORK_TYPE_LTE_CA);
1279   }
1280 
1281   @Test
1282   @Config(minSdk = R)
listen_doesNotNotifyListenerOfCurrentTelephonyDisplayInfoIfUninitialized()1283   public void listen_doesNotNotifyListenerOfCurrentTelephonyDisplayInfoIfUninitialized() {
1284     PhoneStateListener listener = mock(PhoneStateListener.class);
1285 
1286     telephonyManager.listen(listener, LISTEN_DISPLAY_INFO_CHANGED);
1287 
1288     verifyNoMoreInteractions(listener);
1289   }
1290 
1291   @Test
1292   @Config(minSdk = S)
register_doesNotNotifyCallbackOfCurrentTelephonyDisplayInfoIfUninitialized()1293   public void register_doesNotNotifyCallbackOfCurrentTelephonyDisplayInfoIfUninitialized() {
1294     TelephonyCallback callback =
1295         mock(TelephonyCallback.class, withSettings().extraInterfaces(DisplayInfoListener.class));
1296 
1297     telephonyManager.registerTelephonyCallback(directExecutor(), callback);
1298 
1299     verifyNoMoreInteractions(callback);
1300   }
1301 
1302   @Test
1303   @Config(minSdk = R)
listen_notifiesListenerOfCurrentTelephonyDisplayInfoIfInitialized()1304   public void listen_notifiesListenerOfCurrentTelephonyDisplayInfoIfInitialized() {
1305     PhoneStateListener listener = mock(PhoneStateListener.class);
1306     TelephonyDisplayInfo displayInfo =
1307         (TelephonyDisplayInfo)
1308             createTelephonyDisplayInfo(NETWORK_TYPE_EVDO_0, OVERRIDE_NETWORK_TYPE_NONE);
1309     shadowTelephonyManager.setTelephonyDisplayInfo(displayInfo);
1310 
1311     telephonyManager.listen(listener, LISTEN_DISPLAY_INFO_CHANGED);
1312 
1313     verify(listener, times(1)).onDisplayInfoChanged(displayInfo);
1314   }
1315 
1316   @Test
1317   @Config(minSdk = S)
register_notifiesCallbackOfCurrentTelephonyDisplayInfoIfInitialized()1318   public void register_notifiesCallbackOfCurrentTelephonyDisplayInfoIfInitialized() {
1319     TelephonyCallback callback =
1320         mock(TelephonyCallback.class, withSettings().extraInterfaces(DisplayInfoListener.class));
1321     TelephonyDisplayInfo displayInfo =
1322         (TelephonyDisplayInfo)
1323             createTelephonyDisplayInfo(NETWORK_TYPE_EVDO_0, OVERRIDE_NETWORK_TYPE_NONE);
1324     shadowTelephonyManager.setTelephonyDisplayInfo(displayInfo);
1325 
1326     telephonyManager.registerTelephonyCallback(directExecutor(), callback);
1327 
1328     verify((DisplayInfoListener) callback, times(1)).onDisplayInfoChanged(displayInfo);
1329   }
1330 
1331   @Test
1332   @Config(minSdk = R)
setTelephonyDisplayInfo_notifiesListeners()1333   public void setTelephonyDisplayInfo_notifiesListeners() {
1334     PhoneStateListener listener = mock(PhoneStateListener.class);
1335     TelephonyDisplayInfo displayInfo =
1336         (TelephonyDisplayInfo)
1337             createTelephonyDisplayInfo(NETWORK_TYPE_LTE, OVERRIDE_NETWORK_TYPE_NR_NSA);
1338     telephonyManager.listen(listener, LISTEN_DISPLAY_INFO_CHANGED);
1339 
1340     shadowTelephonyManager.setTelephonyDisplayInfo(displayInfo);
1341 
1342     verify(listener, times(1)).onDisplayInfoChanged(displayInfo);
1343   }
1344 
1345   @Test
1346   @Config(minSdk = S)
setTelephonyDisplayInfo_notifiesCallback()1347   public void setTelephonyDisplayInfo_notifiesCallback() {
1348     TelephonyCallback callback =
1349         mock(TelephonyCallback.class, withSettings().extraInterfaces(DisplayInfoListener.class));
1350     TelephonyDisplayInfo displayInfo =
1351         (TelephonyDisplayInfo)
1352             createTelephonyDisplayInfo(NETWORK_TYPE_LTE, OVERRIDE_NETWORK_TYPE_NR_NSA);
1353     shadowTelephonyManager.setTelephonyDisplayInfo(displayInfo);
1354 
1355     telephonyManager.registerTelephonyCallback(directExecutor(), callback);
1356 
1357     verify((DisplayInfoListener) callback, times(1)).onDisplayInfoChanged(displayInfo);
1358   }
1359 
1360   @Test(expected = NullPointerException.class)
1361   @Config(minSdk = R)
setTelephonyDisplayInfo_throwsNullPointerException()1362   public void setTelephonyDisplayInfo_throwsNullPointerException() {
1363     shadowTelephonyManager.setTelephonyDisplayInfo(null);
1364   }
1365 
1366   @Test
1367   @Config(minSdk = R)
isDataConnectionAllowed_returnsFalseByDefault()1368   public void isDataConnectionAllowed_returnsFalseByDefault() {
1369     assertThat(shadowTelephonyManager.isDataConnectionAllowed()).isFalse();
1370   }
1371 
1372   @Test
1373   @Config(minSdk = R)
isDataConnectionAllowed_returnsFalseWhenSetToFalse()1374   public void isDataConnectionAllowed_returnsFalseWhenSetToFalse() {
1375     shadowTelephonyManager.setIsDataConnectionAllowed(false);
1376 
1377     assertThat(shadowTelephonyManager.isDataConnectionAllowed()).isFalse();
1378   }
1379 
1380   @Test
1381   @Config(minSdk = R)
isDataConnectionAllowed_returnsTrueWhenSetToTrue()1382   public void isDataConnectionAllowed_returnsTrueWhenSetToTrue() {
1383     shadowTelephonyManager.setIsDataConnectionAllowed(true);
1384 
1385     assertThat(shadowTelephonyManager.isDataConnectionAllowed()).isTrue();
1386   }
1387 
1388   @Test
1389   @Config(minSdk = S)
getCallComposerStatus_default()1390   public void getCallComposerStatus_default() {
1391     assertThat(telephonyManager.getCallComposerStatus()).isEqualTo(0);
1392   }
1393 
1394   @Test
1395   @Config(minSdk = S)
setCallComposerStatus()1396   public void setCallComposerStatus() {
1397     telephonyManager.setCallComposerStatus(CALL_COMPOSER_STATUS_ON);
1398 
1399     assertThat(telephonyManager.getCallComposerStatus()).isEqualTo(CALL_COMPOSER_STATUS_ON);
1400   }
1401 
1402   @Test
1403   @Config(minSdk = S)
getBootstrapAuthenticationCallback()1404   public void getBootstrapAuthenticationCallback() {
1405     BootstrapAuthenticationCallback callback = mock(BootstrapAuthenticationCallback.class);
1406 
1407     telephonyManager.bootstrapAuthenticationRequest(
1408         TelephonyManager.APPTYPE_ISIM,
1409         Uri.parse("tel:test-uri"),
1410         new UaSecurityProtocolIdentifier.Builder().build(),
1411         true,
1412         directExecutor(),
1413         callback);
1414 
1415     assertThat(shadowTelephonyManager.getBootstrapAuthenticationCallback()).isEqualTo(callback);
1416   }
1417 
1418   @Test
1419   @Config(minSdk = S)
setPhoneCapability_returnsPhoneCapability()1420   public void setPhoneCapability_returnsPhoneCapability() {
1421     PhoneCapability phoneCapability = PhoneCapabilityFactory.create(2, 1, false, new int[0]);
1422 
1423     shadowTelephonyManager.setPhoneCapability(phoneCapability);
1424 
1425     assertThat(telephonyManager.getPhoneCapability()).isEqualTo(phoneCapability);
1426   }
1427 
1428   @Test
1429   @Config(minSdk = O)
sendVisualVoicemailSms_shouldStoreLastSendSmsParameters()1430   public void sendVisualVoicemailSms_shouldStoreLastSendSmsParameters() {
1431     telephonyManager.sendVisualVoicemailSms("destAddress", 0, "message", null);
1432 
1433     ShadowTelephonyManager.VisualVoicemailSmsParams params =
1434         shadowOf(telephonyManager).getLastSentVisualVoicemailSmsParams();
1435 
1436     assertThat(params.getDestinationAddress()).isEqualTo("destAddress");
1437     assertThat(params.getPort()).isEqualTo(0);
1438     assertThat(params.getText()).isEqualTo("message");
1439     assertThat(params.getSentIntent()).isNull();
1440   }
1441 
1442   @Test
1443   @Config(minSdk = O)
setVisualVoicemailSmsFilterSettings_shouldStoreSettings()1444   public void setVisualVoicemailSmsFilterSettings_shouldStoreSettings() {
1445     VisualVoicemailSmsFilterSettings settings =
1446         new VisualVoicemailSmsFilterSettings.Builder()
1447             .setClientPrefix("clientPrefix")
1448             .setDestinationPort(100)
1449             .build();
1450 
1451     telephonyManager.setVisualVoicemailSmsFilterSettings(settings);
1452 
1453     assertThat(shadowOf(telephonyManager).getVisualVoicemailSmsFilterSettings())
1454         .isEqualTo(settings);
1455   }
1456 
1457   @Test
1458   @Config(minSdk = O)
setVisualVoicemailSmsFilterSettings_setNullSettings_clearsSettings()1459   public void setVisualVoicemailSmsFilterSettings_setNullSettings_clearsSettings() {
1460     VisualVoicemailSmsFilterSettings settings =
1461         new VisualVoicemailSmsFilterSettings.Builder().build();
1462 
1463     telephonyManager.setVisualVoicemailSmsFilterSettings(settings);
1464     telephonyManager.setVisualVoicemailSmsFilterSettings(null);
1465 
1466     assertThat(shadowOf(telephonyManager).getVisualVoicemailSmsFilterSettings()).isNull();
1467   }
1468 
1469   @Test
1470   @Config(minSdk = Q)
isEmergencyNumber_telephonyServiceUnavailable_throwsIllegalStateException()1471   public void isEmergencyNumber_telephonyServiceUnavailable_throwsIllegalStateException() {
1472     ShadowServiceManager.setServiceAvailability(Context.TELEPHONY_SERVICE, false);
1473 
1474     assertThrows(IllegalStateException.class, () -> telephonyManager.isEmergencyNumber("911"));
1475   }
1476 
1477   @Test
1478   @Config(minSdk = O)
1479   public void
getEmergencyCallbackMode_noReadPrivilegedPhoneStatePermission_throwsSecurityException()1480       getEmergencyCallbackMode_noReadPrivilegedPhoneStatePermission_throwsSecurityException() {
1481     shadowOf((Application) ApplicationProvider.getApplicationContext())
1482         .denyPermissions(permission.READ_PRIVILEGED_PHONE_STATE);
1483 
1484     assertThrows(SecurityException.class, () -> telephonyManager.getEmergencyCallbackMode());
1485   }
1486 
1487   @Test
1488   @Config(minSdk = O)
getEmergencyCallback_wasSetToTrue_returnsTrue()1489   public void getEmergencyCallback_wasSetToTrue_returnsTrue() {
1490     shadowTelephonyManager.setEmergencyCallbackMode(true);
1491 
1492     assertThat(telephonyManager.getEmergencyCallbackMode()).isTrue();
1493   }
1494 
1495   @Test
1496   @Config(minSdk = O)
getEmergencyCallback_notSet_returnsFalse()1497   public void getEmergencyCallback_notSet_returnsFalse() {
1498     assertThat(telephonyManager.getEmergencyCallbackMode()).isFalse();
1499   }
1500 
1501   @Test
1502   @Config(minSdk = R)
getEmergencyNumbersList_notSet_returnsEmptyList()1503   public void getEmergencyNumbersList_notSet_returnsEmptyList() {
1504     assertThat(telephonyManager.getEmergencyNumberList()).isEmpty();
1505   }
1506 
1507   @Test
1508   @Config(minSdk = R)
getEmergencyNumbersList_wasSet_returnsCorrectList()1509   public void getEmergencyNumbersList_wasSet_returnsCorrectList() throws Exception {
1510     EmergencyNumber emergencyNumber =
1511         EmergencyNumber.class
1512             .getConstructor(
1513                 String.class,
1514                 String.class,
1515                 String.class,
1516                 int.class,
1517                 List.class,
1518                 int.class,
1519                 int.class)
1520             .newInstance(
1521                 "911",
1522                 "us",
1523                 "30",
1524                 EMERGENCY_NUMBER_SOURCE_DATABASE,
1525                 ImmutableList.of(),
1526                 EMERGENCY_SERVICE_CATEGORY_POLICE,
1527                 EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL);
1528     ShadowTelephonyManager.setEmergencyNumberList(
1529         ImmutableMap.of(0, ImmutableList.of(emergencyNumber)));
1530     assertThat(telephonyManager.getEmergencyNumberList().get(0)).containsExactly(emergencyNumber);
1531   }
1532 
1533   @Test
1534   @Config(minSdk = R)
getSubscriptionIdForPhoneAccountHandle()1535   public void getSubscriptionIdForPhoneAccountHandle() {
1536     int subscriptionId = 123;
1537     PhoneAccountHandle phoneAccountHandle =
1538         new PhoneAccountHandle(
1539             new ComponentName(ApplicationProvider.getApplicationContext(), Object.class), "handle");
1540     shadowOf(telephonyManager)
1541         .setPhoneAccountHandleSubscriptionId(phoneAccountHandle, subscriptionId);
1542     assertEquals(subscriptionId, telephonyManager.getSubscriptionId(phoneAccountHandle));
1543   }
1544 
1545   @Test
1546   @Config(minSdk = R)
getSubscriptionIdForPhoneAccountHandle_affectsAllInstances()1547   public void getSubscriptionIdForPhoneAccountHandle_affectsAllInstances() {
1548     int subscriptionId = 123;
1549     PhoneAccountHandle phoneAccountHandle =
1550         new PhoneAccountHandle(
1551             new ComponentName(ApplicationProvider.getApplicationContext(), Object.class), "handle");
1552     shadowOf(telephonyManager)
1553         .setPhoneAccountHandleSubscriptionId(phoneAccountHandle, subscriptionId);
1554     assertEquals(subscriptionId, tmForSub5.getSubscriptionId(phoneAccountHandle));
1555   }
1556 
1557   @Test
1558   @Config(minSdk = R)
getSubscriptionIdForPhoneAccountHandle_doesNotModifyCaller()1559   public void getSubscriptionIdForPhoneAccountHandle_doesNotModifyCaller() {
1560     int subscriptionId = 123;
1561     PhoneAccountHandle phoneAccountHandle =
1562         new PhoneAccountHandle(
1563             new ComponentName(ApplicationProvider.getApplicationContext(), Object.class), "handle");
1564     shadowOf(telephonyManager)
1565         .setPhoneAccountHandleSubscriptionId(phoneAccountHandle, subscriptionId);
1566     assertEquals(5, tmForSub5.getSubscriptionId());
1567   }
1568 
1569   @Test
1570   @Config(minSdk = R)
newInstance_alreadyKnowsSubId()1571   public void newInstance_alreadyKnowsSubId() {
1572     Context context = ApplicationProvider.getApplicationContext();
1573     Class<?>[] parameters = new Class<?>[] {Context.class, int.class};
1574     Object[] arguments = new Object[] {context, 123};
1575     TelephonyManager tm = Shadow.newInstance(TelephonyManager.class, parameters, arguments);
1576 
1577     assertThat(tm.getSubscriptionId()).isEqualTo(123);
1578   }
1579 
1580   @Test
1581   @Config(minSdk = Q)
setDataRoamingEnabledChangesIsDataRoamingEnabled()1582   public void setDataRoamingEnabledChangesIsDataRoamingEnabled() {
1583     shadowOf(telephonyManager).setDataRoamingEnabled(false);
1584     assertThat(telephonyManager.isDataRoamingEnabled()).isFalse();
1585     shadowOf(telephonyManager).setDataRoamingEnabled(true);
1586     assertThat(telephonyManager.isDataRoamingEnabled()).isTrue();
1587   }
1588 
1589   @Test
1590   @Config(minSdk = Q)
setCarrierRestrictionRules_changesCarrierRestrictionRules()1591   public void setCarrierRestrictionRules_changesCarrierRestrictionRules() {
1592     CarrierRestrictionRules carrierRestrictionRules = CarrierRestrictionRules.newBuilder().build();
1593     shadowOf(telephonyManager).setCarrierRestrictionRules(carrierRestrictionRules);
1594 
1595     assertThat(telephonyManager.getCarrierRestrictionRules()).isEqualTo(carrierRestrictionRules);
1596   }
1597 
1598   @Test()
1599   @Config(minSdk = Q)
setCarrierRestrictionRules_throwsIllegalStateException()1600   public void setCarrierRestrictionRules_throwsIllegalStateException() {
1601     assertThrows(
1602         IllegalStateException.class,
1603         () -> shadowTelephonyManager.setCarrierRestrictionRules(new Object()));
1604   }
1605 
1606   @Test
1607   @Config(minSdk = Q)
rebootModem_rebootsModem()1608   public void rebootModem_rebootsModem() {
1609     shadowOf((Application) ApplicationProvider.getApplicationContext())
1610         .grantPermissions(permission.MODIFY_PHONE_STATE);
1611 
1612     shadowTelephonyManager.rebootModem();
1613 
1614     assertThat(shadowTelephonyManager.getModemRebootCount()).isEqualTo(1);
1615   }
1616 
1617   @Test()
1618   @Config(minSdk = Q)
rebootModem_noModifyPhoneStatePermission_throwsSecurityException()1619   public void rebootModem_noModifyPhoneStatePermission_throwsSecurityException() {
1620     assertThrows(SecurityException.class, () -> shadowTelephonyManager.rebootModem());
1621   }
1622 
1623   @Test
1624   @Config(minSdk = O)
telephonyManager_activityContextEnabled_differentInstancesRetrievePhoneCount()1625   public void telephonyManager_activityContextEnabled_differentInstancesRetrievePhoneCount() {
1626     String originalProperty = System.getProperty("robolectric.createActivityContexts", "");
1627     System.setProperty("robolectric.createActivityContexts", "true");
1628     try (ActivityController<Activity> controller =
1629         Robolectric.buildActivity(Activity.class).setup()) {
1630       TelephonyManager applicationTelephonyManager =
1631           (TelephonyManager)
1632               ApplicationProvider.getApplicationContext()
1633                   .getSystemService(Context.TELEPHONY_SERVICE);
1634       Activity activity = controller.get();
1635       TelephonyManager activityTelephonyManager =
1636           (TelephonyManager) activity.getSystemService(Context.TELEPHONY_SERVICE);
1637 
1638       assertThat(applicationTelephonyManager).isNotSameInstanceAs(activityTelephonyManager);
1639 
1640       int applicationPhoneCount = applicationTelephonyManager.getPhoneCount();
1641       int activityPhoneCount = activityTelephonyManager.getPhoneCount();
1642 
1643       assertThat(activityPhoneCount).isEqualTo(applicationPhoneCount);
1644     } finally {
1645       System.setProperty("robolectric.createActivityContexts", originalProperty);
1646     }
1647   }
1648 }
1649