1 /* 2 * Copyright (C) 2017 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.screenlock; 18 19 import static com.google.common.truth.Truth.assertThat; 20 import static org.mockito.Mockito.verify; 21 import static org.mockito.Mockito.when; 22 23 import android.app.admin.DevicePolicyManager; 24 import android.content.Context; 25 import android.support.v14.preference.SwitchPreference; 26 27 import com.android.internal.widget.LockPatternUtils; 28 import com.android.settings.R; 29 import com.android.settings.security.trustagent.TrustAgentManager; 30 import com.android.settings.testutils.FakeFeatureFactory; 31 import com.android.settings.testutils.SettingsRobolectricTestRunner; 32 33 import org.junit.Before; 34 import org.junit.Test; 35 import org.junit.runner.RunWith; 36 import org.mockito.Mock; 37 import org.mockito.MockitoAnnotations; 38 import org.robolectric.RuntimeEnvironment; 39 40 @RunWith(SettingsRobolectricTestRunner.class) 41 public class PowerButtonInstantLockPreferenceControllerTest { 42 43 private static final int TEST_USER_ID = 0; 44 45 @Mock 46 private LockPatternUtils mLockPatternUtils; 47 @Mock 48 private TrustAgentManager mTrustAgentManager; 49 50 private Context mContext; 51 private PowerButtonInstantLockPreferenceController mController; 52 private SwitchPreference mPreference; 53 private FakeFeatureFactory mFeatureFactory; 54 55 @Before 56 public void setUp() { 57 MockitoAnnotations.initMocks(this); 58 mContext = RuntimeEnvironment.application; 59 mFeatureFactory = FakeFeatureFactory.setupForTest(); 60 when(mFeatureFactory.securityFeatureProvider.getTrustAgentManager()) 61 .thenReturn(mTrustAgentManager); 62 63 mPreference = new SwitchPreference(mContext); 64 mController = new PowerButtonInstantLockPreferenceController( 65 mContext, TEST_USER_ID, mLockPatternUtils); 66 } 67 68 @Test 69 public void isAvailable_lockSetToPattern_shouldReturnTrue() { 70 when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(true); 71 when(mLockPatternUtils.getKeyguardStoredPasswordQuality(TEST_USER_ID)) 72 .thenReturn(DevicePolicyManager.PASSWORD_QUALITY_SOMETHING); 73 74 assertThat(mController.isAvailable()).isTrue(); 75 } 76 77 @Test 78 public void isAvailable_lockSetToPin_shouldReturnTrue() { 79 when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(true); 80 when(mLockPatternUtils.getKeyguardStoredPasswordQuality(TEST_USER_ID)) 81 .thenReturn(DevicePolicyManager.PASSWORD_QUALITY_NUMERIC); 82 83 assertThat(mController.isAvailable()).isTrue(); 84 } 85 86 @Test 87 public void isAvailable_lockSetToPassword_shouldReturnTrue() { 88 when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(true); 89 when(mLockPatternUtils.getKeyguardStoredPasswordQuality(TEST_USER_ID)) 90 .thenReturn(DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC); 91 92 assertThat(mController.isAvailable()).isTrue(); 93 } 94 95 @Test 96 public void isAvailable_lockSetToNone_shouldReturnFalse() { 97 when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(false); 98 99 assertThat(mController.isAvailable()).isFalse(); 100 } 101 102 @Test 103 public void updateState_shouldSetPref() { 104 final String fakeTrustAgent = "trust_agent"; 105 when(mTrustAgentManager.getActiveTrustAgentLabel(mContext, mLockPatternUtils)) 106 .thenReturn(fakeTrustAgent); 107 when(mLockPatternUtils.getPowerButtonInstantlyLocks(TEST_USER_ID)).thenReturn(true); 108 mController.updateState(mPreference); 109 assertThat(mPreference.isChecked()).isTrue(); 110 assertThat(mPreference.getSummary()).isEqualTo(mContext.getString( 111 R.string.lockpattern_settings_power_button_instantly_locks_summary, 112 fakeTrustAgent)); 113 114 when(mTrustAgentManager.getActiveTrustAgentLabel(mContext, mLockPatternUtils)) 115 .thenReturn(null); 116 when(mLockPatternUtils.getPowerButtonInstantlyLocks(TEST_USER_ID)).thenReturn(false); 117 mController.updateState(mPreference); 118 assertThat(mPreference.isChecked()).isFalse(); 119 assertThat(mPreference.getSummary()).isEqualTo(mContext.getString( 120 R.string.summary_placeholder)); 121 } 122 123 @Test 124 public void onPreferenceChange_shouldUpdateLockPatternUtils() { 125 mController.onPreferenceChange(mPreference, true /* newValue */); 126 127 verify(mLockPatternUtils).setPowerButtonInstantlyLocks(true, TEST_USER_ID); 128 } 129 } 130