1 package org.robolectric.shadows; 2 3 import static android.os.Build.VERSION_CODES.LOLLIPOP; 4 import static android.os.Build.VERSION_CODES.LOLLIPOP_MR1; 5 import static android.os.Build.VERSION_CODES.M; 6 import static com.google.common.truth.Truth.assertThat; 7 import static org.robolectric.Shadows.shadowOf; 8 9 import android.content.ComponentName; 10 import android.content.Context; 11 import android.telecom.PhoneAccount; 12 import android.telecom.PhoneAccountHandle; 13 import android.telecom.TelecomManager; 14 import androidx.test.core.app.ApplicationProvider; 15 import androidx.test.ext.junit.runners.AndroidJUnit4; 16 import java.util.List; 17 import org.junit.Before; 18 import org.junit.Test; 19 import org.junit.runner.RunWith; 20 import org.robolectric.annotation.Config; 21 22 @RunWith(AndroidJUnit4.class) 23 @Config(minSdk = LOLLIPOP) 24 public class ShadowTelecomManagerTest { 25 26 private TelecomManager telecomService; 27 28 @Before setUp()29 public void setUp() { 30 telecomService = 31 (TelecomManager) 32 ApplicationProvider.getApplicationContext().getSystemService(Context.TELECOM_SERVICE); 33 } 34 35 @Test getSimCallManager()36 public void getSimCallManager() { 37 PhoneAccountHandle handle = createHandle("id"); 38 39 shadowOf(telecomService).setSimCallManager(handle); 40 41 assertThat(telecomService.getConnectionManager().getId()).isEqualTo("id"); 42 } 43 44 @Test registerAndUnRegister()45 public void registerAndUnRegister() { 46 assertThat(shadowOf(telecomService).getAllPhoneAccountsCount()).isEqualTo(0); 47 assertThat(shadowOf(telecomService).getAllPhoneAccounts()).hasSize(0); 48 49 PhoneAccountHandle handler = createHandle("id"); 50 PhoneAccount phoneAccount = PhoneAccount.builder(handler, "main_account").build(); 51 telecomService.registerPhoneAccount(phoneAccount); 52 53 assertThat(shadowOf(telecomService).getAllPhoneAccountsCount()).isEqualTo(1); 54 assertThat(shadowOf(telecomService).getAllPhoneAccounts()).hasSize(1); 55 assertThat(telecomService.getAllPhoneAccountHandles()).hasSize(1); 56 assertThat(telecomService.getAllPhoneAccountHandles()).contains(handler); 57 assertThat(telecomService.getPhoneAccount(handler).getLabel()).isEqualTo(phoneAccount.getLabel()); 58 59 telecomService.unregisterPhoneAccount(handler); 60 61 assertThat(shadowOf(telecomService).getAllPhoneAccountsCount()).isEqualTo(0); 62 assertThat(shadowOf(telecomService).getAllPhoneAccounts()).hasSize(0); 63 assertThat(telecomService.getAllPhoneAccountHandles()).hasSize(0); 64 } 65 66 @Test clearAccounts()67 public void clearAccounts() { 68 PhoneAccountHandle anotherPackageHandle = createHandle("some.other.package", "id"); 69 telecomService.registerPhoneAccount(PhoneAccount.builder(anotherPackageHandle, "another_package") 70 .build()); 71 } 72 73 @Test 74 @Config(minSdk = LOLLIPOP_MR1) clearAccountsForPackage()75 public void clearAccountsForPackage() { 76 PhoneAccountHandle accountHandle1 = createHandle("a.package", "id1"); 77 telecomService.registerPhoneAccount(PhoneAccount.builder(accountHandle1, "another_package") 78 .build()); 79 80 PhoneAccountHandle accountHandle2 = createHandle("some.other.package", "id2"); 81 telecomService.registerPhoneAccount(PhoneAccount.builder(accountHandle2, "another_package") 82 .build()); 83 84 telecomService.clearAccountsForPackage(accountHandle1.getComponentName().getPackageName()); 85 86 assertThat(telecomService.getPhoneAccount(accountHandle1)).isNull(); 87 assertThat(telecomService.getPhoneAccount(accountHandle2)).isNotNull(); 88 } 89 90 @Test getPhoneAccountsSupportingScheme()91 public void getPhoneAccountsSupportingScheme() { 92 PhoneAccountHandle handleMatchingScheme = createHandle("id1"); 93 telecomService.registerPhoneAccount(PhoneAccount.builder(handleMatchingScheme, "some_scheme") 94 .addSupportedUriScheme("some_scheme") 95 .build()); 96 PhoneAccountHandle handleNotMatchingScheme = createHandle("id2"); 97 telecomService.registerPhoneAccount(PhoneAccount.builder(handleNotMatchingScheme, "another_scheme") 98 .addSupportedUriScheme("another_scheme") 99 .build()); 100 101 List<PhoneAccountHandle> actual = telecomService.getPhoneAccountsSupportingScheme("some_scheme"); 102 103 assertThat(actual).contains(handleMatchingScheme); 104 assertThat(actual).doesNotContain(handleNotMatchingScheme); 105 } 106 107 @Test 108 @Config(minSdk = M) getCallCapablePhoneAccounts()109 public void getCallCapablePhoneAccounts() { 110 PhoneAccountHandle callCapableHandle = createHandle("id1"); 111 telecomService.registerPhoneAccount(PhoneAccount.builder(callCapableHandle, "enabled") 112 .setIsEnabled(true) 113 .build()); 114 PhoneAccountHandle notCallCapableHandler = createHandle("id2"); 115 telecomService.registerPhoneAccount(PhoneAccount.builder(notCallCapableHandler, "disabled") 116 .setIsEnabled(false) 117 .build()); 118 119 List<PhoneAccountHandle> callCapablePhoneAccounts = telecomService.getCallCapablePhoneAccounts(); 120 assertThat(callCapablePhoneAccounts).contains(callCapableHandle); 121 assertThat(callCapablePhoneAccounts).doesNotContain(notCallCapableHandler); 122 } 123 124 @Test 125 @Config(minSdk = LOLLIPOP_MR1) getPhoneAccountsForPackage()126 public void getPhoneAccountsForPackage() { 127 PhoneAccountHandle handleInThisApplicationsPackage = createHandle("id1"); 128 telecomService.registerPhoneAccount(PhoneAccount.builder(handleInThisApplicationsPackage, "this_package") 129 .build()); 130 131 PhoneAccountHandle anotherPackageHandle = createHandle("some.other.package", "id2"); 132 telecomService.registerPhoneAccount(PhoneAccount.builder(anotherPackageHandle, "another_package") 133 .build()); 134 135 List<PhoneAccountHandle> phoneAccountsForPackage = telecomService.getPhoneAccountsForPackage(); 136 137 assertThat(phoneAccountsForPackage).contains(handleInThisApplicationsPackage); 138 assertThat(phoneAccountsForPackage).doesNotContain(anotherPackageHandle); 139 } 140 141 @Test testAddNewIncomingCall()142 public void testAddNewIncomingCall() { 143 telecomService.addNewIncomingCall(createHandle("id"), null); 144 145 assertThat(shadowOf(telecomService).getAllIncomingCalls()).hasSize(1); 146 } 147 148 @Test testAddUnknownCall()149 public void testAddUnknownCall() { 150 telecomService.addNewUnknownCall(createHandle("id"), null); 151 152 assertThat(shadowOf(telecomService).getAllUnknownCalls()).hasSize(1); 153 } 154 155 @Test testIsRinging_noIncomingOrUnknownCallsAdded_shouldBeFalse()156 public void testIsRinging_noIncomingOrUnknownCallsAdded_shouldBeFalse() { 157 assertThat(shadowOf(telecomService).isRinging()).isFalse(); 158 } 159 160 @Test testIsRinging_incomingCallAdded_shouldBeTrue()161 public void testIsRinging_incomingCallAdded_shouldBeTrue() { 162 telecomService.addNewIncomingCall(createHandle("id"), null); 163 164 assertThat(shadowOf(telecomService).isRinging()).isTrue(); 165 } 166 167 @Test testIsRinging_unknownCallAdded_shouldBeTrue()168 public void testIsRinging_unknownCallAdded_shouldBeTrue() { 169 shadowOf(telecomService).addNewUnknownCall(createHandle("id"), null); 170 171 assertThat(shadowOf(telecomService).isRinging()).isTrue(); 172 } 173 174 @Test testIsRinging_incomingCallAdded_thenRingerSilenced_shouldBeFalse()175 public void testIsRinging_incomingCallAdded_thenRingerSilenced_shouldBeFalse() { 176 telecomService.addNewIncomingCall(createHandle("id"), null); 177 telecomService.silenceRinger(); 178 179 assertThat(shadowOf(telecomService).isRinging()).isFalse(); 180 } 181 182 @Test testIsRinging_unknownCallAdded_thenRingerSilenced_shouldBeFalse()183 public void testIsRinging_unknownCallAdded_thenRingerSilenced_shouldBeFalse() { 184 shadowOf(telecomService).addNewUnknownCall(createHandle("id"), null); 185 telecomService.silenceRinger(); 186 187 assertThat(shadowOf(telecomService).isRinging()).isFalse(); 188 } 189 190 @Test testIsRinging_ringerSilenced_thenIncomingCallAdded_shouldBeTrue()191 public void testIsRinging_ringerSilenced_thenIncomingCallAdded_shouldBeTrue() { 192 telecomService.silenceRinger(); 193 telecomService.addNewIncomingCall(createHandle("id"), null); 194 195 assertThat(shadowOf(telecomService).isRinging()).isTrue(); 196 } 197 198 @Test testIsRinging_ringerSilenced_thenUnknownCallAdded_shouldBeTrue()199 public void testIsRinging_ringerSilenced_thenUnknownCallAdded_shouldBeTrue() { 200 telecomService.silenceRinger(); 201 shadowOf(telecomService).addNewUnknownCall(createHandle("id"), null); 202 203 assertThat(shadowOf(telecomService).isRinging()).isTrue(); 204 } 205 206 @Test 207 @Config(minSdk = M) setDefaultDialerPackage()208 public void setDefaultDialerPackage() { 209 shadowOf(telecomService).setDefaultDialer("some.package"); 210 assertThat(telecomService.getDefaultDialerPackage()).isEqualTo("some.package"); 211 } 212 213 @Test canSetAndGetIsInCall()214 public void canSetAndGetIsInCall() throws Exception { 215 shadowOf(telecomService).setIsInCall(true); 216 assertThat(telecomService.isInCall()).isTrue(); 217 } 218 219 @Test isInCall_setIsInCallNotCalled_shouldReturnFalse()220 public void isInCall_setIsInCallNotCalled_shouldReturnFalse() throws Exception { 221 assertThat(telecomService.isInCall()).isFalse(); 222 } 223 createHandle(String id)224 private static PhoneAccountHandle createHandle(String id) { 225 return createHandle(ApplicationProvider.getApplicationContext().getPackageName(), id); 226 } 227 createHandle(String packageName, String id)228 private static PhoneAccountHandle createHandle(String packageName, String id) { 229 return new PhoneAccountHandle(new ComponentName(packageName, "component_class_name"), id); 230 } 231 } 232