1/*
2 * Copyright (C) 2016 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
17package com.android.services.telephony;
18
19import android.telephony.RadioAccessFamily;
20import android.telephony.ServiceState;
21import android.support.test.runner.AndroidJUnit4;
22import android.telephony.TelephonyManager;
23import android.test.suitebuilder.annotation.SmallTest;
24
25import com.android.TelephonyTestBase;
26import com.android.internal.telephony.Phone;
27
28import org.junit.After;
29import org.junit.Before;
30import org.junit.Test;
31import org.junit.runner.RunWith;
32import org.mockito.Mock;
33
34import static junit.framework.Assert.assertEquals;
35import static org.mockito.Matchers.eq;
36import static org.mockito.Mockito.mock;
37import static org.mockito.Mockito.when;
38
39/**
40 * Unit tests for TelephonyConnectionService.
41 */
42
43@RunWith(AndroidJUnit4.class)
44public class TelephonyConnectionServiceTest extends TelephonyTestBase {
45
46    private static final int SLOT_0_PHONE_ID = 0;
47    private static final int SLOT_1_PHONE_ID = 1;
48
49    @Mock TelephonyConnectionService.TelephonyManagerProxy mTelephonyManagerProxy;
50    @Mock TelephonyConnectionService.SubscriptionManagerProxy mSubscriptionManagerProxy;
51    @Mock TelephonyConnectionService.PhoneFactoryProxy mPhoneFactoryProxy;
52
53    TelephonyConnectionService mTestConnectionService;
54
55    @Before
56    public void setUp() throws Exception {
57        super.setUp();
58        mTestConnectionService = new TelephonyConnectionService();
59        mTestConnectionService.setPhoneFactoryProxy(mPhoneFactoryProxy);
60        mTestConnectionService.setTelephonyManagerProxy(mTelephonyManagerProxy);
61        mTestConnectionService.setSubscriptionManagerProxy(mSubscriptionManagerProxy);
62    }
63
64    @After
65    public void tearDown() throws Exception {
66        mTestConnectionService = null;
67        super.tearDown();
68    }
69
70    /**
71     * Prerequisites:
72     * - MSIM Device, two slots with SIMs inserted
73     * - Users default Voice SIM choice is IN_SERVICE
74     *
75     * Result: getFirstPhoneForEmergencyCall returns the default Voice SIM choice.
76     */
77    @Test
78    @SmallTest
79    public void testDefaultVoiceSimInService() {
80        Phone slot0Phone = makeTestPhone(SLOT_0_PHONE_ID, ServiceState.STATE_IN_SERVICE,
81                false /*isEmergencyOnly*/);
82        Phone slot1Phone = makeTestPhone(SLOT_1_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
83                true /*isEmergencyOnly*/);
84        setDefaultPhone(slot0Phone);
85        setupDeviceConfig(slot0Phone, slot1Phone, SLOT_0_PHONE_ID);
86
87        Phone resultPhone = mTestConnectionService.getFirstPhoneForEmergencyCall();
88
89        assertEquals(slot0Phone, resultPhone);
90    }
91
92    /**
93     * Prerequisites:
94     * - MSIM Device, two slots with SIMs inserted
95     * - Slot 0 is OUT_OF_SERVICE, Slot 1 is OUT_OF_SERVICE (emergency calls only)
96     *
97     * Result: getFirstPhoneForEmergencyCall returns the slot 1 phone
98     */
99    @Test
100    @SmallTest
101    public void testSlot1EmergencyOnly() {
102        Phone slot0Phone = makeTestPhone(SLOT_0_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
103                false /*isEmergencyOnly*/);
104        Phone slot1Phone = makeTestPhone(SLOT_1_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
105                true /*isEmergencyOnly*/);
106        setDefaultPhone(slot0Phone);
107        setupDeviceConfig(slot0Phone, slot1Phone, SLOT_0_PHONE_ID);
108
109        Phone resultPhone = mTestConnectionService.getFirstPhoneForEmergencyCall();
110
111        assertEquals(slot1Phone, resultPhone);
112    }
113
114    /**
115     * Prerequisites:
116     * - MSIM Device, two slots with SIMs inserted
117     * - Slot 0 is OUT_OF_SERVICE, Slot 1 is IN_SERVICE
118     *
119     * Result: getFirstPhoneForEmergencyCall returns the slot 1 phone
120     */
121    @Test
122    @SmallTest
123    public void testSlot1InService() {
124        Phone slot0Phone = makeTestPhone(SLOT_0_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
125                false /*isEmergencyOnly*/);
126        Phone slot1Phone = makeTestPhone(SLOT_1_PHONE_ID, ServiceState.STATE_IN_SERVICE,
127                false /*isEmergencyOnly*/);
128        setDefaultPhone(slot0Phone);
129        setupDeviceConfig(slot0Phone, slot1Phone, SLOT_0_PHONE_ID);
130
131        Phone resultPhone = mTestConnectionService.getFirstPhoneForEmergencyCall();
132
133        assertEquals(slot1Phone, resultPhone);
134    }
135
136    /**
137     * Prerequisites:
138     * - MSIM Device, two slots with SIMs inserted
139     * - Slot 0 is PUK locked, Slot 1 is ready
140     * - Slot 0 is LTE capable, Slot 1 is GSM capable
141     *
142     * Result: getFirstPhoneForEmergencyCall returns the slot 1 phone. Although Slot 0 is more
143     * capable, it is locked, so use the other slot.
144     */
145    @Test
146    @SmallTest
147    public void testSlot0PukLocked() {
148        Phone slot0Phone = makeTestPhone(SLOT_0_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
149                false /*isEmergencyOnly*/);
150        Phone slot1Phone = makeTestPhone(SLOT_1_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
151                false /*isEmergencyOnly*/);
152        setDefaultPhone(slot0Phone);
153        setupDeviceConfig(slot0Phone, slot1Phone, SLOT_0_PHONE_ID);
154        // Set Slot 0 to be PUK locked
155        setPhoneSlotState(SLOT_0_PHONE_ID, TelephonyManager.SIM_STATE_PUK_REQUIRED);
156        setPhoneSlotState(SLOT_1_PHONE_ID, TelephonyManager.SIM_STATE_READY);
157        // Make Slot 0 higher capability
158        setPhoneRadioAccessFamily(slot0Phone, RadioAccessFamily.RAF_LTE);
159        setPhoneRadioAccessFamily(slot1Phone, RadioAccessFamily.RAF_GSM);
160
161        Phone resultPhone = mTestConnectionService.getFirstPhoneForEmergencyCall();
162
163        assertEquals(slot1Phone, resultPhone);
164    }
165
166    /**
167     * Prerequisites:
168     * - MSIM Device, two slots with SIMs inserted
169     * - Slot 0 is PIN locked, Slot 1 is ready
170     * - Slot 0 is LTE capable, Slot 1 is GSM capable
171     *
172     * Result: getFirstPhoneForEmergencyCall returns the slot 1 phone. Although Slot 0 is more
173     * capable, it is locked, so use the other slot.
174     */
175    @Test
176    @SmallTest
177    public void testSlot0PinLocked() {
178        Phone slot0Phone = makeTestPhone(SLOT_0_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
179                false /*isEmergencyOnly*/);
180        Phone slot1Phone = makeTestPhone(SLOT_1_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
181                false /*isEmergencyOnly*/);
182        setDefaultPhone(slot0Phone);
183        setupDeviceConfig(slot0Phone, slot1Phone, SLOT_0_PHONE_ID);
184        // Set Slot 0 to be PUK locked
185        setPhoneSlotState(SLOT_0_PHONE_ID, TelephonyManager.SIM_STATE_PIN_REQUIRED);
186        setPhoneSlotState(SLOT_1_PHONE_ID, TelephonyManager.SIM_STATE_READY);
187        // Make Slot 0 higher capability
188        setPhoneRadioAccessFamily(slot0Phone, RadioAccessFamily.RAF_LTE);
189        setPhoneRadioAccessFamily(slot1Phone, RadioAccessFamily.RAF_GSM);
190
191        Phone resultPhone = mTestConnectionService.getFirstPhoneForEmergencyCall();
192
193        assertEquals(slot1Phone, resultPhone);
194    }
195
196    /**
197     * Prerequisites:
198     * - MSIM Device, two slots with SIMs inserted
199     * - Slot 1 is PUK locked, Slot 0 is ready
200     * - Slot 1 is LTE capable, Slot 0 is GSM capable
201     *
202     * Result: getFirstPhoneForEmergencyCall returns the slot 0 phone. Although Slot 1 is more
203     * capable, it is locked, so use the other slot.
204     */
205    @Test
206    @SmallTest
207    public void testSlot1PukLocked() {
208        Phone slot0Phone = makeTestPhone(SLOT_0_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
209                false /*isEmergencyOnly*/);
210        Phone slot1Phone = makeTestPhone(SLOT_1_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
211                false /*isEmergencyOnly*/);
212        setDefaultPhone(slot0Phone);
213        setupDeviceConfig(slot0Phone, slot1Phone, SLOT_0_PHONE_ID);
214        // Set Slot 1 to be PUK locked
215        setPhoneSlotState(SLOT_0_PHONE_ID, TelephonyManager.SIM_STATE_READY);
216        setPhoneSlotState(SLOT_1_PHONE_ID, TelephonyManager.SIM_STATE_PUK_REQUIRED);
217        // Make Slot 1 higher capability
218        setPhoneRadioAccessFamily(slot0Phone, RadioAccessFamily.RAF_GSM);
219        setPhoneRadioAccessFamily(slot1Phone, RadioAccessFamily.RAF_LTE);
220
221        Phone resultPhone = mTestConnectionService.getFirstPhoneForEmergencyCall();
222
223        assertEquals(slot0Phone, resultPhone);
224    }
225
226    /**
227     * Prerequisites:
228     * - MSIM Device, two slots with SIMs inserted
229     * - Slot 1 is PIN locked, Slot 0 is ready
230     * - Slot 1 is LTE capable, Slot 0 is GSM capable
231     *
232     * Result: getFirstPhoneForEmergencyCall returns the slot 0 phone. Although Slot 1 is more
233     * capable, it is locked, so use the other slot.
234     */
235    @Test
236    @SmallTest
237    public void testSlot1PinLocked() {
238        Phone slot0Phone = makeTestPhone(SLOT_0_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
239                false /*isEmergencyOnly*/);
240        Phone slot1Phone = makeTestPhone(SLOT_1_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
241                false /*isEmergencyOnly*/);
242        setDefaultPhone(slot0Phone);
243        setupDeviceConfig(slot0Phone, slot1Phone, SLOT_0_PHONE_ID);
244        // Set Slot 1 to be PUK locked
245        setPhoneSlotState(SLOT_0_PHONE_ID, TelephonyManager.SIM_STATE_READY);
246        setPhoneSlotState(SLOT_1_PHONE_ID, TelephonyManager.SIM_STATE_PIN_REQUIRED);
247        // Make Slot 1 higher capability
248        setPhoneRadioAccessFamily(slot0Phone, RadioAccessFamily.RAF_GSM);
249        setPhoneRadioAccessFamily(slot1Phone, RadioAccessFamily.RAF_LTE);
250
251        Phone resultPhone = mTestConnectionService.getFirstPhoneForEmergencyCall();
252
253        assertEquals(slot0Phone, resultPhone);
254    }
255
256    /**
257     * Prerequisites:
258     * - MSIM Device, two slots with SIMs inserted
259     * - Slot 1 is LTE capable, Slot 0 is GSM capable
260     *
261     * Result: getFirstPhoneForEmergencyCall returns the slot 1 phone because it is more capable
262     */
263    @Test
264    @SmallTest
265    public void testSlot1HigherCapablity() {
266        Phone slot0Phone = makeTestPhone(SLOT_0_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
267                false /*isEmergencyOnly*/);
268        Phone slot1Phone = makeTestPhone(SLOT_1_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
269                false /*isEmergencyOnly*/);
270        setDefaultPhone(slot0Phone);
271        setupDeviceConfig(slot0Phone, slot1Phone, SLOT_0_PHONE_ID);
272        setPhoneSlotState(SLOT_0_PHONE_ID, TelephonyManager.SIM_STATE_READY);
273        setPhoneSlotState(SLOT_1_PHONE_ID, TelephonyManager.SIM_STATE_READY);
274        // Make Slot 1 higher capability
275        setPhoneRadioAccessFamily(slot0Phone, RadioAccessFamily.RAF_GSM);
276        setPhoneRadioAccessFamily(slot1Phone, RadioAccessFamily.RAF_LTE);
277
278        Phone resultPhone = mTestConnectionService.getFirstPhoneForEmergencyCall();
279
280        assertEquals(slot1Phone, resultPhone);
281    }
282
283    /**
284     * Prerequisites:
285     * - MSIM Device, two slots with SIMs inserted
286     * - Slot 1 is GSM/LTE capable, Slot 0 is GSM capable
287     *
288     * Result: getFirstPhoneForEmergencyCall returns the slot 1 phone because it has more
289     * capabilities.
290     */
291    @Test
292    @SmallTest
293    public void testSlot1MoreCapabilities() {
294        Phone slot0Phone = makeTestPhone(SLOT_0_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
295                false /*isEmergencyOnly*/);
296        Phone slot1Phone = makeTestPhone(SLOT_1_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
297                false /*isEmergencyOnly*/);
298        setDefaultPhone(slot0Phone);
299        setupDeviceConfig(slot0Phone, slot1Phone, SLOT_0_PHONE_ID);
300        setPhoneSlotState(SLOT_0_PHONE_ID, TelephonyManager.SIM_STATE_READY);
301        setPhoneSlotState(SLOT_1_PHONE_ID, TelephonyManager.SIM_STATE_READY);
302        // Make Slot 1 more capable
303        setPhoneRadioAccessFamily(slot0Phone, RadioAccessFamily.RAF_LTE);
304        setPhoneRadioAccessFamily(slot1Phone,
305                RadioAccessFamily.RAF_GSM | RadioAccessFamily.RAF_LTE);
306
307        Phone resultPhone = mTestConnectionService.getFirstPhoneForEmergencyCall();
308
309        assertEquals(slot1Phone, resultPhone);
310    }
311
312    /**
313     * Prerequisites:
314     * - MSIM Device, two slots with SIMs inserted
315     * - Both SIMs PUK Locked
316     * - Slot 0 is LTE capable, Slot 1 is GSM capable
317     *
318     * Result: getFirstPhoneForEmergencyCall returns the slot 0 phone because it is more capable,
319     * ignoring that both SIMs are PUK locked.
320     */
321    @Test
322    @SmallTest
323    public void testSlot0MoreCapableBothPukLocked() {
324        Phone slot0Phone = makeTestPhone(SLOT_0_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
325                false /*isEmergencyOnly*/);
326        Phone slot1Phone = makeTestPhone(SLOT_1_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
327                false /*isEmergencyOnly*/);
328        setDefaultPhone(slot0Phone);
329        setupDeviceConfig(slot0Phone, slot1Phone, SLOT_0_PHONE_ID);
330        setPhoneSlotState(SLOT_0_PHONE_ID, TelephonyManager.SIM_STATE_PUK_REQUIRED);
331        setPhoneSlotState(SLOT_1_PHONE_ID, TelephonyManager.SIM_STATE_PUK_REQUIRED);
332        // Make Slot 0 higher capability
333        setPhoneRadioAccessFamily(slot0Phone, RadioAccessFamily.RAF_LTE);
334        setPhoneRadioAccessFamily(slot1Phone, RadioAccessFamily.RAF_GSM);
335
336        Phone resultPhone = mTestConnectionService.getFirstPhoneForEmergencyCall();
337
338        assertEquals(slot0Phone, resultPhone);
339    }
340
341    /**
342     * Prerequisites:
343     * - MSIM Device, two slots with SIMs inserted
344     * - Both SIMs have the same capability
345     *
346     * Result: getFirstPhoneForEmergencyCall returns the slot 0 phone because it is the first slot.
347     */
348    @Test
349    @SmallTest
350    public void testEqualCapabilityTwoSimsInserted() {
351        Phone slot0Phone = makeTestPhone(SLOT_0_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
352                false /*isEmergencyOnly*/);
353        Phone slot1Phone = makeTestPhone(SLOT_1_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
354                false /*isEmergencyOnly*/);
355        setDefaultPhone(slot0Phone);
356        setupDeviceConfig(slot0Phone, slot1Phone, SLOT_0_PHONE_ID);
357        setPhoneSlotState(SLOT_0_PHONE_ID, TelephonyManager.SIM_STATE_READY);
358        setPhoneSlotState(SLOT_1_PHONE_ID, TelephonyManager.SIM_STATE_READY);
359        // Make Capability the same
360        setPhoneRadioAccessFamily(slot0Phone, RadioAccessFamily.RAF_LTE);
361        setPhoneRadioAccessFamily(slot1Phone, RadioAccessFamily.RAF_LTE);
362        // Two SIMs inserted
363        setSlotHasIccCard(SLOT_0_PHONE_ID, true /*isInserted*/);
364        setSlotHasIccCard(SLOT_1_PHONE_ID, true /*isInserted*/);
365
366        Phone resultPhone = mTestConnectionService.getFirstPhoneForEmergencyCall();
367
368        assertEquals(slot0Phone, resultPhone);
369    }
370
371    /**
372     * Prerequisites:
373     * - MSIM Device, only slot 0 inserted
374     * - Both SIMs have the same capability
375     *
376     * Result: getFirstPhoneForEmergencyCall returns the slot 0 phone because it is the only one
377     * with a SIM inserted
378     */
379    @Test
380    @SmallTest
381    public void testEqualCapabilitySim0Inserted() {
382        Phone slot0Phone = makeTestPhone(SLOT_0_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
383                false /*isEmergencyOnly*/);
384        Phone slot1Phone = makeTestPhone(SLOT_1_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
385                false /*isEmergencyOnly*/);
386        setDefaultPhone(slot0Phone);
387        setupDeviceConfig(slot0Phone, slot1Phone, SLOT_0_PHONE_ID);
388        setPhoneSlotState(SLOT_0_PHONE_ID, TelephonyManager.SIM_STATE_READY);
389        setPhoneSlotState(SLOT_1_PHONE_ID, TelephonyManager.SIM_STATE_ABSENT);
390        // Make Capability the same
391        setPhoneRadioAccessFamily(slot0Phone, RadioAccessFamily.RAF_LTE);
392        setPhoneRadioAccessFamily(slot1Phone, RadioAccessFamily.RAF_LTE);
393        // Slot 0 has SIM inserted.
394        setSlotHasIccCard(SLOT_0_PHONE_ID, true /*isInserted*/);
395        setSlotHasIccCard(SLOT_1_PHONE_ID, false /*isInserted*/);
396
397        Phone resultPhone = mTestConnectionService.getFirstPhoneForEmergencyCall();
398
399        assertEquals(slot0Phone, resultPhone);
400    }
401
402    /**
403     * Prerequisites:
404     * - MSIM Device, only slot 1 inserted
405     * - Both SIMs have the same capability
406     *
407     * Result: getFirstPhoneForEmergencyCall returns the slot 1 phone because it is the only one
408     * with a SIM inserted
409     */
410    @Test
411    @SmallTest
412    public void testEqualCapabilitySim1Inserted() {
413        Phone slot0Phone = makeTestPhone(SLOT_0_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
414                false /*isEmergencyOnly*/);
415        Phone slot1Phone = makeTestPhone(SLOT_1_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
416                false /*isEmergencyOnly*/);
417        setDefaultPhone(slot0Phone);
418        setupDeviceConfig(slot0Phone, slot1Phone, SLOT_0_PHONE_ID);
419        setPhoneSlotState(SLOT_0_PHONE_ID, TelephonyManager.SIM_STATE_ABSENT);
420        setPhoneSlotState(SLOT_1_PHONE_ID, TelephonyManager.SIM_STATE_READY);
421        // Make Capability the same
422        setPhoneRadioAccessFamily(slot0Phone, RadioAccessFamily.RAF_LTE);
423        setPhoneRadioAccessFamily(slot1Phone, RadioAccessFamily.RAF_LTE);
424        // Slot 1 has SIM inserted.
425        setSlotHasIccCard(SLOT_0_PHONE_ID, false /*isInserted*/);
426        setSlotHasIccCard(SLOT_1_PHONE_ID, true /*isInserted*/);
427
428        Phone resultPhone = mTestConnectionService.getFirstPhoneForEmergencyCall();
429
430        assertEquals(slot1Phone, resultPhone);
431    }
432
433    /**
434     * Prerequisites:
435     * - MSIM Device, no SIMs inserted
436     * - SIM 1 has the higher capability
437     *
438     * Result: getFirstPhoneForEmergencyCall returns the slot 1 phone, since it is a higher
439     * capability
440     */
441    @Test
442    @SmallTest
443    public void testSim1HigherCapabilityNoSimsInserted() {
444        Phone slot0Phone = makeTestPhone(SLOT_0_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
445                false /*isEmergencyOnly*/);
446        Phone slot1Phone = makeTestPhone(SLOT_1_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
447                false /*isEmergencyOnly*/);
448        setDefaultPhone(slot0Phone);
449        setupDeviceConfig(slot0Phone, slot1Phone, SLOT_0_PHONE_ID);
450        setPhoneSlotState(SLOT_0_PHONE_ID, TelephonyManager.SIM_STATE_ABSENT);
451        setPhoneSlotState(SLOT_1_PHONE_ID, TelephonyManager.SIM_STATE_ABSENT);
452        // Make Capability the same
453        setPhoneRadioAccessFamily(slot0Phone, RadioAccessFamily.RAF_GSM);
454        setPhoneRadioAccessFamily(slot1Phone, RadioAccessFamily.RAF_LTE);
455        // No SIMs inserted
456        setSlotHasIccCard(SLOT_0_PHONE_ID, false /*isInserted*/);
457        setSlotHasIccCard(SLOT_1_PHONE_ID, false /*isInserted*/);
458
459        Phone resultPhone = mTestConnectionService.getFirstPhoneForEmergencyCall();
460
461        assertEquals(slot1Phone, resultPhone);
462    }
463
464    /**
465     * Prerequisites:
466     * - MSIM Device, no SIMs inserted
467     * - Both SIMs have the same capability (Unknown)
468     *
469     * Result: getFirstPhoneForEmergencyCall returns the slot 0 phone, since it is the first slot.
470     */
471    @Test
472    @SmallTest
473    public void testEqualCapabilityNoSimsInserted() {
474        Phone slot0Phone = makeTestPhone(SLOT_0_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
475                false /*isEmergencyOnly*/);
476        Phone slot1Phone = makeTestPhone(SLOT_1_PHONE_ID, ServiceState.STATE_OUT_OF_SERVICE,
477                false /*isEmergencyOnly*/);
478        setDefaultPhone(slot0Phone);
479        setupDeviceConfig(slot0Phone, slot1Phone, SLOT_0_PHONE_ID);
480        setPhoneSlotState(SLOT_0_PHONE_ID, TelephonyManager.SIM_STATE_ABSENT);
481        setPhoneSlotState(SLOT_1_PHONE_ID, TelephonyManager.SIM_STATE_ABSENT);
482        // Make Capability the same
483        setPhoneRadioAccessFamily(slot0Phone, RadioAccessFamily.RAF_UNKNOWN);
484        setPhoneRadioAccessFamily(slot1Phone, RadioAccessFamily.RAF_UNKNOWN);
485        // No SIMs inserted
486        setSlotHasIccCard(SLOT_0_PHONE_ID, false /*isInserted*/);
487        setSlotHasIccCard(SLOT_1_PHONE_ID, false /*isInserted*/);
488
489        Phone resultPhone = mTestConnectionService.getFirstPhoneForEmergencyCall();
490
491        assertEquals(slot0Phone, resultPhone);
492    }
493
494    private Phone makeTestPhone(int phoneId, int serviceState, boolean isEmergencyOnly) {
495        Phone phone = mock(Phone.class);
496        ServiceState testServiceState = new ServiceState();
497        testServiceState.setState(serviceState);
498        testServiceState.setEmergencyOnly(isEmergencyOnly);
499        when(phone.getServiceState()).thenReturn(testServiceState);
500        when(phone.getPhoneId()).thenReturn(phoneId);
501        return phone;
502    }
503
504    // Setup 2 SIM device
505    private void setupDeviceConfig(Phone slot0Phone, Phone slot1Phone, int defaultVoicePhoneId) {
506        when(mTelephonyManagerProxy.getPhoneCount()).thenReturn(2);
507        when(mSubscriptionManagerProxy.getDefaultVoicePhoneId()).thenReturn(defaultVoicePhoneId);
508        when(mPhoneFactoryProxy.getPhone(eq(SLOT_0_PHONE_ID))).thenReturn(slot0Phone);
509        when(mPhoneFactoryProxy.getPhone(eq(SLOT_1_PHONE_ID))).thenReturn(slot1Phone);
510    }
511
512    private void setPhoneRadioAccessFamily(Phone phone, int radioAccessFamily) {
513        when(phone.getRadioAccessFamily()).thenReturn(radioAccessFamily);
514    }
515
516    private void setPhoneSlotState(int slotId, int slotState) {
517        when(mSubscriptionManagerProxy.getSimStateForSlotIdx(slotId)).thenReturn(slotState);
518    }
519
520    private void setSlotHasIccCard(int slotId, boolean isInserted) {
521        when(mTelephonyManagerProxy.hasIccCard(slotId)).thenReturn(isInserted);
522    }
523
524    private void setDefaultPhone(Phone phone) {
525        when(mPhoneFactoryProxy.getDefaultPhone()).thenReturn(phone);
526    }
527}
528