Home | History | Annotate | Download | only in screenlock
      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 android.provider.Settings.System.SCREEN_OFF_TIMEOUT;
     20 import static com.google.common.truth.Truth.assertThat;
     21 import static org.mockito.Mockito.mock;
     22 import static org.mockito.Mockito.spy;
     23 import static org.mockito.Mockito.verify;
     24 import static org.mockito.Mockito.when;
     25 
     26 import android.app.admin.DevicePolicyManager;
     27 import android.content.Context;
     28 import android.os.UserHandle;
     29 import android.os.UserManager;
     30 import android.provider.Settings;
     31 
     32 import com.android.internal.widget.LockPatternUtils;
     33 import com.android.settings.TimeoutListPreference;
     34 import com.android.settings.security.trustagent.TrustAgentManager;
     35 import com.android.settings.testutils.FakeFeatureFactory;
     36 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     37 import com.android.settings.testutils.shadow.ShadowDevicePolicyManager;
     38 
     39 import org.junit.Before;
     40 import org.junit.Test;
     41 import org.junit.runner.RunWith;
     42 import org.mockito.Mock;
     43 import org.mockito.MockitoAnnotations;
     44 import org.robolectric.RuntimeEnvironment;
     45 import org.robolectric.annotation.Config;
     46 
     47 import java.util.Collections;
     48 
     49 @RunWith(SettingsRobolectricTestRunner.class)
     50 @Config(shadows = ShadowDevicePolicyManager.class)
     51 public class LockAfterTimeoutPreferenceControllerTest {
     52 
     53     private static final int TEST_USER_ID = 0;
     54 
     55     @Mock
     56     private LockPatternUtils mLockPatternUtils;
     57     @Mock
     58     private TrustAgentManager mTrustAgentManager;
     59     @Mock
     60     private TimeoutListPreference mPreference;
     61 
     62     private Context mContext;
     63     private LockAfterTimeoutPreferenceController mController;
     64     private FakeFeatureFactory mFeatureFactory;
     65 
     66     @Before
     67     public void setUp() {
     68         MockitoAnnotations.initMocks(this);
     69         mContext = spy(RuntimeEnvironment.application);
     70         mFeatureFactory = FakeFeatureFactory.setupForTest();
     71         when(mFeatureFactory.securityFeatureProvider.getTrustAgentManager())
     72                 .thenReturn(mTrustAgentManager);
     73 
     74         mController = new LockAfterTimeoutPreferenceController(
     75                 mContext, TEST_USER_ID, mLockPatternUtils);
     76     }
     77 
     78     @Test
     79     public void isAvailable_lockSetToPattern_shouldReturnTrue() {
     80         when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(true);
     81         when(mLockPatternUtils.getKeyguardStoredPasswordQuality(TEST_USER_ID))
     82                 .thenReturn(DevicePolicyManager.PASSWORD_QUALITY_SOMETHING);
     83 
     84         assertThat(mController.isAvailable()).isTrue();
     85     }
     86 
     87     @Test
     88     public void isAvailable_lockSetToPin_shouldReturnTrue() {
     89         when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(true);
     90         when(mLockPatternUtils.getKeyguardStoredPasswordQuality(TEST_USER_ID))
     91                 .thenReturn(DevicePolicyManager.PASSWORD_QUALITY_NUMERIC);
     92 
     93         assertThat(mController.isAvailable()).isTrue();
     94     }
     95 
     96     @Test
     97     public void isAvailable_lockSetToPassword_shouldReturnTrue() {
     98         when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(true);
     99         when(mLockPatternUtils.getKeyguardStoredPasswordQuality(TEST_USER_ID))
    100                 .thenReturn(DevicePolicyManager.PASSWORD_QUALITY_ALPHANUMERIC);
    101 
    102         assertThat(mController.isAvailable()).isTrue();
    103     }
    104 
    105     @Test
    106     public void isAvailable_lockSetToNone_shouldReturnFalse() {
    107         when(mLockPatternUtils.isSecure(TEST_USER_ID)).thenReturn(false);
    108 
    109         assertThat(mController.isAvailable()).isFalse();
    110     }
    111 
    112     @Test
    113     public void testUpdateStateWithAdminTimeouts() {
    114         final int userId = UserHandle.myUserId();
    115         final long adminTimeout = 10000;
    116         final int displayTimeout = 5000;
    117 
    118         final UserManager um = mock(UserManager.class);
    119         when(mContext.getSystemService(Context.USER_SERVICE)).thenReturn(um);
    120         when(um.getProfiles(userId)).thenReturn(Collections.emptyList());
    121 
    122         // Fake list of timeout values.
    123         when(mPreference.getEntries()).thenReturn(new CharSequence[] {"10"});
    124         when(mPreference.getEntryValues()).thenReturn(new CharSequence[] {"10000"});
    125 
    126         Settings.System.putInt(mContext.getContentResolver(), SCREEN_OFF_TIMEOUT, displayTimeout);
    127         ShadowDevicePolicyManager.getShadow().setMaximumTimeToLock(userId, adminTimeout);
    128 
    129         mController.updateState(mPreference);
    130 
    131         verify(mPreference).removeUnusableTimeouts(adminTimeout - displayTimeout, null);
    132     }
    133 }
    134