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 private Context mContext; 69 70 @Before setUp()71 public void setUp() { 72 MockitoAnnotations.initMocks(this); 73 ShadowApplication shadowApplication = ShadowApplication.getInstance(); 74 shadowApplication.setSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE, 75 mSubscriptionManager); 76 shadowApplication.setSystemService(Context.CARRIER_CONFIG_SERVICE, mCarrierManager); 77 shadowApplication.setSystemService(Context.USER_SERVICE, mUserManager); 78 shadowApplication.setSystemService(Context.TELEPHONY_SERVICE, mTelephonyManager); 79 mContext = RuntimeEnvironment.application; 80 mController = new SimLockPreferenceController(mContext); 81 mPreference = new Preference(mContext); 82 mPreference.setKey(mController.getPreferenceKey()); 83 when(mScreen.findPreference(mController.getPreferenceKey())).thenReturn(mPreference); 84 } 85 86 @Test isAvailable_notAdmin_false()87 public void isAvailable_notAdmin_false() { 88 when(mUserManager.isAdminUser()).thenReturn(false); 89 90 assertThat(mController.getAvailabilityStatus()) 91 .isEqualTo(BasePreferenceController.DISABLED_FOR_USER); 92 } 93 94 @Test isAvailable_simIccNotReady_false()95 public void isAvailable_simIccNotReady_false() { 96 when(mUserManager.isAdminUser()).thenReturn(true); 97 98 assertThat(mController.getAvailabilityStatus()) 99 .isEqualTo(BasePreferenceController.DISABLED_FOR_USER); 100 } 101 102 @Test isAvailable_carrierConfigDisabled_false()103 public void isAvailable_carrierConfigDisabled_false() { 104 when(mUserManager.isAdminUser()).thenReturn(true); 105 setupMockIcc(); 106 final PersistableBundle pb = new PersistableBundle(); 107 pb.putBoolean(CarrierConfigManager.KEY_HIDE_SIM_LOCK_SETTINGS_BOOL, true); 108 when(mCarrierManager.getConfigForSubId(anyInt())).thenReturn(pb); 109 110 assertThat(mController.getAvailabilityStatus()) 111 .isEqualTo(BasePreferenceController.DISABLED_FOR_USER); 112 } 113 114 @Test isAvailable_true()115 public void isAvailable_true() { 116 when(mUserManager.isAdminUser()).thenReturn(true); 117 setupMockIcc(); 118 final PersistableBundle pb = new PersistableBundle(); 119 when(mCarrierManager.getConfigForSubId(anyInt())).thenReturn(pb); 120 121 assertThat(mController.getAvailabilityStatus()) 122 .isEqualTo(BasePreferenceController.AVAILABLE); 123 } 124 125 @Test displayPreference_simReady_enablePreference()126 public void displayPreference_simReady_enablePreference() { 127 mController.displayPreference(mScreen); 128 129 assertThat(mPreference.isEnabled()).isFalse(); 130 } 131 132 @Test displayPreference_simNotReady_disablePreference()133 public void displayPreference_simNotReady_disablePreference() { 134 setupMockSimReady(); 135 136 mController.displayPreference(mScreen); 137 138 assertThat(mPreference.isEnabled()).isTrue(); 139 } 140 141 @Test getPreferenceKey_byDefault_returnsDefaultValue()142 public void getPreferenceKey_byDefault_returnsDefaultValue() { 143 assertThat(mController.getPreferenceKey()).isEqualTo("sim_lock_settings"); 144 } 145 146 @Test getPreferenceKey_whenGivenValue_returnsGivenValue()147 public void getPreferenceKey_whenGivenValue_returnsGivenValue() { 148 mController = new SimLockPreferenceController(mContext, "key"); 149 150 assertThat(mController.getPreferenceKey()).isEqualTo("key"); 151 } 152 setupMockIcc()153 private void setupMockIcc() { 154 final List<SubscriptionInfo> subscriptionInfoList = new ArrayList<>(); 155 SubscriptionInfo info = mock(SubscriptionInfo.class); 156 subscriptionInfoList.add(info); 157 when(mTelephonyManager.createForSubscriptionId(anyInt())).thenReturn(mTelephonyManager); 158 when(mTelephonyManager.hasIccCard()).thenReturn(true); 159 when(mSubscriptionManager.getActiveSubscriptionInfoList()) 160 .thenReturn(subscriptionInfoList); 161 } 162 setupMockSimReady()163 private void setupMockSimReady() { 164 final List<SubscriptionInfo> subscriptionInfoList = new ArrayList<>(); 165 SubscriptionInfo info = mock(SubscriptionInfo.class); 166 subscriptionInfoList.add(info); 167 when(mTelephonyManager.getSimState(anyInt())).thenReturn(SIM_STATE_READY); 168 when(mSubscriptionManager.getActiveSubscriptionInfoList()) 169 .thenReturn(subscriptionInfoList); 170 } 171 } 172