1 /*
2  * Copyright (C) 2023 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.biometrics2.utils;
18 
19 import static org.mockito.ArgumentMatchers.any;
20 import static org.mockito.Mockito.doAnswer;
21 import static org.mockito.Mockito.when;
22 
23 import android.content.Context;
24 import android.content.res.Resources;
25 import android.hardware.biometrics.SensorProperties;
26 import android.hardware.fingerprint.Fingerprint;
27 import android.hardware.fingerprint.FingerprintManager;
28 import android.hardware.fingerprint.FingerprintSensorProperties;
29 import android.hardware.fingerprint.FingerprintSensorPropertiesInternal;
30 import android.hardware.fingerprint.IFingerprintAuthenticatorsRegisteredCallback;
31 
32 import androidx.annotation.NonNull;
33 
34 import com.android.settings.biometrics2.data.repository.FingerprintRepository;
35 
36 import java.util.ArrayList;
37 
38 public class FingerprintRepositoryUtils {
39 
setupSuwMaxFingerprintsEnrollable( @onNull Context context, @NonNull Resources mockedResources, int numOfFp)40     public static void setupSuwMaxFingerprintsEnrollable(
41             @NonNull Context context,
42             @NonNull Resources mockedResources,
43             int numOfFp) {
44         final int resId = context.getResources().getIdentifier("suw_max_fingerprints_enrollable",
45                 "integer", context.getPackageName());
46         when(mockedResources.getInteger(resId)).thenReturn(numOfFp);
47     }
48 
newFingerprintRepository( @onNull FingerprintManager mockedFingerprintManager, @FingerprintSensorProperties.SensorType int sensorType, int maxEnrollmentsPerUser)49     public static FingerprintRepository newFingerprintRepository(
50             @NonNull FingerprintManager mockedFingerprintManager,
51             @FingerprintSensorProperties.SensorType int sensorType,
52             int maxEnrollmentsPerUser) {
53 
54         final ArrayList<FingerprintSensorPropertiesInternal> props = new ArrayList<>();
55         props.add(new FingerprintSensorPropertiesInternal(
56                 0 /* sensorId */,
57                 SensorProperties.STRENGTH_STRONG,
58                 maxEnrollmentsPerUser,
59                 new ArrayList<>() /* componentInfo */,
60                 sensorType,
61                 true /* resetLockoutRequiresHardwareAuthToken */));
62         doAnswer(invocation -> {
63             final IFingerprintAuthenticatorsRegisteredCallback callback =
64                     invocation.getArgument(0);
65             callback.onAllAuthenticatorsRegistered(props);
66             return null;
67         }).when(mockedFingerprintManager).addAuthenticatorsRegisteredCallback(any());
68         return new FingerprintRepository(mockedFingerprintManager);
69     }
70 
setupFingerprintEnrolledFingerprints( @onNull FingerprintManager mockedFingerprintManager, int userId, int enrolledFingerprints)71     public static void setupFingerprintEnrolledFingerprints(
72             @NonNull FingerprintManager mockedFingerprintManager,
73             int userId,
74             int enrolledFingerprints) {
75         final ArrayList<Fingerprint> ret = new ArrayList<>();
76         for (int i = 0; i < enrolledFingerprints; ++i) {
77             ret.add(new Fingerprint("name", 0, 0, 0L));
78         }
79         when(mockedFingerprintManager.getEnrolledFingerprints(userId)).thenReturn(ret);
80     }
81 }
82