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