• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2022 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.phone;
18 
19 import static org.junit.Assert.assertEquals;
20 import static org.junit.Assert.assertTrue;
21 import static org.mockito.Mockito.eq;
22 import static org.mockito.Mockito.when;
23 
24 import android.content.ComponentName;
25 import android.os.UserHandle;
26 import android.telecom.PhoneAccountHandle;
27 import android.telephony.SubscriptionInfo;
28 import android.telephony.SubscriptionManager;
29 
30 import androidx.test.runner.AndroidJUnit4;
31 
32 import com.android.TelephonyTestBase;
33 import com.android.internal.telephony.GsmCdmaPhone;
34 import com.android.internal.telephony.Phone;
35 import com.android.internal.telephony.PhoneFactory;
36 
37 import org.junit.Before;
38 import org.junit.Test;
39 import org.junit.runner.RunWith;
40 import org.mockito.Mock;
41 import org.mockito.MockitoAnnotations;
42 
43 @RunWith(AndroidJUnit4.class)
44 public class PhoneUtilsTest extends TelephonyTestBase {
45     @Mock
46     private SubscriptionManager mMockSubscriptionManager;
47     @Mock
48     private SubscriptionInfo mMockSubscriptionInfo;
49     @Mock
50     private GsmCdmaPhone mMockPhone;
51 
52     private final int mPhoneAccountHandleIdInteger = 123;
53     private final String mPhoneAccountHandleIdString = "123";
54     private static final ComponentName PSTN_CONNECTION_SERVICE_COMPONENT = new ComponentName(
55             "com.android.phone", "com.android.services.telephony.TelephonyConnectionService");
56     private PhoneAccountHandle mPhoneAccountHandleTest = new PhoneAccountHandle(
57             PSTN_CONNECTION_SERVICE_COMPONENT, mPhoneAccountHandleIdString);
58 
59     @Before
setUp()60     public void setUp() throws Exception {
61         MockitoAnnotations.initMocks(this);
62         when(mMockSubscriptionManager.getActiveSubscriptionInfo(
63                 eq(mPhoneAccountHandleIdInteger))).thenReturn(mMockSubscriptionInfo);
64         when(mMockPhone.getSubId()).thenReturn(mPhoneAccountHandleIdInteger);
65         Phone[] mPhones = new Phone[] {mMockPhone};
66         replaceInstance(PhoneFactory.class, "sPhones", null, mPhones);
67     }
68 
69     @Test
testIsPhoneAccountActive()70     public void testIsPhoneAccountActive() throws Exception {
71         assertTrue(PhoneUtils.isPhoneAccountActive(
72                 mMockSubscriptionManager, mPhoneAccountHandleTest));
73     }
74 
75     @Test
testGetPhoneForPhoneAccountHandle()76     public void testGetPhoneForPhoneAccountHandle() throws Exception {
77         assertEquals(mMockPhone, PhoneUtils.getPhoneForPhoneAccountHandle(
78                 mPhoneAccountHandleTest));
79     }
80 
81     @Test
testMakePstnPhoneAccountHandleWithPrefix()82     public void testMakePstnPhoneAccountHandleWithPrefix() throws Exception {
83         PhoneAccountHandle phoneAccountHandleTest = new PhoneAccountHandle(
84                 PSTN_CONNECTION_SERVICE_COMPONENT, mPhoneAccountHandleIdString);
85         assertEquals(phoneAccountHandleTest, PhoneUtils.makePstnPhoneAccountHandleWithId(
86                 mPhoneAccountHandleIdString, null));
87     }
88 
89     @Test
testMakePstnPhoneAccountHandleWithPrefixForAnotherUser()90     public void testMakePstnPhoneAccountHandleWithPrefixForAnotherUser() throws Exception {
91         UserHandle userHandle = new UserHandle(10);
92         PhoneAccountHandle phoneAccountHandleTest = new PhoneAccountHandle(
93                 PSTN_CONNECTION_SERVICE_COMPONENT, mPhoneAccountHandleIdString, userHandle);
94         assertEquals(phoneAccountHandleTest, PhoneUtils.makePstnPhoneAccountHandleWithId(
95                 mPhoneAccountHandleIdString, userHandle));
96     }
97 }
98