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