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 com.google.common.truth.Truth.assertThat; 20 21 import static org.mockito.ArgumentMatchers.anyInt; 22 import static org.mockito.ArgumentMatchers.anyString; 23 import static org.mockito.Mockito.doReturn; 24 import static org.mockito.Mockito.spy; 25 import static org.mockito.Mockito.when; 26 27 import android.app.admin.DevicePolicyManager; 28 import android.content.Context; 29 import android.os.UserManager; 30 import android.view.LayoutInflater; 31 import android.view.View; 32 33 import androidx.preference.PreferenceScreen; 34 import androidx.preference.PreferenceViewHolder; 35 36 import com.android.internal.widget.LockPatternUtils; 37 import com.android.settings.R; 38 import com.android.settings.testutils.FakeFeatureFactory; 39 import com.android.settings.testutils.shadow.ShadowUtils; 40 import com.android.settings.widget.GearPreference; 41 42 import org.junit.Before; 43 import org.junit.Test; 44 import org.junit.runner.RunWith; 45 import org.mockito.Mock; 46 import org.mockito.MockitoAnnotations; 47 import org.robolectric.RobolectricTestRunner; 48 import org.robolectric.RuntimeEnvironment; 49 import org.robolectric.annotation.Config; 50 51 @RunWith(RobolectricTestRunner.class) 52 @Config(shadows = ShadowUtils.class) 53 public class ChangeScreenLockPreferenceControllerTest { 54 55 @Mock 56 private LockPatternUtils mLockPatternUtils; 57 @Mock 58 private UserManager mUserManager; 59 @Mock 60 private DevicePolicyManager mDevicePolicyManager; 61 @Mock 62 private PreferenceScreen mPreferenceScreen; 63 64 private Context mContext; 65 private FakeFeatureFactory mFeatureFactory; 66 private ChangeScreenLockPreferenceController mController; 67 private View mGearView; 68 private GearPreference mGearPreference; 69 private PreferenceViewHolder mPreferenceViewHolder; 70 71 @Before setUp()72 public void setUp() { 73 MockitoAnnotations.initMocks(this); 74 mContext = spy(RuntimeEnvironment.application.getApplicationContext()); 75 mFeatureFactory = FakeFeatureFactory.setupForTest(); 76 when(mFeatureFactory.securityFeatureProvider.getLockPatternUtils(mContext)) 77 .thenReturn(mLockPatternUtils); 78 when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(mUserManager); 79 when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE)) 80 .thenReturn(mDevicePolicyManager); 81 mController = new ChangeScreenLockPreferenceController(mContext, null /* Host */ ); 82 } 83 84 @Test testDeviceAdministrators_byDefault_shouldBeShown()85 public void testDeviceAdministrators_byDefault_shouldBeShown() { 86 assertThat(mController.isAvailable()).isTrue(); 87 } 88 89 @Test 90 @Config(qualifiers = "mcc999") testDeviceAdministrators_ifDisabled_shouldNotBeShown()91 public void testDeviceAdministrators_ifDisabled_shouldNotBeShown() { 92 assertThat(mController.isAvailable()).isFalse(); 93 } 94 95 @Test updateState_notSecureDisableKeyguard_shouldNotShowGear()96 public void updateState_notSecureDisableKeyguard_shouldNotShowGear() { 97 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false); 98 when(mLockPatternUtils.isLockScreenDisabled(anyInt())).thenReturn(true); 99 mockGearPreferenceAndViewHolder(); 100 101 showPreference(); 102 103 assertThat(mGearView.getVisibility()).isEqualTo(View.GONE); 104 } 105 106 @Test updateState_notSecureDisableKeyguard_summaryShouldShowOff()107 public void updateState_notSecureDisableKeyguard_summaryShouldShowOff() { 108 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false); 109 when(mLockPatternUtils.isLockScreenDisabled(anyInt())).thenReturn(true); 110 mockGearPreferenceAndViewHolder(); 111 112 showPreference(); 113 114 assertThat(mGearPreference.getSummary()) 115 .isEqualTo(mContext.getString(R.string.unlock_set_unlock_mode_off)); 116 } 117 118 @Test updateState_notSecureWithSwipeKeyguard_shouldNotShowGear()119 public void updateState_notSecureWithSwipeKeyguard_shouldNotShowGear() { 120 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false); 121 when(mLockPatternUtils.isLockScreenDisabled(anyInt())).thenReturn(false); 122 mockGearPreferenceAndViewHolder(); 123 124 showPreference(); 125 126 assertThat(mGearView.getVisibility()).isEqualTo(View.GONE); 127 } 128 129 @Test updateState_notSecureWithSwipeKeyguard_summaryShouldShowSwipe()130 public void updateState_notSecureWithSwipeKeyguard_summaryShouldShowSwipe() { 131 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false); 132 when(mLockPatternUtils.isLockScreenDisabled(anyInt())).thenReturn(false); 133 mockGearPreferenceAndViewHolder(); 134 135 showPreference(); 136 137 assertThat(mGearPreference.getSummary()) 138 .isEqualTo(mContext.getString(R.string.unlock_set_unlock_mode_none)); 139 } 140 141 @Test updateState_secureWithPinKeyguard_shouldShowGear()142 public void updateState_secureWithPinKeyguard_shouldShowGear() { 143 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true); 144 when(mLockPatternUtils.isLockScreenDisabled(anyInt())).thenReturn(false); 145 doReturn(DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX).when(mLockPatternUtils) 146 .getKeyguardStoredPasswordQuality(anyInt()); 147 mockGearPreferenceAndViewHolder(); 148 149 showPreference(); 150 151 assertThat(mGearView.getVisibility()).isEqualTo(View.VISIBLE); 152 } 153 154 @Test updateState_secureWithPinKeyguard_summaryShouldShowPin()155 public void updateState_secureWithPinKeyguard_summaryShouldShowPin() { 156 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true); 157 when(mLockPatternUtils.isLockScreenDisabled(anyInt())).thenReturn(false); 158 doReturn(DevicePolicyManager.PASSWORD_QUALITY_NUMERIC_COMPLEX).when(mLockPatternUtils) 159 .getKeyguardStoredPasswordQuality(anyInt()); 160 161 mockGearPreferenceAndViewHolder(); 162 163 showPreference(); 164 165 assertThat(mGearPreference.getSummary()) 166 .isEqualTo(mContext.getString(R.string.unlock_set_unlock_mode_pin)); 167 } 168 169 @Test updateState_secureWithPasswordKeyguard_shouldShowGear()170 public void updateState_secureWithPasswordKeyguard_shouldShowGear() { 171 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true); 172 when(mLockPatternUtils.isLockScreenDisabled(anyInt())).thenReturn(false); 173 doReturn(DevicePolicyManager.PASSWORD_QUALITY_COMPLEX).when(mLockPatternUtils) 174 .getKeyguardStoredPasswordQuality(anyInt()); 175 mockGearPreferenceAndViewHolder(); 176 177 showPreference(); 178 179 assertThat(mGearView.getVisibility()).isEqualTo(View.VISIBLE); 180 } 181 182 @Test updateState_secureWithPasswordKeyguard_summaryShouldShowPassword()183 public void updateState_secureWithPasswordKeyguard_summaryShouldShowPassword() { 184 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true); 185 when(mLockPatternUtils.isLockScreenDisabled(anyInt())).thenReturn(false); 186 doReturn(DevicePolicyManager.PASSWORD_QUALITY_COMPLEX).when(mLockPatternUtils) 187 .getKeyguardStoredPasswordQuality(anyInt()); 188 mockGearPreferenceAndViewHolder(); 189 190 showPreference(); 191 192 assertThat(mGearPreference.getSummary()) 193 .isEqualTo(mContext.getString(R.string.unlock_set_unlock_mode_password)); 194 } 195 196 @Test updateState_secureWithPatternKeyguard_shouldShowGear()197 public void updateState_secureWithPatternKeyguard_shouldShowGear() { 198 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true); 199 when(mLockPatternUtils.isLockScreenDisabled(anyInt())).thenReturn(false); 200 doReturn(DevicePolicyManager.PASSWORD_QUALITY_SOMETHING).when(mLockPatternUtils) 201 .getKeyguardStoredPasswordQuality(anyInt()); 202 mockGearPreferenceAndViewHolder(); 203 204 showPreference(); 205 206 assertThat(mGearView.getVisibility()).isEqualTo(View.VISIBLE); 207 } 208 209 @Test updateState_secureWithPatternKeyguard_summaryShouldShowPattern()210 public void updateState_secureWithPatternKeyguard_summaryShouldShowPattern() { 211 when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true); 212 when(mLockPatternUtils.isLockScreenDisabled(anyInt())).thenReturn(false); 213 doReturn(DevicePolicyManager.PASSWORD_QUALITY_SOMETHING).when(mLockPatternUtils) 214 .getKeyguardStoredPasswordQuality(anyInt()); 215 mockGearPreferenceAndViewHolder(); 216 217 showPreference(); 218 219 assertThat(mGearPreference.getSummary()) 220 .isEqualTo(mContext.getString(R.string.unlock_set_unlock_mode_pattern)); 221 } 222 mockGearPreferenceAndViewHolder()223 private void mockGearPreferenceAndViewHolder() { 224 mGearPreference = new GearPreference(mContext, null); 225 mGearView = new View(mContext); 226 PreferenceViewHolder viewHolder = PreferenceViewHolder.createInstanceForTests( 227 LayoutInflater.from(mContext).inflate( 228 mGearPreference.getLayoutResource(), null, false)); 229 mPreferenceViewHolder = spy(viewHolder); 230 doReturn(mGearView).when(mPreferenceViewHolder).findViewById(R.id.settings_button); 231 when(mPreferenceScreen.findPreference(anyString())).thenReturn(mGearPreference); 232 } 233 showPreference()234 private void showPreference() { 235 mController.displayPreference(mPreferenceScreen); 236 mController.updateState(mGearPreference); 237 mGearPreference.onBindViewHolder(mPreferenceViewHolder); 238 } 239 }