1 package org.robolectric.shadows; 2 3 import static android.content.Context.TELEPHONY_SERVICE; 4 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR1; 5 import static android.os.Build.VERSION_CODES.JELLY_BEAN_MR2; 6 import static android.os.Build.VERSION_CODES.M; 7 import static android.os.Build.VERSION_CODES.N; 8 import static android.os.Build.VERSION_CODES.O; 9 import static android.os.Build.VERSION_CODES.P; 10 import static android.telephony.PhoneStateListener.LISTEN_CALL_STATE; 11 import static android.telephony.PhoneStateListener.LISTEN_CELL_INFO; 12 import static android.telephony.PhoneStateListener.LISTEN_CELL_LOCATION; 13 import static android.telephony.TelephonyManager.CALL_STATE_IDLE; 14 import static android.telephony.TelephonyManager.CALL_STATE_OFFHOOK; 15 import static android.telephony.TelephonyManager.CALL_STATE_RINGING; 16 import static com.google.common.truth.Truth.assertThat; 17 import static org.junit.Assert.assertEquals; 18 import static org.junit.Assert.assertNotNull; 19 import static org.junit.Assert.assertTrue; 20 import static org.mockito.Mockito.mock; 21 import static org.mockito.Mockito.verify; 22 import static org.robolectric.RuntimeEnvironment.application; 23 import static org.robolectric.Shadows.shadowOf; 24 25 import android.content.ComponentName; 26 import android.content.Intent; 27 import android.net.Uri; 28 import android.os.Build.VERSION; 29 import android.os.PersistableBundle; 30 import android.telecom.PhoneAccountHandle; 31 import android.telephony.CellInfo; 32 import android.telephony.CellLocation; 33 import android.telephony.PhoneStateListener; 34 import android.telephony.ServiceState; 35 import android.telephony.SubscriptionManager; 36 import android.telephony.TelephonyManager; 37 import androidx.test.core.app.ApplicationProvider; 38 import androidx.test.ext.junit.runners.AndroidJUnit4; 39 import java.util.Collections; 40 import java.util.List; 41 import org.junit.Before; 42 import org.junit.Test; 43 import org.junit.runner.RunWith; 44 import org.robolectric.annotation.Config; 45 46 @RunWith(AndroidJUnit4.class) 47 public class ShadowTelephonyManagerTest { 48 49 private TelephonyManager telephonyManager; 50 private ShadowTelephonyManager shadowTelephonyManager; 51 52 @Before setUp()53 public void setUp() throws Exception { 54 telephonyManager = (TelephonyManager) application.getSystemService(TELEPHONY_SERVICE); 55 shadowTelephonyManager = shadowOf(telephonyManager); 56 } 57 58 @Test testListenInit()59 public void testListenInit() { 60 PhoneStateListener listener = mock(PhoneStateListener.class); 61 telephonyManager.listen(listener, LISTEN_CALL_STATE | LISTEN_CELL_INFO | LISTEN_CELL_LOCATION); 62 63 verify(listener).onCallStateChanged(CALL_STATE_IDLE, null); 64 verify(listener).onCellLocationChanged(null); 65 if (VERSION.SDK_INT >= JELLY_BEAN_MR1) { 66 verify(listener).onCellInfoChanged(Collections.emptyList()); 67 } 68 } 69 70 @Test shouldGiveDeviceId()71 public void shouldGiveDeviceId() { 72 String testId = "TESTING123"; 73 shadowTelephonyManager.setDeviceId(testId); 74 assertEquals(testId, telephonyManager.getDeviceId()); 75 } 76 77 @Test 78 @Config(minSdk = M) shouldGiveDeviceIdForSlot()79 public void shouldGiveDeviceIdForSlot() { 80 shadowTelephonyManager.setDeviceId(1, "device in slot 1"); 81 shadowTelephonyManager.setDeviceId(2, "device in slot 2"); 82 83 assertEquals("device in slot 1", telephonyManager.getDeviceId(1)); 84 assertEquals("device in slot 2", telephonyManager.getDeviceId(2)); 85 } 86 87 @Test 88 @Config(minSdk = O) getImei()89 public void getImei() { 90 String testImei = "4test imei"; 91 shadowTelephonyManager.setImei(testImei); 92 assertEquals(testImei, telephonyManager.getImei()); 93 } 94 95 @Test 96 @Config(minSdk = O) getMeid()97 public void getMeid() { 98 String testMeid = "4test meid"; 99 shadowTelephonyManager.setMeid(testMeid); 100 assertEquals(testMeid, telephonyManager.getMeid()); 101 } 102 103 @Test shouldGiveNetworkOperatorName()104 public void shouldGiveNetworkOperatorName() { 105 shadowTelephonyManager.setNetworkOperatorName("SomeOperatorName"); 106 assertEquals("SomeOperatorName", telephonyManager.getNetworkOperatorName()); 107 } 108 109 @Test shouldGiveSimOperatorName()110 public void shouldGiveSimOperatorName() { 111 shadowTelephonyManager.setSimOperatorName("SomeSimOperatorName"); 112 assertEquals("SomeSimOperatorName", telephonyManager.getSimOperatorName()); 113 } 114 115 @Test(expected = SecurityException.class) getSimSerialNumber_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted()116 public void getSimSerialNumber_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted() 117 throws Exception { 118 shadowTelephonyManager.setReadPhoneStatePermission(false); 119 telephonyManager.getSimSerialNumber(); 120 } 121 122 @Test shouldGetSimSerialNumber()123 public void shouldGetSimSerialNumber() { 124 shadowTelephonyManager.setSimSerialNumber("SomeSerialNumber"); 125 assertEquals("SomeSerialNumber", telephonyManager.getSimSerialNumber()); 126 } 127 128 @Test shouldGiveNetworkType()129 public void shouldGiveNetworkType() { 130 shadowTelephonyManager.setNetworkType(TelephonyManager.NETWORK_TYPE_CDMA); 131 assertEquals(TelephonyManager.NETWORK_TYPE_CDMA, telephonyManager.getNetworkType()); 132 } 133 134 @Test 135 @Config(minSdk = N) shouldGiveVoiceNetworkType()136 public void shouldGiveVoiceNetworkType() { 137 shadowTelephonyManager.setVoiceNetworkType(TelephonyManager.NETWORK_TYPE_CDMA); 138 assertThat(telephonyManager.getVoiceNetworkType()) 139 .isEqualTo(TelephonyManager.NETWORK_TYPE_CDMA); 140 } 141 142 @Test 143 @Config(minSdk = JELLY_BEAN_MR1) shouldGiveAllCellInfo()144 public void shouldGiveAllCellInfo() { 145 PhoneStateListener listener = mock(PhoneStateListener.class); 146 telephonyManager.listen(listener, LISTEN_CELL_INFO); 147 148 List<CellInfo> allCellInfo = Collections.singletonList(mock(CellInfo.class)); 149 shadowTelephonyManager.setAllCellInfo(allCellInfo); 150 assertEquals(allCellInfo, telephonyManager.getAllCellInfo()); 151 verify(listener).onCellInfoChanged(allCellInfo); 152 } 153 154 @Test shouldGiveNetworkCountryIso()155 public void shouldGiveNetworkCountryIso() { 156 shadowTelephonyManager.setNetworkCountryIso("SomeIso"); 157 assertEquals("SomeIso", telephonyManager.getNetworkCountryIso()); 158 } 159 160 @Test shouldGiveNetworkOperator()161 public void shouldGiveNetworkOperator() { 162 shadowTelephonyManager.setNetworkOperator("SomeOperator"); 163 assertEquals("SomeOperator", telephonyManager.getNetworkOperator()); 164 } 165 166 @Test shouldGiveLine1Number()167 public void shouldGiveLine1Number() { 168 shadowTelephonyManager.setLine1Number("123-244-2222"); 169 assertEquals("123-244-2222", telephonyManager.getLine1Number()); 170 } 171 172 @Test 173 @Config(minSdk = JELLY_BEAN_MR2) shouldGiveGroupIdLevel1()174 public void shouldGiveGroupIdLevel1() { 175 shadowTelephonyManager.setGroupIdLevel1("SomeGroupId"); 176 assertEquals("SomeGroupId", telephonyManager.getGroupIdLevel1()); 177 } 178 179 @Test(expected = SecurityException.class) getDeviceId_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted()180 public void getDeviceId_shouldThrowSecurityExceptionWhenReadPhoneStatePermissionNotGranted() 181 throws Exception { 182 shadowTelephonyManager.setReadPhoneStatePermission(false); 183 telephonyManager.getDeviceId(); 184 } 185 186 @Test shouldGivePhoneType()187 public void shouldGivePhoneType() { 188 shadowTelephonyManager.setPhoneType(TelephonyManager.PHONE_TYPE_CDMA); 189 assertEquals(TelephonyManager.PHONE_TYPE_CDMA, telephonyManager.getPhoneType()); 190 shadowTelephonyManager.setPhoneType(TelephonyManager.PHONE_TYPE_GSM); 191 assertEquals(TelephonyManager.PHONE_TYPE_GSM, telephonyManager.getPhoneType()); 192 } 193 194 @Test shouldGiveCellLocation()195 public void shouldGiveCellLocation() { 196 PhoneStateListener listener = mock(PhoneStateListener.class); 197 telephonyManager.listen(listener, LISTEN_CELL_LOCATION); 198 199 CellLocation mockCellLocation = mock(CellLocation.class); 200 shadowOf(telephonyManager).setCellLocation(mockCellLocation); 201 assertEquals(mockCellLocation, telephonyManager.getCellLocation()); 202 verify(listener).onCellLocationChanged(mockCellLocation); 203 } 204 205 @Test shouldGiveCallState()206 public void shouldGiveCallState() { 207 PhoneStateListener listener = mock(PhoneStateListener.class); 208 telephonyManager.listen(listener, LISTEN_CALL_STATE); 209 210 shadowOf(telephonyManager).setCallState(CALL_STATE_RINGING, "911"); 211 assertEquals(CALL_STATE_RINGING, telephonyManager.getCallState()); 212 verify(listener).onCallStateChanged(CALL_STATE_RINGING, "911"); 213 214 shadowOf(telephonyManager).setCallState(CALL_STATE_OFFHOOK, "911"); 215 assertEquals(CALL_STATE_OFFHOOK, telephonyManager.getCallState()); 216 verify(listener).onCallStateChanged(CALL_STATE_OFFHOOK, null); 217 } 218 219 @Test isSmsCapable()220 public void isSmsCapable() { 221 assertThat(telephonyManager.isSmsCapable()).isTrue(); 222 shadowTelephonyManager.setIsSmsCapable(false); 223 assertThat(telephonyManager.isSmsCapable()).isFalse(); 224 } 225 226 @Test 227 @Config(minSdk = O) shouldGiveCarrierConfigIfSet()228 public void shouldGiveCarrierConfigIfSet() { 229 PersistableBundle bundle = new PersistableBundle(); 230 bundle.putInt("foo", 42); 231 shadowTelephonyManager.setCarrierConfig(bundle); 232 233 assertEquals(bundle, telephonyManager.getCarrierConfig()); 234 } 235 236 @Test 237 @Config(minSdk = O) shouldGiveNonNullCarrierConfigIfNotSet()238 public void shouldGiveNonNullCarrierConfigIfNotSet() { 239 assertNotNull(telephonyManager.getCarrierConfig()); 240 } 241 242 @Test shouldGiveVoiceMailNumber()243 public void shouldGiveVoiceMailNumber() { 244 shadowTelephonyManager.setVoiceMailNumber("123"); 245 246 assertEquals("123", telephonyManager.getVoiceMailNumber()); 247 } 248 249 @Test shouldGiveVoiceMailAlphaTag()250 public void shouldGiveVoiceMailAlphaTag() { 251 shadowTelephonyManager.setVoiceMailAlphaTag("tag"); 252 253 assertEquals("tag", telephonyManager.getVoiceMailAlphaTag()); 254 } 255 256 @Test 257 @Config(minSdk = M) shouldGivePhoneCount()258 public void shouldGivePhoneCount() { 259 shadowTelephonyManager.setPhoneCount(42); 260 261 assertEquals(42, telephonyManager.getPhoneCount()); 262 } 263 264 @Test 265 @Config(minSdk = N) shouldGiveVoiceVibrationEnabled()266 public void shouldGiveVoiceVibrationEnabled() { 267 PhoneAccountHandle phoneAccountHandle = 268 new PhoneAccountHandle( 269 new ComponentName(ApplicationProvider.getApplicationContext(), Object.class), "handle"); 270 271 shadowTelephonyManager.setVoicemailVibrationEnabled(phoneAccountHandle, true); 272 273 assertTrue(telephonyManager.isVoicemailVibrationEnabled(phoneAccountHandle)); 274 } 275 276 @Test 277 @Config(minSdk = N) shouldGiveVoicemailRingtoneUri()278 public void shouldGiveVoicemailRingtoneUri() { 279 PhoneAccountHandle phoneAccountHandle = 280 new PhoneAccountHandle( 281 new ComponentName(ApplicationProvider.getApplicationContext(), Object.class), "handle"); 282 Uri ringtoneUri = Uri.fromParts("file", "ringtone.mp3", /* fragment = */ null); 283 284 shadowTelephonyManager.setVoicemailRingtoneUri(phoneAccountHandle, ringtoneUri); 285 286 assertEquals(ringtoneUri, telephonyManager.getVoicemailRingtoneUri(phoneAccountHandle)); 287 } 288 289 @Test 290 @Config(minSdk = O) // The setter on the real manager was added in O shouldSetVoicemailRingtoneUri()291 public void shouldSetVoicemailRingtoneUri() { 292 PhoneAccountHandle phoneAccountHandle = 293 new PhoneAccountHandle( 294 new ComponentName(ApplicationProvider.getApplicationContext(), Object.class), "handle"); 295 Uri ringtoneUri = Uri.fromParts("file", "ringtone.mp3", /* fragment = */ null); 296 297 // Note: Using the real manager to set, instead of the shadow. 298 telephonyManager.setVoicemailRingtoneUri(phoneAccountHandle, ringtoneUri); 299 300 assertEquals(ringtoneUri, telephonyManager.getVoicemailRingtoneUri(phoneAccountHandle)); 301 } 302 303 @Test 304 @Config(minSdk = O) shouldCreateForPhoneAccountHandle()305 public void shouldCreateForPhoneAccountHandle() { 306 PhoneAccountHandle phoneAccountHandle = 307 new PhoneAccountHandle( 308 new ComponentName(ApplicationProvider.getApplicationContext(), Object.class), "handle"); 309 TelephonyManager mockTelephonyManager = mock(TelephonyManager.class); 310 311 shadowTelephonyManager.setTelephonyManagerForHandle(phoneAccountHandle, mockTelephonyManager); 312 313 assertEquals( 314 mockTelephonyManager, telephonyManager.createForPhoneAccountHandle(phoneAccountHandle)); 315 } 316 317 @Test 318 @Config(minSdk = N) shouldCreateForSubscriptionId()319 public void shouldCreateForSubscriptionId() { 320 int subscriptionId = 42; 321 TelephonyManager mockTelephonyManager = mock(TelephonyManager.class); 322 323 shadowTelephonyManager.setTelephonyManagerForSubscriptionId( 324 subscriptionId, mockTelephonyManager); 325 326 assertEquals(mockTelephonyManager, telephonyManager.createForSubscriptionId(subscriptionId)); 327 } 328 329 @Test 330 @Config(minSdk = O) shouldSetServiceState()331 public void shouldSetServiceState() { 332 ServiceState serviceState = new ServiceState(); 333 serviceState.setState(ServiceState.STATE_OUT_OF_SERVICE); 334 335 shadowTelephonyManager.setServiceState(serviceState); 336 337 assertEquals(serviceState, telephonyManager.getServiceState()); 338 } 339 340 @Test shouldSetIsNetworkRoaming()341 public void shouldSetIsNetworkRoaming() { 342 shadowTelephonyManager.setIsNetworkRoaming(true); 343 344 assertTrue(telephonyManager.isNetworkRoaming()); 345 } 346 347 @Test shouldGetSimState()348 public void shouldGetSimState() { 349 assertThat(telephonyManager.getSimState()).isEqualTo(TelephonyManager.SIM_STATE_READY); 350 } 351 352 @Test 353 @Config(minSdk = O) shouldGetSimStateUsingSlotNumber()354 public void shouldGetSimStateUsingSlotNumber() { 355 int expectedSimState = TelephonyManager.SIM_STATE_ABSENT; 356 int slotNumber = 3; 357 shadowTelephonyManager.setSimState(slotNumber, expectedSimState); 358 359 assertThat(telephonyManager.getSimState(slotNumber)).isEqualTo(expectedSimState); 360 } 361 362 @Test shouldGetSimIso()363 public void shouldGetSimIso() { 364 assertThat(telephonyManager.getSimCountryIso()).isEmpty(); 365 } 366 367 @Test 368 @Config(minSdk = N) shouldGetSimIosWhenSetUsingSlotNumber()369 public void shouldGetSimIosWhenSetUsingSlotNumber() { 370 String expectedSimIso = "usa"; 371 int subId = 2; 372 shadowTelephonyManager.setSimCountryIso(subId, expectedSimIso); 373 374 assertThat(telephonyManager.getSimCountryIso(subId)).isEqualTo(expectedSimIso); 375 } 376 377 @Test 378 @Config(minSdk = P) shouldGetSimCarrierId()379 public void shouldGetSimCarrierId() { 380 int expectedCarrierId = 132; 381 shadowTelephonyManager.setSimCarrierId(expectedCarrierId); 382 383 assertThat(telephonyManager.getSimCarrierId()).isEqualTo(expectedCarrierId); 384 } 385 386 @Test 387 @Config(minSdk = M) shouldGetCurrentPhoneTypeGivenSubId()388 public void shouldGetCurrentPhoneTypeGivenSubId() { 389 int subId = 1; 390 int expectedPhoneType = TelephonyManager.PHONE_TYPE_GSM; 391 shadowTelephonyManager.setCurrentPhoneType(subId, expectedPhoneType); 392 393 assertThat(telephonyManager.getCurrentPhoneType(subId)).isEqualTo(expectedPhoneType); 394 } 395 396 @Test 397 @Config(minSdk = M) shouldGetCarrierPackageNamesForIntentAndPhone()398 public void shouldGetCarrierPackageNamesForIntentAndPhone() { 399 List<String> packages = Collections.singletonList("package1"); 400 int phoneId = 123; 401 shadowTelephonyManager.setCarrierPackageNamesForPhone(phoneId, packages); 402 403 assertThat(telephonyManager.getCarrierPackageNamesForIntentAndPhone(new Intent(), phoneId)) 404 .isEqualTo(packages); 405 } 406 407 @Test 408 @Config(minSdk = M) shouldGetCarrierPackageNamesForIntent()409 public void shouldGetCarrierPackageNamesForIntent() { 410 List<String> packages = Collections.singletonList("package1"); 411 shadowTelephonyManager.setCarrierPackageNamesForPhone( 412 SubscriptionManager.DEFAULT_SUBSCRIPTION_ID, packages); 413 414 assertThat(telephonyManager.getCarrierPackageNamesForIntent(new Intent())).isEqualTo(packages); 415 } 416 417 @Test resetSimStates_shouldRetainDefaultState()418 public void resetSimStates_shouldRetainDefaultState() { 419 shadowTelephonyManager.resetSimStates(); 420 421 assertThat(telephonyManager.getSimState()).isEqualTo(TelephonyManager.SIM_STATE_READY); 422 } 423 424 @Test 425 @Config(minSdk = N) resetSimCountryIsos_shouldRetainDefaultState()426 public void resetSimCountryIsos_shouldRetainDefaultState() { 427 shadowTelephonyManager.resetSimCountryIsos(); 428 429 assertThat(shadowTelephonyManager.getSimCountryIso()).isEmpty(); 430 } 431 432 @Test shouldSetSubscriberId()433 public void shouldSetSubscriberId() { 434 String subscriberId = "123451234512345"; 435 shadowTelephonyManager.setSubscriberId(subscriberId); 436 437 assertThat(shadowTelephonyManager.getSubscriberId()).isEqualTo(subscriberId); 438 } 439 } 440