• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2018 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.security;
18 
19 import static android.telephony.TelephonyManager.SIM_STATE_READY;
20 
21 import static com.google.common.truth.Truth.assertThat;
22 
23 import static org.mockito.ArgumentMatchers.anyInt;
24 import static org.mockito.Mockito.mock;
25 import static org.mockito.Mockito.spy;
26 import static org.mockito.Mockito.when;
27 
28 import android.content.Context;
29 import android.content.res.Resources;
30 import android.os.PersistableBundle;
31 import android.os.UserManager;
32 import android.telephony.CarrierConfigManager;
33 import android.telephony.SubscriptionInfo;
34 import android.telephony.SubscriptionManager;
35 import android.telephony.TelephonyManager;
36 
37 import androidx.preference.Preference;
38 import androidx.preference.PreferenceScreen;
39 
40 import com.android.settings.R;
41 import com.android.settings.core.BasePreferenceController;
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 import org.robolectric.RobolectricTestRunner;
49 import org.robolectric.RuntimeEnvironment;
50 import org.robolectric.shadows.ShadowApplication;
51 
52 import java.util.ArrayList;
53 import java.util.List;
54 
55 @RunWith(RobolectricTestRunner.class)
56 public class SimLockPreferenceControllerTest {
57 
58     @Mock
59     private SubscriptionManager mSubscriptionManager;
60     @Mock
61     private CarrierConfigManager mCarrierManager;
62     @Mock
63     private UserManager mUserManager;
64     @Mock
65     private TelephonyManager mTelephonyManager;
66     @Mock
67     private PreferenceScreen mScreen;
68 
69     private SimLockPreferenceController mController;
70     private Preference mPreference;
71     private Context mContext;
72     private Resources mResources;
73 
74     @Before
setUp()75     public void setUp() {
76         MockitoAnnotations.initMocks(this);
77         ShadowApplication shadowApplication = ShadowApplication.getInstance();
78         shadowApplication.setSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE,
79                 mSubscriptionManager);
80         when(mSubscriptionManager.createForAllUserProfiles()).thenReturn(mSubscriptionManager);
81         shadowApplication.setSystemService(Context.CARRIER_CONFIG_SERVICE, mCarrierManager);
82         shadowApplication.setSystemService(Context.USER_SERVICE, mUserManager);
83         shadowApplication.setSystemService(Context.TELEPHONY_SERVICE, mTelephonyManager);
84         mContext = spy(RuntimeEnvironment.application);
85 
86         mResources = spy(mContext.getResources());
87         when(mContext.getResources()).thenReturn(mResources);
88 
89         mController = new SimLockPreferenceController(mContext, "key");
90         mPreference = new Preference(mContext);
91         mPreference.setKey(mController.getPreferenceKey());
92         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
93     }
94 
95     @Test
isAvailable_notShowSimUi_false()96     public void isAvailable_notShowSimUi_false() {
97         when(mResources.getBoolean(R.bool.config_show_sim_info)).thenReturn(false);
98 
99         assertThat(mController.getAvailabilityStatus())
100                 .isEqualTo(BasePreferenceController.UNSUPPORTED_ON_DEVICE);
101     }
102 
103     @Test
isAvailable_notAdmin_false()104     public void isAvailable_notAdmin_false() {
105         when(mResources.getBoolean(R.bool.config_show_sim_info)).thenReturn(true);
106         when(mUserManager.isAdminUser()).thenReturn(false);
107 
108         assertThat(mController.getAvailabilityStatus())
109                 .isEqualTo(BasePreferenceController.DISABLED_FOR_USER);
110     }
111 
112     @Test
isAvailable_simIccNotReady_false()113     public void isAvailable_simIccNotReady_false() {
114         when(mResources.getBoolean(R.bool.config_show_sim_info)).thenReturn(true);
115         when(mUserManager.isAdminUser()).thenReturn(true);
116 
117         assertThat(mController.getAvailabilityStatus())
118                 .isEqualTo(BasePreferenceController.DISABLED_FOR_USER);
119     }
120 
121     @Test
isAvailable_carrierConfigDisabled_false()122     public void isAvailable_carrierConfigDisabled_false() {
123         when(mUserManager.isAdminUser()).thenReturn(true);
124         setupMockIcc();
125         final PersistableBundle pb = new PersistableBundle();
126         pb.putBoolean(CarrierConfigManager.KEY_HIDE_SIM_LOCK_SETTINGS_BOOL, true);
127         when(mCarrierManager.getConfigForSubId(anyInt())).thenReturn(pb);
128 
129         assertThat(mController.getAvailabilityStatus())
130                 .isEqualTo(BasePreferenceController.DISABLED_FOR_USER);
131     }
132 
133     @Test
isAvailable_true()134     public void isAvailable_true() {
135         when(mUserManager.isAdminUser()).thenReturn(true);
136         setupMockIcc();
137         final PersistableBundle pb = new PersistableBundle();
138         when(mCarrierManager.getConfigForSubId(anyInt())).thenReturn(pb);
139 
140         assertThat(mController.getAvailabilityStatus())
141                 .isEqualTo(BasePreferenceController.AVAILABLE);
142     }
143 
144     @Test
displayPreference_simReady_enablePreference()145     public void displayPreference_simReady_enablePreference() {
146         when(mResources.getBoolean(R.bool.config_show_sim_info)).thenReturn(true);
147         mController.displayPreference(mScreen);
148 
149         assertThat(mPreference.isEnabled()).isFalse();
150     }
151 
152     @Test
displayPreference_simNotReady_disablePreference()153     public void displayPreference_simNotReady_disablePreference() {
154         setupMockSimReady();
155 
156         mController.displayPreference(mScreen);
157 
158         assertThat(mPreference.isEnabled()).isTrue();
159     }
160 
161     @Test
getPreferenceKey_whenGivenValue_returnsGivenValue()162     public void getPreferenceKey_whenGivenValue_returnsGivenValue() {
163         when(mResources.getBoolean(R.bool.config_show_sim_info)).thenReturn(true);
164         mController = new SimLockPreferenceController(mContext, "key");
165 
166         assertThat(mController.getPreferenceKey()).isEqualTo("key");
167     }
168 
setupMockIcc()169     private void setupMockIcc() {
170         when(mResources.getBoolean(R.bool.config_show_sim_info)).thenReturn(true);
171         final List<SubscriptionInfo> subscriptionInfoList = new ArrayList<>();
172         SubscriptionInfo info = mock(SubscriptionInfo.class);
173         subscriptionInfoList.add(info);
174         when(mTelephonyManager.createForSubscriptionId(anyInt())).thenReturn(mTelephonyManager);
175         when(mTelephonyManager.hasIccCard()).thenReturn(true);
176         when(mSubscriptionManager.getActiveSubscriptionInfoList())
177                 .thenReturn(subscriptionInfoList);
178     }
179 
setupMockSimReady()180     private void setupMockSimReady() {
181         when(mResources.getBoolean(R.bool.config_show_sim_info)).thenReturn(true);
182         final List<SubscriptionInfo> subscriptionInfoList = new ArrayList<>();
183         SubscriptionInfo info = mock(SubscriptionInfo.class);
184         subscriptionInfoList.add(info);
185         when(mTelephonyManager.getSimState(anyInt())).thenReturn(SIM_STATE_READY);
186         when(mSubscriptionManager.getActiveSubscriptionInfoList())
187                 .thenReturn(subscriptionInfoList);
188     }
189 }
190