Hall Liu | 220b419 | 2015-12-11 11:33:08 -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 android.Manifest; |
| 20 | import android.app.Activity; |
| 21 | import android.app.AppOpsManager; |
| 22 | import android.content.BroadcastReceiver; |
| 23 | import android.content.ComponentName; |
| 24 | import android.content.Context; |
| 25 | import android.content.Intent; |
| 26 | import android.net.Uri; |
| 27 | import android.os.Bundle; |
| 28 | import android.os.Handler; |
| 29 | import android.os.UserHandle; |
| 30 | import android.telecom.GatewayInfo; |
| 31 | import android.telecom.TelecomManager; |
| 32 | import android.telecom.VideoProfile; |
| 33 | import android.telephony.DisconnectCause; |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 34 | import android.test.suitebuilder.annotation.SmallTest; |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 35 | |
| 36 | import com.android.server.telecom.Call; |
| 37 | import com.android.server.telecom.CallsManager; |
| 38 | import com.android.server.telecom.NewOutgoingCallIntentBroadcaster; |
| 39 | import com.android.server.telecom.PhoneNumberUtilsAdapter; |
| 40 | import com.android.server.telecom.PhoneNumberUtilsAdapterImpl; |
Hall Liu | c9cf544 | 2016-06-29 10:08:10 -0700 | [diff] [blame] | 41 | import com.android.server.telecom.TelecomSystem; |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 42 | |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 43 | import org.junit.Before; |
| 44 | import org.junit.Test; |
| 45 | import org.junit.runner.RunWith; |
| 46 | import org.junit.runners.JUnit4; |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 47 | import org.mockito.ArgumentCaptor; |
| 48 | import org.mockito.Mock; |
| 49 | |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 50 | import static org.junit.Assert.assertEquals; |
| 51 | import static org.junit.Assert.assertNull; |
| 52 | import static org.junit.Assert.assertTrue; |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 53 | import static org.mockito.ArgumentMatchers.nullable; |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 54 | import static org.mockito.Matchers.any; |
| 55 | import static org.mockito.Matchers.anyBoolean; |
| 56 | import static org.mockito.Matchers.anyInt; |
| 57 | import static org.mockito.Matchers.anyString; |
| 58 | import static org.mockito.Matchers.eq; |
| 59 | import static org.mockito.Matchers.isNotNull; |
| 60 | import static org.mockito.Matchers.isNull; |
| 61 | import static org.mockito.Mockito.doReturn; |
| 62 | import static org.mockito.Mockito.never; |
| 63 | import static org.mockito.Mockito.spy; |
| 64 | import static org.mockito.Mockito.verify; |
Hall Liu | 508b280 | 2016-02-17 17:31:43 -0800 | [diff] [blame] | 65 | import static org.mockito.Mockito.when; |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 66 | |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 67 | @RunWith(JUnit4.class) |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 68 | public class NewOutgoingCallIntentBroadcasterTest extends TelecomTestCase { |
| 69 | private static class ReceiverIntentPair { |
| 70 | public BroadcastReceiver receiver; |
| 71 | public Intent intent; |
| 72 | |
| 73 | public ReceiverIntentPair(BroadcastReceiver receiver, Intent intent) { |
| 74 | this.receiver = receiver; |
| 75 | this.intent = intent; |
| 76 | } |
| 77 | } |
| 78 | |
| 79 | @Mock private CallsManager mCallsManager; |
| 80 | @Mock private Call mCall; |
| 81 | |
| 82 | private PhoneNumberUtilsAdapter mPhoneNumberUtilsAdapterSpy; |
| 83 | |
| 84 | @Override |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 85 | @Before |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 86 | public void setUp() throws Exception { |
| 87 | super.setUp(); |
| 88 | mContext = mComponentContextFixture.getTestDouble().getApplicationContext(); |
| 89 | mPhoneNumberUtilsAdapterSpy = spy(new PhoneNumberUtilsAdapterImpl()); |
Hall Liu | 508b280 | 2016-02-17 17:31:43 -0800 | [diff] [blame] | 90 | when(mCall.getInitiatingUser()).thenReturn(UserHandle.CURRENT); |
Hall Liu | c9cf544 | 2016-06-29 10:08:10 -0700 | [diff] [blame] | 91 | when(mCallsManager.getLock()).thenReturn(new TelecomSystem.SyncRoot() { }); |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 92 | } |
| 93 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 94 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 95 | @Test |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 96 | public void testNullHandle() { |
| 97 | Intent intent = new Intent(Intent.ACTION_CALL, null); |
| 98 | int result = processIntent(intent, true); |
| 99 | assertEquals(DisconnectCause.INVALID_NUMBER, result); |
| 100 | verifyNoBroadcastSent(); |
| 101 | verifyNoCallPlaced(); |
| 102 | } |
| 103 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 104 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 105 | @Test |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 106 | public void testVoicemailCall() { |
| 107 | String voicemailNumber = "voicemail:18005551234"; |
| 108 | Intent intent = new Intent(Intent.ACTION_CALL, Uri.parse(voicemailNumber)); |
| 109 | intent.putExtra(TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, true); |
| 110 | |
| 111 | int result = processIntent(intent, true); |
| 112 | |
| 113 | assertEquals(DisconnectCause.NOT_DISCONNECTED, result); |
| 114 | verify(mCallsManager).placeOutgoingCall(eq(mCall), eq(Uri.parse(voicemailNumber)), |
Brad Ebinger | d0fe76e | 2017-03-20 13:17:15 -0700 | [diff] [blame] | 115 | nullable(GatewayInfo.class), eq(true), eq(VideoProfile.STATE_AUDIO_ONLY)); |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 116 | } |
| 117 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 118 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 119 | @Test |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 120 | public void testVoicemailCallWithBadAction() { |
| 121 | badCallActionHelper(Uri.parse("voicemail:18005551234"), DisconnectCause.OUTGOING_CANCELED); |
| 122 | } |
| 123 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 124 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 125 | @Test |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 126 | public void testTelCallWithBadCallAction() { |
| 127 | badCallActionHelper(Uri.parse("tel:6505551234"), DisconnectCause.INVALID_NUMBER); |
| 128 | } |
| 129 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 130 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 131 | @Test |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 132 | public void testSipCallWithBadCallAction() { |
| 133 | badCallActionHelper(Uri.parse("sip:testuser@testsite.com"), DisconnectCause.INVALID_NUMBER); |
| 134 | } |
| 135 | |
| 136 | private void badCallActionHelper(Uri handle, int expectedCode) { |
| 137 | Intent intent = new Intent(Intent.ACTION_ALARM_CHANGED, handle); |
| 138 | |
| 139 | int result = processIntent(intent, true); |
| 140 | |
| 141 | assertEquals(expectedCode, result); |
| 142 | verifyNoBroadcastSent(); |
| 143 | verifyNoCallPlaced(); |
| 144 | } |
| 145 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 146 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 147 | @Test |
Sanket Agarwal | e6143f5 | 2016-05-26 11:22:57 -0700 | [diff] [blame] | 148 | public void testAlreadyDisconnectedCall() { |
| 149 | Uri handle = Uri.parse("tel:6505551234"); |
| 150 | doReturn(true).when(mCall).isDisconnected(); |
| 151 | Intent callIntent = buildIntent(handle, Intent.ACTION_CALL, null); |
| 152 | ReceiverIntentPair result = regularCallTestHelper(callIntent, null); |
| 153 | |
| 154 | result.receiver.setResultData( |
| 155 | result.intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER)); |
| 156 | |
| 157 | result.receiver.onReceive(mContext, result.intent); |
| 158 | verifyNoCallPlaced(); |
| 159 | } |
| 160 | |
| 161 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 162 | @Test |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 163 | public void testNoNumberSupplied() { |
| 164 | Uri handle = Uri.parse("tel:"); |
| 165 | Intent intent = new Intent(Intent.ACTION_CALL, handle); |
| 166 | |
| 167 | int result = processIntent(intent, true); |
| 168 | |
| 169 | assertEquals(DisconnectCause.NO_PHONE_NUMBER_SUPPLIED, result); |
| 170 | verifyNoBroadcastSent(); |
| 171 | verifyNoCallPlaced(); |
| 172 | } |
| 173 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 174 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 175 | @Test |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 176 | public void testEmergencyCallWithNonDefaultDialer() { |
| 177 | Uri handle = Uri.parse("tel:6505551911"); |
| 178 | doReturn(true).when(mPhoneNumberUtilsAdapterSpy).isPotentialLocalEmergencyNumber( |
| 179 | any(Context.class), eq(handle.getSchemeSpecificPart())); |
| 180 | Intent intent = new Intent(Intent.ACTION_CALL, handle); |
| 181 | |
| 182 | String ui_package_string = "sample_string_1"; |
| 183 | String dialer_default_class_string = "sample_string_2"; |
| 184 | mComponentContextFixture.putResource(R.string.ui_default_package, ui_package_string); |
| 185 | mComponentContextFixture.putResource(R.string.dialer_default_class, |
| 186 | dialer_default_class_string); |
| 187 | |
| 188 | int result = processIntent(intent, false); |
| 189 | |
| 190 | assertEquals(DisconnectCause.OUTGOING_CANCELED, result); |
| 191 | verifyNoBroadcastSent(); |
| 192 | verifyNoCallPlaced(); |
| 193 | |
| 194 | ArgumentCaptor<Intent> dialerIntentCaptor = ArgumentCaptor.forClass(Intent.class); |
| 195 | verify(mContext).startActivityAsUser(dialerIntentCaptor.capture(), any(UserHandle.class)); |
| 196 | Intent dialerIntent = dialerIntentCaptor.getValue(); |
| 197 | assertEquals(new ComponentName(ui_package_string, dialer_default_class_string), |
| 198 | dialerIntent.getComponent()); |
| 199 | assertEquals(Intent.ACTION_DIAL, dialerIntent.getAction()); |
| 200 | assertEquals(handle, dialerIntent.getData()); |
| 201 | assertEquals(Intent.FLAG_ACTIVITY_NEW_TASK, dialerIntent.getFlags()); |
| 202 | } |
| 203 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 204 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 205 | @Test |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 206 | public void testActionCallEmergencyCall() { |
| 207 | Uri handle = Uri.parse("tel:6505551911"); |
| 208 | Intent intent = buildIntent(handle, Intent.ACTION_CALL, null); |
| 209 | emergencyCallTestHelper(intent, null); |
| 210 | } |
| 211 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 212 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 213 | @Test |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 214 | public void testActionEmergencyWithEmergencyNumber() { |
| 215 | Uri handle = Uri.parse("tel:6505551911"); |
| 216 | Intent intent = buildIntent(handle, Intent.ACTION_CALL_EMERGENCY, null); |
| 217 | emergencyCallTestHelper(intent, null); |
| 218 | } |
| 219 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 220 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 221 | @Test |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 222 | public void testActionPrivCallWithEmergencyNumber() { |
| 223 | Uri handle = Uri.parse("tel:6505551911"); |
| 224 | Intent intent = buildIntent(handle, Intent.ACTION_CALL_PRIVILEGED, null); |
| 225 | emergencyCallTestHelper(intent, null); |
| 226 | } |
| 227 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 228 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 229 | @Test |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 230 | public void testEmergencyCallWithGatewayExtras() { |
| 231 | Uri handle = Uri.parse("tel:6505551911"); |
| 232 | Bundle gatewayExtras = new Bundle(); |
| 233 | gatewayExtras.putString(NewOutgoingCallIntentBroadcaster.EXTRA_GATEWAY_PROVIDER_PACKAGE, |
| 234 | "sample1"); |
| 235 | gatewayExtras.putString(NewOutgoingCallIntentBroadcaster.EXTRA_GATEWAY_URI, "sample2"); |
| 236 | |
| 237 | Intent intent = buildIntent(handle, Intent.ACTION_CALL, gatewayExtras); |
| 238 | emergencyCallTestHelper(intent, gatewayExtras); |
| 239 | } |
| 240 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 241 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 242 | @Test |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 243 | public void testActionEmergencyWithNonEmergencyNumber() { |
| 244 | Uri handle = Uri.parse("tel:6505551911"); |
| 245 | doReturn(false).when(mPhoneNumberUtilsAdapterSpy).isPotentialLocalEmergencyNumber( |
| 246 | any(Context.class), eq(handle.getSchemeSpecificPart())); |
| 247 | Intent intent = new Intent(Intent.ACTION_CALL_EMERGENCY, handle); |
| 248 | int result = processIntent(intent, true); |
| 249 | |
| 250 | assertEquals(DisconnectCause.OUTGOING_CANCELED, result); |
| 251 | verifyNoCallPlaced(); |
| 252 | verifyNoBroadcastSent(); |
| 253 | } |
| 254 | |
| 255 | private void emergencyCallTestHelper(Intent intent, Bundle expectedAdditionalExtras) { |
| 256 | Uri handle = intent.getData(); |
| 257 | int videoState = VideoProfile.STATE_BIDIRECTIONAL; |
| 258 | boolean isSpeakerphoneOn = true; |
| 259 | doReturn(true).when(mPhoneNumberUtilsAdapterSpy).isPotentialLocalEmergencyNumber( |
| 260 | any(Context.class), eq(handle.getSchemeSpecificPart())); |
| 261 | intent.putExtra(TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, isSpeakerphoneOn); |
| 262 | intent.putExtra(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE, videoState); |
| 263 | int result = processIntent(intent, true); |
| 264 | |
| 265 | assertEquals(DisconnectCause.NOT_DISCONNECTED, result); |
| 266 | verify(mCallsManager).placeOutgoingCall(eq(mCall), eq(handle), isNull(GatewayInfo.class), |
| 267 | eq(isSpeakerphoneOn), eq(videoState)); |
| 268 | |
| 269 | Bundle expectedExtras = createNumberExtras(handle.getSchemeSpecificPart()); |
| 270 | if (expectedAdditionalExtras != null) { |
| 271 | expectedExtras.putAll(expectedAdditionalExtras); |
| 272 | } |
| 273 | BroadcastReceiver receiver = verifyBroadcastSent(handle.getSchemeSpecificPart(), |
| 274 | expectedExtras).receiver; |
| 275 | assertNull(receiver); |
| 276 | } |
| 277 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 278 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 279 | @Test |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 280 | public void testUnmodifiedRegularCall() { |
| 281 | Uri handle = Uri.parse("tel:6505551234"); |
| 282 | Intent callIntent = buildIntent(handle, Intent.ACTION_CALL, null); |
| 283 | ReceiverIntentPair result = regularCallTestHelper(callIntent, null); |
| 284 | |
| 285 | result.receiver.setResultData( |
| 286 | result.intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER)); |
| 287 | |
| 288 | result.receiver.onReceive(mContext, result.intent); |
| 289 | |
| 290 | verify(mCallsManager).placeOutgoingCall(eq(mCall), eq(handle), isNull(GatewayInfo.class), |
| 291 | eq(true), eq(VideoProfile.STATE_BIDIRECTIONAL)); |
| 292 | } |
| 293 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 294 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 295 | @Test |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 296 | public void testUnmodifiedSipCall() { |
| 297 | Uri handle = Uri.parse("sip:test@test.com"); |
| 298 | Intent callIntent = buildIntent(handle, Intent.ACTION_CALL, null); |
| 299 | ReceiverIntentPair result = regularCallTestHelper(callIntent, null); |
| 300 | |
| 301 | result.receiver.setResultData( |
| 302 | result.intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER)); |
| 303 | |
| 304 | result.receiver.onReceive(mContext, result.intent); |
| 305 | |
| 306 | Uri encHandle = Uri.fromParts(handle.getScheme(), |
| 307 | handle.getSchemeSpecificPart(), null); |
| 308 | verify(mCallsManager).placeOutgoingCall(eq(mCall), eq(encHandle), isNull(GatewayInfo.class), |
| 309 | eq(true), eq(VideoProfile.STATE_BIDIRECTIONAL)); |
| 310 | } |
| 311 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 312 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 313 | @Test |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 314 | public void testCallWithGatewayInfo() { |
| 315 | Uri handle = Uri.parse("tel:6505551234"); |
| 316 | Intent callIntent = buildIntent(handle, Intent.ACTION_CALL, null); |
| 317 | |
| 318 | callIntent.putExtra(NewOutgoingCallIntentBroadcaster |
| 319 | .EXTRA_GATEWAY_PROVIDER_PACKAGE, "sample1"); |
| 320 | callIntent.putExtra(NewOutgoingCallIntentBroadcaster.EXTRA_GATEWAY_URI, "sample2"); |
| 321 | ReceiverIntentPair result = regularCallTestHelper(callIntent, callIntent.getExtras()); |
| 322 | |
| 323 | result.receiver.setResultData( |
| 324 | result.intent.getStringExtra(Intent.EXTRA_PHONE_NUMBER)); |
| 325 | |
| 326 | result.receiver.onReceive(mContext, result.intent); |
| 327 | |
| 328 | verify(mCallsManager).placeOutgoingCall(eq(mCall), eq(handle), |
| 329 | isNotNull(GatewayInfo.class), eq(true), eq(VideoProfile.STATE_BIDIRECTIONAL)); |
| 330 | } |
| 331 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 332 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 333 | @Test |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 334 | public void testCallNumberModifiedToNull() { |
| 335 | Uri handle = Uri.parse("tel:6505551234"); |
| 336 | Intent callIntent = buildIntent(handle, Intent.ACTION_CALL, null); |
| 337 | ReceiverIntentPair result = regularCallTestHelper(callIntent, null); |
| 338 | |
| 339 | result.receiver.setResultData(null); |
| 340 | |
| 341 | result.receiver.onReceive(mContext, result.intent); |
| 342 | verifyNoCallPlaced(); |
Hall Liu | 63e690c | 2017-02-14 18:05:03 -0800 | [diff] [blame] | 343 | ArgumentCaptor<Long> timeoutCaptor = ArgumentCaptor.forClass(Long.class); |
| 344 | verify(mCall).disconnect(timeoutCaptor.capture()); |
| 345 | assertTrue(timeoutCaptor.getValue() > 0); |
| 346 | } |
| 347 | |
| 348 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 349 | @Test |
Hall Liu | 63e690c | 2017-02-14 18:05:03 -0800 | [diff] [blame] | 350 | public void testCallNumberModifiedToNullWithLongCustomTimeout() { |
| 351 | Uri handle = Uri.parse("tel:6505551234"); |
| 352 | Intent callIntent = buildIntent(handle, Intent.ACTION_CALL, null); |
| 353 | ReceiverIntentPair result = regularCallTestHelper(callIntent, null); |
| 354 | |
| 355 | long customTimeout = 100000000; |
| 356 | Bundle bundle = new Bundle(); |
| 357 | bundle.putLong(TelecomManager.EXTRA_NEW_OUTGOING_CALL_CANCEL_TIMEOUT, customTimeout); |
| 358 | result.receiver.setResultData(null); |
| 359 | result.receiver.setResultExtras(bundle); |
| 360 | |
| 361 | result.receiver.onReceive(mContext, result.intent); |
| 362 | verifyNoCallPlaced(); |
| 363 | ArgumentCaptor<Long> timeoutCaptor = ArgumentCaptor.forClass(Long.class); |
| 364 | verify(mCall).disconnect(timeoutCaptor.capture()); |
| 365 | assertTrue(timeoutCaptor.getValue() < customTimeout); |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 366 | } |
| 367 | |
Hall Liu | 84b6c87 | 2016-02-03 12:19:00 -0800 | [diff] [blame] | 368 | @SmallTest |
Hall Liu | c8a396b | 2017-12-27 18:23:28 -0800 | [diff] [blame] | 369 | @Test |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 370 | public void testCallModifiedToEmergency() { |
| 371 | Uri handle = Uri.parse("tel:6505551234"); |
| 372 | Intent callIntent = buildIntent(handle, Intent.ACTION_CALL, null); |
| 373 | ReceiverIntentPair result = regularCallTestHelper(callIntent, null); |
| 374 | |
| 375 | String newEmergencyNumber = "1234567890"; |
| 376 | result.receiver.setResultData(newEmergencyNumber); |
| 377 | |
| 378 | doReturn(true).when(mPhoneNumberUtilsAdapterSpy).isPotentialLocalEmergencyNumber( |
| 379 | any(Context.class), eq(newEmergencyNumber)); |
| 380 | result.receiver.onReceive(mContext, result.intent); |
Hall Liu | 63e690c | 2017-02-14 18:05:03 -0800 | [diff] [blame] | 381 | verify(mCall).disconnect(eq(0L)); |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 382 | } |
| 383 | |
| 384 | private ReceiverIntentPair regularCallTestHelper(Intent intent, |
| 385 | Bundle expectedAdditionalExtras) { |
| 386 | Uri handle = intent.getData(); |
| 387 | int videoState = VideoProfile.STATE_BIDIRECTIONAL; |
| 388 | boolean isSpeakerphoneOn = true; |
| 389 | intent.putExtra(TelecomManager.EXTRA_START_CALL_WITH_SPEAKERPHONE, isSpeakerphoneOn); |
| 390 | intent.putExtra(TelecomManager.EXTRA_START_CALL_WITH_VIDEO_STATE, videoState); |
| 391 | |
| 392 | int result = processIntent(intent, true); |
| 393 | |
| 394 | assertEquals(DisconnectCause.NOT_DISCONNECTED, result); |
| 395 | Bundle expectedExtras = createNumberExtras(handle.getSchemeSpecificPart()); |
| 396 | if (expectedAdditionalExtras != null) { |
| 397 | expectedExtras.putAll(expectedAdditionalExtras); |
| 398 | } |
| 399 | return verifyBroadcastSent(handle.getSchemeSpecificPart(), expectedExtras); |
| 400 | } |
| 401 | |
| 402 | private Intent buildIntent(Uri handle, String action, Bundle extras) { |
| 403 | Intent i = new Intent(action, handle); |
| 404 | if (extras != null) { |
| 405 | i.putExtras(extras); |
| 406 | } |
| 407 | return i; |
| 408 | } |
| 409 | |
| 410 | private int processIntent(Intent intent, |
| 411 | boolean isDefaultPhoneApp) { |
| 412 | NewOutgoingCallIntentBroadcaster b = new NewOutgoingCallIntentBroadcaster( |
| 413 | mContext, mCallsManager, mCall, intent, mPhoneNumberUtilsAdapterSpy, |
| 414 | isDefaultPhoneApp); |
| 415 | return b.processIntent(); |
| 416 | } |
| 417 | |
| 418 | private ReceiverIntentPair verifyBroadcastSent(String number, Bundle expectedExtras) { |
| 419 | ArgumentCaptor<Intent> intentCaptor = ArgumentCaptor.forClass(Intent.class); |
| 420 | ArgumentCaptor<BroadcastReceiver> receiverCaptor = |
| 421 | ArgumentCaptor.forClass(BroadcastReceiver.class); |
| 422 | |
| 423 | verify(mContext).sendOrderedBroadcastAsUser( |
| 424 | intentCaptor.capture(), |
| 425 | eq(UserHandle.CURRENT), |
| 426 | eq(Manifest.permission.PROCESS_OUTGOING_CALLS), |
| 427 | eq(AppOpsManager.OP_PROCESS_OUTGOING_CALLS), |
| 428 | receiverCaptor.capture(), |
| 429 | isNull(Handler.class), |
| 430 | eq(Activity.RESULT_OK), |
| 431 | eq(number), |
| 432 | isNull(Bundle.class)); |
| 433 | |
| 434 | Intent capturedIntent = intentCaptor.getValue(); |
| 435 | assertEquals(Intent.ACTION_NEW_OUTGOING_CALL, capturedIntent.getAction()); |
Brad Ebinger | 68d17c6 | 2017-04-11 17:14:11 -0700 | [diff] [blame] | 436 | assertEquals(Intent.FLAG_RECEIVER_FOREGROUND | Intent.FLAG_RECEIVER_INCLUDE_BACKGROUND, |
| 437 | capturedIntent.getFlags()); |
Hall Liu | 220b419 | 2015-12-11 11:33:08 -0800 | [diff] [blame] | 438 | assertTrue(areBundlesEqual(expectedExtras, capturedIntent.getExtras())); |
| 439 | |
| 440 | BroadcastReceiver receiver = receiverCaptor.getValue(); |
| 441 | if (receiver != null) { |
| 442 | receiver.setPendingResult( |
| 443 | new BroadcastReceiver.PendingResult(0, "", null, 0, true, false, null, 0, 0)); |
| 444 | } |
| 445 | |
| 446 | return new ReceiverIntentPair(receiver, capturedIntent); |
| 447 | } |
| 448 | |
| 449 | private Bundle createNumberExtras(String number) { |
| 450 | Bundle b = new Bundle(); |
| 451 | b.putString(Intent.EXTRA_PHONE_NUMBER, number); |
| 452 | return b; |
| 453 | } |
| 454 | |
| 455 | private void verifyNoCallPlaced() { |
| 456 | verify(mCallsManager, never()).placeOutgoingCall(any(Call.class), any(Uri.class), |
| 457 | any(GatewayInfo.class), anyBoolean(), anyInt()); |
| 458 | } |
| 459 | |
| 460 | private void verifyNoBroadcastSent() { |
| 461 | verify(mContext, never()).sendOrderedBroadcastAsUser( |
| 462 | any(Intent.class), |
| 463 | any(UserHandle.class), |
| 464 | anyString(), |
| 465 | anyInt(), |
| 466 | any(BroadcastReceiver.class), |
| 467 | any(Handler.class), |
| 468 | anyInt(), |
| 469 | anyString(), |
| 470 | any(Bundle.class)); |
| 471 | } |
| 472 | |
| 473 | private static boolean areBundlesEqual(Bundle b1, Bundle b2) { |
| 474 | for (String key1 : b1.keySet()) { |
| 475 | if (!b1.get(key1).equals(b2.get(key1))) { |
| 476 | return false; |
| 477 | } |
| 478 | } |
| 479 | |
| 480 | for (String key2 : b2.keySet()) { |
| 481 | if (!b2.get(key2).equals(b1.get(key2))) { |
| 482 | return false; |
| 483 | } |
| 484 | } |
| 485 | return true; |
| 486 | } |
| 487 | } |