Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License |
| 15 | */ |
| 16 | |
| 17 | package com.android.server.telecom.tests; |
| 18 | |
| 19 | import static android.Manifest.permission.CALL_PHONE; |
Tyler Gunn | b652dad | 2018-03-27 18:49:49 +0000 | [diff] [blame] | 20 | import static android.Manifest.permission.CALL_PRIVILEGED; |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 21 | import static android.Manifest.permission.MODIFY_PHONE_STATE; |
| 22 | import static android.Manifest.permission.READ_PHONE_STATE; |
| 23 | import static android.Manifest.permission.READ_PRIVILEGED_PHONE_STATE; |
| 24 | |
Hall Liu | 7c92832 | 2016-12-06 18:15:39 -0800 | [diff] [blame] | 25 | import android.app.ActivityManager; |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 26 | import android.app.AppOpsManager; |
| 27 | import android.content.ComponentName; |
| 28 | import android.content.Context; |
| 29 | import android.content.Intent; |
| 30 | import android.content.pm.ApplicationInfo; |
| 31 | import android.content.pm.PackageManager; |
| 32 | import android.net.Uri; |
| 33 | import android.os.Binder; |
| 34 | import android.os.Bundle; |
| 35 | import android.os.RemoteException; |
| 36 | import android.os.UserHandle; |
| 37 | import android.os.UserManager; |
| 38 | import android.telecom.PhoneAccount; |
| 39 | import android.telecom.PhoneAccountHandle; |
| 40 | import android.telecom.TelecomManager; |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 41 | import android.telecom.VideoProfile; |
| 42 | import android.telephony.TelephonyManager; |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 43 | import android.test.suitebuilder.annotation.SmallTest; |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 44 | |
| 45 | import com.android.internal.telecom.ITelecomService; |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 46 | import com.android.server.telecom.Call; |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 47 | import com.android.server.telecom.CallIntentProcessor; |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 48 | import com.android.server.telecom.CallState; |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 49 | import com.android.server.telecom.CallsManager; |
Hall Liu | 7c92832 | 2016-12-06 18:15:39 -0800 | [diff] [blame] | 50 | import com.android.server.telecom.DefaultDialerCache; |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 51 | import com.android.server.telecom.PhoneAccountRegistrar; |
| 52 | import com.android.server.telecom.TelecomServiceImpl; |
| 53 | import com.android.server.telecom.TelecomSystem; |
| 54 | import com.android.server.telecom.components.UserCallIntentProcessor; |
| 55 | import com.android.server.telecom.components.UserCallIntentProcessorFactory; |
| 56 | |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 57 | import org.junit.Before; |
| 58 | import org.junit.Test; |
| 59 | import org.junit.runner.RunWith; |
| 60 | import org.junit.runners.JUnit4; |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 61 | import org.mockito.ArgumentCaptor; |
Philip P. Moltmann | 3168f6a | 2017-11-22 15:55:59 -0800 | [diff] [blame] | 62 | import org.mockito.ArgumentMatcher; |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 63 | import org.mockito.Mock; |
| 64 | |
| 65 | import java.util.ArrayList; |
| 66 | import java.util.Collection; |
| 67 | import java.util.List; |
| 68 | |
| 69 | import static android.Manifest.permission.REGISTER_SIM_SUBSCRIPTION; |
| 70 | import static android.Manifest.permission.WRITE_SECURE_SETTINGS; |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 71 | import static org.junit.Assert.assertEquals; |
| 72 | import static org.junit.Assert.assertFalse; |
| 73 | import static org.junit.Assert.assertNull; |
| 74 | import static org.junit.Assert.assertTrue; |
| 75 | import static org.junit.Assert.fail; |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 76 | import static org.mockito.ArgumentMatchers.nullable; |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 77 | import static org.mockito.Matchers.any; |
| 78 | import static org.mockito.Matchers.anyBoolean; |
| 79 | import static org.mockito.Matchers.anyInt; |
| 80 | import static org.mockito.Matchers.anyString; |
| 81 | import static org.mockito.Matchers.argThat; |
| 82 | import static org.mockito.Matchers.eq; |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 83 | import static org.mockito.Matchers.isNull; |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 84 | import static org.mockito.Mockito.doNothing; |
| 85 | import static org.mockito.Mockito.doReturn; |
| 86 | import static org.mockito.Mockito.doThrow; |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 87 | import static org.mockito.Mockito.mock; |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 88 | import static org.mockito.Mockito.never; |
| 89 | import static org.mockito.Mockito.spy; |
| 90 | import static org.mockito.Mockito.verify; |
| 91 | import static org.mockito.Mockito.when; |
| 92 | |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 93 | @RunWith(JUnit4.class) |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 94 | public class TelecomServiceImplTest extends TelecomTestCase { |
Tyler Gunn | b652dad | 2018-03-27 18:49:49 +0000 | [diff] [blame] | 95 | |
| 96 | public static final String TEST_PACKAGE = "com.test"; |
| 97 | |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 98 | public static class CallIntentProcessAdapterFake implements CallIntentProcessor.Adapter { |
| 99 | @Override |
| 100 | public void processOutgoingCallIntent(Context context, CallsManager callsManager, |
| 101 | Intent intent) { |
| 102 | |
| 103 | } |
| 104 | |
| 105 | @Override |
| 106 | public void processIncomingCallIntent(CallsManager callsManager, Intent intent) { |
| 107 | |
| 108 | } |
| 109 | |
| 110 | @Override |
| 111 | public void processUnknownCallIntent(CallsManager callsManager, Intent intent) { |
| 112 | |
| 113 | } |
| 114 | } |
| 115 | |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 116 | public static class SubscriptionManagerAdapterFake |
| 117 | implements TelecomServiceImpl.SubscriptionManagerAdapter { |
| 118 | @Override |
| 119 | public int getDefaultVoiceSubId() { |
| 120 | return 0; |
| 121 | } |
| 122 | } |
| 123 | |
Philip P. Moltmann | 3168f6a | 2017-11-22 15:55:59 -0800 | [diff] [blame] | 124 | private static class AnyStringIn implements ArgumentMatcher<String> { |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 125 | private Collection<String> mStrings; |
| 126 | public AnyStringIn(Collection<String> strings) { |
| 127 | this.mStrings = strings; |
| 128 | } |
| 129 | |
| 130 | @Override |
Philip P. Moltmann | 3168f6a | 2017-11-22 15:55:59 -0800 | [diff] [blame] | 131 | public boolean matches(String string) { |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 132 | return mStrings.contains(string); |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | private ITelecomService.Stub mTSIBinder; |
| 137 | private AppOpsManager mAppOpsManager; |
| 138 | private UserManager mUserManager; |
| 139 | |
| 140 | @Mock private CallsManager mFakeCallsManager; |
| 141 | @Mock private PhoneAccountRegistrar mFakePhoneAccountRegistrar; |
| 142 | @Mock private TelecomManager mTelecomManager; |
| 143 | private CallIntentProcessor.Adapter mCallIntentProcessorAdapter = |
| 144 | spy(new CallIntentProcessAdapterFake()); |
Hall Liu | 7c92832 | 2016-12-06 18:15:39 -0800 | [diff] [blame] | 145 | @Mock private DefaultDialerCache mDefaultDialerCache; |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 146 | private TelecomServiceImpl.SubscriptionManagerAdapter mSubscriptionManagerAdapter = |
| 147 | spy(new SubscriptionManagerAdapterFake()); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 148 | @Mock private UserCallIntentProcessor mUserCallIntentProcessor; |
| 149 | |
| 150 | private final TelecomSystem.SyncRoot mLock = new TelecomSystem.SyncRoot() { }; |
| 151 | |
| 152 | private static final String DEFAULT_DIALER_PACKAGE = "com.google.android.dialer"; |
| 153 | private static final UserHandle USER_HANDLE_16 = new UserHandle(16); |
| 154 | private static final UserHandle USER_HANDLE_17 = new UserHandle(17); |
| 155 | private static final PhoneAccountHandle TEL_PA_HANDLE_16 = new PhoneAccountHandle( |
| 156 | new ComponentName("test", "telComponentName"), "0", USER_HANDLE_16); |
| 157 | private static final PhoneAccountHandle SIP_PA_HANDLE_17 = new PhoneAccountHandle( |
| 158 | new ComponentName("test", "sipComponentName"), "1", USER_HANDLE_17); |
| 159 | private static final PhoneAccountHandle TEL_PA_HANDLE_CURRENT = new PhoneAccountHandle( |
| 160 | new ComponentName("test", "telComponentName"), "2", Binder.getCallingUserHandle()); |
| 161 | private static final PhoneAccountHandle SIP_PA_HANDLE_CURRENT = new PhoneAccountHandle( |
| 162 | new ComponentName("test", "sipComponentName"), "3", Binder.getCallingUserHandle()); |
| 163 | |
| 164 | @Override |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 165 | @Before |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 166 | public void setUp() throws Exception { |
| 167 | super.setUp(); |
| 168 | mContext = mComponentContextFixture.getTestDouble().getApplicationContext(); |
| 169 | mComponentContextFixture.putBooleanResource( |
| 170 | com.android.internal.R.bool.config_voice_capable, true); |
| 171 | |
| 172 | doReturn(mContext).when(mContext).getApplicationContext(); |
| 173 | doNothing().when(mContext).sendBroadcastAsUser(any(Intent.class), any(UserHandle.class), |
| 174 | anyString()); |
| 175 | TelecomServiceImpl telecomServiceImpl = new TelecomServiceImpl( |
| 176 | mContext, |
| 177 | mFakeCallsManager, |
| 178 | mFakePhoneAccountRegistrar, |
| 179 | mCallIntentProcessorAdapter, |
| 180 | new UserCallIntentProcessorFactory() { |
| 181 | @Override |
| 182 | public UserCallIntentProcessor create(Context context, UserHandle userHandle) { |
| 183 | return mUserCallIntentProcessor; |
| 184 | } |
| 185 | }, |
Hall Liu | 7c92832 | 2016-12-06 18:15:39 -0800 | [diff] [blame] | 186 | mDefaultDialerCache, |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 187 | mSubscriptionManagerAdapter, |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 188 | mLock); |
| 189 | mTSIBinder = telecomServiceImpl.getBinder(); |
| 190 | mComponentContextFixture.setTelecomManager(mTelecomManager); |
| 191 | when(mTelecomManager.getDefaultDialerPackage()).thenReturn(DEFAULT_DIALER_PACKAGE); |
| 192 | when(mTelecomManager.getSystemDialerPackage()).thenReturn(DEFAULT_DIALER_PACKAGE); |
| 193 | |
| 194 | mAppOpsManager = (AppOpsManager) mContext.getSystemService(Context.APP_OPS_SERVICE); |
| 195 | mUserManager = (UserManager) mContext.getSystemService(Context.USER_SERVICE); |
| 196 | |
Hall Liu | 7c92832 | 2016-12-06 18:15:39 -0800 | [diff] [blame] | 197 | when(mDefaultDialerCache.getDefaultDialerApplication(anyInt())) |
| 198 | .thenReturn(DEFAULT_DIALER_PACKAGE); |
| 199 | when(mDefaultDialerCache.isDefaultOrSystemDialer(eq(DEFAULT_DIALER_PACKAGE), anyInt())) |
| 200 | .thenReturn(true); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 201 | } |
| 202 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 203 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 204 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 205 | public void testGetDefaultOutgoingPhoneAccount() throws RemoteException { |
| 206 | when(mFakePhoneAccountRegistrar |
| 207 | .getOutgoingPhoneAccountForScheme(eq("tel"), any(UserHandle.class))) |
| 208 | .thenReturn(TEL_PA_HANDLE_16); |
| 209 | when(mFakePhoneAccountRegistrar |
| 210 | .getOutgoingPhoneAccountForScheme(eq("sip"), any(UserHandle.class))) |
| 211 | .thenReturn(SIP_PA_HANDLE_17); |
| 212 | makeAccountsVisibleToAllUsers(TEL_PA_HANDLE_16, SIP_PA_HANDLE_17); |
| 213 | |
| 214 | PhoneAccountHandle returnedHandleTel |
| 215 | = mTSIBinder.getDefaultOutgoingPhoneAccount("tel", DEFAULT_DIALER_PACKAGE); |
| 216 | assertEquals(TEL_PA_HANDLE_16, returnedHandleTel); |
| 217 | |
| 218 | PhoneAccountHandle returnedHandleSip |
| 219 | = mTSIBinder.getDefaultOutgoingPhoneAccount("sip", DEFAULT_DIALER_PACKAGE); |
| 220 | assertEquals(SIP_PA_HANDLE_17, returnedHandleSip); |
| 221 | } |
| 222 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 223 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 224 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 225 | public void testGetDefaultOutgoingPhoneAccountFailure() throws RemoteException { |
| 226 | // make sure that the list of user profiles doesn't include anything the PhoneAccountHandles |
| 227 | // are associated with |
| 228 | |
| 229 | when(mFakePhoneAccountRegistrar |
| 230 | .getOutgoingPhoneAccountForScheme(eq("tel"), any(UserHandle.class))) |
| 231 | .thenReturn(TEL_PA_HANDLE_16); |
| 232 | when(mFakePhoneAccountRegistrar.getPhoneAccountUnchecked(TEL_PA_HANDLE_16)).thenReturn( |
| 233 | makePhoneAccount(TEL_PA_HANDLE_16).build()); |
| 234 | when(mAppOpsManager.noteOp(eq(AppOpsManager.OP_READ_PHONE_STATE), anyInt(), anyString())) |
| 235 | .thenReturn(AppOpsManager.MODE_IGNORED); |
| 236 | doThrow(new SecurityException()).when(mContext) |
| 237 | .enforceCallingOrSelfPermission(eq(READ_PRIVILEGED_PHONE_STATE), anyString()); |
| 238 | |
| 239 | PhoneAccountHandle returnedHandleTel |
| 240 | = mTSIBinder.getDefaultOutgoingPhoneAccount("tel", ""); |
| 241 | assertNull(returnedHandleTel); |
| 242 | } |
| 243 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 244 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 245 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 246 | public void testGetUserSelectedOutgoingPhoneAccount() throws RemoteException { |
| 247 | when(mFakePhoneAccountRegistrar.getUserSelectedOutgoingPhoneAccount(any(UserHandle.class))) |
| 248 | .thenReturn(TEL_PA_HANDLE_16); |
| 249 | when(mFakePhoneAccountRegistrar.getPhoneAccountUnchecked(TEL_PA_HANDLE_16)).thenReturn( |
| 250 | makeMultiUserPhoneAccount(TEL_PA_HANDLE_16).build()); |
| 251 | |
| 252 | PhoneAccountHandle returnedHandle |
| 253 | = mTSIBinder.getUserSelectedOutgoingPhoneAccount(); |
| 254 | assertEquals(TEL_PA_HANDLE_16, returnedHandle); |
| 255 | } |
| 256 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 257 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 258 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 259 | public void testSetUserSelectedOutgoingPhoneAccount() throws RemoteException { |
| 260 | mTSIBinder.setUserSelectedOutgoingPhoneAccount(TEL_PA_HANDLE_16); |
| 261 | verify(mFakePhoneAccountRegistrar) |
| 262 | .setUserSelectedOutgoingPhoneAccount(eq(TEL_PA_HANDLE_16), any(UserHandle.class)); |
| 263 | } |
| 264 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 265 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 266 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 267 | public void testSetUserSelectedOutgoingPhoneAccountFailure() throws RemoteException { |
| 268 | doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 269 | anyString(), nullable(String.class)); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 270 | try { |
| 271 | mTSIBinder.setUserSelectedOutgoingPhoneAccount(TEL_PA_HANDLE_16); |
| 272 | } catch (SecurityException e) { |
| 273 | // desired result |
| 274 | } |
| 275 | verify(mFakePhoneAccountRegistrar, never()) |
| 276 | .setUserSelectedOutgoingPhoneAccount( |
| 277 | any(PhoneAccountHandle.class), any(UserHandle.class)); |
| 278 | } |
| 279 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 280 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 281 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 282 | public void testGetCallCapablePhoneAccounts() throws RemoteException { |
| 283 | List<PhoneAccountHandle> fullPHList = new ArrayList<PhoneAccountHandle>() {{ |
| 284 | add(TEL_PA_HANDLE_16); |
| 285 | add(SIP_PA_HANDLE_17); |
| 286 | }}; |
| 287 | |
| 288 | List<PhoneAccountHandle> smallPHList = new ArrayList<PhoneAccountHandle>() {{ |
| 289 | add(SIP_PA_HANDLE_17); |
| 290 | }}; |
| 291 | // Returns all phone accounts when getCallCapablePhoneAccounts is called. |
| 292 | when(mFakePhoneAccountRegistrar |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 293 | .getCallCapablePhoneAccounts(nullable(String.class), eq(true), |
| 294 | nullable(UserHandle.class))).thenReturn(fullPHList); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 295 | // Returns only enabled phone accounts when getCallCapablePhoneAccounts is called. |
| 296 | when(mFakePhoneAccountRegistrar |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 297 | .getCallCapablePhoneAccounts(nullable(String.class), eq(false), |
| 298 | nullable(UserHandle.class))).thenReturn(smallPHList); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 299 | makeAccountsVisibleToAllUsers(TEL_PA_HANDLE_16, SIP_PA_HANDLE_17); |
| 300 | |
| 301 | assertEquals(fullPHList, |
| 302 | mTSIBinder.getCallCapablePhoneAccounts(true, DEFAULT_DIALER_PACKAGE)); |
| 303 | assertEquals(smallPHList, |
| 304 | mTSIBinder.getCallCapablePhoneAccounts(false, DEFAULT_DIALER_PACKAGE)); |
| 305 | } |
| 306 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 307 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 308 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 309 | public void testGetCallCapablePhoneAccountsFailure() throws RemoteException { |
| 310 | List<String> enforcedPermissions = new ArrayList<String>() {{ |
| 311 | add(READ_PHONE_STATE); |
| 312 | add(READ_PRIVILEGED_PHONE_STATE); |
| 313 | }}; |
| 314 | doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( |
| 315 | argThat(new AnyStringIn(enforcedPermissions)), anyString()); |
| 316 | |
| 317 | List<PhoneAccountHandle> result = null; |
| 318 | try { |
| 319 | result = mTSIBinder.getCallCapablePhoneAccounts(true, ""); |
| 320 | } catch (SecurityException e) { |
| 321 | // intended behavior |
| 322 | } |
| 323 | assertNull(result); |
| 324 | verify(mFakePhoneAccountRegistrar, never()) |
| 325 | .getCallCapablePhoneAccounts(anyString(), anyBoolean(), any(UserHandle.class)); |
| 326 | } |
| 327 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 328 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 329 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 330 | public void testGetPhoneAccountsSupportingScheme() throws RemoteException { |
| 331 | List<PhoneAccountHandle> sipPHList = new ArrayList<PhoneAccountHandle>() {{ |
| 332 | add(SIP_PA_HANDLE_17); |
| 333 | }}; |
| 334 | |
| 335 | List<PhoneAccountHandle> telPHList = new ArrayList<PhoneAccountHandle>() {{ |
| 336 | add(TEL_PA_HANDLE_16); |
| 337 | }}; |
| 338 | when(mFakePhoneAccountRegistrar |
| 339 | .getCallCapablePhoneAccounts(eq("tel"), anyBoolean(), any(UserHandle.class))) |
| 340 | .thenReturn(telPHList); |
| 341 | when(mFakePhoneAccountRegistrar |
| 342 | .getCallCapablePhoneAccounts(eq("sip"), anyBoolean(), any(UserHandle.class))) |
| 343 | .thenReturn(sipPHList); |
| 344 | makeAccountsVisibleToAllUsers(TEL_PA_HANDLE_16, SIP_PA_HANDLE_17); |
| 345 | |
| 346 | assertEquals(telPHList, |
| 347 | mTSIBinder.getPhoneAccountsSupportingScheme("tel", DEFAULT_DIALER_PACKAGE)); |
| 348 | assertEquals(sipPHList, |
| 349 | mTSIBinder.getPhoneAccountsSupportingScheme("sip", DEFAULT_DIALER_PACKAGE)); |
| 350 | } |
| 351 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 352 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 353 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 354 | public void testGetPhoneAccountsForPackage() throws RemoteException { |
| 355 | List<PhoneAccountHandle> phoneAccountHandleList = new ArrayList<PhoneAccountHandle>() {{ |
| 356 | add(TEL_PA_HANDLE_16); |
| 357 | add(SIP_PA_HANDLE_17); |
| 358 | }}; |
| 359 | when(mFakePhoneAccountRegistrar |
| 360 | .getPhoneAccountsForPackage(anyString(), any(UserHandle.class))) |
| 361 | .thenReturn(phoneAccountHandleList); |
| 362 | makeAccountsVisibleToAllUsers(TEL_PA_HANDLE_16, SIP_PA_HANDLE_17); |
| 363 | assertEquals(phoneAccountHandleList, |
| 364 | mTSIBinder.getPhoneAccountsForPackage( |
| 365 | TEL_PA_HANDLE_16.getComponentName().getPackageName())); |
| 366 | } |
| 367 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 368 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 369 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 370 | public void testGetPhoneAccount() throws RemoteException { |
| 371 | makeAccountsVisibleToAllUsers(TEL_PA_HANDLE_16, SIP_PA_HANDLE_17); |
| 372 | assertEquals(TEL_PA_HANDLE_16, mTSIBinder.getPhoneAccount(TEL_PA_HANDLE_16) |
| 373 | .getAccountHandle()); |
| 374 | assertEquals(SIP_PA_HANDLE_17, mTSIBinder.getPhoneAccount(SIP_PA_HANDLE_17) |
| 375 | .getAccountHandle()); |
| 376 | } |
| 377 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 378 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 379 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 380 | public void testGetAllPhoneAccounts() throws RemoteException { |
| 381 | List<PhoneAccount> phoneAccountList = new ArrayList<PhoneAccount>() {{ |
| 382 | add(makePhoneAccount(TEL_PA_HANDLE_16).build()); |
| 383 | add(makePhoneAccount(SIP_PA_HANDLE_17).build()); |
| 384 | }}; |
| 385 | when(mFakePhoneAccountRegistrar.getAllPhoneAccounts(any(UserHandle.class))) |
| 386 | .thenReturn(phoneAccountList); |
| 387 | |
| 388 | assertEquals(2, mTSIBinder.getAllPhoneAccounts().size()); |
| 389 | } |
| 390 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 391 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 392 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 393 | public void testRegisterPhoneAccount() throws RemoteException { |
| 394 | String packageNameToUse = "com.android.officialpackage"; |
| 395 | PhoneAccountHandle phHandle = new PhoneAccountHandle(new ComponentName( |
| 396 | packageNameToUse, "cs"), "test", Binder.getCallingUserHandle()); |
| 397 | PhoneAccount phoneAccount = makePhoneAccount(phHandle).build(); |
| 398 | doReturn(PackageManager.PERMISSION_GRANTED) |
| 399 | .when(mContext).checkCallingOrSelfPermission(MODIFY_PHONE_STATE); |
| 400 | |
| 401 | registerPhoneAccountTestHelper(phoneAccount, true); |
| 402 | } |
| 403 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 404 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 405 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 406 | public void testRegisterPhoneAccountWithoutModifyPermission() throws RemoteException { |
| 407 | // tests the case where the package does not have MODIFY_PHONE_STATE but is |
| 408 | // registering its own phone account as a third-party connection service |
| 409 | String packageNameToUse = "com.thirdparty.connectionservice"; |
| 410 | PhoneAccountHandle phHandle = new PhoneAccountHandle(new ComponentName( |
| 411 | packageNameToUse, "cs"), "asdf", Binder.getCallingUserHandle()); |
| 412 | PhoneAccount phoneAccount = makePhoneAccount(phHandle).build(); |
| 413 | |
| 414 | doReturn(PackageManager.PERMISSION_DENIED) |
| 415 | .when(mContext).checkCallingOrSelfPermission(MODIFY_PHONE_STATE); |
| 416 | PackageManager pm = mContext.getPackageManager(); |
| 417 | when(pm.hasSystemFeature(PackageManager.FEATURE_CONNECTION_SERVICE)).thenReturn(true); |
| 418 | |
| 419 | registerPhoneAccountTestHelper(phoneAccount, true); |
| 420 | } |
| 421 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 422 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 423 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 424 | public void testRegisterPhoneAccountWithoutModifyPermissionFailure() throws RemoteException { |
| 425 | // tests the case where the third party package should not be allowed to register a phone |
| 426 | // account due to the lack of modify permission. |
| 427 | String packageNameToUse = "com.thirdparty.connectionservice"; |
| 428 | PhoneAccountHandle phHandle = new PhoneAccountHandle(new ComponentName( |
| 429 | packageNameToUse, "cs"), "asdf", Binder.getCallingUserHandle()); |
| 430 | PhoneAccount phoneAccount = makePhoneAccount(phHandle).build(); |
| 431 | |
| 432 | doReturn(PackageManager.PERMISSION_DENIED) |
| 433 | .when(mContext).checkCallingOrSelfPermission(MODIFY_PHONE_STATE); |
| 434 | PackageManager pm = mContext.getPackageManager(); |
| 435 | when(pm.hasSystemFeature(PackageManager.FEATURE_CONNECTION_SERVICE)).thenReturn(false); |
| 436 | |
| 437 | registerPhoneAccountTestHelper(phoneAccount, false); |
| 438 | } |
| 439 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 440 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 441 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 442 | public void testRegisterPhoneAccountWithoutSimSubscriptionPermissionFailure() |
| 443 | throws RemoteException { |
| 444 | String packageNameToUse = "com.thirdparty.connectionservice"; |
| 445 | PhoneAccountHandle phHandle = new PhoneAccountHandle(new ComponentName( |
| 446 | packageNameToUse, "cs"), "asdf", Binder.getCallingUserHandle()); |
| 447 | PhoneAccount phoneAccount = makePhoneAccount(phHandle) |
| 448 | .setCapabilities(PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION).build(); |
| 449 | |
| 450 | doReturn(PackageManager.PERMISSION_GRANTED) |
| 451 | .when(mContext).checkCallingOrSelfPermission(MODIFY_PHONE_STATE); |
| 452 | doThrow(new SecurityException()) |
| 453 | .when(mContext) |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 454 | .enforceCallingOrSelfPermission(eq(REGISTER_SIM_SUBSCRIPTION), |
| 455 | nullable(String.class)); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 456 | |
| 457 | registerPhoneAccountTestHelper(phoneAccount, false); |
| 458 | } |
| 459 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 460 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 461 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 462 | public void testRegisterPhoneAccountWithoutMultiUserPermissionFailure() |
| 463 | throws Exception { |
| 464 | String packageNameToUse = "com.thirdparty.connectionservice"; |
| 465 | PhoneAccountHandle phHandle = new PhoneAccountHandle(new ComponentName( |
| 466 | packageNameToUse, "cs"), "asdf", Binder.getCallingUserHandle()); |
| 467 | PhoneAccount phoneAccount = makeMultiUserPhoneAccount(phHandle).build(); |
| 468 | |
| 469 | doReturn(PackageManager.PERMISSION_GRANTED) |
| 470 | .when(mContext).checkCallingOrSelfPermission(MODIFY_PHONE_STATE); |
| 471 | |
| 472 | PackageManager packageManager = mContext.getPackageManager(); |
| 473 | when(packageManager.getApplicationInfo(packageNameToUse, PackageManager.GET_META_DATA)) |
| 474 | .thenReturn(new ApplicationInfo()); |
| 475 | |
| 476 | registerPhoneAccountTestHelper(phoneAccount, false); |
| 477 | } |
| 478 | |
| 479 | private void registerPhoneAccountTestHelper(PhoneAccount testPhoneAccount, |
| 480 | boolean shouldSucceed) throws RemoteException { |
| 481 | ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); |
| 482 | boolean didExceptionOccur = false; |
| 483 | try { |
| 484 | mTSIBinder.registerPhoneAccount(testPhoneAccount); |
| 485 | } catch (Exception e) { |
| 486 | didExceptionOccur = true; |
| 487 | } |
| 488 | |
| 489 | if (shouldSucceed) { |
| 490 | assertFalse(didExceptionOccur); |
| 491 | verify(mFakePhoneAccountRegistrar).registerPhoneAccount(testPhoneAccount); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 492 | } else { |
| 493 | assertTrue(didExceptionOccur); |
| 494 | verify(mFakePhoneAccountRegistrar, never()) |
| 495 | .registerPhoneAccount(any(PhoneAccount.class)); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 496 | } |
| 497 | } |
| 498 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 499 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 500 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 501 | public void testUnregisterPhoneAccount() throws RemoteException { |
| 502 | String packageNameToUse = "com.android.officialpackage"; |
| 503 | PhoneAccountHandle phHandle = new PhoneAccountHandle(new ComponentName( |
| 504 | packageNameToUse, "cs"), "test", Binder.getCallingUserHandle()); |
| 505 | |
| 506 | ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); |
| 507 | doReturn(PackageManager.PERMISSION_GRANTED) |
| 508 | .when(mContext).checkCallingOrSelfPermission(MODIFY_PHONE_STATE); |
| 509 | |
| 510 | mTSIBinder.unregisterPhoneAccount(phHandle); |
| 511 | verify(mFakePhoneAccountRegistrar).unregisterPhoneAccount(phHandle); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 512 | } |
| 513 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 514 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 515 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 516 | public void testUnregisterPhoneAccountFailure() throws RemoteException { |
| 517 | String packageNameToUse = "com.thirdparty.connectionservice"; |
| 518 | PhoneAccountHandle phHandle = new PhoneAccountHandle(new ComponentName( |
| 519 | packageNameToUse, "cs"), "asdf", Binder.getCallingUserHandle()); |
| 520 | |
| 521 | doReturn(PackageManager.PERMISSION_DENIED) |
| 522 | .when(mContext).checkCallingOrSelfPermission(MODIFY_PHONE_STATE); |
| 523 | PackageManager pm = mContext.getPackageManager(); |
| 524 | when(pm.hasSystemFeature(PackageManager.FEATURE_CONNECTION_SERVICE)).thenReturn(false); |
| 525 | |
| 526 | try { |
| 527 | mTSIBinder.unregisterPhoneAccount(phHandle); |
| 528 | } catch (UnsupportedOperationException e) { |
| 529 | // expected behavior |
| 530 | } |
| 531 | verify(mFakePhoneAccountRegistrar, never()) |
| 532 | .unregisterPhoneAccount(any(PhoneAccountHandle.class)); |
| 533 | verify(mContext, never()) |
| 534 | .sendBroadcastAsUser(any(Intent.class), any(UserHandle.class), anyString()); |
| 535 | } |
| 536 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 537 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 538 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 539 | public void testAddNewIncomingCall() throws Exception { |
Brad Ebinger | c34d474 | 2016-02-23 16:58:48 -0800 | [diff] [blame] | 540 | PhoneAccount phoneAccount = makePhoneAccount(TEL_PA_HANDLE_CURRENT).build(); |
| 541 | phoneAccount.setIsEnabled(true); |
| 542 | doReturn(phoneAccount).when(mFakePhoneAccountRegistrar).getPhoneAccount( |
| 543 | eq(TEL_PA_HANDLE_CURRENT), any(UserHandle.class)); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 544 | doNothing().when(mAppOpsManager).checkPackage(anyInt(), anyString()); |
| 545 | Bundle extras = createSampleExtras(); |
| 546 | |
| 547 | mTSIBinder.addNewIncomingCall(TEL_PA_HANDLE_CURRENT, extras); |
| 548 | |
| 549 | addCallTestHelper(TelecomManager.ACTION_INCOMING_CALL, |
| 550 | CallIntentProcessor.KEY_IS_INCOMING_CALL, extras, false); |
| 551 | } |
| 552 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 553 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 554 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 555 | public void testAddNewIncomingCallFailure() throws Exception { |
| 556 | try { |
| 557 | mTSIBinder.addNewIncomingCall(TEL_PA_HANDLE_16, null); |
| 558 | } catch (SecurityException e) { |
| 559 | // expected |
| 560 | } |
| 561 | |
| 562 | doThrow(new SecurityException()).when(mAppOpsManager).checkPackage(anyInt(), anyString()); |
| 563 | |
| 564 | try { |
| 565 | mTSIBinder.addNewIncomingCall(TEL_PA_HANDLE_CURRENT, null); |
| 566 | } catch (SecurityException e) { |
| 567 | // expected |
| 568 | } |
| 569 | |
| 570 | // Verify that neither of these attempts got through |
| 571 | verify(mCallIntentProcessorAdapter, never()) |
| 572 | .processIncomingCallIntent(any(CallsManager.class), any(Intent.class)); |
| 573 | } |
| 574 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 575 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 576 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 577 | public void testAddNewUnknownCall() throws Exception { |
Brad Ebinger | c34d474 | 2016-02-23 16:58:48 -0800 | [diff] [blame] | 578 | PhoneAccount phoneAccount = makePhoneAccount(TEL_PA_HANDLE_CURRENT).build(); |
| 579 | phoneAccount.setIsEnabled(true); |
| 580 | doReturn(phoneAccount).when(mFakePhoneAccountRegistrar).getPhoneAccount( |
| 581 | eq(TEL_PA_HANDLE_CURRENT), any(UserHandle.class)); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 582 | doNothing().when(mAppOpsManager).checkPackage(anyInt(), anyString()); |
| 583 | Bundle extras = createSampleExtras(); |
| 584 | |
| 585 | mTSIBinder.addNewUnknownCall(TEL_PA_HANDLE_CURRENT, extras); |
| 586 | |
| 587 | addCallTestHelper(TelecomManager.ACTION_NEW_UNKNOWN_CALL, |
| 588 | CallIntentProcessor.KEY_IS_UNKNOWN_CALL, extras, true); |
| 589 | } |
| 590 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 591 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 592 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 593 | public void testAddNewUnknownCallFailure() throws Exception { |
| 594 | try { |
| 595 | mTSIBinder.addNewUnknownCall(TEL_PA_HANDLE_16, null); |
| 596 | } catch (SecurityException e) { |
| 597 | // expected |
| 598 | } |
| 599 | |
| 600 | doThrow(new SecurityException()).when(mAppOpsManager).checkPackage(anyInt(), anyString()); |
| 601 | |
| 602 | try { |
| 603 | mTSIBinder.addNewUnknownCall(TEL_PA_HANDLE_CURRENT, null); |
| 604 | } catch (SecurityException e) { |
| 605 | // expected |
| 606 | } |
| 607 | |
| 608 | // Verify that neither of these attempts got through |
| 609 | verify(mCallIntentProcessorAdapter, never()) |
| 610 | .processIncomingCallIntent(any(CallsManager.class), any(Intent.class)); |
| 611 | } |
| 612 | |
| 613 | private void addCallTestHelper(String expectedAction, String extraCallKey, |
| 614 | Bundle expectedExtras, boolean isUnknown) { |
| 615 | ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); |
| 616 | if (isUnknown) { |
| 617 | verify(mCallIntentProcessorAdapter).processUnknownCallIntent(any(CallsManager.class), |
| 618 | intentCaptor.capture()); |
| 619 | } else { |
| 620 | verify(mCallIntentProcessorAdapter).processIncomingCallIntent(any(CallsManager.class), |
| 621 | intentCaptor.capture()); |
| 622 | } |
| 623 | Intent capturedIntent = intentCaptor.getValue(); |
| 624 | assertEquals(expectedAction, capturedIntent.getAction()); |
| 625 | Bundle intentExtras = capturedIntent.getExtras(); |
| 626 | assertEquals(TEL_PA_HANDLE_CURRENT, |
| 627 | intentExtras.get(TelecomManager.EXTRA_PHONE_ACCOUNT_HANDLE)); |
| 628 | assertTrue(intentExtras.getBoolean(extraCallKey)); |
| 629 | |
| 630 | if (isUnknown) { |
| 631 | for (String expectedKey : expectedExtras.keySet()) { |
| 632 | assertTrue(intentExtras.containsKey(expectedKey)); |
| 633 | assertEquals(expectedExtras.get(expectedKey), intentExtras.get(expectedKey)); |
| 634 | } |
| 635 | } |
| 636 | else { |
| 637 | assertTrue(areBundlesEqual(expectedExtras, |
| 638 | (Bundle) intentExtras.get(TelecomManager.EXTRA_INCOMING_CALL_EXTRAS))); |
| 639 | } |
| 640 | } |
| 641 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 642 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 643 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 644 | public void testPlaceCallWithNonEmergencyPermission() throws Exception { |
| 645 | Uri handle = Uri.parse("tel:6505551234"); |
| 646 | Bundle extras = createSampleExtras(); |
| 647 | |
| 648 | when(mAppOpsManager.noteOp(eq(AppOpsManager.OP_CALL_PHONE), anyInt(), anyString())) |
| 649 | .thenReturn(AppOpsManager.MODE_ALLOWED); |
| 650 | doReturn(PackageManager.PERMISSION_GRANTED) |
| 651 | .when(mContext).checkCallingPermission(CALL_PHONE); |
Tyler Gunn | b652dad | 2018-03-27 18:49:49 +0000 | [diff] [blame] | 652 | doReturn(PackageManager.PERMISSION_DENIED) |
| 653 | .when(mContext).checkCallingPermission(CALL_PRIVILEGED); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 654 | |
| 655 | mTSIBinder.placeCall(handle, extras, DEFAULT_DIALER_PACKAGE); |
| 656 | placeCallTestHelper(handle, extras, true); |
| 657 | } |
| 658 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 659 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 660 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 661 | public void testPlaceCallWithAppOpsOff() throws Exception { |
| 662 | Uri handle = Uri.parse("tel:6505551234"); |
| 663 | Bundle extras = createSampleExtras(); |
| 664 | |
| 665 | when(mAppOpsManager.noteOp(eq(AppOpsManager.OP_CALL_PHONE), anyInt(), anyString())) |
| 666 | .thenReturn(AppOpsManager.MODE_IGNORED); |
| 667 | doReturn(PackageManager.PERMISSION_GRANTED) |
| 668 | .when(mContext).checkCallingPermission(CALL_PHONE); |
Tyler Gunn | b652dad | 2018-03-27 18:49:49 +0000 | [diff] [blame] | 669 | doReturn(PackageManager.PERMISSION_DENIED) |
| 670 | .when(mContext).checkCallingPermission(CALL_PRIVILEGED); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 671 | |
| 672 | mTSIBinder.placeCall(handle, extras, DEFAULT_DIALER_PACKAGE); |
| 673 | placeCallTestHelper(handle, extras, false); |
| 674 | } |
| 675 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 676 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 677 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 678 | public void testPlaceCallWithNoCallingPermission() throws Exception { |
| 679 | Uri handle = Uri.parse("tel:6505551234"); |
| 680 | Bundle extras = createSampleExtras(); |
| 681 | |
| 682 | when(mAppOpsManager.noteOp(eq(AppOpsManager.OP_CALL_PHONE), anyInt(), anyString())) |
| 683 | .thenReturn(AppOpsManager.MODE_ALLOWED); |
| 684 | doReturn(PackageManager.PERMISSION_DENIED) |
| 685 | .when(mContext).checkCallingPermission(CALL_PHONE); |
Tyler Gunn | b652dad | 2018-03-27 18:49:49 +0000 | [diff] [blame] | 686 | doReturn(PackageManager.PERMISSION_DENIED) |
| 687 | .when(mContext).checkCallingPermission(CALL_PRIVILEGED); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 688 | |
| 689 | mTSIBinder.placeCall(handle, extras, DEFAULT_DIALER_PACKAGE); |
| 690 | placeCallTestHelper(handle, extras, false); |
| 691 | } |
| 692 | |
| 693 | private void placeCallTestHelper(Uri expectedHandle, Bundle expectedExtras, |
| 694 | boolean shouldNonEmergencyBeAllowed) { |
| 695 | ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); |
| 696 | verify(mUserCallIntentProcessor).processIntent(intentCaptor.capture(), anyString(), |
Tyler Gunn | b652dad | 2018-03-27 18:49:49 +0000 | [diff] [blame] | 697 | eq(shouldNonEmergencyBeAllowed), eq(true)); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 698 | Intent capturedIntent = intentCaptor.getValue(); |
| 699 | assertEquals(Intent.ACTION_CALL, capturedIntent.getAction()); |
| 700 | assertEquals(expectedHandle, capturedIntent.getData()); |
| 701 | assertTrue(areBundlesEqual(expectedExtras, capturedIntent.getExtras())); |
| 702 | } |
| 703 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 704 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 705 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 706 | public void testPlaceCallFailure() throws Exception { |
| 707 | Uri handle = Uri.parse("tel:6505551234"); |
| 708 | Bundle extras = createSampleExtras(); |
| 709 | |
| 710 | doThrow(new SecurityException()) |
| 711 | .when(mContext).enforceCallingOrSelfPermission(eq(CALL_PHONE), anyString()); |
| 712 | |
| 713 | try { |
| 714 | mTSIBinder.placeCall(handle, extras, "arbitrary_package_name"); |
| 715 | } catch (SecurityException e) { |
| 716 | // expected |
| 717 | } |
| 718 | |
| 719 | verify(mUserCallIntentProcessor, never()) |
Tyler Gunn | b652dad | 2018-03-27 18:49:49 +0000 | [diff] [blame] | 720 | .processIntent(any(Intent.class), anyString(), anyBoolean(), eq(true)); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 721 | } |
| 722 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 723 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 724 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 725 | public void testSetDefaultDialer() throws Exception { |
| 726 | String packageName = "sample.package"; |
Hall Liu | 7c92832 | 2016-12-06 18:15:39 -0800 | [diff] [blame] | 727 | int currentUser = ActivityManager.getCurrentUser(); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 728 | |
Hall Liu | 7c92832 | 2016-12-06 18:15:39 -0800 | [diff] [blame] | 729 | when(mDefaultDialerCache.setDefaultDialer(eq(packageName), eq(currentUser))) |
| 730 | .thenReturn(true); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 731 | |
| 732 | mTSIBinder.setDefaultDialer(packageName); |
| 733 | |
Hall Liu | 7c92832 | 2016-12-06 18:15:39 -0800 | [diff] [blame] | 734 | verify(mDefaultDialerCache).setDefaultDialer(eq(packageName), eq(currentUser)); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 735 | ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); |
| 736 | verify(mContext).sendBroadcastAsUser(intentCaptor.capture(), any(UserHandle.class)); |
| 737 | Intent capturedIntent = intentCaptor.getValue(); |
| 738 | assertEquals(TelecomManager.ACTION_DEFAULT_DIALER_CHANGED, capturedIntent.getAction()); |
| 739 | String packageNameExtra = capturedIntent.getStringExtra( |
| 740 | TelecomManager.EXTRA_CHANGE_DEFAULT_DIALER_PACKAGE_NAME); |
| 741 | assertEquals(packageName, packageNameExtra); |
| 742 | } |
| 743 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 744 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 745 | @Test |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 746 | public void testSetDefaultDialerNoModifyPhoneStatePermission() throws Exception { |
| 747 | doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 748 | eq(MODIFY_PHONE_STATE), nullable(String.class)); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 749 | setDefaultDialerFailureTestHelper(); |
| 750 | } |
| 751 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 752 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 753 | @Test |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 754 | public void testSetDefaultDialerNoWriteSecureSettingsPermission() throws Exception { |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 755 | doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 756 | eq(WRITE_SECURE_SETTINGS), nullable(String.class)); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 757 | setDefaultDialerFailureTestHelper(); |
| 758 | } |
| 759 | |
| 760 | private void setDefaultDialerFailureTestHelper() throws Exception { |
| 761 | boolean exceptionThrown = false; |
| 762 | try { |
| 763 | mTSIBinder.setDefaultDialer(DEFAULT_DIALER_PACKAGE); |
| 764 | } catch (SecurityException e) { |
| 765 | exceptionThrown = true; |
| 766 | } |
| 767 | assertTrue(exceptionThrown); |
Hall Liu | 7c92832 | 2016-12-06 18:15:39 -0800 | [diff] [blame] | 768 | verify(mDefaultDialerCache, never()).setDefaultDialer(anyString(), anyInt()); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 769 | verify(mContext, never()).sendBroadcastAsUser(any(Intent.class), any(UserHandle.class)); |
| 770 | } |
| 771 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 772 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 773 | @Test |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 774 | public void testIsVoicemailNumber() throws Exception { |
| 775 | String vmNumber = "010"; |
| 776 | makeAccountsVisibleToAllUsers(TEL_PA_HANDLE_CURRENT); |
| 777 | |
| 778 | doReturn(true).when(mFakePhoneAccountRegistrar).isVoiceMailNumber(TEL_PA_HANDLE_CURRENT, |
| 779 | vmNumber); |
| 780 | assertTrue(mTSIBinder.isVoiceMailNumber(TEL_PA_HANDLE_CURRENT, |
| 781 | vmNumber, DEFAULT_DIALER_PACKAGE)); |
| 782 | } |
| 783 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 784 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 785 | @Test |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 786 | public void testIsVoicemailNumberAccountNotVisibleFailure() throws Exception { |
| 787 | String vmNumber = "010"; |
| 788 | |
| 789 | doReturn(true).when(mFakePhoneAccountRegistrar).isVoiceMailNumber(TEL_PA_HANDLE_CURRENT, |
| 790 | vmNumber); |
| 791 | |
| 792 | when(mFakePhoneAccountRegistrar.getPhoneAccount(TEL_PA_HANDLE_CURRENT, |
| 793 | Binder.getCallingUserHandle())).thenReturn(null); |
| 794 | assertFalse(mTSIBinder |
| 795 | .isVoiceMailNumber(TEL_PA_HANDLE_CURRENT, vmNumber, DEFAULT_DIALER_PACKAGE)); |
| 796 | } |
| 797 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 798 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 799 | @Test |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 800 | public void testGetVoicemailNumberWithNullAccountHandle() throws Exception { |
| 801 | when(mFakePhoneAccountRegistrar.getPhoneAccount(isNull(PhoneAccountHandle.class), |
| 802 | eq(Binder.getCallingUserHandle()))) |
| 803 | .thenReturn(makePhoneAccount(TEL_PA_HANDLE_CURRENT).build()); |
| 804 | int subId = 58374; |
| 805 | String vmNumber = "543"; |
| 806 | doReturn(subId).when(mSubscriptionManagerAdapter).getDefaultVoiceSubId(); |
| 807 | |
| 808 | TelephonyManager mockTelephonyManager = |
| 809 | (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
| 810 | when(mockTelephonyManager.getVoiceMailNumber(subId)).thenReturn(vmNumber); |
| 811 | |
| 812 | assertEquals(vmNumber, mTSIBinder.getVoiceMailNumber(null, DEFAULT_DIALER_PACKAGE)); |
| 813 | } |
| 814 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 815 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 816 | @Test |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 817 | public void testGetVoicemailNumberWithNonNullAccountHandle() throws Exception { |
| 818 | when(mFakePhoneAccountRegistrar.getPhoneAccount(eq(TEL_PA_HANDLE_CURRENT), |
| 819 | eq(Binder.getCallingUserHandle()))) |
| 820 | .thenReturn(makePhoneAccount(TEL_PA_HANDLE_CURRENT).build()); |
| 821 | int subId = 58374; |
| 822 | String vmNumber = "543"; |
| 823 | |
| 824 | TelephonyManager mockTelephonyManager = |
| 825 | (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
| 826 | when(mockTelephonyManager.getVoiceMailNumber(subId)).thenReturn(vmNumber); |
| 827 | when(mFakePhoneAccountRegistrar.getSubscriptionIdForPhoneAccount(TEL_PA_HANDLE_CURRENT)) |
| 828 | .thenReturn(subId); |
| 829 | |
| 830 | assertEquals(vmNumber, |
| 831 | mTSIBinder.getVoiceMailNumber(TEL_PA_HANDLE_CURRENT, DEFAULT_DIALER_PACKAGE)); |
| 832 | } |
| 833 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 834 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 835 | @Test |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 836 | public void testGetLine1Number() throws Exception { |
| 837 | int subId = 58374; |
| 838 | String line1Number = "9482752023479"; |
| 839 | makeAccountsVisibleToAllUsers(TEL_PA_HANDLE_CURRENT); |
| 840 | when(mFakePhoneAccountRegistrar.getSubscriptionIdForPhoneAccount(TEL_PA_HANDLE_CURRENT)) |
| 841 | .thenReturn(subId); |
| 842 | TelephonyManager mockTelephonyManager = |
| 843 | (TelephonyManager) mContext.getSystemService(Context.TELEPHONY_SERVICE); |
Shishir Agrawal | ce68874 | 2016-01-25 15:02:21 -0800 | [diff] [blame] | 844 | when(mockTelephonyManager.getLine1Number(subId)).thenReturn(line1Number); |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 845 | |
| 846 | assertEquals(line1Number, |
| 847 | mTSIBinder.getLine1Number(TEL_PA_HANDLE_CURRENT, DEFAULT_DIALER_PACKAGE)); |
| 848 | } |
| 849 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 850 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 851 | @Test |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 852 | public void testEndCallWithRingingForegroundCall() throws Exception { |
| 853 | Call call = mock(Call.class); |
| 854 | when(call.getState()).thenReturn(CallState.RINGING); |
| 855 | when(mFakeCallsManager.getForegroundCall()).thenReturn(call); |
Tyler Gunn | b652dad | 2018-03-27 18:49:49 +0000 | [diff] [blame] | 856 | assertTrue(mTSIBinder.endCall(TEST_PACKAGE)); |
| 857 | verify(call).reject(eq(false), isNull(), eq(TEST_PACKAGE)); |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 858 | } |
| 859 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 860 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 861 | @Test |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 862 | public void testEndCallWithNonRingingForegroundCall() throws Exception { |
| 863 | Call call = mock(Call.class); |
| 864 | when(call.getState()).thenReturn(CallState.ACTIVE); |
| 865 | when(mFakeCallsManager.getForegroundCall()).thenReturn(call); |
Tyler Gunn | b652dad | 2018-03-27 18:49:49 +0000 | [diff] [blame] | 866 | assertTrue(mTSIBinder.endCall(TEST_PACKAGE)); |
| 867 | verify(call).disconnect(eq(0L), eq(TEST_PACKAGE)); |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 868 | } |
| 869 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 870 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 871 | @Test |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 872 | public void testEndCallWithNoForegroundCall() throws Exception { |
| 873 | Call call = mock(Call.class); |
| 874 | when(call.getState()).thenReturn(CallState.ACTIVE); |
Philip P. Moltmann | 3168f6a | 2017-11-22 15:55:59 -0800 | [diff] [blame] | 875 | when(mFakeCallsManager.getFirstCallWithState(any())) |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 876 | .thenReturn(call); |
Tyler Gunn | b652dad | 2018-03-27 18:49:49 +0000 | [diff] [blame] | 877 | assertTrue(mTSIBinder.endCall(TEST_PACKAGE)); |
| 878 | verify(call).disconnect(eq(0L), eq(TEST_PACKAGE)); |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 879 | } |
| 880 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 881 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 882 | @Test |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 883 | public void testEndCallWithNoCalls() throws Exception { |
Tyler Gunn | 587fc27 | 2018-02-07 16:07:29 -0800 | [diff] [blame] | 884 | assertFalse(mTSIBinder.endCall(null)); |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 885 | } |
| 886 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 887 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 888 | @Test |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 889 | public void testAcceptRingingCall() throws Exception { |
| 890 | Call call = mock(Call.class); |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 891 | when(mFakeCallsManager.getFirstCallWithState(anyInt())).thenReturn(call); |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 892 | // Not intended to be a real video state. Here to ensure that the call will be answered |
| 893 | // with whatever video state it's currently in. |
| 894 | int fakeVideoState = 29578215; |
| 895 | when(call.getVideoState()).thenReturn(fakeVideoState); |
Eugene Susla | ccba720 | 2017-02-10 10:56:53 -0800 | [diff] [blame] | 896 | mTSIBinder.acceptRingingCall(""); |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 897 | verify(call).answer(eq(fakeVideoState)); |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 898 | } |
| 899 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 900 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 901 | @Test |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 902 | public void testAcceptRingingCallWithValidVideoState() throws Exception { |
| 903 | Call call = mock(Call.class); |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 904 | when(mFakeCallsManager.getFirstCallWithState(anyInt())).thenReturn(call); |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 905 | // Not intended to be a real video state. Here to ensure that the call will be answered |
| 906 | // with the video state passed in to acceptRingingCallWithVideoState |
| 907 | int fakeVideoState = 29578215; |
| 908 | int realVideoState = VideoProfile.STATE_RX_ENABLED | VideoProfile.STATE_TX_ENABLED; |
| 909 | when(call.getVideoState()).thenReturn(fakeVideoState); |
Eugene Susla | ccba720 | 2017-02-10 10:56:53 -0800 | [diff] [blame] | 910 | mTSIBinder.acceptRingingCallWithVideoState("", realVideoState); |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 911 | verify(call).answer(realVideoState); |
| 912 | } |
| 913 | |
Tyler Gunn | baf105b | 2017-04-11 15:21:03 -0700 | [diff] [blame] | 914 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 915 | @Test |
Tyler Gunn | baf105b | 2017-04-11 15:21:03 -0700 | [diff] [blame] | 916 | public void testIsInCall() throws Exception { |
| 917 | when(mFakeCallsManager.hasOngoingCalls()).thenReturn(true); |
| 918 | assertTrue(mTSIBinder.isInCall(DEFAULT_DIALER_PACKAGE)); |
| 919 | } |
| 920 | |
| 921 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 922 | @Test |
Tyler Gunn | baf105b | 2017-04-11 15:21:03 -0700 | [diff] [blame] | 923 | public void testNotIsInCall() throws Exception { |
| 924 | when(mFakeCallsManager.hasOngoingCalls()).thenReturn(false); |
| 925 | assertFalse(mTSIBinder.isInCall(DEFAULT_DIALER_PACKAGE)); |
| 926 | } |
| 927 | |
| 928 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 929 | @Test |
Tyler Gunn | baf105b | 2017-04-11 15:21:03 -0700 | [diff] [blame] | 930 | public void testIsInCallFail() throws Exception { |
| 931 | doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( |
| 932 | anyString(), any()); |
| 933 | try { |
| 934 | mTSIBinder.isInCall("blah"); |
| 935 | fail(); |
| 936 | } catch (SecurityException e) { |
| 937 | // desired result |
| 938 | } |
| 939 | verify(mFakeCallsManager, never()).hasOngoingCalls(); |
| 940 | } |
| 941 | |
| 942 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 943 | @Test |
Tyler Gunn | baf105b | 2017-04-11 15:21:03 -0700 | [diff] [blame] | 944 | public void testIsInManagedCall() throws Exception { |
| 945 | when(mFakeCallsManager.hasOngoingManagedCalls()).thenReturn(true); |
| 946 | assertTrue(mTSIBinder.isInManagedCall(DEFAULT_DIALER_PACKAGE)); |
| 947 | } |
| 948 | |
| 949 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 950 | @Test |
Tyler Gunn | baf105b | 2017-04-11 15:21:03 -0700 | [diff] [blame] | 951 | public void testNotIsInManagedCall() throws Exception { |
| 952 | when(mFakeCallsManager.hasOngoingManagedCalls()).thenReturn(false); |
| 953 | assertFalse(mTSIBinder.isInManagedCall(DEFAULT_DIALER_PACKAGE)); |
| 954 | } |
| 955 | |
| 956 | @SmallTest |
Hall Liu | 74bb635 | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 957 | @Test |
Tyler Gunn | baf105b | 2017-04-11 15:21:03 -0700 | [diff] [blame] | 958 | public void testIsInManagedCallFail() throws Exception { |
| 959 | doThrow(new SecurityException()).when(mContext).enforceCallingOrSelfPermission( |
| 960 | anyString(), any()); |
| 961 | try { |
| 962 | mTSIBinder.isInManagedCall("blah"); |
| 963 | fail(); |
| 964 | } catch (SecurityException e) { |
| 965 | // desired result |
| 966 | } |
| 967 | verify(mFakeCallsManager, never()).hasOngoingCalls(); |
| 968 | } |
| 969 | |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 970 | /** |
| 971 | * Register phone accounts for the supplied PhoneAccountHandles to make them |
| 972 | * visible to all users (via the isVisibleToCaller method in TelecomServiceImpl. |
| 973 | * @param handles the handles for which phone accounts should be created for. |
| 974 | */ |
| 975 | private void makeAccountsVisibleToAllUsers(PhoneAccountHandle... handles) { |
| 976 | for (PhoneAccountHandle ph : handles) { |
| 977 | when(mFakePhoneAccountRegistrar.getPhoneAccountUnchecked(eq(ph))).thenReturn( |
| 978 | makeMultiUserPhoneAccount(ph).build()); |
| 979 | when(mFakePhoneAccountRegistrar |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 980 | .getPhoneAccount(eq(ph), nullable(UserHandle.class), anyBoolean())) |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 981 | .thenReturn(makeMultiUserPhoneAccount(ph).build()); |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 982 | when(mFakePhoneAccountRegistrar |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 983 | .getPhoneAccount(eq(ph), nullable(UserHandle.class))) |
Hall Liu | 0a6dd30 | 2015-12-16 15:06:49 -0800 | [diff] [blame] | 984 | .thenReturn(makeMultiUserPhoneAccount(ph).build()); |
Hall Liu | ecda554 | 2015-12-04 11:31:31 -0800 | [diff] [blame] | 985 | } |
| 986 | } |
| 987 | |
| 988 | private PhoneAccount.Builder makeMultiUserPhoneAccount(PhoneAccountHandle paHandle) { |
| 989 | PhoneAccount.Builder paBuilder = makePhoneAccount(paHandle); |
| 990 | paBuilder.setCapabilities(PhoneAccount.CAPABILITY_MULTI_USER); |
| 991 | return paBuilder; |
| 992 | } |
| 993 | |
| 994 | private PhoneAccount.Builder makePhoneAccount(PhoneAccountHandle paHandle) { |
| 995 | return new PhoneAccount.Builder(paHandle, "testLabel"); |
| 996 | } |
| 997 | |
| 998 | private Bundle createSampleExtras() { |
| 999 | Bundle extras = new Bundle(); |
| 1000 | extras.putString("test_key", "test_value"); |
| 1001 | return extras; |
| 1002 | } |
| 1003 | |
| 1004 | private static boolean areBundlesEqual(Bundle b1, Bundle b2) { |
| 1005 | for (String key1 : b1.keySet()) { |
| 1006 | if (!b1.get(key1).equals(b2.get(key1))) { |
| 1007 | return false; |
| 1008 | } |
| 1009 | } |
| 1010 | |
| 1011 | for (String key2 : b2.keySet()) { |
| 1012 | if (!b2.get(key2).equals(b1.get(key2))) { |
| 1013 | return false; |
| 1014 | } |
| 1015 | } |
| 1016 | return true; |
| 1017 | } |
| 1018 | } |