• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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.settings.deviceinfo;
18 
19 import static com.google.common.truth.Truth.assertThat;
20 
21 import static org.mockito.ArgumentMatchers.anyInt;
22 import static org.mockito.Mockito.doReturn;
23 import static org.mockito.Mockito.spy;
24 import static org.mockito.Mockito.verify;
25 import static org.mockito.Mockito.when;
26 
27 import android.content.Context;
28 import android.os.Looper;
29 import android.telephony.SubscriptionInfo;
30 import android.telephony.SubscriptionManager;
31 import android.telephony.TelephonyManager;
32 
33 import androidx.preference.Preference;
34 import androidx.preference.PreferenceCategory;
35 import androidx.preference.PreferenceManager;
36 import androidx.preference.PreferenceScreen;
37 import androidx.test.core.app.ApplicationProvider;
38 import androidx.test.ext.junit.runners.AndroidJUnit4;
39 
40 import com.android.settings.core.BasePreferenceController;
41 import com.android.settings.testutils.ResourcesUtils;
42 
43 import org.junit.Before;
44 import org.junit.Test;
45 import org.junit.runner.RunWith;
46 import org.mockito.Mock;
47 import org.mockito.MockitoAnnotations;
48 
49 import java.util.ArrayList;
50 import java.util.List;
51 
52 @RunWith(AndroidJUnit4.class)
53 public class PhoneNumberPreferenceControllerTest {
54 
55     private Preference mPreference;
56     @Mock
57     private Preference mSecondPreference;
58     @Mock
59     private TelephonyManager mTelephonyManager;
60     @Mock
61     private SubscriptionInfo mSubscriptionInfo;
62     @Mock
63     private SubscriptionManager mSubscriptionManager;
64     private PreferenceCategory mCategory;
65     private PreferenceScreen mScreen;
66 
67     private Context mContext;
68     private PhoneNumberPreferenceController mController;
69 
70     @Before
setup()71     public void setup() {
72         MockitoAnnotations.initMocks(this);
73         mContext = spy(ApplicationProvider.getApplicationContext());
74         when(mContext.getSystemService(SubscriptionManager.class)).thenReturn(mSubscriptionManager);
75         when(mContext.getSystemService(TelephonyManager.class)).thenReturn(mTelephonyManager);
76         mController = spy(new PhoneNumberPreferenceController(mContext, "phone_number"));
77 
78         if (Looper.myLooper() == null) {
79             Looper.prepare();
80         }
81         final PreferenceManager preferenceManager = new PreferenceManager(mContext);
82         mScreen = preferenceManager.createPreferenceScreen(mContext);
83         mPreference = spy(new Preference(mContext));
84         mPreference.setKey(mController.getPreferenceKey());
85         mPreference.setVisible(true);
86         mScreen.addPreference(mPreference);
87         final String categoryKey = "basic_info_category";
88         mCategory = new PreferenceCategory(mContext);
89         mCategory.setKey(categoryKey);
90         mScreen.addPreference(mCategory);
91 
92         doReturn(mSubscriptionInfo).when(mController).getSubscriptionInfo(anyInt());
93         doReturn(mSecondPreference).when(mController).createNewPreference(mContext);
94     }
95 
96     @Test
getAvailabilityStatus_isVoiceCapable_shouldBeAVAILABLE()97     public void getAvailabilityStatus_isVoiceCapable_shouldBeAVAILABLE() {
98         when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
99 
100         assertThat(mController.getAvailabilityStatus()).isEqualTo(
101                 BasePreferenceController.AVAILABLE);
102     }
103 
104     @Test
getAvailabilityStatus_isNotVoiceCapable_shouldBeUNSUPPORTED_ON_DEVICE()105     public void getAvailabilityStatus_isNotVoiceCapable_shouldBeUNSUPPORTED_ON_DEVICE() {
106         when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
107 
108         assertThat(mController.getAvailabilityStatus()).isEqualTo(
109                 BasePreferenceController.UNSUPPORTED_ON_DEVICE);
110     }
111 
112     @Test
displayPreference_multiSim_shouldAddSecondPreference()113     public void displayPreference_multiSim_shouldAddSecondPreference() {
114         when(mTelephonyManager.getPhoneCount()).thenReturn(2);
115 
116         final Preference sim1Preference = new Preference(mContext);
117         mCategory.addItemFromInflater(sim1Preference);
118         mController.displayPreference(mScreen);
119 
120         assertThat(mCategory.getPreferenceCount()).isEqualTo(2);
121     }
122 
123     @Test
updateState_singleSim_shouldUpdateTitleAndPhoneNumber()124     public void updateState_singleSim_shouldUpdateTitleAndPhoneNumber() {
125         final String phoneNumber = "1111111111";
126         doReturn(phoneNumber).when(mController).getFormattedPhoneNumber(mSubscriptionInfo);
127         when(mTelephonyManager.getPhoneCount()).thenReturn(1);
128         mController.displayPreference(mScreen);
129 
130         mController.updateState(mPreference);
131 
132         verify(mPreference).setTitle(ResourcesUtils.getResourcesString(mContext, "status_number"));
133         verify(mPreference).setSummary(phoneNumber);
134     }
135 
136     @Test
updateState_multiSim_shouldUpdateTitleAndPhoneNumberOfMultiplePreferences()137     public void updateState_multiSim_shouldUpdateTitleAndPhoneNumberOfMultiplePreferences() {
138         final String phoneNumber = "1111111111";
139         doReturn(phoneNumber).when(mController).getFormattedPhoneNumber(mSubscriptionInfo);
140         when(mTelephonyManager.getPhoneCount()).thenReturn(2);
141         mController.displayPreference(mScreen);
142 
143         mController.updateState(mPreference);
144 
145         verify(mPreference).setTitle(ResourcesUtils.getResourcesString(
146                 mContext, "status_number_sim_slot", 1 /* sim slot */));
147         verify(mPreference).setSummary(phoneNumber);
148         verify(mSecondPreference).setTitle(ResourcesUtils.getResourcesString(
149                 mContext, "status_number_sim_slot", 2 /* sim slot */));
150         verify(mSecondPreference).setSummary(phoneNumber);
151     }
152 
153     @Test
getSummary_cannotGetActiveSubscriptionInfo_shouldShowUnknown()154     public void getSummary_cannotGetActiveSubscriptionInfo_shouldShowUnknown() {
155         when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(null);
156 
157         CharSequence primaryNumber = mController.getSummary();
158 
159         assertThat(primaryNumber).isNotNull();
160         assertThat(primaryNumber).isEqualTo(ResourcesUtils.getResourcesString(
161                 mContext, "device_info_default"));
162     }
163 
164     @Test
getSummary_getEmptySubscriptionInfo_shouldShowUnknown()165     public void getSummary_getEmptySubscriptionInfo_shouldShowUnknown() {
166         List<SubscriptionInfo> infos = new ArrayList<>();
167         when(mSubscriptionManager.getActiveSubscriptionInfoList()).thenReturn(infos);
168 
169         CharSequence primaryNumber = mController.getSummary();
170 
171         assertThat(primaryNumber).isEqualTo(ResourcesUtils.getResourcesString(
172                 mContext, "device_info_default"));
173     }
174 }
175