summaryrefslogtreecommitdiff
path: root/tests/src/com/android/server/telecom/tests/CreateConnectionProcessorTest.java
blob: 8a85a87862eb0c92771d6499a2e504e4db20ce28 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
/*
 * Copyright (C) 2015 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License
 */

package com.android.server.telecom.tests;

import android.Manifest;
import android.content.ComponentName;
import android.content.Context;
import android.content.pm.PackageManager;
import android.graphics.drawable.Icon;
import android.net.Uri;
import android.os.Binder;
import android.os.UserHandle;
import android.telecom.DisconnectCause;
import android.telecom.PhoneAccount;
import android.telecom.PhoneAccountHandle;
import android.telephony.SubscriptionManager;
import android.test.suitebuilder.annotation.SmallTest;

import com.android.server.telecom.Call;
import com.android.server.telecom.CallIdMapper;
import com.android.server.telecom.ConnectionServiceFocusManager;
import com.android.server.telecom.ConnectionServiceRepository;
import com.android.server.telecom.ConnectionServiceWrapper;
import com.android.server.telecom.CreateConnectionProcessor;
import com.android.server.telecom.CreateConnectionResponse;
import com.android.server.telecom.PhoneAccountRegistrar;

import org.junit.After;
import org.junit.Before;
import org.junit.Test;
import org.junit.runner.RunWith;
import org.junit.runners.JUnit4;
import org.mockito.ArgumentCaptor;
import org.mockito.Mock;
import org.mockito.MockitoAnnotations;
import org.mockito.invocation.InvocationOnMock;
import org.mockito.stubbing.Answer;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Random;
import java.util.UUID;

import static org.junit.Assert.fail;
import static org.mockito.ArgumentMatchers.anyBoolean;
import static org.mockito.ArgumentMatchers.anyInt;
import static org.mockito.ArgumentMatchers.anyList;
import static org.mockito.ArgumentMatchers.nullable;
import static org.mockito.Matchers.any;
import static org.mockito.Matchers.eq;
import static org.mockito.Mockito.doAnswer;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.never;
import static org.mockito.Mockito.reset;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;

/**
 * Unit testing for CreateConnectionProcessor as well as CreateConnectionTimeout classes.
 */
@RunWith(JUnit4.class)
public class CreateConnectionProcessorTest extends TelecomTestCase {

    private static final String TEST_PACKAGE = "com.android.server.telecom.tests";
    private static final String TEST_CLASS =
            "com.android.server.telecom.tests.MockConnectionService";
    private static final UserHandle USER_HANDLE_10 = new UserHandle(10);

    @Mock
    ConnectionServiceRepository mMockConnectionServiceRepository;
    @Mock
    PhoneAccountRegistrar mMockAccountRegistrar;
    @Mock
    CreateConnectionResponse mMockCreateConnectionResponse;
    @Mock
    Call mMockCall;
    @Mock
    ConnectionServiceFocusManager mConnectionServiceFocusManager;

    CreateConnectionProcessor mTestCreateConnectionProcessor;

    private ArrayList<PhoneAccount> phoneAccounts;
    private HashMap<Integer,Integer> mSubToSlot;
    private HashMap<PhoneAccount,Integer> mAccountToSub;

    @Override
    @Before
    public void setUp() throws Exception {
        super.setUp();
        MockitoAnnotations.initMocks(this);
        mContext = mComponentContextFixture.getTestDouble().getApplicationContext();

        when(mMockCall.getConnectionServiceFocusManager()).thenReturn(
                mConnectionServiceFocusManager);
        doAnswer(new Answer<Void>() {
                     @Override
                     public Void answer(InvocationOnMock invocation) {
                         Object[] args = invocation.getArguments();
                         ConnectionServiceFocusManager.CallFocus focus =
                                 (ConnectionServiceFocusManager.CallFocus) args[0];
                         ConnectionServiceFocusManager.RequestFocusCallback callback =
                                 (ConnectionServiceFocusManager.RequestFocusCallback) args[1];
                         callback.onRequestFocusDone(focus);
                         return null;
                     }
                 }
                ).when(mConnectionServiceFocusManager).requestFocus(any(), any());

        mTestCreateConnectionProcessor = new CreateConnectionProcessor(mMockCall,
                mMockConnectionServiceRepository, mMockCreateConnectionResponse,
                mMockAccountRegistrar, mContext);

        mAccountToSub = new HashMap<>();
        phoneAccounts = new ArrayList<>();
        mSubToSlot = new HashMap<>();
        mTestCreateConnectionProcessor.setTelephonyManagerAdapter(
                new CreateConnectionProcessor.ITelephonyManagerAdapter() {
                    @Override
                    public int getSubIdForPhoneAccount(Context context, PhoneAccount account) {
                        return mAccountToSub.getOrDefault(account,
                                SubscriptionManager.INVALID_SIM_SLOT_INDEX);
                    }

                    @Override
                    public int getSlotIndex(int subId) {
                        return mSubToSlot.getOrDefault(subId,
                                SubscriptionManager.INVALID_SIM_SLOT_INDEX);
                    }
                });
        when(mMockAccountRegistrar.getAllPhoneAccounts(any(UserHandle.class), anyBoolean()))
                .thenReturn(phoneAccounts);
        when(mMockCall.getAssociatedUser()).
                thenReturn(Binder.getCallingUserHandle());
    }

    @Override
    @After
    public void tearDown() throws Exception {
        mTestCreateConnectionProcessor = null;
        super.tearDown();
    }

    @SmallTest
    @Test
    public void testSimPhoneAccountSuccess() throws Exception {
        PhoneAccountHandle pAHandle = getNewTargetPhoneAccountHandle("tel_acct");
        setTargetPhoneAccount(mMockCall, pAHandle);
        when(mMockCall.isEmergencyCall()).thenReturn(false);
        // No Connection Manager in this case
        when(mMockAccountRegistrar.getSimCallManagerFromCall(any(Call.class))).thenReturn(null);
        ConnectionServiceWrapper service = makeConnectionServiceWrapper();

        mTestCreateConnectionProcessor.process();

        verify(mMockCall).setConnectionManagerPhoneAccount(eq(pAHandle));
        verify(mMockCall).setTargetPhoneAccount(eq(pAHandle));
        verify(mMockCall).setConnectionService(eq(service));
        verify(service).createConnection(eq(mMockCall), any(CreateConnectionResponse.class));
        // Notify successful connection to call
        CallIdMapper mockCallIdMapper = mock(CallIdMapper.class);
        mTestCreateConnectionProcessor.handleCreateConnectionSuccess(mockCallIdMapper, null);
        verify(mMockCreateConnectionResponse).handleCreateConnectionSuccess(mockCallIdMapper, null);
    }

    @SmallTest
    @Test
    public void testbadPhoneAccount() throws Exception {
        PhoneAccountHandle pAHandle = null;
        when(mMockCall.isEmergencyCall()).thenReturn(false);
        when(mMockCall.getTargetPhoneAccount()).thenReturn(pAHandle);
        givePhoneAccountBindPermission(pAHandle);
        // No Connection Manager in this case
        when(mMockAccountRegistrar.getSimCallManagerFromCall(any(Call.class))).thenReturn(null);
        ConnectionServiceWrapper service = makeConnectionServiceWrapper();

        mTestCreateConnectionProcessor.process();

        verify(service, never()).createConnection(eq(mMockCall),
                any(CreateConnectionResponse.class));
        verify(mMockCreateConnectionResponse).handleCreateConnectionFailure(
                eq(new DisconnectCause(DisconnectCause.ERROR)));
    }

    @SmallTest
    @Test
    public void testConnectionManagerSuccess() throws Exception {
        PhoneAccountHandle pAHandle = getNewTargetPhoneAccountHandle("tel_acct");
        setTargetPhoneAccount(mMockCall, pAHandle);
        when(mMockCall.isEmergencyCall()).thenReturn(false);
        // Include a Connection Manager
        PhoneAccountHandle callManagerPAHandle = getNewConnectionMangerHandleForCall(mMockCall,
                "cm_acct");
        ConnectionServiceWrapper service = makeConnectionServiceWrapper();
        // Make sure the target phone account has the correct permissions
        PhoneAccount mFakeTargetPhoneAccount = makeQuickAccount("cm_acct",
                PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION, null);
        when(mMockAccountRegistrar.getPhoneAccountUnchecked(pAHandle)).thenReturn(
                mFakeTargetPhoneAccount);

        mTestCreateConnectionProcessor.process();

        verify(mMockCall).setConnectionManagerPhoneAccount(eq(callManagerPAHandle));
        verify(mMockCall).setTargetPhoneAccount(eq(pAHandle));
        verify(mMockCall).setConnectionService(eq(service));
        verify(service).createConnection(eq(mMockCall),
                any(CreateConnectionResponse.class));
        // Notify successful connection to call
        CallIdMapper mockCallIdMapper = mock(CallIdMapper.class);
        mTestCreateConnectionProcessor.handleCreateConnectionSuccess(mockCallIdMapper, null);
        verify(mMockCreateConnectionResponse).handleCreateConnectionSuccess(mockCallIdMapper, null);
    }

    @SmallTest
    @Test
    public void testConnectionManagerFailedFallToSim() throws Exception {
        PhoneAccountHandle pAHandle = getNewTargetPhoneAccountHandle("tel_acct");
        setTargetPhoneAccount(mMockCall, pAHandle);
        when(mMockCall.isEmergencyCall()).thenReturn(false);
        // Include a Connection Manager
        PhoneAccountHandle callManagerPAHandle = getNewConnectionMangerHandleForCall(mMockCall,
                "cm_acct");
        ConnectionServiceWrapper service = makeConnectionServiceWrapper();
        when(mMockCall.getConnectionManagerPhoneAccount()).thenReturn(callManagerPAHandle);
        PhoneAccount mFakeTargetPhoneAccount = makeQuickAccount("cm_acct",
                PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION, null);
        when(mMockAccountRegistrar.getPhoneAccountUnchecked(pAHandle)).thenReturn(
                mFakeTargetPhoneAccount);
        when(mMockCall.getConnectionService()).thenReturn(service);
        // Put CreateConnectionProcessor in correct state to fail with ConnectionManager
        mTestCreateConnectionProcessor.process();
        reset(mMockCall);
        reset(service);

        when(mMockCall.getConnectionServiceFocusManager()).thenReturn(
                mConnectionServiceFocusManager);
        // Notify that the ConnectionManager has denied the call.
        when(mMockCall.getConnectionManagerPhoneAccount()).thenReturn(callManagerPAHandle);
        when(mMockCall.getConnectionService()).thenReturn(service);
        mTestCreateConnectionProcessor.handleCreateConnectionFailure(
                new DisconnectCause(DisconnectCause.CONNECTION_MANAGER_NOT_SUPPORTED));

        // Verify that the Sim Phone Account is used correctly
        verify(mMockCall).setConnectionManagerPhoneAccount(eq(pAHandle));
        verify(mMockCall).setTargetPhoneAccount(eq(pAHandle));
        verify(mMockCall).setConnectionService(eq(service));
        verify(service).createConnection(eq(mMockCall), any(CreateConnectionResponse.class));
        // Notify successful connection to call
        CallIdMapper mockCallIdMapper = mock(CallIdMapper.class);
        mTestCreateConnectionProcessor.handleCreateConnectionSuccess(mockCallIdMapper, null);
        verify(mMockCreateConnectionResponse).handleCreateConnectionSuccess(mockCallIdMapper, null);
    }

    @SmallTest
    @Test
    public void testConnectionManagerFailedDoNotFallToSim() throws Exception {
        PhoneAccountHandle pAHandle = getNewTargetPhoneAccountHandle("tel_acct");
        setTargetPhoneAccount(mMockCall, pAHandle);
        when(mMockCall.isEmergencyCall()).thenReturn(false);
        // Include a Connection Manager
        PhoneAccountHandle callManagerPAHandle = getNewConnectionMangerHandleForCall(mMockCall,
                "cm_acct");
        ConnectionServiceWrapper service = makeConnectionServiceWrapper();
        when(mMockCall.getConnectionManagerPhoneAccount()).thenReturn(callManagerPAHandle);
        PhoneAccount mFakeTargetPhoneAccount = makeQuickAccount("cm_acct",
                PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION, null);
        when(mMockAccountRegistrar.getPhoneAccountUnchecked(pAHandle)).thenReturn(
                mFakeTargetPhoneAccount);
        when(mMockCall.getConnectionService()).thenReturn(service);
        // Put CreateConnectionProcessor in correct state to fail with ConnectionManager
        mTestCreateConnectionProcessor.process();
        reset(mMockCall);
        reset(service);

        when(mMockCall.getConnectionServiceFocusManager()).thenReturn(
                mConnectionServiceFocusManager);
        // Notify that the ConnectionManager has rejected the call.
        when(mMockCall.getConnectionManagerPhoneAccount()).thenReturn(callManagerPAHandle);
        when(mMockCall.getConnectionService()).thenReturn(service);
        when(service.isServiceValid("createConnection")).thenReturn(true);
        mTestCreateConnectionProcessor.handleCreateConnectionFailure(
                new DisconnectCause(DisconnectCause.OTHER));

        // Verify call connection rejected
        verify(mMockCreateConnectionResponse).handleCreateConnectionFailure(
                new DisconnectCause(DisconnectCause.OTHER));
    }

    /**
     * Ensure that when a test emergency number is being dialed and we restrict the usable
     * PhoneAccounts using {@link PhoneAccountRegistrar#filterRestrictedPhoneAccounts(List)}, the
     * test emergency call is sent on the filtered PhoneAccount.
     */
    @SmallTest
    @Test
    public void testFakeEmergencyNumber() throws Exception {
        when(mMockCall.isEmergencyCall()).thenReturn(true);
        when(mMockCall.isTestEmergencyCall()).thenReturn(true);
        // Put in a regular phone account as the target and make sure it calls that.
        PhoneAccount regularAccount = makeEmergencyTestPhoneAccount("tel_acct1", 0);
        PhoneAccountHandle regularAccountHandle = regularAccount.getAccountHandle();
        List<PhoneAccount> filteredList = new ArrayList<>();
        filteredList.add(regularAccount);
        when(mMockAccountRegistrar.filterRestrictedPhoneAccounts(anyList()))
                .thenReturn(filteredList);
        mapToSubSlot(regularAccount, 1 /*subId*/, 0 /*slotId*/);
        setTargetPhoneAccount(mMockCall, regularAccount.getAccountHandle());
        phoneAccounts.add(regularAccount);
        // Do not use this account, even though it is a SIM subscription and can place emergency
        // calls
        ConnectionServiceWrapper service = makeConnectionServiceWrapper();
        PhoneAccount emergencyPhoneAccount = makeEmergencyPhoneAccount("tel_emer", 0, null);
        mapToSubSlot(emergencyPhoneAccount, 2 /*subId*/, 1 /*slotId*/);
        phoneAccounts.add(emergencyPhoneAccount);

        mTestCreateConnectionProcessor.process();

        verify(mMockCall).setConnectionManagerPhoneAccount(eq(regularAccountHandle));
        verify(mMockCall).setTargetPhoneAccount(eq(regularAccountHandle));
        verify(mMockCall).setConnectionService(eq(service));
        verify(service).createConnection(eq(mMockCall), any(CreateConnectionResponse.class));
        // Notify successful connection to call
        CallIdMapper mockCallIdMapper = mock(CallIdMapper.class);
        mTestCreateConnectionProcessor.handleCreateConnectionSuccess(mockCallIdMapper, null);
        verify(mMockCreateConnectionResponse).handleCreateConnectionSuccess(mockCallIdMapper, null);
    }

    /**
     * Ensure that when no phone accounts (visible to the user) are available for the call, we use
     * an available sim from other another user (on the condition that the user has the
     * INTERACT_ACROSS_USERS permission).
     */
    @SmallTest
    @Test
    public void testEmergencyCallAcrossUsers() throws Exception {
        when(mMockCall.isEmergencyCall()).thenReturn(true);
        when(mMockCall.isTestEmergencyCall()).thenReturn(false);
        ConnectionServiceWrapper service = makeConnectionServiceWrapper();
        // Add an emergency account associated with a different user and expect this to be called.
        PhoneAccount emergencyPhoneAccount = makeEmergencyPhoneAccount("tel_emer",
                0, USER_HANDLE_10);
        mapToSubSlot(emergencyPhoneAccount, 1 /*subId*/, 0 /*slotId*/);
        phoneAccounts.add(emergencyPhoneAccount);
        PhoneAccountHandle emergencyPhoneAccountHandle = emergencyPhoneAccount.getAccountHandle();

        mTestCreateConnectionProcessor.process();

        verify(mMockCall).setConnectionManagerPhoneAccount(eq(emergencyPhoneAccountHandle));
        verify(mMockCall).setTargetPhoneAccount(eq(emergencyPhoneAccountHandle));
        verify(mMockCall).setConnectionService(eq(service));
        verify(service).createConnection(eq(mMockCall), any(CreateConnectionResponse.class));
        // Notify successful connection to call
        CallIdMapper mockCallIdMapper = mock(CallIdMapper.class);
        mTestCreateConnectionProcessor.handleCreateConnectionSuccess(mockCallIdMapper, null);
        verify(mMockCreateConnectionResponse).handleCreateConnectionSuccess(mockCallIdMapper, null);
    }

    /**
     * Ensure that the non-emergency capable PhoneAccount and the SIM manager is not chosen to place
     * the emergency call if there is an emergency capable PhoneAccount available as well.
     */
    @SmallTest
    @Test
    public void testEmergencyCall() throws Exception {
        when(mMockCall.isEmergencyCall()).thenReturn(true);
        when(mMockCall.isTestEmergencyCall()).thenReturn(false);
        // Put in a regular phone account as the target to be sure it doesn't call that
        PhoneAccount regularAccount = makePhoneAccount("tel_acct1",
                PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION);
        mapToSubSlot(regularAccount, 1 /*subId*/, 0 /*slotId*/);
        setTargetPhoneAccount(mMockCall, regularAccount.getAccountHandle());
        phoneAccounts.add(regularAccount);
        // Include a Connection Manager to be sure it doesn't call that
        PhoneAccount callManagerPA = createNewConnectionManagerPhoneAccountForCall(mMockCall,
                "cm_acct", 0);
        phoneAccounts.add(callManagerPA);
        ConnectionServiceWrapper service = makeConnectionServiceWrapper();
        PhoneAccount emergencyPhoneAccount = makeEmergencyPhoneAccount("tel_emer", 0, null);
        mapToSubSlot(emergencyPhoneAccount, 2 /*subId*/, 1 /*slotId*/);
        phoneAccounts.add(emergencyPhoneAccount);
        PhoneAccountHandle emergencyPhoneAccountHandle = emergencyPhoneAccount.getAccountHandle();

        mTestCreateConnectionProcessor.process();

        verify(mMockCall).setConnectionManagerPhoneAccount(eq(emergencyPhoneAccountHandle));
        verify(mMockCall).setTargetPhoneAccount(eq(emergencyPhoneAccountHandle));
        verify(mMockCall).setConnectionService(eq(service));
        verify(service).createConnection(eq(mMockCall), any(CreateConnectionResponse.class));
        // Notify successful connection to call
        CallIdMapper mockCallIdMapper = mock(CallIdMapper.class);
        mTestCreateConnectionProcessor.handleCreateConnectionSuccess(mockCallIdMapper, null);
        verify(mMockCreateConnectionResponse).handleCreateConnectionSuccess(mockCallIdMapper, null);
    }

    /**
     * 1) Ensure that if there is a non-SIM PhoneAccount, it is not chosen as the Phone Account to
     * dial the emergency call.
     * 2) Register multiple emergency capable PhoneAccounts. Since there is not preference, we
     * default to sending on the lowest slot.
     */
    @SmallTest
    @Test
    public void testEmergencyCallMultiSimNoPreferred() throws Exception {
        when(mMockCall.isEmergencyCall()).thenReturn(true);
        when(mMockCall.isTestEmergencyCall()).thenReturn(false);
        // Put in a non-SIM phone account as the target to be sure it doesn't call that.
        PhoneAccount regularAccount = makePhoneAccount("tel_acct1", 0);
        setTargetPhoneAccount(mMockCall, regularAccount.getAccountHandle());
        // Include a Connection Manager to be sure it doesn't call that
        PhoneAccount callManagerPA = createNewConnectionManagerPhoneAccountForCall(mMockCall,
                "cm_acct", 0);
        phoneAccounts.add(callManagerPA);
        ConnectionServiceWrapper service = makeConnectionServiceWrapper();
        PhoneAccount emergencyPhoneAccount1 = makeEmergencyPhoneAccount("tel_emer1", 0, null);
        phoneAccounts.add(emergencyPhoneAccount1);
        mapToSubSlot(emergencyPhoneAccount1, 1 /*subId*/, 1 /*slotId*/);
        PhoneAccount emergencyPhoneAccount2 = makeEmergencyPhoneAccount("tel_emer2", 0, null);
        phoneAccounts.add(emergencyPhoneAccount2);
        mapToSubSlot(emergencyPhoneAccount2, 2 /*subId*/, 0 /*slotId*/);
        PhoneAccountHandle emergencyPhoneAccountHandle2 = emergencyPhoneAccount2.getAccountHandle();

        mTestCreateConnectionProcessor.process();

        // We did not set a preference
        verify(mMockCall).setConnectionManagerPhoneAccount(eq(emergencyPhoneAccountHandle2));
        verify(mMockCall).setTargetPhoneAccount(eq(emergencyPhoneAccountHandle2));
        verify(mMockCall).setConnectionService(eq(service));
        verify(service).createConnection(eq(mMockCall), any(CreateConnectionResponse.class));
        // Notify successful connection to call
        CallIdMapper mockCallIdMapper = mock(CallIdMapper.class);
        mTestCreateConnectionProcessor.handleCreateConnectionSuccess(mockCallIdMapper, null);
        verify(mMockCreateConnectionResponse).handleCreateConnectionSuccess(mockCallIdMapper, null);
    }

    /**
     * Ensure that the call goes out on the PhoneAccount that has the CAPABILITY_EMERGENCY_PREFERRED
     * capability, even if the user specifically chose the other emergency capable PhoneAccount.
     */
    @SmallTest
    @Test
    public void testEmergencyCallMultiSimTelephonyPreferred() throws Exception {
        when(mMockCall.isEmergencyCall()).thenReturn(true);
        when(mMockCall.isTestEmergencyCall()).thenReturn(false);
        ConnectionServiceWrapper service = makeConnectionServiceWrapper();
        PhoneAccount emergencyPhoneAccount1 = makeEmergencyPhoneAccount("tel_emer1", 0, null);
        mapToSubSlot(emergencyPhoneAccount1, 1 /*subId*/, 0 /*slotId*/);
        setTargetPhoneAccount(mMockCall, emergencyPhoneAccount1.getAccountHandle());
        phoneAccounts.add(emergencyPhoneAccount1);
        PhoneAccount emergencyPhoneAccount2 = makeEmergencyPhoneAccount("tel_emer2",
                PhoneAccount.CAPABILITY_EMERGENCY_PREFERRED, null);
        mapToSubSlot(emergencyPhoneAccount2, 2 /*subId*/, 1 /*slotId*/);
        phoneAccounts.add(emergencyPhoneAccount2);
        PhoneAccountHandle emergencyPhoneAccountHandle2 = emergencyPhoneAccount2.getAccountHandle();

        mTestCreateConnectionProcessor.process();

        // Make sure the telephony preferred SIM is the one that is chosen.
        verify(mMockCall).setConnectionManagerPhoneAccount(eq(emergencyPhoneAccountHandle2));
        verify(mMockCall).setTargetPhoneAccount(eq(emergencyPhoneAccountHandle2));
        verify(mMockCall).setConnectionService(eq(service));
        verify(service).createConnection(eq(mMockCall), any(CreateConnectionResponse.class));
        // Notify successful connection to call
        CallIdMapper mockCallIdMapper = mock(CallIdMapper.class);
        mTestCreateConnectionProcessor.handleCreateConnectionSuccess(mockCallIdMapper, null);
        verify(mMockCreateConnectionResponse).handleCreateConnectionSuccess(mockCallIdMapper, null);
    }

    /**
     * If there is no phone account with CAPABILITY_EMERGENCY_PREFERRED capability, choose the user
     * chosen target account.
     */
    @SmallTest
    @Test
    public void testEmergencyCallMultiSimUserPreferred() throws Exception {
        when(mMockCall.isEmergencyCall()).thenReturn(true);
        when(mMockCall.isTestEmergencyCall()).thenReturn(false);
        // Include a Connection Manager to be sure it doesn't call that
        PhoneAccount callManagerPA = createNewConnectionManagerPhoneAccountForCall(mMockCall,
                "cm_acct", 0);
        phoneAccounts.add(callManagerPA);
        ConnectionServiceWrapper service = makeConnectionServiceWrapper();
        PhoneAccount emergencyPhoneAccount1 = makeEmergencyPhoneAccount("tel_emer1", 0, null);
        mapToSubSlot(emergencyPhoneAccount1, 1 /*subId*/, 0 /*slotId*/);
        phoneAccounts.add(emergencyPhoneAccount1);
        PhoneAccount emergencyPhoneAccount2 = makeEmergencyPhoneAccount("tel_emer2", 0, null);
        // Make this the user preferred account
        mapToSubSlot(emergencyPhoneAccount2, 2 /*subId*/, 1 /*slotId*/);
        setTargetPhoneAccount(mMockCall, emergencyPhoneAccount2.getAccountHandle());
        phoneAccounts.add(emergencyPhoneAccount2);
        PhoneAccountHandle emergencyPhoneAccountHandle2 = emergencyPhoneAccount2.getAccountHandle();

        mTestCreateConnectionProcessor.process();

        // Make sure the user preferred SIM is the one that is chosen.
        verify(mMockCall).setConnectionManagerPhoneAccount(eq(emergencyPhoneAccountHandle2));
        verify(mMockCall).setTargetPhoneAccount(eq(emergencyPhoneAccountHandle2));
        verify(mMockCall).setConnectionService(eq(service));
        verify(service).createConnection(eq(mMockCall), any(CreateConnectionResponse.class));
        // Notify successful connection to call
        CallIdMapper mockCallIdMapper = mock(CallIdMapper.class);
        mTestCreateConnectionProcessor.handleCreateConnectionSuccess(mockCallIdMapper, null);
        verify(mMockCreateConnectionResponse).handleCreateConnectionSuccess(mockCallIdMapper, null);
    }

    /**
     * Ensure that the call goes out on the PhoneAccount for the incoming call and not the
     * Telephony preferred emergency account.
     */
    @SmallTest
    @Test
    public void testMTEmergencyCallMultiSimUserPreferred() throws Exception {
        when(mMockCall.isEmergencyCall()).thenReturn(true);
        when(mMockCall.isTestEmergencyCall()).thenReturn(false);
        when(mMockCall.isIncoming()).thenReturn(true);
        ConnectionServiceWrapper service = makeConnectionServiceWrapper();
        PhoneAccount emergencyPhoneAccount1 = makeEmergencyPhoneAccount("tel_emer1", 0, null);
        mapToSubSlot(emergencyPhoneAccount1, 1 /*subId*/, 0 /*slotId*/);
        setTargetPhoneAccount(mMockCall, emergencyPhoneAccount1.getAccountHandle());
        phoneAccounts.add(emergencyPhoneAccount1);
        // Make this the user preferred phone account
        setTargetPhoneAccount(mMockCall, emergencyPhoneAccount1.getAccountHandle());
        PhoneAccount emergencyPhoneAccount2 = makeEmergencyPhoneAccount("tel_emer2",
                PhoneAccount.CAPABILITY_EMERGENCY_PREFERRED, null);
        mapToSubSlot(emergencyPhoneAccount2, 2 /*subId*/, 1 /*slotId*/);
        phoneAccounts.add(emergencyPhoneAccount2);
        PhoneAccountHandle emergencyPhoneAccountHandle2 = emergencyPhoneAccount2.getAccountHandle();

        mTestCreateConnectionProcessor.process();

        verify(mMockCall).setConnectionManagerPhoneAccount(
                eq(emergencyPhoneAccount1.getAccountHandle()));
        // The account we're using to place the call should be the user preferred account
        verify(mMockCall).setTargetPhoneAccount(eq(emergencyPhoneAccount1.getAccountHandle()));
        verify(mMockCall).setConnectionService(eq(service));
        verify(service).createConnection(eq(mMockCall), any(CreateConnectionResponse.class));
        // Notify successful connection to call
        CallIdMapper mockCallIdMapper = mock(CallIdMapper.class);
        mTestCreateConnectionProcessor.handleCreateConnectionSuccess(mockCallIdMapper, null);
        verify(mMockCreateConnectionResponse).handleCreateConnectionSuccess(mockCallIdMapper, null);
    }

    /**
     * If the user preferred PhoneAccount is associated with an invalid slot, place on the other,
     * valid slot.
     */
    @SmallTest
    @Test
    public void testEmergencyCallMultiSimUserPreferredInvalidSlot() throws Exception {
        when(mMockCall.isEmergencyCall()).thenReturn(true);
        when(mMockCall.isTestEmergencyCall()).thenReturn(false);
        // Include a Connection Manager to be sure it doesn't call that
        PhoneAccount callManagerPA = createNewConnectionManagerPhoneAccountForCall(mMockCall,
                "cm_acct", 0);
        phoneAccounts.add(callManagerPA);
        ConnectionServiceWrapper service = makeConnectionServiceWrapper();
        PhoneAccount emergencyPhoneAccount1 = makeEmergencyPhoneAccount("tel_emer1", 0, null);
        // make this the user preferred account
        setTargetPhoneAccount(mMockCall, emergencyPhoneAccount1.getAccountHandle());
        mapToSubSlot(emergencyPhoneAccount1, 1 /*subId*/,
                SubscriptionManager.INVALID_SIM_SLOT_INDEX /*slotId*/);
        phoneAccounts.add(emergencyPhoneAccount1);
        PhoneAccount emergencyPhoneAccount2 = makeEmergencyPhoneAccount("tel_emer2", 0, null);
        mapToSubSlot(emergencyPhoneAccount2, 2 /*subId*/, 1 /*slotId*/);
        phoneAccounts.add(emergencyPhoneAccount2);
        PhoneAccountHandle emergencyPhoneAccountHandle2 = emergencyPhoneAccount2.getAccountHandle();

        mTestCreateConnectionProcessor.process();

        // Make sure the valid SIM is the one that is chosen.
        verify(mMockCall).setConnectionManagerPhoneAccount(eq(emergencyPhoneAccountHandle2));
        verify(mMockCall).setTargetPhoneAccount(eq(emergencyPhoneAccountHandle2));
        verify(mMockCall).setConnectionService(eq(service));
        verify(service).createConnection(eq(mMockCall), any(CreateConnectionResponse.class));
        // Notify successful connection to call
        CallIdMapper mockCallIdMapper = mock(CallIdMapper.class);
        mTestCreateConnectionProcessor.handleCreateConnectionSuccess(mockCallIdMapper, null);
        verify(mMockCreateConnectionResponse).handleCreateConnectionSuccess(mockCallIdMapper, null);
    }

    /**
     * If a PhoneAccount is associated with an invalid slot, place on the other, valid slot.
     */
    @SmallTest
    @Test
    public void testEmergencyCallMultiSimNoPreferenceInvalidSlot() throws Exception {
        when(mMockCall.isEmergencyCall()).thenReturn(true);
        when(mMockCall.isTestEmergencyCall()).thenReturn(false);
        // Include a Connection Manager to be sure it doesn't call that
        PhoneAccount callManagerPA = createNewConnectionManagerPhoneAccountForCall(mMockCall,
                "cm_acct", 0);
        phoneAccounts.add(callManagerPA);
        ConnectionServiceWrapper service = makeConnectionServiceWrapper();
        PhoneAccount emergencyPhoneAccount1 = makeEmergencyPhoneAccount("tel_emer1", 0, null);
        mapToSubSlot(emergencyPhoneAccount1, 1 /*subId*/,
                SubscriptionManager.INVALID_SIM_SLOT_INDEX /*slotId*/);
        phoneAccounts.add(emergencyPhoneAccount1);
        PhoneAccount emergencyPhoneAccount2 = makeEmergencyPhoneAccount("tel_emer2", 0, null);
        mapToSubSlot(emergencyPhoneAccount2, 2 /*subId*/, 1 /*slotId*/);
        phoneAccounts.add(emergencyPhoneAccount2);
        PhoneAccountHandle emergencyPhoneAccountHandle2 = emergencyPhoneAccount2.getAccountHandle();

        mTestCreateConnectionProcessor.process();

        // Make sure the valid SIM is the one that is chosen.
        verify(mMockCall).setConnectionManagerPhoneAccount(eq(emergencyPhoneAccountHandle2));
        verify(mMockCall).setTargetPhoneAccount(eq(emergencyPhoneAccountHandle2));
        verify(mMockCall).setConnectionService(eq(service));
        verify(service).createConnection(eq(mMockCall), any(CreateConnectionResponse.class));
        // Notify successful connection to call
        CallIdMapper mockCallIdMapper = mock(CallIdMapper.class);
        mTestCreateConnectionProcessor.handleCreateConnectionSuccess(mockCallIdMapper, null);
        verify(mMockCreateConnectionResponse).handleCreateConnectionSuccess(mockCallIdMapper, null);
    }

    /**
     * Ensures that a self-managed phone account won't be considered when attempting to place an
     * emergency call.
     */
    @SmallTest
    @Test
    public void testDontAttemptSelfManaged() {
        when(mMockCall.isEmergencyCall()).thenReturn(true);
        when(mMockCall.isTestEmergencyCall()).thenReturn(false);
        when(mMockCall.getHandle()).thenReturn(Uri.parse(""));

        PhoneAccount selfManagedAcct = makePhoneAccount("sm-acct",
                PhoneAccount.CAPABILITY_SELF_MANAGED
                        | PhoneAccount.CAPABILITY_PLACE_EMERGENCY_CALLS);
        phoneAccounts.add(selfManagedAcct);

        mTestCreateConnectionProcessor.process();
        verify(mMockCall, never()).setTargetPhoneAccount(any(PhoneAccountHandle.class));
    }

    @SmallTest
    @Test
    public void testEmergencyCallSimFailToConnectionManager() throws Exception {
        when(mMockCall.isEmergencyCall()).thenReturn(true);
        when(mMockCall.isTestEmergencyCall()).thenReturn(false);
        when(mMockCall.getHandle()).thenReturn(Uri.parse(""));
        // Put in a regular phone account to be sure it doesn't call that
        PhoneAccount regularAccount = makePhoneAccount("tel_acct1",
                PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION);
        mapToSubSlot(regularAccount, 1 /*subId*/, 0 /*slotId*/);
        setTargetPhoneAccount(mMockCall, regularAccount.getAccountHandle());
        phoneAccounts.add(regularAccount);
        when(mMockAccountRegistrar.getOutgoingPhoneAccountForSchemeOfCurrentUser(
                nullable(String.class))).thenReturn(regularAccount.getAccountHandle());
        // Include a normal Connection Manager to be sure it doesn't call that
        PhoneAccount callManagerPA = createNewConnectionManagerPhoneAccountForCall(mMockCall,
                "cm_acct", 0);
        phoneAccounts.add(callManagerPA);
        // Include a connection Manager for the user with the capability to make calls
        PhoneAccount emerCallManagerPA = getNewEmergencyConnectionManagerPhoneAccount("cm_acct",
                PhoneAccount.CAPABILITY_PLACE_EMERGENCY_CALLS);
        ConnectionServiceWrapper service = makeConnectionServiceWrapper();
        PhoneAccount emergencyPhoneAccount = makeEmergencyPhoneAccount("tel_emer", 0, null);
        phoneAccounts.add(emergencyPhoneAccount);
        mapToSubSlot(regularAccount, 2 /*subId*/, 1 /*slotId*/);
        mTestCreateConnectionProcessor.process();
        reset(mMockCall);
        reset(service);

        when(mMockCall.getConnectionServiceFocusManager()).thenReturn(
                mConnectionServiceFocusManager);
        when(mMockCall.isEmergencyCall()).thenReturn(true);

        // When Notify SIM connection fails, fall back to connection manager
        mTestCreateConnectionProcessor.handleCreateConnectionFailure(new DisconnectCause(
                DisconnectCause.REJECTED));

        verify(mMockCall).setConnectionManagerPhoneAccount(
                eq(emerCallManagerPA.getAccountHandle()));
        verify(mMockCall).setTargetPhoneAccount(eq(regularAccount.getAccountHandle()));
        verify(mMockCall).setConnectionService(eq(service));
        verify(service).createConnection(eq(mMockCall), any(CreateConnectionResponse.class));
    }

    /**
     * Tests to verify that the
     * {@link CreateConnectionProcessor#sortSimPhoneAccountsForEmergency(List, PhoneAccount)} can
     * successfully sort without running into sort issues related to the hashcodes of the
     * PhoneAccounts.
     */
    @Test
    public void testSortIntegrity() {
        // Note: 5L was chosen as a random seed on purpose since in combination with a count of
        // 500 accounts it would result in a crash in the sort algorithm.
        ArrayList<PhoneAccount> accounts = generateRandomPhoneAccounts(5L, 500);
        try {
            mTestCreateConnectionProcessor.sortSimPhoneAccountsForEmergency(accounts,
                    null);
        } catch (Exception e) {
            fail("Failed to sort phone accounts");
        }
    }

    /**
     * Generates random phone accounts.
     * @param seed random seed to use for random UUIDs; passed in for determinism.
     * @param count How many phone accounts to use.
     * @return Random phone accounts.
     */
    private ArrayList<PhoneAccount> generateRandomPhoneAccounts(long seed, int count) {
        Random random = new Random(seed);
        ArrayList<PhoneAccount> accounts = new ArrayList<>();
        for (int ix = 0 ; ix < count; ix++) {
            ArrayList<String> supportedSchemes = new ArrayList<>();
            supportedSchemes.add("tel");
            supportedSchemes.add("sip");
            supportedSchemes.add("custom");

            PhoneAccountHandle handle = new PhoneAccountHandle(
                    ComponentName.unflattenFromString(
                            "com.android.server.telecom.testapps/"
                                    + "com.android.server.telecom.testapps"
                                    + ".SelfManagedConnectionService"),
                    getRandomUuid(random).toString(), new UserHandle(0));
            PhoneAccount acct = new PhoneAccount.Builder(handle, "TelecommTests")
                    .setAddress(Uri.fromParts("tel", "555-1212", null))
                    .setCapabilities(3080)
                    .setHighlightColor(0)
                    .setShortDescription("test_" + ix)
                    .setSupportedUriSchemes(supportedSchemes)
                    .setIsEnabled(true)
                    .setSupportedAudioRoutes(15)
                    .build();
            accounts.add(acct);
        }
        return accounts;
    }

    /**
     * Returns a random UUID based on the passed in Random generator.
     * @param random Random generator.
     * @return The UUID.
     */
    private UUID getRandomUuid(Random random) {
        byte[] array = new byte[16];
        random.nextBytes(array);
        return UUID.nameUUIDFromBytes(array);
    }

    private PhoneAccount makeEmergencyTestPhoneAccount(String id, int capabilities) {
        final PhoneAccount emergencyPhoneAccount = makeQuickAccount(id, capabilities |
                PhoneAccount.CAPABILITY_PLACE_EMERGENCY_CALLS, null);
        PhoneAccountHandle emergencyPhoneAccountHandle = emergencyPhoneAccount.getAccountHandle();
        givePhoneAccountBindPermission(emergencyPhoneAccountHandle);
        when(mMockAccountRegistrar.getPhoneAccountUnchecked(emergencyPhoneAccountHandle))
                .thenReturn(emergencyPhoneAccount);
        return emergencyPhoneAccount;
    }

    private PhoneAccount makeEmergencyPhoneAccount(String id, int capabilities,
            UserHandle userHandle) {
        final PhoneAccount emergencyPhoneAccount = makeQuickAccount(id, capabilities |
                PhoneAccount.CAPABILITY_PLACE_EMERGENCY_CALLS |
                        PhoneAccount.CAPABILITY_SIM_SUBSCRIPTION, userHandle);
        PhoneAccountHandle emergencyPhoneAccountHandle = emergencyPhoneAccount.getAccountHandle();
        givePhoneAccountBindPermission(emergencyPhoneAccountHandle);
        when(mMockAccountRegistrar.getPhoneAccountUnchecked(emergencyPhoneAccountHandle))
                .thenReturn(emergencyPhoneAccount);
        return emergencyPhoneAccount;
    }

    private PhoneAccount makePhoneAccount(String id, int capabilities) {
        final PhoneAccount phoneAccount = makeQuickAccount(id, capabilities, null);
        PhoneAccountHandle phoneAccountHandle = phoneAccount.getAccountHandle();
        givePhoneAccountBindPermission(phoneAccountHandle);
        when(mMockAccountRegistrar.getPhoneAccountUnchecked(
                phoneAccount.getAccountHandle())).thenReturn(phoneAccount);
        return phoneAccount;
    }

    private void mapToSubSlot(PhoneAccount account, int subId, int slotId) {
        mAccountToSub.put(account, subId);
        mSubToSlot.put(subId, slotId);
    }

    private void givePhoneAccountBindPermission(PhoneAccountHandle handle) {
        when(mMockAccountRegistrar.phoneAccountRequiresBindPermission(eq(handle))).thenReturn(true);
    }

    private PhoneAccountHandle getNewConnectionMangerHandleForCall(Call call, String id) {
        PhoneAccountHandle callManagerPAHandle = makeQuickAccountHandle(id, null);
        when(mMockAccountRegistrar.getSimCallManagerFromCall(eq(call))).thenReturn(
                callManagerPAHandle);
        givePhoneAccountBindPermission(callManagerPAHandle);
        return callManagerPAHandle;
    }

    private PhoneAccountHandle getNewTargetPhoneAccountHandle(String id) {
        PhoneAccountHandle pAHandle = makeQuickAccountHandle(id, null);
        givePhoneAccountBindPermission(pAHandle);
        return pAHandle;
    }

    private void setTargetPhoneAccount(Call call, PhoneAccountHandle pAHandle) {
        when(call.getTargetPhoneAccount()).thenReturn(pAHandle);
    }

    private PhoneAccount createNewConnectionManagerPhoneAccountForCall(Call call, String id,
            int capability) {
        PhoneAccount callManagerPA = makeQuickAccount(id, capability, null);
        when(mMockAccountRegistrar.getSimCallManagerFromCall(eq(call))).thenReturn(
                callManagerPA.getAccountHandle());
        givePhoneAccountBindPermission(callManagerPA.getAccountHandle());
        when(mMockAccountRegistrar.getPhoneAccountUnchecked(
                callManagerPA.getAccountHandle())).thenReturn(callManagerPA);
        return callManagerPA;
    }

    private PhoneAccount getNewEmergencyConnectionManagerPhoneAccount(String id, int capability) {
        PhoneAccount callManagerPA = makeQuickAccount(id, capability, null);
        when(mMockAccountRegistrar.getSimCallManagerOfCurrentUser()).thenReturn(
                callManagerPA.getAccountHandle());
        givePhoneAccountBindPermission(callManagerPA.getAccountHandle());
        when(mMockAccountRegistrar.getPhoneAccountUnchecked(
                callManagerPA.getAccountHandle())).thenReturn(callManagerPA);
        return callManagerPA;
    }

    private static ComponentName makeQuickConnectionServiceComponentName() {
        return new ComponentName(TEST_PACKAGE, TEST_CLASS);
    }

    private ConnectionServiceWrapper makeConnectionServiceWrapper() {
        ConnectionServiceWrapper wrapper = mock(ConnectionServiceWrapper.class);
        when(mMockConnectionServiceRepository.getService(
                eq(makeQuickConnectionServiceComponentName()),
                any(UserHandle.class))).thenReturn(wrapper);
        return wrapper;
    }

    private static PhoneAccountHandle makeQuickAccountHandle(String id, UserHandle userHandle) {
        if (userHandle == null) {
            userHandle = Binder.getCallingUserHandle();
        }
        return new PhoneAccountHandle(makeQuickConnectionServiceComponentName(), id, userHandle);
    }

    private PhoneAccount.Builder makeQuickAccountBuilder(String id, int idx,
            UserHandle userHandle) {
        return new PhoneAccount.Builder(makeQuickAccountHandle(id, userHandle), "label" + idx);
    }

    private PhoneAccount makeQuickAccount(String id, int idx, UserHandle userHandle) {
        return makeQuickAccountBuilder(id, idx, userHandle)
                .setAddress(Uri.parse("http://foo.com/" + idx))
                .setSubscriptionAddress(Uri.parse("tel:555-000" + idx))
                .setCapabilities(idx)
                .setIcon(Icon.createWithResource(
                        "com.android.server.telecom.tests", R.drawable.stat_sys_phone_call))
                .setShortDescription("desc" + idx)
                .setIsEnabled(true)
                .build();
    }
}