• 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 import static com.google.common.truth.Truth.assertThat;
21 import static org.mockito.ArgumentMatchers.anyInt;
22 import static org.mockito.Mockito.mock;
23 import static org.mockito.Mockito.when;
24 
25 import android.content.Context;
26 import android.os.PersistableBundle;
27 import android.os.UserManager;
28 import android.support.v7.preference.Preference;
29 import android.support.v7.preference.PreferenceScreen;
30 import android.telephony.CarrierConfigManager;
31 import android.telephony.SubscriptionInfo;
32 import android.telephony.SubscriptionManager;
33 import android.telephony.TelephonyManager;
34 
35 import com.android.settings.core.BasePreferenceController;
36 import com.android.settings.testutils.SettingsRobolectricTestRunner;
37 
38 import org.junit.Before;
39 import org.junit.Test;
40 import org.junit.runner.RunWith;
41 import org.mockito.Mock;
42 import org.mockito.MockitoAnnotations;
43 import org.robolectric.RuntimeEnvironment;
44 import org.robolectric.shadows.ShadowApplication;
45 
46 import java.util.ArrayList;
47 import java.util.List;
48 
49 @RunWith(SettingsRobolectricTestRunner.class)
50 public class SimLockPreferenceControllerTest {
51 
52     @Mock
53     private SubscriptionManager mSubscriptionManager;
54     @Mock
55     private CarrierConfigManager mCarrierManager;
56     @Mock
57     private UserManager mUserManager;
58     @Mock
59     private TelephonyManager mTelephonyManager;
60     @Mock
61     private PreferenceScreen mScreen;
62 
63     private SimLockPreferenceController mController;
64     private Preference mPreference;
65 
66     @Before
setUp()67     public void setUp() {
68         MockitoAnnotations.initMocks(this);
69         ShadowApplication shadowApplication = ShadowApplication.getInstance();
70         shadowApplication.setSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE,
71                 mSubscriptionManager);
72         shadowApplication.setSystemService(Context.CARRIER_CONFIG_SERVICE, mCarrierManager);
73         shadowApplication.setSystemService(Context.USER_SERVICE, mUserManager);
74         shadowApplication.setSystemService(Context.TELEPHONY_SERVICE, mTelephonyManager);
75         mController = new SimLockPreferenceController(RuntimeEnvironment.application);
76         mPreference = new Preference(RuntimeEnvironment.application);
77         mPreference.setKey(mController.getPreferenceKey());
78         when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference);
79     }
80 
81     @Test
isAvailable_notAdmin_false()82     public void isAvailable_notAdmin_false() {
83         when(mUserManager.isAdminUser()).thenReturn(false);
84 
85         assertThat(mController.getAvailabilityStatus())
86                 .isEqualTo(BasePreferenceController.DISABLED_FOR_USER);
87     }
88 
89     @Test
isAvailable_simIccNotReady_false()90     public void isAvailable_simIccNotReady_false() {
91         when(mUserManager.isAdminUser()).thenReturn(true);
92 
93         assertThat(mController.getAvailabilityStatus())
94                 .isEqualTo(BasePreferenceController.DISABLED_FOR_USER);
95     }
96 
97     @Test
isAvailable_carrierConfigDisabled_false()98     public void isAvailable_carrierConfigDisabled_false() {
99         when(mUserManager.isAdminUser()).thenReturn(true);
100         setupMockIcc();
101         final PersistableBundle pb = new PersistableBundle();
102         pb.putBoolean(CarrierConfigManager.KEY_HIDE_SIM_LOCK_SETTINGS_BOOL, true);
103         when(mCarrierManager.getConfig()).thenReturn(pb);
104 
105         assertThat(mController.getAvailabilityStatus())
106                 .isEqualTo(BasePreferenceController.DISABLED_FOR_USER);
107     }
108 
109     @Test
isAvailable_true()110     public void isAvailable_true() {
111         when(mUserManager.isAdminUser()).thenReturn(true);
112         setupMockIcc();
113         final PersistableBundle pb = new PersistableBundle();
114         when(mCarrierManager.getConfig()).thenReturn(pb);
115 
116         assertThat(mController.getAvailabilityStatus())
117                 .isEqualTo(BasePreferenceController.AVAILABLE);
118     }
119 
120     @Test
displayPreference_simReady_enablePreference()121     public void displayPreference_simReady_enablePreference() {
122         mController.displayPreference(mScreen);
123 
124         assertThat(mPreference.isEnabled()).isFalse();
125     }
126 
127     @Test
displayPreference_simNotReady_disablePreference()128     public void displayPreference_simNotReady_disablePreference() {
129         setupMockSimReady();
130 
131         mController.displayPreference(mScreen);
132 
133         assertThat(mPreference.isEnabled()).isTrue();
134     }
135 
setupMockIcc()136     private void setupMockIcc() {
137         final List<SubscriptionInfo> subscriptionInfoList = new ArrayList<>();
138         SubscriptionInfo info = mock(SubscriptionInfo.class);
139         subscriptionInfoList.add(info);
140         when(mTelephonyManager.hasIccCard(anyInt())).thenReturn(true);
141         when(mSubscriptionManager.getActiveSubscriptionInfoList())
142                 .thenReturn(subscriptionInfoList);
143     }
144 
setupMockSimReady()145     private void setupMockSimReady() {
146         final List<SubscriptionInfo> subscriptionInfoList = new ArrayList<>();
147         SubscriptionInfo info = mock(SubscriptionInfo.class);
148         subscriptionInfoList.add(info);
149         when(mTelephonyManager.getSimState(anyInt())).thenReturn(SIM_STATE_READY);
150         when(mSubscriptionManager.getActiveSubscriptionInfoList())
151                 .thenReturn(subscriptionInfoList);
152     }
153 }
154