• 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 junit.framework.Assert.assertFalse;
20 import static junit.framework.Assert.assertTrue;
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.support.v7.preference.Preference;
29 import android.support.v7.preference.PreferenceScreen;
30 import android.telephony.SubscriptionInfo;
31 import android.telephony.TelephonyManager;
32 
33 import com.android.settings.R;
34 import com.android.settings.testutils.SettingsRobolectricTestRunner;
35 
36 import org.junit.Before;
37 import org.junit.Test;
38 import org.junit.runner.RunWith;
39 import org.mockito.Mock;
40 import org.mockito.MockitoAnnotations;
41 import org.robolectric.RuntimeEnvironment;
42 import org.robolectric.util.ReflectionHelpers;
43 
44 @RunWith(SettingsRobolectricTestRunner.class)
45 public class PhoneNumberPreferenceControllerTest {
46 
47     @Mock
48     private Preference mPreference;
49     @Mock
50     private Preference mSecondPreference;
51     @Mock
52     private TelephonyManager mTelephonyManager;
53     @Mock
54     private SubscriptionInfo mSubscriptionInfo;
55     @Mock
56     private PreferenceScreen mScreen;
57 
58     private Context mContext;
59     private PhoneNumberPreferenceController mController;
60 
61     @Before
setup()62     public void setup() {
63         MockitoAnnotations.initMocks(this);
64         mContext = RuntimeEnvironment.application;
65         mController = spy(new PhoneNumberPreferenceController(mContext));
66         ReflectionHelpers.setField(mController, "mTelephonyManager", mTelephonyManager);
67         final String prefKey = mController.getPreferenceKey();
68         when(mScreen.findPreference(prefKey)).thenReturn(mPreference);
69         when(mScreen.getContext()).thenReturn(mContext);
70         doReturn(mSubscriptionInfo).when(mController).getSubscriptionInfo(anyInt());
71         doReturn(mSecondPreference).when(mController).createNewPreference(mContext);
72         when(mPreference.isVisible()).thenReturn(true);
73     }
74 
75     @Test
isAvailable_shouldBeTrueIfCallCapable()76     public void isAvailable_shouldBeTrueIfCallCapable() {
77         when(mTelephonyManager.isVoiceCapable()).thenReturn(true);
78 
79         assertTrue(mController.isAvailable());
80     }
81 
82     @Test
isAvailable_shouldBeFalseIfNotCallCapable()83     public void isAvailable_shouldBeFalseIfNotCallCapable() {
84         when(mTelephonyManager.isVoiceCapable()).thenReturn(false);
85 
86         assertFalse(mController.isAvailable());
87     }
88 
89     @Test
displayPreference_multiSim_shouldAddSecondPreference()90     public void displayPreference_multiSim_shouldAddSecondPreference() {
91         when(mTelephonyManager.getPhoneCount()).thenReturn(2);
92 
93         mController.displayPreference(mScreen);
94 
95         verify(mScreen).addPreference(mSecondPreference);
96     }
97 
98     @Test
updateState_singleSim_shouldUpdateTitleAndPhoneNumber()99     public void updateState_singleSim_shouldUpdateTitleAndPhoneNumber() {
100         final String phoneNumber = "1111111111";
101         doReturn(phoneNumber).when(mController).getFormattedPhoneNumber(mSubscriptionInfo);
102         when(mTelephonyManager.getPhoneCount()).thenReturn(1);
103         mController.displayPreference(mScreen);
104 
105         mController.updateState(mPreference);
106 
107         verify(mPreference).setTitle(mContext.getString(R.string.status_number));
108         verify(mPreference).setSummary(phoneNumber);
109     }
110 
111     @Test
updateState_multiSim_shouldUpdateTitleAndPhoneNumberOfMultiplePreferences()112     public void updateState_multiSim_shouldUpdateTitleAndPhoneNumberOfMultiplePreferences() {
113         final String phoneNumber = "1111111111";
114         doReturn(phoneNumber).when(mController).getFormattedPhoneNumber(mSubscriptionInfo);
115         when(mTelephonyManager.getPhoneCount()).thenReturn(2);
116         mController.displayPreference(mScreen);
117 
118         mController.updateState(mPreference);
119 
120         verify(mPreference).setTitle(
121                 mContext.getString(R.string.status_number_sim_slot, 1 /* sim slot */));
122         verify(mPreference).setSummary(phoneNumber);
123         verify(mSecondPreference).setTitle(
124                 mContext.getString(R.string.status_number_sim_slot, 2 /* sim slot */));
125         verify(mSecondPreference).setSummary(phoneNumber);
126     }
127 }
128