1 package org.robolectric.shadows; 2 3 import static android.content.Context.TELEPHONY_SERVICE; 4 import static android.os.Build.VERSION_CODES.JELLY_BEAN; 5 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1; 6 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2; 7 import static android.os.Build.VERSION_CODES.LOLLIPOP_MR1; 8 import static android.os.Build.VERSION_CODES.M; 9 import static android.os.Build.VERSION_CODES.N; 10 import static android.os.Build.VERSION_CODES.O; 11 import static android.os.Build.VERSION_CODES.P; 12 import static android.os.Build.VERSION_CODES.Q; 13 import static android.os.Build.VERSION_CODES.R; 14 import static android.os.Build.VERSION_CODES.S; 15 import static android.telephony.PhoneStateListener.LISTEN_CALL_STATE; 16 import static android.telephony.PhoneStateListener.LISTEN_CELL_INFO; 17 import static android.telephony.PhoneStateListener.LISTEN_CELL_LOCATION; 18 import static android.telephony.PhoneStateListener.LISTEN_DISPLAY_INFO_CHANGED; 19 import static android.telephony.PhoneStateListener.LISTEN_SERVICE_STATE; 20 import static android.telephony.PhoneStateListener.LISTEN_SIGNAL_STRENGTHS; 21 import static android.telephony.TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_LTE_CA; 22 import static android.telephony.TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NONE; 23 import static android.telephony.TelephonyDisplayInfo.OVERRIDE_NETWORK_TYPE_NR_NSA; 24 import static android.telephony.TelephonyManager.CALL_COMPOSER_STATUS_ON; 25 import static android.telephony.TelephonyManager.CALL_STATE_IDLE; 26 import static android.telephony.TelephonyManager.CALL_STATE_OFFHOOK; 27 import static android.telephony.TelephonyManager.CALL_STATE_RINGING; 28 import static android.telephony.TelephonyManager.NETWORK_TYPE_EVDO_0; 29 import static android.telephony.TelephonyManager.NETWORK_TYPE_LTE; 30 import static android.telephony.emergency.EmergencyNumber.EMERGENCY_NUMBER_SOURCE_DATABASE; 31 import static android.telephony.emergency.EmergencyNumber.EMERGENCY_SERVICE_CATEGORY_POLICE; 32 import static com.google.common.truth.Truth.assertThat; 33 import static com.google.common.util.concurrent.MoreExecutors.directExecutor; 34 import static org.junit.Assert.assertEquals; 35 import static org.junit.Assert.assertFalse; 36 import static org.junit.Assert.assertNotEquals; 37 import static org.junit.Assert.assertNotNull; 38 import static org.junit.Assert.assertThrows; 39 import static org.junit.Assert.assertTrue; 40 import static org.mockito.Mockito.mock; 41 import static org.mockito.Mockito.reset; 42 import static org.mockito.Mockito.times; 43 import static org.mockito.Mockito.verify; 44 import static org.mockito.Mockito.verifyNoMoreInteractions; 45 import static org.mockito.Mockito.withSettings; 46 import static org.robolectric.RuntimeEnvironment.getApplication; 47 import static org.robolectric.Shadows.shadowOf; 48 import static org.robolectric.shadows.ShadowTelephonyManager.createTelephonyDisplayInfo; 49 50 import android.Manifest.permission; 51 import android.app.Application; 52 import android.content.ComponentName; 53 import android.content.Context; 54 import android.content.Intent; 55 import android.net.Uri; 56 import android.os.Build.VERSION; 57 import android.os.PersistableBundle; 58 import android.telecom.PhoneAccountHandle; 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.AuthenticationFailureReason; 76 import android.telephony.TelephonyManager.BootstrapAuthenticationCallback; 77 import android.telephony.TelephonyManager.CellInfoCallback; 78 import android.telephony.UiccSlotInfo; 79 import android.telephony.VisualVoicemailSmsFilterSettings; 80 import android.telephony.emergency.EmergencyNumber; 81 import android.telephony.gba.UaSecurityProtocolIdentifier; 82 import androidx.test.core.app.ApplicationProvider; 83 import androidx.test.ext.junit.runners.AndroidJUnit4; 84 import com.google.common.collect.ImmutableList; 85 import com.google.common.collect.ImmutableMap; 86 import java.util.Collections; 87 import java.util.List; 88 import java.util.Locale; 89 import java.util.concurrent.CountDownLatch; 90 import java.util.concurrent.Executor; 91 import java.util.concurrent.TimeUnit; 92 import org.junit.Before; 93 import org.junit.Test; 94 import org.junit.runner.RunWith; 95 import org.robolectric.annotation.Config; 96 import org.robolectric.shadow.api.Shadow; 97 import org.robolectric.util.ReflectionHelpers; 98 import org.robolectric.util.ReflectionHelpers.ClassParameter; 99 100 @RunWith(AndroidJUnit4.class) 101 @Config(minSdk = JELLY_BEAN) 102 public class ShadowTelephonyManagerTest { 103 104 private TelephonyManager telephonyManager; 105 private ShadowTelephonyManager shadowTelephonyManager; 106 107 @Before setUp()108 public void setUp() throws Exception { 109 telephonyManager = (TelephonyManager) getApplication().getSystemService(TELEPHONY_SERVICE); 110 shadowTelephonyManager = Shadow.extract(telephonyManager); 111 shadowOf((Application) ApplicationProvider.getApplicationContext()) 112 .grantPermissions(permission.READ_PRIVILEGED_PHONE_STATE); 113 } 114 115 @Test testListenInit()116 public void testListenInit() { 117 PhoneStateListener listener = mock(PhoneStateListener.class); 118 telephonyManager.listen(listener, LISTEN_CALL_STATE | LISTEN_CELL_INFO | LISTEN_CELL_LOCATION); 119 120 verify(listener).onCallStateChanged(CALL_STATE_IDLE, null); 121 verify(listener).onCellLocationChanged(null); 122 if (VERSION.SDK_INT >= JELLY_BEAN_MR1) { 123 verify(listener).onCellInfoChanged(Collections.emptyList()); 124 } 125 } 126 127 @Test 128 @Config(minSdk = S) registerTelephonyCallback_shouldInitCallback()129 public void registerTelephonyCallback_shouldInitCallback() { 130 TelephonyCallback callback = 131 mock( 132 TelephonyCallback.class, 133 withSettings() 134 .extraInterfaces( 135 CallStateListener.class, CellInfoListener.class, CellLocationListener.class)); 136 telephonyManager.registerTelephonyCallback(directExecutor(), callback); 137 138 assertThat(shadowTelephonyManager.getLastTelephonyCallback()).isEqualTo(callback); 139 verify((CallStateListener) callback).onCallStateChanged(CALL_STATE_IDLE); 140 verify((CellInfoListener) callback).onCellInfoChanged(ImmutableList.of()); 141 verify((CellLocationListener) callback).onCellLocationChanged(null); 142 } 143 144 @Test 145 @Config(minSdk = S) unregisterTelephonyCallback_shouldRemoveCallback()146 public void unregisterTelephonyCallback_shouldRemoveCallback() { 147 TelephonyCallback callback = 148 mock(TelephonyCallback.class, withSettings().extraInterfaces(CallStateListener.class)); 149 telephonyManager.registerTelephonyCallback(directExecutor(), callback); 150 reset(callback); 151 telephonyManager.unregisterTelephonyCallback(callback); 152 153 shadowOf(telephonyManager).setCallState(CALL_STATE_RINGING, "123"); 154 155 verifyNoMoreInteractions(callback); 156 } 157 158 @Test shouldGiveDeviceId()159 public void shouldGiveDeviceId() { 160 String testId = "TESTING123"; 161 shadowOf(telephonyManager).setDeviceId(testId); 162 assertEquals(testId, telephonyManager.getDeviceId()); 163 } 164 165 @Test 166 @Config(minSdk = M) shouldGiveDeviceIdForSlot()167 public void shouldGiveDeviceIdForSlot() { 168 shadowOf(telephonyManager).setDeviceId(1, "device in slot 1"); 169 shadowOf(telephonyManager).setDeviceId(2, "device in slot 2"); 170 171 assertEquals("device in slot 1", telephonyManager.getDeviceId(1)); 172 assertEquals("device in slot 2", telephonyManager.getDeviceId(2)); 173 } 174 175 @Test shouldGiveDeviceSoftwareVersion()176 public void shouldGiveDeviceSoftwareVersion() { 177 String testSoftwareVersion = "getDeviceSoftwareVersion"; 178 shadowOf(telephonyManager).setDeviceSoftwareVersion(testSoftwareVersion); 179 assertEquals(testSoftwareVersion, telephonyManager.getDeviceSoftwareVersion()); 180 } 181 182 @Test 183 @Config(minSdk = O) getImei()184 public void getImei() { 185 String testImei = "4test imei"; 186 shadowOf(telephonyManager).setImei(testImei); 187 assertEquals(testImei, telephonyManager.getImei()); 188 } 189 190 @Test 191 @Config(minSdk = O) getImeiForSlot()192 public void getImeiForSlot() { 193 shadowOf(telephonyManager).setImei("defaultImei"); 194 shadowOf(telephonyManager).setImei(0, "imei0"); 195 shadowOf(telephonyManager).setImei(1, "imei1"); 196 assertEquals("imei0", telephonyManager.getImei(0)); 197 assertEquals("imei1", telephonyManager.getImei(1)); 198 } 199 200 @Test 201 @Config(minSdk = O) getMeid()202 public void getMeid() { 203 String testMeid = "4test meid"; 204 shadowOf(telephonyManager).setMeid(testMeid); 205 assertEquals(testMeid, telephonyManager.getMeid()); 206 } 207 208 @Test 209 @Config(minSdk = O) getMeidForSlot()210 public void getMeidForSlot() { 211 shadowOf(telephonyManager).setMeid("defaultMeid"); 212 shadowOf(telephonyManager).setMeid(0, "meid0"); 213 shadowOf(telephonyManager).setMeid(1, "meid1"); 214 assertEquals("meid0", telephonyManager.getMeid(0)); 215 assertEquals("meid1", telephonyManager.getMeid(1)); 216 } 217 218 @Test shouldGiveNetworkOperatorName()219 public void shouldGiveNetworkOperatorName() { 220 shadowOf(telephonyManager).setNetworkOperatorName("SomeOperatorName"); 221 assertEquals("SomeOperatorName", telephonyManager.getNetworkOperatorName()); 222 } 223 224 @Test shouldGiveSimOperatorName()225 public void shouldGiveSimOperatorName() { 226 shadowOf(telephonyManager).setSimOperatorName("SomeSimOperatorName"); 227 assertEquals("SomeSimOperatorName", telephonyManager.getSimOperatorName()); 228 } 229 230 @Test(expected = SecurityException.class) 231 public void getSimSerialNumber_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted()232 getSimSerialNumber_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted() 233 throws Exception { 234 shadowOf(telephonyManager).setReadPhoneStatePermission(false); 235 telephonyManager.getSimSerialNumber(); 236 } 237 238 @Test shouldGetSimSerialNumber()239 public void shouldGetSimSerialNumber() { 240 shadowOf(telephonyManager).setSimSerialNumber("SomeSerialNumber"); 241 assertEquals("SomeSerialNumber", telephonyManager.getSimSerialNumber()); 242 } 243 244 @Test shouldGiveNetworkType()245 public void shouldGiveNetworkType() { 246 shadowOf(telephonyManager).setNetworkType(TelephonyManager.NETWORK_TYPE_CDMA); 247 assertEquals(TelephonyManager.NETWORK_TYPE_CDMA, telephonyManager.getNetworkType()); 248 } 249 250 @Test 251 @Config(minSdk = N) shouldGiveDataNetworkType()252 public void shouldGiveDataNetworkType() { 253 shadowOf(telephonyManager).setDataNetworkType(TelephonyManager.NETWORK_TYPE_CDMA); 254 assertEquals(TelephonyManager.NETWORK_TYPE_CDMA, telephonyManager.getDataNetworkType()); 255 } 256 257 @Test 258 @Config(minSdk = N) shouldGiveVoiceNetworkType()259 public void shouldGiveVoiceNetworkType() { 260 shadowOf(telephonyManager).setVoiceNetworkType(TelephonyManager.NETWORK_TYPE_CDMA); 261 assertThat(telephonyManager.getVoiceNetworkType()) 262 .isEqualTo(TelephonyManager.NETWORK_TYPE_CDMA); 263 } 264 265 @Test 266 @Config(minSdk = JELLY_BEAN_MR1) shouldGiveAllCellInfo()267 public void shouldGiveAllCellInfo() { 268 PhoneStateListener listener = mock(PhoneStateListener.class); 269 telephonyManager.listen(listener, LISTEN_CELL_INFO); 270 271 List<CellInfo> allCellInfo = Collections.singletonList(mock(CellInfo.class)); 272 shadowOf(telephonyManager).setAllCellInfo(allCellInfo); 273 assertEquals(allCellInfo, telephonyManager.getAllCellInfo()); 274 verify(listener).onCellInfoChanged(allCellInfo); 275 } 276 277 @Test 278 @Config(minSdk = S) shouldGiveAllCellInfo_toCallback()279 public void shouldGiveAllCellInfo_toCallback() { 280 TelephonyCallback callback = 281 mock(TelephonyCallback.class, withSettings().extraInterfaces(CellInfoListener.class)); 282 telephonyManager.registerTelephonyCallback(directExecutor(), callback); 283 284 List<CellInfo> allCellInfo = Collections.singletonList(mock(CellInfo.class)); 285 shadowOf(telephonyManager).setAllCellInfo(allCellInfo); 286 assertEquals(allCellInfo, telephonyManager.getAllCellInfo()); 287 verify((CellInfoListener) callback).onCellInfoChanged(allCellInfo); 288 } 289 290 @Test 291 @Config(minSdk = Q) shouldGiveCellInfoUpdate()292 public void shouldGiveCellInfoUpdate() throws Exception { 293 List<CellInfo> callbackCellInfo = Collections.singletonList(mock(CellInfo.class)); 294 shadowOf(telephonyManager).setCallbackCellInfos(callbackCellInfo); 295 assertNotEquals(callbackCellInfo, telephonyManager.getAllCellInfo()); 296 297 CountDownLatch callbackLatch = new CountDownLatch(1); 298 shadowOf(telephonyManager) 299 .requestCellInfoUpdate( 300 new Executor() { 301 @Override 302 public void execute(Runnable r) { 303 r.run(); 304 } 305 }, 306 new CellInfoCallback() { 307 @Override 308 public void onCellInfo(List<CellInfo> list) { 309 assertEquals(callbackCellInfo, list); 310 callbackLatch.countDown(); 311 } 312 }); 313 314 assertTrue(callbackLatch.await(5000, TimeUnit.MILLISECONDS)); 315 } 316 317 @Test shouldGiveNetworkCountryIsoInLowercase()318 public void shouldGiveNetworkCountryIsoInLowercase() { 319 shadowOf(telephonyManager).setNetworkCountryIso("SomeIso"); 320 assertEquals("someiso", telephonyManager.getNetworkCountryIso()); 321 } 322 323 @Test 324 @Config(minSdk = Q) shouldGiveSimLocale()325 public void shouldGiveSimLocale() { 326 shadowOf(telephonyManager).setSimLocale(Locale.FRANCE); 327 assertEquals(Locale.FRANCE, telephonyManager.getSimLocale()); 328 } 329 330 @Test shouldGiveNetworkOperator()331 public void shouldGiveNetworkOperator() { 332 shadowOf(telephonyManager).setNetworkOperator("SomeOperator"); 333 assertEquals("SomeOperator", telephonyManager.getNetworkOperator()); 334 } 335 336 @Test 337 @Config(minSdk = O) shouldGiveNetworkSpecifier()338 public void shouldGiveNetworkSpecifier() { 339 shadowOf(telephonyManager).setNetworkSpecifier("SomeSpecifier"); 340 assertEquals("SomeSpecifier", telephonyManager.getNetworkSpecifier()); 341 } 342 343 @Test shouldGiveLine1Number()344 public void shouldGiveLine1Number() { 345 shadowOf(telephonyManager).setLine1Number("123-244-2222"); 346 assertEquals("123-244-2222", telephonyManager.getLine1Number()); 347 } 348 349 @Test 350 @Config(minSdk = JELLY_BEAN_MR2) shouldGiveGroupIdLevel1()351 public void shouldGiveGroupIdLevel1() { 352 shadowOf(telephonyManager).setGroupIdLevel1("SomeGroupId"); 353 assertEquals("SomeGroupId", telephonyManager.getGroupIdLevel1()); 354 } 355 356 @Test(expected = SecurityException.class) getDeviceId_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted()357 public void getDeviceId_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted() 358 throws Exception { 359 shadowOf(telephonyManager).setReadPhoneStatePermission(false); 360 telephonyManager.getDeviceId(); 361 } 362 363 @Test 364 @Config(minSdk = M) 365 public void getDeviceIdForSlot_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted()366 getDeviceIdForSlot_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted() 367 throws Exception { 368 shadowOf(telephonyManager).setReadPhoneStatePermission(false); 369 assertThrows(SecurityException.class, () -> telephonyManager.getDeviceId(1)); 370 } 371 372 @Test 373 public void getDeviceSoftwareVersion_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted()374 getDeviceSoftwareVersion_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted() 375 throws Exception { 376 shadowOf(telephonyManager).setReadPhoneStatePermission(false); 377 assertThrows(SecurityException.class, () -> telephonyManager.getDeviceSoftwareVersion()); 378 } 379 380 @Test shouldGivePhoneType()381 public void shouldGivePhoneType() { 382 shadowOf(telephonyManager).setPhoneType(TelephonyManager.PHONE_TYPE_CDMA); 383 assertEquals(TelephonyManager.PHONE_TYPE_CDMA, telephonyManager.getPhoneType()); 384 shadowOf(telephonyManager).setPhoneType(TelephonyManager.PHONE_TYPE_GSM); 385 assertEquals(TelephonyManager.PHONE_TYPE_GSM, telephonyManager.getPhoneType()); 386 } 387 388 @Test shouldGiveCellLocation()389 public void shouldGiveCellLocation() { 390 PhoneStateListener listener = mock(PhoneStateListener.class); 391 telephonyManager.listen(listener, LISTEN_CELL_LOCATION); 392 393 CellLocation mockCellLocation = mock(CellLocation.class); 394 shadowOf(telephonyManager).setCellLocation(mockCellLocation); 395 assertEquals(mockCellLocation, telephonyManager.getCellLocation()); 396 verify(listener).onCellLocationChanged(mockCellLocation); 397 } 398 399 @Test 400 @Config(minSdk = S) shouldGiveCellLocation_toCallback()401 public void shouldGiveCellLocation_toCallback() { 402 TelephonyCallback callback = 403 mock(TelephonyCallback.class, withSettings().extraInterfaces(CellLocationListener.class)); 404 telephonyManager.registerTelephonyCallback(directExecutor(), callback); 405 406 CellLocation mockCellLocation = mock(CellLocation.class); 407 shadowOf(telephonyManager).setCellLocation(mockCellLocation); 408 assertEquals(mockCellLocation, telephonyManager.getCellLocation()); 409 verify((CellLocationListener) callback).onCellLocationChanged(mockCellLocation); 410 } 411 412 @Test 413 @Config(minSdk = S) shouldGiveCallStateForSubscription()414 public void shouldGiveCallStateForSubscription() { 415 PhoneStateListener listener = mock(PhoneStateListener.class); 416 telephonyManager.listen(listener, LISTEN_CALL_STATE); 417 418 shadowOf(telephonyManager).setCallState(CALL_STATE_RINGING, "911"); 419 assertEquals(CALL_STATE_RINGING, telephonyManager.getCallStateForSubscription()); 420 verify(listener).onCallStateChanged(CALL_STATE_RINGING, "911"); 421 422 shadowOf(telephonyManager).setCallState(CALL_STATE_OFFHOOK, "911"); 423 assertEquals(CALL_STATE_OFFHOOK, telephonyManager.getCallStateForSubscription()); 424 verify(listener).onCallStateChanged(CALL_STATE_OFFHOOK, null); 425 } 426 427 @Test shouldGiveCallState()428 public void shouldGiveCallState() { 429 PhoneStateListener listener = mock(PhoneStateListener.class); 430 telephonyManager.listen(listener, LISTEN_CALL_STATE); 431 432 shadowOf(telephonyManager).setCallState(CALL_STATE_RINGING, "911"); 433 assertEquals(CALL_STATE_RINGING, telephonyManager.getCallState()); 434 verify(listener).onCallStateChanged(CALL_STATE_RINGING, "911"); 435 436 shadowOf(telephonyManager).setCallState(CALL_STATE_OFFHOOK, "911"); 437 assertEquals(CALL_STATE_OFFHOOK, telephonyManager.getCallState()); 438 verify(listener).onCallStateChanged(CALL_STATE_OFFHOOK, null); 439 } 440 441 @Test 442 @Config(minSdk = S) shouldGiveCallState_toCallback()443 public void shouldGiveCallState_toCallback() { 444 TelephonyCallback callback = 445 mock(TelephonyCallback.class, withSettings().extraInterfaces(CallStateListener.class)); 446 telephonyManager.registerTelephonyCallback(directExecutor(), callback); 447 448 shadowOf(telephonyManager).setCallState(CALL_STATE_RINGING, "911"); 449 assertEquals(CALL_STATE_RINGING, telephonyManager.getCallState()); 450 verify((CallStateListener) callback).onCallStateChanged(CALL_STATE_RINGING); 451 452 shadowOf(telephonyManager).setCallState(CALL_STATE_OFFHOOK, "911"); 453 assertEquals(CALL_STATE_OFFHOOK, telephonyManager.getCallState()); 454 verify((CallStateListener) callback).onCallStateChanged(CALL_STATE_OFFHOOK); 455 } 456 457 @Test isSmsCapable()458 public void isSmsCapable() { 459 assertThat(telephonyManager.isSmsCapable()).isTrue(); 460 shadowOf(telephonyManager).setIsSmsCapable(false); 461 assertThat(telephonyManager.isSmsCapable()).isFalse(); 462 } 463 464 @Test 465 @Config(minSdk = O) shouldGiveCarrierConfigIfSet()466 public void shouldGiveCarrierConfigIfSet() { 467 PersistableBundle bundle = new PersistableBundle(); 468 bundle.putInt("foo", 42); 469 shadowOf(telephonyManager).setCarrierConfig(bundle); 470 471 assertEquals(bundle, telephonyManager.getCarrierConfig()); 472 } 473 474 @Test 475 @Config(minSdk = O) shouldGiveNonNullCarrierConfigIfNotSet()476 public void shouldGiveNonNullCarrierConfigIfNotSet() { 477 assertNotNull(telephonyManager.getCarrierConfig()); 478 } 479 480 @Test shouldGiveVoiceMailNumber()481 public void shouldGiveVoiceMailNumber() { 482 shadowOf(telephonyManager).setVoiceMailNumber("123"); 483 484 assertEquals("123", telephonyManager.getVoiceMailNumber()); 485 } 486 487 @Test shouldGiveVoiceMailAlphaTag()488 public void shouldGiveVoiceMailAlphaTag() { 489 shadowOf(telephonyManager).setVoiceMailAlphaTag("tag"); 490 491 assertEquals("tag", telephonyManager.getVoiceMailAlphaTag()); 492 } 493 494 @Test 495 @Config(minSdk = M) shouldGivePhoneCount()496 public void shouldGivePhoneCount() { 497 shadowOf(telephonyManager).setPhoneCount(42); 498 499 assertEquals(42, telephonyManager.getPhoneCount()); 500 } 501 502 @Test 503 @Config(minSdk = R) shouldGiveDefaultActiveModemCount()504 public void shouldGiveDefaultActiveModemCount() { 505 assertThat(telephonyManager.getActiveModemCount()).isEqualTo(1); 506 } 507 508 @Test 509 @Config(minSdk = R) shouldGiveActiveModemCount()510 public void shouldGiveActiveModemCount() { 511 shadowOf(telephonyManager).setActiveModemCount(42); 512 513 assertThat(telephonyManager.getActiveModemCount()).isEqualTo(42); 514 } 515 516 @Test 517 @Config(minSdk = LOLLIPOP_MR1) shouldGiveVoiceCapableTrue()518 public void shouldGiveVoiceCapableTrue() { 519 shadowOf(telephonyManager).setVoiceCapable(true); 520 521 assertTrue(telephonyManager.isVoiceCapable()); 522 } 523 524 @Test 525 @Config(minSdk = LOLLIPOP_MR1) shouldGiveVoiceCapableFalse()526 public void shouldGiveVoiceCapableFalse() { 527 shadowOf(telephonyManager).setVoiceCapable(false); 528 529 assertFalse(telephonyManager.isVoiceCapable()); 530 } 531 532 @Test 533 @Config(minSdk = N) shouldGiveVoiceVibrationEnabled()534 public void shouldGiveVoiceVibrationEnabled() { 535 PhoneAccountHandle phoneAccountHandle = 536 new PhoneAccountHandle( 537 new ComponentName(ApplicationProvider.getApplicationContext(), Object.class), "handle"); 538 539 shadowOf(telephonyManager).setVoicemailVibrationEnabled(phoneAccountHandle, true); 540 541 assertTrue(telephonyManager.isVoicemailVibrationEnabled(phoneAccountHandle)); 542 } 543 544 @Test 545 @Config(minSdk = N) shouldGiveVoicemailRingtoneUri()546 public void shouldGiveVoicemailRingtoneUri() { 547 PhoneAccountHandle phoneAccountHandle = 548 new PhoneAccountHandle( 549 new ComponentName(ApplicationProvider.getApplicationContext(), Object.class), "handle"); 550 Uri ringtoneUri = Uri.fromParts("file", "ringtone.mp3", /* fragment= */ null); 551 552 shadowOf(telephonyManager).setVoicemailRingtoneUri(phoneAccountHandle, ringtoneUri); 553 554 assertEquals(ringtoneUri, telephonyManager.getVoicemailRingtoneUri(phoneAccountHandle)); 555 } 556 557 @Test 558 @Config(minSdk = O) // The setter on the real manager was added in O shouldSetVoicemailRingtoneUri()559 public void shouldSetVoicemailRingtoneUri() { 560 PhoneAccountHandle phoneAccountHandle = 561 new PhoneAccountHandle( 562 new ComponentName(ApplicationProvider.getApplicationContext(), Object.class), "handle"); 563 Uri ringtoneUri = Uri.fromParts("file", "ringtone.mp3", /* fragment= */ null); 564 565 // Note: Using the real manager to set, instead of the shadow. 566 telephonyManager.setVoicemailRingtoneUri(phoneAccountHandle, ringtoneUri); 567 568 assertEquals(ringtoneUri, telephonyManager.getVoicemailRingtoneUri(phoneAccountHandle)); 569 } 570 571 @Test 572 @Config(minSdk = O) shouldCreateForPhoneAccountHandle()573 public void shouldCreateForPhoneAccountHandle() { 574 PhoneAccountHandle phoneAccountHandle = 575 new PhoneAccountHandle( 576 new ComponentName(ApplicationProvider.getApplicationContext(), Object.class), "handle"); 577 TelephonyManager mockTelephonyManager = mock(TelephonyManager.class); 578 579 shadowOf(telephonyManager) 580 .setTelephonyManagerForHandle(phoneAccountHandle, mockTelephonyManager); 581 582 assertEquals( 583 mockTelephonyManager, telephonyManager.createForPhoneAccountHandle(phoneAccountHandle)); 584 } 585 586 @Test 587 @Config(minSdk = N) shouldCreateForSubscriptionId()588 public void shouldCreateForSubscriptionId() { 589 int subscriptionId = 42; 590 TelephonyManager mockTelephonyManager = mock(TelephonyManager.class); 591 592 shadowOf(telephonyManager) 593 .setTelephonyManagerForSubscriptionId(subscriptionId, mockTelephonyManager); 594 595 assertEquals(mockTelephonyManager, telephonyManager.createForSubscriptionId(subscriptionId)); 596 } 597 598 @Test 599 @Config(minSdk = O) shouldSetServiceState()600 public void shouldSetServiceState() { 601 PhoneStateListener listener = mock(PhoneStateListener.class); 602 telephonyManager.listen(listener, LISTEN_SERVICE_STATE); 603 ServiceState serviceState = new ServiceState(); 604 serviceState.setState(ServiceState.STATE_OUT_OF_SERVICE); 605 606 shadowOf(telephonyManager).setServiceState(serviceState); 607 608 assertEquals(serviceState, telephonyManager.getServiceState()); 609 verify(listener).onServiceStateChanged(serviceState); 610 } 611 612 @Test 613 @Config(minSdk = S) shouldSetServiceState_toCallback()614 public void shouldSetServiceState_toCallback() { 615 TelephonyCallback callback = 616 mock(TelephonyCallback.class, withSettings().extraInterfaces(ServiceStateListener.class)); 617 telephonyManager.registerTelephonyCallback(directExecutor(), callback); 618 ServiceState serviceState = new ServiceState(); 619 serviceState.setState(ServiceState.STATE_OUT_OF_SERVICE); 620 621 shadowOf(telephonyManager).setServiceState(serviceState); 622 623 assertEquals(serviceState, telephonyManager.getServiceState()); 624 verify((ServiceStateListener) callback).onServiceStateChanged(serviceState); 625 } 626 627 @Test 628 @Config(minSdk = O) listen_doesNotNotifyListenerOfCurrentServiceStateIfUninitialized()629 public void listen_doesNotNotifyListenerOfCurrentServiceStateIfUninitialized() { 630 PhoneStateListener listener = mock(PhoneStateListener.class); 631 632 telephonyManager.listen(listener, LISTEN_SERVICE_STATE); 633 634 verifyNoMoreInteractions(listener); 635 } 636 637 @Test 638 @Config(minSdk = S) register_doesNotNotifyCallbackOfCurrentServiceStateIfUninitialized()639 public void register_doesNotNotifyCallbackOfCurrentServiceStateIfUninitialized() { 640 TelephonyCallback callback = 641 mock(TelephonyCallback.class, withSettings().extraInterfaces(ServiceStateListener.class)); 642 643 telephonyManager.registerTelephonyCallback(directExecutor(), callback); 644 645 verifyNoMoreInteractions(callback); 646 } 647 648 @Test 649 @Config(minSdk = O) listen_notifiesListenerOfCurrentServiceStateIfInitialized()650 public void listen_notifiesListenerOfCurrentServiceStateIfInitialized() { 651 PhoneStateListener listener = mock(PhoneStateListener.class); 652 ServiceState serviceState = new ServiceState(); 653 serviceState.setState(ServiceState.STATE_OUT_OF_SERVICE); 654 shadowOf(telephonyManager).setServiceState(serviceState); 655 656 telephonyManager.listen(listener, LISTEN_SERVICE_STATE); 657 658 verify(listener, times(1)).onServiceStateChanged(serviceState); 659 } 660 661 @Test 662 @Config(minSdk = S) register_notifiesCallbackOfCurrentServiceStateIfInitialized()663 public void register_notifiesCallbackOfCurrentServiceStateIfInitialized() { 664 TelephonyCallback callback = 665 mock(TelephonyCallback.class, withSettings().extraInterfaces(ServiceStateListener.class)); 666 ServiceState serviceState = new ServiceState(); 667 serviceState.setState(ServiceState.STATE_OUT_OF_SERVICE); 668 shadowOf(telephonyManager).setServiceState(serviceState); 669 670 telephonyManager.registerTelephonyCallback(directExecutor(), callback); 671 672 verify((ServiceStateListener) callback, times(1)).onServiceStateChanged(serviceState); 673 } 674 675 @Test shouldSetIsNetworkRoaming()676 public void shouldSetIsNetworkRoaming() { 677 shadowOf(telephonyManager).setIsNetworkRoaming(true); 678 679 assertTrue(telephonyManager.isNetworkRoaming()); 680 } 681 682 @Test shouldGetSimState()683 public void shouldGetSimState() { 684 assertThat(telephonyManager.getSimState()).isEqualTo(TelephonyManager.SIM_STATE_READY); 685 } 686 687 @Test 688 @Config(minSdk = O) shouldGetSimStateUsingSlotNumber()689 public void shouldGetSimStateUsingSlotNumber() { 690 int expectedSimState = TelephonyManager.SIM_STATE_ABSENT; 691 int slotNumber = 3; 692 shadowOf(telephonyManager).setSimState(slotNumber, expectedSimState); 693 694 assertThat(telephonyManager.getSimState(slotNumber)).isEqualTo(expectedSimState); 695 } 696 697 @Test shouldGetSimIso()698 public void shouldGetSimIso() { 699 assertThat(telephonyManager.getSimCountryIso()).isEmpty(); 700 } 701 702 @Test 703 @Config(minSdk = N, maxSdk = Q) shouldGetSimIsoWhenSetUsingSlotNumber()704 public void shouldGetSimIsoWhenSetUsingSlotNumber() { 705 String expectedSimIso = "usa"; 706 int subId = 2; 707 shadowOf(telephonyManager).setSimCountryIso(subId, expectedSimIso); 708 709 assertThat( 710 (String) 711 ReflectionHelpers.callInstanceMethod( 712 telephonyManager, "getSimCountryIso", ClassParameter.from(int.class, subId))) 713 .isEqualTo(expectedSimIso); 714 } 715 716 @Test 717 @Config(minSdk = P) shouldGetSimCarrierId()718 public void shouldGetSimCarrierId() { 719 int expectedCarrierId = 132; 720 shadowOf(telephonyManager).setSimCarrierId(expectedCarrierId); 721 722 assertThat(telephonyManager.getSimCarrierId()).isEqualTo(expectedCarrierId); 723 } 724 725 @Test 726 @Config(minSdk = Q) shouldGetCarrierIdFromSimMccMnc()727 public void shouldGetCarrierIdFromSimMccMnc() { 728 int expectedCarrierId = 419; 729 shadowOf(telephonyManager).setCarrierIdFromSimMccMnc(expectedCarrierId); 730 731 assertThat(telephonyManager.getCarrierIdFromSimMccMnc()).isEqualTo(expectedCarrierId); 732 } 733 734 @Test 735 @Config(minSdk = M) shouldGetCurrentPhoneTypeGivenSubId()736 public void shouldGetCurrentPhoneTypeGivenSubId() { 737 int subId = 1; 738 int expectedPhoneType = TelephonyManager.PHONE_TYPE_GSM; 739 shadowOf(telephonyManager).setCurrentPhoneType(subId, expectedPhoneType); 740 741 assertThat(telephonyManager.getCurrentPhoneType(subId)).isEqualTo(expectedPhoneType); 742 } 743 744 @Test 745 @Config(minSdk = M) shouldGetCarrierPackageNamesForIntentAndPhone()746 public void shouldGetCarrierPackageNamesForIntentAndPhone() { 747 List<String> packages = Collections.singletonList("package1"); 748 int phoneId = 123; 749 shadowOf(telephonyManager).setCarrierPackageNamesForPhone(phoneId, packages); 750 751 assertThat(telephonyManager.getCarrierPackageNamesForIntentAndPhone(new Intent(), phoneId)) 752 .isEqualTo(packages); 753 } 754 755 @Test 756 @Config(minSdk = M) shouldGetCarrierPackageNamesForIntent()757 public void shouldGetCarrierPackageNamesForIntent() { 758 List<String> packages = Collections.singletonList("package1"); 759 shadowOf(telephonyManager) 760 .setCarrierPackageNamesForPhone(SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, packages); 761 762 assertThat(telephonyManager.getCarrierPackageNamesForIntent(new Intent())).isEqualTo(packages); 763 } 764 765 @Test resetSimStates_shouldRetainDefaultState()766 public void resetSimStates_shouldRetainDefaultState() { 767 shadowOf(telephonyManager).resetSimStates(); 768 769 assertThat(telephonyManager.getSimState()).isEqualTo(TelephonyManager.SIM_STATE_READY); 770 } 771 772 @Test 773 @Config(minSdk = N) resetSimCountryIsos_shouldRetainDefaultState()774 public void resetSimCountryIsos_shouldRetainDefaultState() { 775 shadowOf(telephonyManager).resetSimCountryIsos(); 776 777 assertThat(telephonyManager.getSimCountryIso()).isEmpty(); 778 } 779 780 @Test shouldSetSubscriberId()781 public void shouldSetSubscriberId() { 782 String subscriberId = "123451234512345"; 783 shadowOf(telephonyManager).setSubscriberId(subscriberId); 784 785 assertThat(telephonyManager.getSubscriberId()).isEqualTo(subscriberId); 786 } 787 788 @Test(expected = SecurityException.class) getSubscriberId_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted()789 public void getSubscriberId_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted() { 790 shadowOf(telephonyManager).setReadPhoneStatePermission(false); 791 telephonyManager.getSubscriberId(); 792 } 793 794 @Test 795 @Config(minSdk = P) getUiccSlotsInfo()796 public void getUiccSlotsInfo() { 797 UiccSlotInfo slotInfo1 = new UiccSlotInfo(true, true, null, 0, 0, true); 798 UiccSlotInfo slotInfo2 = new UiccSlotInfo(true, true, null, 0, 1, true); 799 UiccSlotInfo[] slotInfos = new UiccSlotInfo[] {slotInfo1, slotInfo2}; 800 shadowOf(telephonyManager).setUiccSlotsInfo(slotInfos); 801 802 assertThat(shadowOf(telephonyManager).getUiccSlotsInfo()).isEqualTo(slotInfos); 803 } 804 805 @Test 806 @Config(minSdk = O) shouldSetVisualVoicemailPackage()807 public void shouldSetVisualVoicemailPackage() { 808 shadowOf(telephonyManager).setVisualVoicemailPackageName("org.foo"); 809 810 assertThat(telephonyManager.getVisualVoicemailPackageName()).isEqualTo("org.foo"); 811 } 812 813 @Test 814 @Config(minSdk = P) canSetAndGetSignalStrength()815 public void canSetAndGetSignalStrength() { 816 SignalStrength ss = Shadow.newInstanceOf(SignalStrength.class); 817 shadowOf(telephonyManager).setSignalStrength(ss); 818 assertThat(telephonyManager.getSignalStrength()).isEqualTo(ss); 819 } 820 821 @Test 822 @Config(minSdk = P) shouldGiveSignalStrength()823 public void shouldGiveSignalStrength() { 824 PhoneStateListener listener = mock(PhoneStateListener.class); 825 telephonyManager.listen(listener, LISTEN_SIGNAL_STRENGTHS); 826 SignalStrength ss = Shadow.newInstanceOf(SignalStrength.class); 827 828 shadowOf(telephonyManager).setSignalStrength(ss); 829 830 verify(listener).onSignalStrengthsChanged(ss); 831 } 832 833 @Test 834 @Config(minSdk = S) shouldGiveSignalStrength_toCallback()835 public void shouldGiveSignalStrength_toCallback() { 836 TelephonyCallback callback = 837 mock( 838 TelephonyCallback.class, withSettings().extraInterfaces(SignalStrengthsListener.class)); 839 telephonyManager.registerTelephonyCallback(directExecutor(), callback); 840 SignalStrength ss = Shadow.newInstanceOf(SignalStrength.class); 841 842 shadowOf(telephonyManager).setSignalStrength(ss); 843 844 verify((SignalStrengthsListener) callback).onSignalStrengthsChanged(ss); 845 } 846 847 @Test 848 @Config(minSdk = O) setDataEnabledChangesIsDataEnabled()849 public void setDataEnabledChangesIsDataEnabled() { 850 shadowOf(telephonyManager).setDataEnabled(false); 851 assertThat(telephonyManager.isDataEnabled()).isFalse(); 852 shadowOf(telephonyManager).setDataEnabled(true); 853 assertThat(telephonyManager.isDataEnabled()).isTrue(); 854 } 855 856 @Test 857 @Config(minSdk = S) setDataEnabledForReasonChangesIsDataEnabledForReason()858 public void setDataEnabledForReasonChangesIsDataEnabledForReason() { 859 int correctReason = TelephonyManager.DATA_ENABLED_REASON_POLICY; 860 int incorrectReason = TelephonyManager.DATA_ENABLED_REASON_USER; 861 862 assertThat(telephonyManager.isDataEnabledForReason(correctReason)).isTrue(); 863 assertThat(telephonyManager.isDataEnabledForReason(incorrectReason)).isTrue(); 864 865 telephonyManager.setDataEnabledForReason(correctReason, false); 866 assertThat(telephonyManager.isDataEnabledForReason(correctReason)).isFalse(); 867 assertThat(telephonyManager.isDataEnabledForReason(incorrectReason)).isTrue(); 868 869 telephonyManager.setDataEnabledForReason(correctReason, true); 870 assertThat(telephonyManager.isDataEnabledForReason(correctReason)).isTrue(); 871 assertThat(telephonyManager.isDataEnabledForReason(incorrectReason)).isTrue(); 872 } 873 874 @Test setDataStateChangesDataState()875 public void setDataStateChangesDataState() { 876 assertThat(telephonyManager.getDataState()).isEqualTo(TelephonyManager.DATA_DISCONNECTED); 877 shadowOf(telephonyManager).setDataState(TelephonyManager.DATA_CONNECTING); 878 assertThat(telephonyManager.getDataState()).isEqualTo(TelephonyManager.DATA_CONNECTING); 879 shadowOf(telephonyManager).setDataState(TelephonyManager.DATA_CONNECTED); 880 assertThat(telephonyManager.getDataState()).isEqualTo(TelephonyManager.DATA_CONNECTED); 881 } 882 883 @Test setDataActivityChangesDataActivity()884 public void setDataActivityChangesDataActivity() { 885 assertThat(telephonyManager.getDataActivity()).isEqualTo(TelephonyManager.DATA_ACTIVITY_NONE); 886 shadowOf(telephonyManager).setDataActivity(TelephonyManager.DATA_ACTIVITY_IN); 887 assertThat(telephonyManager.getDataActivity()).isEqualTo(TelephonyManager.DATA_ACTIVITY_IN); 888 shadowOf(telephonyManager).setDataActivity(TelephonyManager.DATA_ACTIVITY_OUT); 889 assertThat(telephonyManager.getDataActivity()).isEqualTo(TelephonyManager.DATA_ACTIVITY_OUT); 890 } 891 892 @Test 893 @Config(minSdk = Q) setRttSupportedChangesIsRttSupported()894 public void setRttSupportedChangesIsRttSupported() { 895 shadowOf(telephonyManager).setRttSupported(false); 896 assertThat(telephonyManager.isRttSupported()).isFalse(); 897 shadowOf(telephonyManager).setRttSupported(true); 898 assertThat(telephonyManager.isRttSupported()).isTrue(); 899 } 900 901 @Test 902 @Config(minSdk = M) setTtyModeSupportedChangesIsTtyModeSupported()903 public void setTtyModeSupportedChangesIsTtyModeSupported() { 904 shadowOf(telephonyManager).setTtyModeSupported(false); 905 assertThat(telephonyManager.isTtyModeSupported()).isFalse(); 906 shadowOf(telephonyManager).setTtyModeSupported(true); 907 assertThat(telephonyManager.isTtyModeSupported()).isTrue(); 908 } 909 910 @Test 911 @Config(minSdk = M) 912 public void isTtyModeSupported_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted()913 isTtyModeSupported_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted() 914 throws Exception { 915 shadowOf(telephonyManager).setReadPhoneStatePermission(false); 916 assertThrows(SecurityException.class, () -> telephonyManager.isTtyModeSupported()); 917 } 918 919 @Test 920 @Config(minSdk = N) hasCarrierPrivilegesWithSubId()921 public void hasCarrierPrivilegesWithSubId() { 922 int subId = 3; 923 assertThat(telephonyManager.hasCarrierPrivileges(subId)).isFalse(); 924 shadowOf(telephonyManager).setHasCarrierPrivileges(subId, true); 925 assertThat(telephonyManager.hasCarrierPrivileges(subId)).isTrue(); 926 } 927 928 @Test 929 @Config(minSdk = O) sendDialerSpecialCode()930 public void sendDialerSpecialCode() { 931 shadowOf(telephonyManager).sendDialerSpecialCode("1234"); 932 shadowOf(telephonyManager).sendDialerSpecialCode("123456"); 933 shadowOf(telephonyManager).sendDialerSpecialCode("1234"); 934 935 assertThat(shadowOf(telephonyManager).getSentDialerSpecialCodes()) 936 .containsExactly("1234", "123456", "1234") 937 .inOrder(); 938 } 939 940 @Test 941 @Config(minSdk = M) setHearingAidCompatibilitySupportedChangesisHearingAidCompatibilitySupported()942 public void setHearingAidCompatibilitySupportedChangesisHearingAidCompatibilitySupported() { 943 shadowOf(telephonyManager).setHearingAidCompatibilitySupported(false); 944 assertThat(telephonyManager.isHearingAidCompatibilitySupported()).isFalse(); 945 shadowOf(telephonyManager).setHearingAidCompatibilitySupported(true); 946 assertThat(telephonyManager.isHearingAidCompatibilitySupported()).isTrue(); 947 } 948 949 @Test 950 @Config(minSdk = R) createTelephonyDisplayInfo_correctlyCreatesDisplayInfo()951 public void createTelephonyDisplayInfo_correctlyCreatesDisplayInfo() { 952 TelephonyDisplayInfo displayInfo = 953 (TelephonyDisplayInfo) 954 createTelephonyDisplayInfo(NETWORK_TYPE_LTE, OVERRIDE_NETWORK_TYPE_LTE_CA); 955 956 assertThat(displayInfo.getNetworkType()).isEqualTo(NETWORK_TYPE_LTE); 957 assertThat(displayInfo.getOverrideNetworkType()).isEqualTo(OVERRIDE_NETWORK_TYPE_LTE_CA); 958 } 959 960 @Test 961 @Config(minSdk = R) listen_doesNotNotifyListenerOfCurrentTelephonyDisplayInfoIfUninitialized()962 public void listen_doesNotNotifyListenerOfCurrentTelephonyDisplayInfoIfUninitialized() { 963 PhoneStateListener listener = mock(PhoneStateListener.class); 964 965 telephonyManager.listen(listener, LISTEN_DISPLAY_INFO_CHANGED); 966 967 verifyNoMoreInteractions(listener); 968 } 969 970 @Test 971 @Config(minSdk = S) register_doesNotNotifyCallbackOfCurrentTelephonyDisplayInfoIfUninitialized()972 public void register_doesNotNotifyCallbackOfCurrentTelephonyDisplayInfoIfUninitialized() { 973 TelephonyCallback callback = 974 mock(TelephonyCallback.class, withSettings().extraInterfaces(DisplayInfoListener.class)); 975 976 telephonyManager.registerTelephonyCallback(directExecutor(), callback); 977 978 verifyNoMoreInteractions(callback); 979 } 980 981 @Test 982 @Config(minSdk = R) listen_notifiesListenerOfCurrentTelephonyDisplayInfoIfInitialized()983 public void listen_notifiesListenerOfCurrentTelephonyDisplayInfoIfInitialized() { 984 PhoneStateListener listener = mock(PhoneStateListener.class); 985 TelephonyDisplayInfo displayInfo = 986 (TelephonyDisplayInfo) 987 createTelephonyDisplayInfo(NETWORK_TYPE_EVDO_0, OVERRIDE_NETWORK_TYPE_NONE); 988 shadowTelephonyManager.setTelephonyDisplayInfo(displayInfo); 989 990 telephonyManager.listen(listener, LISTEN_DISPLAY_INFO_CHANGED); 991 992 verify(listener, times(1)).onDisplayInfoChanged(displayInfo); 993 } 994 995 @Test 996 @Config(minSdk = S) register_notifiesCallbackOfCurrentTelephonyDisplayInfoIfInitialized()997 public void register_notifiesCallbackOfCurrentTelephonyDisplayInfoIfInitialized() { 998 TelephonyCallback callback = 999 mock(TelephonyCallback.class, withSettings().extraInterfaces(DisplayInfoListener.class)); 1000 TelephonyDisplayInfo displayInfo = 1001 (TelephonyDisplayInfo) 1002 createTelephonyDisplayInfo(NETWORK_TYPE_EVDO_0, OVERRIDE_NETWORK_TYPE_NONE); 1003 shadowTelephonyManager.setTelephonyDisplayInfo(displayInfo); 1004 1005 telephonyManager.registerTelephonyCallback(directExecutor(), callback); 1006 1007 verify((DisplayInfoListener) callback, times(1)).onDisplayInfoChanged(displayInfo); 1008 } 1009 1010 @Test 1011 @Config(minSdk = R) setTelephonyDisplayInfo_notifiesListeners()1012 public void setTelephonyDisplayInfo_notifiesListeners() { 1013 PhoneStateListener listener = mock(PhoneStateListener.class); 1014 TelephonyDisplayInfo displayInfo = 1015 (TelephonyDisplayInfo) 1016 createTelephonyDisplayInfo(NETWORK_TYPE_LTE, OVERRIDE_NETWORK_TYPE_NR_NSA); 1017 telephonyManager.listen(listener, LISTEN_DISPLAY_INFO_CHANGED); 1018 1019 shadowTelephonyManager.setTelephonyDisplayInfo(displayInfo); 1020 1021 verify(listener, times(1)).onDisplayInfoChanged(displayInfo); 1022 } 1023 1024 @Test 1025 @Config(minSdk = S) setTelephonyDisplayInfo_notifiesCallback()1026 public void setTelephonyDisplayInfo_notifiesCallback() { 1027 TelephonyCallback callback = 1028 mock(TelephonyCallback.class, withSettings().extraInterfaces(DisplayInfoListener.class)); 1029 TelephonyDisplayInfo displayInfo = 1030 (TelephonyDisplayInfo) 1031 createTelephonyDisplayInfo(NETWORK_TYPE_LTE, OVERRIDE_NETWORK_TYPE_NR_NSA); 1032 shadowTelephonyManager.setTelephonyDisplayInfo(displayInfo); 1033 1034 telephonyManager.registerTelephonyCallback(directExecutor(), callback); 1035 1036 verify((DisplayInfoListener) callback, times(1)).onDisplayInfoChanged(displayInfo); 1037 } 1038 1039 @Test(expected = NullPointerException.class) 1040 @Config(minSdk = R) setTelephonyDisplayInfo_throwsNullPointerException()1041 public void setTelephonyDisplayInfo_throwsNullPointerException() { 1042 shadowTelephonyManager.setTelephonyDisplayInfo(null); 1043 } 1044 1045 @Test 1046 @Config(minSdk = R) isDataConnectionAllowed_returnsFalseByDefault()1047 public void isDataConnectionAllowed_returnsFalseByDefault() { 1048 assertThat(shadowTelephonyManager.isDataConnectionAllowed()).isFalse(); 1049 } 1050 1051 @Test 1052 @Config(minSdk = R) isDataConnectionAllowed_returnsFalseWhenSetToFalse()1053 public void isDataConnectionAllowed_returnsFalseWhenSetToFalse() { 1054 shadowTelephonyManager.setIsDataConnectionAllowed(false); 1055 1056 assertThat(shadowTelephonyManager.isDataConnectionAllowed()).isFalse(); 1057 } 1058 1059 @Test 1060 @Config(minSdk = R) isDataConnectionAllowed_returnsTrueWhenSetToTrue()1061 public void isDataConnectionAllowed_returnsTrueWhenSetToTrue() { 1062 shadowTelephonyManager.setIsDataConnectionAllowed(true); 1063 1064 assertThat(shadowTelephonyManager.isDataConnectionAllowed()).isTrue(); 1065 } 1066 1067 @Test 1068 @Config(minSdk = S) getCallComposerStatus_default()1069 public void getCallComposerStatus_default() { 1070 assertThat(telephonyManager.getCallComposerStatus()).isEqualTo(0); 1071 } 1072 1073 @Test 1074 @Config(minSdk = S) setCallComposerStatus()1075 public void setCallComposerStatus() { 1076 telephonyManager.setCallComposerStatus(CALL_COMPOSER_STATUS_ON); 1077 1078 assertThat(telephonyManager.getCallComposerStatus()).isEqualTo(CALL_COMPOSER_STATUS_ON); 1079 } 1080 1081 @Test 1082 @Config(minSdk = S) getBootstrapAuthenticationCallback()1083 public void getBootstrapAuthenticationCallback() { 1084 BootstrapAuthenticationCallback callback = 1085 new BootstrapAuthenticationCallback() { 1086 @Override 1087 public void onKeysAvailable(byte[] gbaKey, String transactionId) {} 1088 1089 @Override 1090 public void onAuthenticationFailure(@AuthenticationFailureReason int reason) {} 1091 }; 1092 1093 telephonyManager.bootstrapAuthenticationRequest( 1094 TelephonyManager.APPTYPE_ISIM, 1095 Uri.parse("tel:test-uri"), 1096 new UaSecurityProtocolIdentifier.Builder().build(), 1097 true, 1098 directExecutor(), 1099 callback); 1100 1101 assertThat(shadowTelephonyManager.getBootstrapAuthenticationCallback()).isEqualTo(callback); 1102 } 1103 1104 @Test 1105 @Config(minSdk = S) setPhoneCapability_returnsPhoneCapability()1106 public void setPhoneCapability_returnsPhoneCapability() { 1107 PhoneCapability phoneCapability = PhoneCapabilityFactory.create(2, 1, false, new int[0]); 1108 1109 shadowTelephonyManager.setPhoneCapability(phoneCapability); 1110 1111 assertThat(telephonyManager.getPhoneCapability()).isEqualTo(phoneCapability); 1112 } 1113 1114 @Test 1115 @Config(minSdk = O) sendVisualVoicemailSms_shouldStoreLastSendSmsParameters()1116 public void sendVisualVoicemailSms_shouldStoreLastSendSmsParameters() { 1117 telephonyManager.sendVisualVoicemailSms("destAddress", 0, "message", null); 1118 1119 ShadowTelephonyManager.VisualVoicemailSmsParams params = 1120 shadowOf(telephonyManager).getLastSentVisualVoicemailSmsParams(); 1121 1122 assertThat(params.getDestinationAddress()).isEqualTo("destAddress"); 1123 assertThat(params.getPort()).isEqualTo(0); 1124 assertThat(params.getText()).isEqualTo("message"); 1125 assertThat(params.getSentIntent()).isNull(); 1126 } 1127 1128 @Test 1129 @Config(minSdk = O) setVisualVoicemailSmsFilterSettings_shouldStoreSettings()1130 public void setVisualVoicemailSmsFilterSettings_shouldStoreSettings() { 1131 VisualVoicemailSmsFilterSettings settings = 1132 new VisualVoicemailSmsFilterSettings.Builder() 1133 .setClientPrefix("clientPrefix") 1134 .setDestinationPort(100) 1135 .build(); 1136 1137 telephonyManager.setVisualVoicemailSmsFilterSettings(settings); 1138 1139 assertThat(shadowOf(telephonyManager).getVisualVoicemailSmsFilterSettings()) 1140 .isEqualTo(settings); 1141 } 1142 1143 @Test 1144 @Config(minSdk = O) setVisualVoicemailSmsFilterSettings_setNullSettings_clearsSettings()1145 public void setVisualVoicemailSmsFilterSettings_setNullSettings_clearsSettings() { 1146 VisualVoicemailSmsFilterSettings settings = 1147 new VisualVoicemailSmsFilterSettings.Builder().build(); 1148 1149 telephonyManager.setVisualVoicemailSmsFilterSettings(settings); 1150 telephonyManager.setVisualVoicemailSmsFilterSettings(null); 1151 1152 assertThat(shadowOf(telephonyManager).getVisualVoicemailSmsFilterSettings()).isNull(); 1153 } 1154 1155 @Test 1156 @Config(minSdk = Q) isEmergencyNumber_telephonyServiceUnavailable_throwsIllegalStateException()1157 public void isEmergencyNumber_telephonyServiceUnavailable_throwsIllegalStateException() { 1158 ShadowServiceManager.setServiceAvailability(Context.TELEPHONY_SERVICE, false); 1159 1160 assertThrows(IllegalStateException.class, () -> telephonyManager.isEmergencyNumber("911")); 1161 } 1162 1163 @Test 1164 @Config(minSdk = O) 1165 public void getEmergencyCallbackMode_noReadPrivilegedPhoneStatePermission_throwsSecurityException()1166 getEmergencyCallbackMode_noReadPrivilegedPhoneStatePermission_throwsSecurityException() { 1167 shadowOf((Application) ApplicationProvider.getApplicationContext()) 1168 .denyPermissions(permission.READ_PRIVILEGED_PHONE_STATE); 1169 1170 assertThrows(SecurityException.class, () -> telephonyManager.getEmergencyCallbackMode()); 1171 } 1172 1173 @Test 1174 @Config(minSdk = O) getEmergencyCallback_wasSetToTrue_returnsTrue()1175 public void getEmergencyCallback_wasSetToTrue_returnsTrue() { 1176 shadowTelephonyManager.setEmergencyCallbackMode(true); 1177 1178 assertThat(telephonyManager.getEmergencyCallbackMode()).isTrue(); 1179 } 1180 1181 @Test 1182 @Config(minSdk = O) getEmergencyCallback_notSet_returnsFalse()1183 public void getEmergencyCallback_notSet_returnsFalse() { 1184 assertThat(telephonyManager.getEmergencyCallbackMode()).isFalse(); 1185 } 1186 1187 @Test 1188 @Config(minSdk = R) getEmergencyNumbersList_notSet_returnsEmptyList()1189 public void getEmergencyNumbersList_notSet_returnsEmptyList() { 1190 assertThat(telephonyManager.getEmergencyNumberList()).isEmpty(); 1191 } 1192 1193 @Test 1194 @Config(minSdk = R) getEmergencyNumbersList_wasSet_returnsCorrectList()1195 public void getEmergencyNumbersList_wasSet_returnsCorrectList() throws Exception { 1196 EmergencyNumber emergencyNumber = 1197 EmergencyNumber.class 1198 .getConstructor( 1199 String.class, 1200 String.class, 1201 String.class, 1202 int.class, 1203 List.class, 1204 int.class, 1205 int.class) 1206 .newInstance( 1207 "911", 1208 "us", 1209 "30", 1210 EMERGENCY_NUMBER_SOURCE_DATABASE, 1211 ImmutableList.of(), 1212 EMERGENCY_SERVICE_CATEGORY_POLICE, 1213 EmergencyNumber.EMERGENCY_CALL_ROUTING_NORMAL); 1214 ShadowTelephonyManager.setEmergencyNumberList( 1215 ImmutableMap.of(0, ImmutableList.of(emergencyNumber))); 1216 assertThat(telephonyManager.getEmergencyNumberList().get(0)).containsExactly(emergencyNumber); 1217 } 1218 1219 @Test 1220 @Config(minSdk = R) getSubscriptionIdForPhoneAccountHandle()1221 public void getSubscriptionIdForPhoneAccountHandle() { 1222 int subscriptionId = 123; 1223 PhoneAccountHandle phoneAccountHandle = 1224 new PhoneAccountHandle( 1225 new ComponentName(ApplicationProvider.getApplicationContext(), Object.class), "handle"); 1226 shadowOf(telephonyManager) 1227 .setPhoneAccountHandleSubscriptionId(phoneAccountHandle, subscriptionId); 1228 assertEquals(subscriptionId, telephonyManager.getSubscriptionId(phoneAccountHandle)); 1229 } 1230 } 1231