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