Home | History | Annotate | Download | only in cts
      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
     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
     70     public void test_hasFingerprintHardware() {
     71         if (!mHasFingerprintManager) {
     72             return; // skip test if no fingerprint feature
     73         }
     74         assertTrue(mFingerprintManager.isHardwareDetected());
     75     }
     76 
     77     public void test_hasEnrolledFingerprints() {
     78         if (!mHasFingerprintManager) {
     79             return; // skip test if no fingerprint feature
     80         }
     81         boolean hasEnrolledFingerprints = mFingerprintManager.hasEnrolledFingerprints();
     82         assertTrue(!hasEnrolledFingerprints);
     83     }
     84 
     85     public void test_authenticateNullCallback() {
     86         if (!mHasFingerprintManager) {
     87             return; // skip test if no fingerprint feature
     88         }
     89         boolean exceptionTaken = false;
     90         CancellationSignal cancelAuth = new CancellationSignal();
     91         try {
     92             mFingerprintManager.authenticate(null, cancelAuth, 0, null, null);
     93         } catch (IllegalArgumentException e) {
     94             exceptionTaken = true;
     95         } finally {
     96             assertTrue(mAuthState == AuthState.AUTH_UNKNOWN);
     97             assertTrue(exceptionTaken);
     98             cancelAuth.cancel();
     99         }
    100     }
    101 
    102     public void test_authenticate() {
    103         if (!mHasFingerprintManager) {
    104             return; // skip test if no fingerprint feature
    105         }
    106         boolean exceptionTaken = false;
    107         CancellationSignal cancelAuth = new CancellationSignal();
    108         try {
    109             mFingerprintManager.authenticate(null, cancelAuth, 0, mAuthCallback, null);
    110         } catch (IllegalArgumentException e) {
    111             exceptionTaken = true;
    112         } finally {
    113             assertFalse(exceptionTaken);
    114             // We should never get out of this state without user interaction
    115             assertTrue(mAuthState == AuthState.AUTH_UNKNOWN);
    116             cancelAuth.cancel();
    117         }
    118     }
    119 }
    120