RestrictedLockUtilsTest.java revision fdfc88faef4d722b7ae09d8acd7a57a511b3eb72
1/* 2 * Copyright (C) 2016 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 17package com.android.settingslib; 18 19import android.app.admin.DevicePolicyManager; 20import android.content.ComponentName; 21import android.content.Context; 22import android.content.pm.UserInfo; 23import android.os.UserManager; 24 25import org.junit.Before; 26import org.junit.Test; 27import org.junit.runner.RunWith; 28import org.mockito.Mock; 29import org.mockito.MockitoAnnotations; 30import org.robolectric.annotation.Config; 31 32import java.util.Arrays; 33 34import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT; 35import static android.app.admin.DevicePolicyManager.KEYGUARD_DISABLE_REMOTE_INPUT; 36import static com.android.settingslib.RestrictedLockUtils.EnforcedAdmin; 37import static com.google.common.truth.Truth.assertThat; 38import static org.mockito.Mockito.when; 39 40@RunWith(SettingLibRobolectricTestRunner.class) 41@Config(manifest = TestConfig.MANIFEST_PATH, sdk = TestConfig.SDK_VERSION) 42public class RestrictedLockUtilsTest { 43 44 @Mock 45 private Context mContext; 46 @Mock 47 private DevicePolicyManager mDevicePolicyManager; 48 @Mock 49 private UserManager mUserManager; 50 51 private static final int mUserId = 194; 52 private static final ComponentName mAdmin1 = new ComponentName("admin1", "admin1class"); 53 private static final ComponentName mAdmin2 = new ComponentName("admin2", "admin2class"); 54 55 @Before 56 public void setUp() { 57 MockitoAnnotations.initMocks(this); 58 59 when(mContext.getSystemService(Context.DEVICE_POLICY_SERVICE)) 60 .thenReturn(mDevicePolicyManager); 61 when(mContext.getSystemService(Context.USER_SERVICE)) 62 .thenReturn(mUserManager); 63 } 64 65 @Test 66 public void checkIfKeyguardFeaturesDisabled_noEnforcedAdminForManagedProfile() { 67 setUpManagedProfile(mUserId); 68 setUpActiveAdmins(mUserId, new ComponentName[] {mAdmin1, mAdmin2}); 69 70 final EnforcedAdmin enforcedAdmin = RestrictedLockUtils.checkIfKeyguardFeaturesDisabled( 71 mContext, KEYGUARD_DISABLE_FINGERPRINT, mUserId); 72 73 assertThat(enforcedAdmin).isEqualTo(null); 74 } 75 76 @Test 77 public void checkIfKeyguardFeaturesDisabled_oneEnforcedAdminForManagedProfile() { 78 setUpManagedProfile(mUserId); 79 setUpActiveAdmins(mUserId, new ComponentName[] {mAdmin1, mAdmin2}); 80 81 when(mDevicePolicyManager.getKeyguardDisabledFeatures(mAdmin1, mUserId)) 82 .thenReturn(KEYGUARD_DISABLE_FINGERPRINT); 83 84 final EnforcedAdmin enforcedAdmin = RestrictedLockUtils.checkIfKeyguardFeaturesDisabled( 85 mContext, KEYGUARD_DISABLE_FINGERPRINT, mUserId); 86 87 assertThat(enforcedAdmin).isEqualTo(new EnforcedAdmin(mAdmin1, mUserId)); 88 } 89 90 @Test 91 public void checkIfKeyguardFeaturesDisabled_multipleEnforcedAdminForManagedProfile() { 92 setUpManagedProfile(mUserId); 93 setUpActiveAdmins(mUserId, new ComponentName[] {mAdmin1, mAdmin2}); 94 95 when(mDevicePolicyManager.getKeyguardDisabledFeatures(mAdmin1, mUserId)) 96 .thenReturn(KEYGUARD_DISABLE_REMOTE_INPUT); 97 when(mDevicePolicyManager.getKeyguardDisabledFeatures(mAdmin2, mUserId)) 98 .thenReturn(KEYGUARD_DISABLE_REMOTE_INPUT); 99 100 final EnforcedAdmin enforcedAdmin = RestrictedLockUtils.checkIfKeyguardFeaturesDisabled( 101 mContext, KEYGUARD_DISABLE_REMOTE_INPUT, mUserId); 102 103 assertThat(enforcedAdmin).isEqualTo(EnforcedAdmin.MULTIPLE_ENFORCED_ADMIN); 104 } 105 106 private UserInfo setUpManagedProfile(int userId) { 107 final UserInfo userInfo = new UserInfo(userId, "myuser", UserInfo.FLAG_MANAGED_PROFILE); 108 when(mUserManager.getUserInfo(userId)).thenReturn(userInfo); 109 return userInfo; 110 } 111 112 private void setUpActiveAdmins(int userId, ComponentName[] activeAdmins) { 113 when(mDevicePolicyManager.getActiveAdminsAsUser(userId)) 114 .thenReturn(Arrays.asList(activeAdmins)); 115 } 116} 117