1 /* 2 * Copyright (C) 2015 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 android.hardware.fingerprint.cts; 18 19 import android.content.Context; 20 import android.content.pm.PackageManager; 21 import android.hardware.fingerprint.FingerprintManager; 22 import android.hardware.fingerprint.FingerprintManager.AuthenticationCallback; 23 import android.os.CancellationSignal; 24 import android.platform.test.annotations.Presubmit; 25 import android.test.AndroidTestCase; 26 27 /** 28 * Basic test cases for FingerprintManager 29 */ 30 public class FingerprintManagerTest extends AndroidTestCase { 31 private enum AuthState { 32 AUTH_UNKNOWN, AUTH_ERROR, AUTH_FAILED, AUTH_SUCCEEDED 33 } 34 private AuthState mAuthState = AuthState.AUTH_UNKNOWN; 35 private FingerprintManager mFingerprintManager; 36 37 boolean mHasFingerprintManager; 38 private AuthenticationCallback mAuthCallback = new AuthenticationCallback() { 39 40 @Override 41 public void onAuthenticationError(int errorCode, CharSequence errString) { 42 } 43 44 @Override 45 public void onAuthenticationFailed() { 46 mAuthState = AuthState.AUTH_SUCCEEDED; 47 } 48 49 @Override 50 public void onAuthenticationSucceeded( 51 android.hardware.fingerprint.FingerprintManager.AuthenticationResult result) { 52 mAuthState = AuthState.AUTH_SUCCEEDED; 53 } 54 }; 55 56 @Override setUp()57 public void setUp() throws Exception { 58 super.setUp(); 59 mAuthState = AuthState.AUTH_UNKNOWN; 60 61 PackageManager pm = getContext().getPackageManager(); 62 if (pm.hasSystemFeature(PackageManager.FEATURE_FINGERPRINT)) { 63 mHasFingerprintManager = true; 64 mFingerprintManager = (FingerprintManager) 65 getContext().getSystemService(Context.FINGERPRINT_SERVICE); 66 } 67 } 68 69 @Presubmit test_managerAndFeature()70 public void test_managerAndFeature() { 71 final boolean hasFingerprintFeature = getContext().getPackageManager() 72 .hasSystemFeature(PackageManager.FEATURE_FINGERPRINT); 73 final FingerprintManager fpm = getContext().getSystemService(FingerprintManager.class); 74 75 if (hasFingerprintFeature || fpm != null) { 76 assertTrue(hasFingerprintFeature); 77 assertTrue(fpm != null); 78 } 79 } 80 81 @Presubmit test_hasFingerprintHardware()82 public void test_hasFingerprintHardware() { 83 if (!mHasFingerprintManager) { 84 return; // skip test if no fingerprint feature 85 } 86 assertTrue(mFingerprintManager.isHardwareDetected()); 87 } 88 test_hasEnrolledFingerprints()89 public void test_hasEnrolledFingerprints() { 90 if (!mHasFingerprintManager) { 91 return; // skip test if no fingerprint feature 92 } 93 boolean hasEnrolledFingerprints = mFingerprintManager.hasEnrolledFingerprints(); 94 assertTrue(!hasEnrolledFingerprints); 95 } 96 test_authenticateNullCallback()97 public void test_authenticateNullCallback() { 98 if (!mHasFingerprintManager) { 99 return; // skip test if no fingerprint feature 100 } 101 boolean exceptionTaken = false; 102 CancellationSignal cancelAuth = new CancellationSignal(); 103 try { 104 mFingerprintManager.authenticate(null, cancelAuth, 0, null, null); 105 } catch (IllegalArgumentException e) { 106 exceptionTaken = true; 107 } finally { 108 assertTrue(mAuthState == AuthState.AUTH_UNKNOWN); 109 assertTrue(exceptionTaken); 110 cancelAuth.cancel(); 111 } 112 } 113 test_authenticate()114 public void test_authenticate() { 115 if (!mHasFingerprintManager) { 116 return; // skip test if no fingerprint feature 117 } 118 boolean exceptionTaken = false; 119 CancellationSignal cancelAuth = new CancellationSignal(); 120 try { 121 mFingerprintManager.authenticate(null, cancelAuth, 0, mAuthCallback, null); 122 } catch (IllegalArgumentException e) { 123 exceptionTaken = true; 124 } finally { 125 assertFalse(exceptionTaken); 126 // We should never get out of this state without user interaction 127 assertTrue(mAuthState == AuthState.AUTH_UNKNOWN); 128 cancelAuth.cancel(); 129 } 130 } 131 } 132