Home | History | Annotate | Download | only in screenlock
      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.screenlock;
     18 
     19 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_ALPHABETIC;
     20 import static android.app.admin.DevicePolicyManager.PASSWORD_QUALITY_UNSPECIFIED;
     21 import static android.arch.lifecycle.Lifecycle.Event.ON_RESUME;
     22 import static com.android.settings.core.BasePreferenceController.AVAILABLE;
     23 import static com.android.settings.core.BasePreferenceController.DISABLED_FOR_USER;
     24 import static com.google.common.truth.Truth.assertThat;
     25 import static org.mockito.ArgumentMatchers.anyInt;
     26 import static org.mockito.ArgumentMatchers.anyString;
     27 import static org.mockito.Mockito.when;
     28 
     29 import android.arch.lifecycle.LifecycleOwner;
     30 import android.content.Context;
     31 import android.os.UserManager;
     32 import android.support.v7.preference.Preference;
     33 import android.support.v7.preference.PreferenceScreen;
     34 
     35 import com.android.internal.widget.LockPatternUtils;
     36 import com.android.settings.testutils.FakeFeatureFactory;
     37 import com.android.settings.testutils.SettingsRobolectricTestRunner;
     38 import com.android.settingslib.core.lifecycle.Lifecycle;
     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.RuntimeEnvironment;
     46 import org.robolectric.shadows.ShadowApplication;
     47 
     48 @RunWith(SettingsRobolectricTestRunner.class)
     49 public class LockScreenPreferenceControllerTest {
     50 
     51     private static final int FAKE_PROFILE_USER_ID = 1234;
     52 
     53     @Mock
     54     private LockPatternUtils mLockPatternUtils;
     55     @Mock
     56     private UserManager mUm;
     57     @Mock
     58     private PreferenceScreen mScreen;
     59 
     60     private Lifecycle mLifecycle;
     61     private LifecycleOwner mLifecycleOwner;
     62     private FakeFeatureFactory mFeatureFactory;
     63     private Context mContext;
     64     private LockScreenPreferenceController mController;
     65     private Preference mPreference;
     66 
     67 
     68     @Before
     69     public void setUp() {
     70         MockitoAnnotations.initMocks(this);
     71         mContext = RuntimeEnvironment.application;
     72         ShadowApplication.getInstance().setSystemService(Context.USER_SERVICE, mUm);
     73 
     74         mFeatureFactory = FakeFeatureFactory.setupForTest();
     75         when(mFeatureFactory.securityFeatureProvider.getLockPatternUtils(mContext))
     76                 .thenReturn(mLockPatternUtils);
     77         when(mUm.getProfileIdsWithDisabled(anyInt())).thenReturn(new int[] {FAKE_PROFILE_USER_ID});
     78         mPreference = new Preference(mContext);
     79         when(mScreen.findPreference(anyString())).thenReturn(mPreference);
     80         mLifecycleOwner = () -> mLifecycle;
     81         mLifecycle = new Lifecycle(mLifecycleOwner);
     82         mController = new LockScreenPreferenceController(mContext, mLifecycle);
     83     }
     84 
     85     @Test
     86     public void getAvailabilityStatus_notSecure_lockscreenDisabled_DISABLED() {
     87         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false);
     88         when(mLockPatternUtils.isLockScreenDisabled(anyInt())).thenReturn(true);
     89 
     90         assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER);
     91     }
     92 
     93     @Test
     94     public void getAvailabilityStatus_notSecure_lockscreenEnabled_AVAILABLE() {
     95         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(false);
     96         when(mLockPatternUtils.isLockScreenDisabled(anyInt())).thenReturn(false);
     97 
     98         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
     99     }
    100 
    101     @Test
    102     public void getAvailabilityStatus_secure_hasLockScreen_AVAILABLE() {
    103         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
    104         when(mLockPatternUtils.getKeyguardStoredPasswordQuality(anyInt()))
    105                 .thenReturn(PASSWORD_QUALITY_ALPHABETIC);
    106 
    107         assertThat(mController.getAvailabilityStatus()).isEqualTo(AVAILABLE);
    108     }
    109 
    110     @Test
    111     public void getAvailabilityStatus_secure_noLockScreen_DISABLED() {
    112         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
    113         when(mLockPatternUtils.getKeyguardStoredPasswordQuality(anyInt()))
    114                 .thenReturn(PASSWORD_QUALITY_UNSPECIFIED);
    115 
    116         assertThat(mController.getAvailabilityStatus()).isEqualTo(DISABLED_FOR_USER);
    117     }
    118 
    119     @Test
    120     public void onResume_available_shouldShow() {
    121         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
    122         when(mLockPatternUtils.getKeyguardStoredPasswordQuality(anyInt()))
    123                 .thenReturn(PASSWORD_QUALITY_ALPHABETIC);
    124 
    125         mController.displayPreference(mScreen);
    126         mLifecycle.handleLifecycleEvent(ON_RESUME);
    127 
    128         assertThat(mPreference.isVisible()).isTrue();
    129     }
    130 
    131     @Test
    132     public void onResume_unavailable_shouldHide() {
    133         when(mLockPatternUtils.isSecure(anyInt())).thenReturn(true);
    134         when(mLockPatternUtils.getKeyguardStoredPasswordQuality(anyInt()))
    135                 .thenReturn(PASSWORD_QUALITY_UNSPECIFIED);
    136 
    137         mController.displayPreference(mScreen);
    138         mLifecycle.handleLifecycleEvent(ON_RESUME);
    139 
    140         assertThat(mPreference.isVisible()).isFalse();
    141     }
    142 }
    143