1 /*
2  * Copyright (C) 2021 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.biometrics;
18 
19 import static junit.framework.TestCase.assertNotNull;
20 import static junit.framework.TestCase.assertNull;
21 
22 import static org.junit.Assert.assertEquals;
23 import static org.mockito.ArgumentMatchers.any;
24 import static org.mockito.ArgumentMatchers.eq;
25 import static org.mockito.Mockito.mock;
26 import static org.mockito.Mockito.when;
27 
28 import android.app.admin.DevicePolicyManager;
29 import android.content.ComponentName;
30 import android.content.ContentResolver;
31 import android.content.Context;
32 import static android.hardware.biometrics.BiometricAuthenticator.TYPE_FACE;
33 import static android.hardware.biometrics.BiometricAuthenticator.TYPE_FINGERPRINT;
34 import static android.hardware.biometrics.BiometricAuthenticator.TYPE_IRIS;
35 
36 import android.hardware.biometrics.BiometricAuthenticator;
37 import android.os.UserHandle;
38 import android.os.UserManager;
39 
40 import androidx.annotation.Nullable;
41 import androidx.test.ext.junit.runners.AndroidJUnit4;
42 
43 import com.android.settingslib.RestrictedLockUtils;
44 
45 import org.junit.Before;
46 import org.junit.Test;
47 import org.junit.runner.RunWith;
48 import org.mockito.Mock;
49 import org.mockito.MockitoAnnotations;
50 
51 @RunWith(AndroidJUnit4.class)
52 public class ParentalControlsUtilsTest {
53 
54     @Mock
55     private Context mContext;
56     @Mock
57     private DevicePolicyManager mDpm;
58     private ComponentName mSupervisionComponentName = new ComponentName("pkg", "cls");
59 
60     @Before
setUp()61     public void setUp() {
62         MockitoAnnotations.initMocks(this);
63         when(mContext.getContentResolver()).thenReturn(mock(ContentResolver.class));
64     }
65 
66     /**
67      * Helper that sets the appropriate mocks and testing behavior before returning the actual
68      * EnforcedAdmin from ParentalControlsUtils.
69      */
70     @Nullable
getEnforcedAdminForCombination( @ullable ComponentName supervisionComponentName, @BiometricAuthenticator.Modality int modality, int keyguardDisabledFlags)71     private RestrictedLockUtils.EnforcedAdmin getEnforcedAdminForCombination(
72             @Nullable ComponentName supervisionComponentName,
73             @BiometricAuthenticator.Modality int modality, int keyguardDisabledFlags) {
74         when(mDpm.getProfileOwnerOrDeviceOwnerSupervisionComponent(any(UserHandle.class)))
75                 .thenReturn(supervisionComponentName);
76         when(mDpm.getKeyguardDisabledFeatures(eq(supervisionComponentName)))
77                 .thenReturn(keyguardDisabledFlags);
78 
79         return ParentalControlsUtils.parentConsentRequiredInternal(mDpm, modality,
80                 new UserHandle(UserHandle.myUserId()));
81     }
82 
83     @Test
testEnforcedAdmin_whenDpmDisablesBiometricsAndSupervisionComponentExists()84     public void testEnforcedAdmin_whenDpmDisablesBiometricsAndSupervisionComponentExists() {
85         int[][] tests = {
86                 {TYPE_FINGERPRINT, DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT},
87                 {TYPE_FACE, DevicePolicyManager.KEYGUARD_DISABLE_FACE},
88                 {TYPE_IRIS, DevicePolicyManager.KEYGUARD_DISABLE_IRIS},
89         };
90 
91         for (int i = 0; i < tests.length; i++) {
92             RestrictedLockUtils.EnforcedAdmin admin = getEnforcedAdminForCombination(
93                     mSupervisionComponentName, tests[i][0] /* modality */,
94                     tests[i][1] /* keyguardDisableFlags */);
95             assertNotNull(admin);
96             assertEquals(UserManager.DISALLOW_BIOMETRIC, admin.enforcedRestriction);
97             assertEquals(mSupervisionComponentName, admin.component);
98         }
99     }
100 
101     @Test
testNoEnforcedAdmin_whenNoSupervisionComponent()102     public void testNoEnforcedAdmin_whenNoSupervisionComponent() {
103         // Even if DPM flag exists, returns null EnforcedAdmin when no supervision component exists
104         RestrictedLockUtils.EnforcedAdmin admin = getEnforcedAdminForCombination(
105                 null /* supervisionComponentName */, TYPE_FINGERPRINT,
106                 DevicePolicyManager.KEYGUARD_DISABLE_FINGERPRINT);
107         assertNull(admin);
108     }
109 }
110