Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit aa060057 authored by Joe Bolinger's avatar Joe Bolinger Committed by Automerger Merge Worker
Browse files

Merge "Add coex tests." into tm-d1-dev am: 4f931d51

parents 67de5e5d 4f931d51
Loading
Loading
Loading
Loading
+0 −26
Original line number Diff line number Diff line
<!--
  ~ Copyright (C) 2022 The Android Open Source Project
  ~
  ~ Licensed under the Apache License, Version 2.0 (the "License");
  ~ you may not use this file except in compliance with the License.
  ~ You may obtain a copy of the License at
  ~
  ~      http://www.apache.org/licenses/LICENSE-2.0
  ~
  ~ Unless required by applicable law or agreed to in writing, software
  ~ distributed under the License is distributed on an "AS IS" BASIS,
  ~ WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  ~ See the License for the specific language governing permissions and
  ~ limitations under the License.
  -->

<com.android.systemui.biometrics.AuthBiometricView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:id="@+id/contents"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical">

    <include layout="@layout/auth_biometric_contents"/>

</com.android.systemui.biometrics.AuthBiometricView>
 No newline at end of file
+16 −15
Original line number Diff line number Diff line
@@ -59,7 +59,7 @@ import java.util.Set;
/**
 * Contains the Biometric views (title, subtitle, icon, buttons, etc.) and its controllers.
 */
public class AuthBiometricView extends LinearLayout {
public abstract class AuthBiometricView extends LinearLayout {

    private static final String TAG = "AuthBiometricView";

@@ -116,7 +116,7 @@ public class AuthBiometricView extends LinearLayout {
        void onAction(int action);
    }

    protected final Handler mHandler;
    private final Handler mHandler;
    private final AccessibilityManager mAccessibilityManager;
    private final LockPatternUtils mLockPatternUtils;
    protected final int mTextColorError;
@@ -155,8 +155,8 @@ public class AuthBiometricView extends LinearLayout {
    // Measurements when biometric view is showing text, buttons, etc.
    @Nullable @VisibleForTesting AuthDialog.LayoutParams mLayoutParams;

    protected Callback mCallback;
    protected @BiometricState int mState;
    private Callback mCallback;
    @BiometricState private int mState;

    private float mIconOriginalY;

@@ -250,18 +250,9 @@ public class AuthBiometricView extends LinearLayout {
        return false;
    }

    /**
     * Create the controller for managing the icons transitions during the prompt.
     *
     * Subclass should override.
     */
    /** Create the controller for managing the icons transitions during the prompt.*/
    @NonNull
    protected AuthIconController createIconController() {
        return new AuthIconController(mContext, mIconView) {
            @Override
            public void updateIcon(int lastState, int newState) {}
        };
    }
    protected abstract AuthIconController createIconController();

    void setPanelController(AuthPanelController panelController) {
        mPanelController = panelController;
@@ -881,4 +872,14 @@ public class AuthBiometricView extends LinearLayout {
    @AuthDialog.DialogSize int getSize() {
        return mSize;
    }

    /** If authentication has successfully occurred and the view is done. */
    boolean isAuthenticated() {
        return mState == STATE_AUTHENTICATED;
    }

    /** If authentication is currently in progress. */
    boolean isAuthenticating() {
        return mState == STATE_AUTHENTICATING;
    }
}
+111 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.biometrics

import android.hardware.biometrics.BiometricAuthenticator.TYPE_FACE
import android.hardware.biometrics.BiometricAuthenticator.TYPE_FINGERPRINT
import android.testing.AndroidTestingRunner
import android.testing.TestableLooper
import android.testing.TestableLooper.RunWithLooper
import android.view.View
import androidx.test.filters.SmallTest
import com.android.systemui.R
import com.android.systemui.SysuiTestCase
import com.google.common.truth.Truth.assertThat
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.never
import org.mockito.Mockito.verify
import org.mockito.junit.MockitoJUnit

@RunWith(AndroidTestingRunner::class)
@RunWithLooper(setAsMainLooper = true)
@SmallTest
class AuthBiometricFingerprintAndFaceViewTest : SysuiTestCase() {

    @JvmField @Rule
    var mockitoRule = MockitoJUnit.rule()

    @Mock
    private lateinit var callback: AuthBiometricView.Callback
    @Mock
    private lateinit var panelController: AuthPanelController

    private lateinit var biometricView: AuthBiometricFingerprintAndFaceView

    @Before
    fun setup() {
        biometricView = R.layout.auth_biometric_fingerprint_and_face_view
                .asTestAuthBiometricView(mContext, callback, panelController)
        waitForIdleSync()
    }

    @After
    fun tearDown() {
        biometricView.destroyDialog()
    }

    @Test
    fun fingerprintSuccessDoesNotRequireExplicitConfirmation() {
        biometricView.onDialogAnimatedIn()
        biometricView.onAuthenticationSucceeded(TYPE_FINGERPRINT)
        waitForIdleSync()

        assertThat(biometricView.isAuthenticated).isTrue()
        verify(callback).onAction(AuthBiometricView.Callback.ACTION_AUTHENTICATED)
    }

    @Test
    fun faceSuccessRequiresExplicitConfirmation() {
        biometricView.onDialogAnimatedIn()
        biometricView.onAuthenticationSucceeded(TYPE_FACE)
        waitForIdleSync()

        assertThat(biometricView.isAuthenticated).isFalse()
        assertThat(biometricView.isAuthenticating).isFalse()
        assertThat(biometricView.mConfirmButton.visibility).isEqualTo(View.GONE)
        verify(callback, never()).onAction(AuthBiometricView.Callback.ACTION_AUTHENTICATED)

        // icon acts as confirm button
        biometricView.mIconView.performClick()
        waitForIdleSync()

        assertThat(biometricView.isAuthenticated).isTrue()
        verify(callback).onAction(AuthBiometricView.Callback.ACTION_AUTHENTICATED)
    }

    @Test
    fun ignoresFaceErrors() {
        biometricView.onDialogAnimatedIn()
        biometricView.onError(TYPE_FACE, "not a face")
        waitForIdleSync()

        assertThat(biometricView.isAuthenticating).isTrue()
        verify(callback, never()).onAction(AuthBiometricView.Callback.ACTION_ERROR)

        biometricView.onError(TYPE_FINGERPRINT, "that's a nope")
        waitForIdleSync()

        verify(callback).onAction(AuthBiometricView.Callback.ACTION_ERROR)
    }

    override fun waitForIdleSync() = TestableLooper.get(this).processAllMessages()
}
+269 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2022 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package com.android.systemui.biometrics

import android.hardware.biometrics.BiometricAuthenticator
import android.os.Bundle
import android.testing.AndroidTestingRunner
import android.testing.TestableLooper
import android.testing.TestableLooper.RunWithLooper
import android.view.View
import androidx.test.filters.SmallTest
import com.android.systemui.R
import com.android.systemui.SysuiTestCase
import com.google.common.truth.Truth.assertThat
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
import org.mockito.ArgumentMatchers.eq
import org.mockito.Mock
import org.mockito.Mockito.never
import org.mockito.Mockito.verify
import org.mockito.junit.MockitoJUnit

@RunWith(AndroidTestingRunner::class)
@RunWithLooper(setAsMainLooper = true)
@SmallTest
class AuthBiometricFingerprintViewTest : SysuiTestCase() {

    @JvmField @Rule
    val mockitoRule = MockitoJUnit.rule()

    @Mock
    private lateinit var callback: AuthBiometricView.Callback
    @Mock
    private lateinit var panelController: AuthPanelController

    private lateinit var biometricView: AuthBiometricView

    private fun createView(allowDeviceCredential: Boolean = false): AuthBiometricFingerprintView {
        val view = R.layout.auth_biometric_fingerprint_view.asTestAuthBiometricView(
                mContext, callback, panelController, allowDeviceCredential = allowDeviceCredential
        ) as AuthBiometricFingerprintView
        waitForIdleSync()
        return view
    }

    @Before
    fun setup() {
        biometricView = createView()
    }

    @After
    fun tearDown() {
        biometricView.destroyDialog()
    }

    @Test
    fun testOnAuthenticationSucceeded_noConfirmationRequired_sendsActionAuthenticated() {
        biometricView.onAuthenticationSucceeded(BiometricAuthenticator.TYPE_FINGERPRINT)
        waitForIdleSync()

        assertThat(biometricView.isAuthenticated).isTrue()
        verify(callback).onAction(AuthBiometricView.Callback.ACTION_AUTHENTICATED)
    }

    @Test
    fun testOnAuthenticationSucceeded_confirmationRequired_updatesDialogContents() {
        biometricView.setRequireConfirmation(true)
        biometricView.onAuthenticationSucceeded(BiometricAuthenticator.TYPE_FINGERPRINT)
        waitForIdleSync()

        // TODO: this should be tested in the subclasses
        if (biometricView.supportsRequireConfirmation()) {
            verify(callback, never()).onAction(ArgumentMatchers.anyInt())
            assertThat(biometricView.mNegativeButton.visibility).isEqualTo(View.GONE)
            assertThat(biometricView.mCancelButton.visibility).isEqualTo(View.VISIBLE)
            assertThat(biometricView.mCancelButton.isEnabled).isTrue()
            assertThat(biometricView.mConfirmButton.isEnabled).isTrue()
            assertThat(biometricView.mIndicatorView.text)
                    .isEqualTo(mContext.getText(R.string.biometric_dialog_tap_confirm))
            assertThat(biometricView.mIndicatorView.visibility).isEqualTo(View.VISIBLE)
        } else {
            assertThat(biometricView.isAuthenticated).isTrue()
            verify(callback).onAction(eq(AuthBiometricView.Callback.ACTION_AUTHENTICATED))
        }
    }

    @Test
    fun testPositiveButton_sendsActionAuthenticated() {
        biometricView.mConfirmButton.performClick()
        waitForIdleSync()

        verify(callback).onAction(AuthBiometricView.Callback.ACTION_AUTHENTICATED)
        assertThat(biometricView.isAuthenticated).isTrue()
    }

    @Test
    fun testNegativeButton_beforeAuthentication_sendsActionButtonNegative() {
        biometricView.onDialogAnimatedIn()
        biometricView.mNegativeButton.performClick()
        waitForIdleSync()

        verify(callback).onAction(AuthBiometricView.Callback.ACTION_BUTTON_NEGATIVE)
    }

    @Test
    fun testCancelButton_whenPendingConfirmation_sendsActionUserCanceled() {
        biometricView.setRequireConfirmation(true)
        biometricView.onAuthenticationSucceeded(BiometricAuthenticator.TYPE_FINGERPRINT)

        assertThat(biometricView.mNegativeButton.visibility).isEqualTo(View.GONE)
        biometricView.mCancelButton.performClick()
        waitForIdleSync()

        verify(callback).onAction(AuthBiometricView.Callback.ACTION_USER_CANCELED)
    }

    @Test
    fun testTryAgainButton_sendsActionTryAgain() {
        biometricView.mTryAgainButton.performClick()
        waitForIdleSync()

        verify(callback).onAction(AuthBiometricView.Callback.ACTION_BUTTON_TRY_AGAIN)
        assertThat(biometricView.mTryAgainButton.visibility).isEqualTo(View.GONE)
        assertThat(biometricView.isAuthenticating).isTrue()
    }

    @Test
    fun testOnErrorSendsActionError() {
        biometricView.onError(BiometricAuthenticator.TYPE_FACE, "testError")
        waitForIdleSync()

        verify(callback).onAction(eq(AuthBiometricView.Callback.ACTION_ERROR))
    }

    @Test
    fun testOnErrorShowsMessage() {
        // prevent error state from instantly returning to authenticating in the test
        biometricView.mAnimationDurationHideDialog = 10_000

        val message = "another error"
        biometricView.onError(BiometricAuthenticator.TYPE_FACE, message)
        waitForIdleSync()

        assertThat(biometricView.isAuthenticating).isFalse()
        assertThat(biometricView.isAuthenticated).isFalse()
        assertThat(biometricView.mIndicatorView.visibility).isEqualTo(View.VISIBLE)
        assertThat(biometricView.mIndicatorView.text).isEqualTo(message)
    }

    @Test
    fun testBackgroundClicked_sendsActionUserCanceled() {
        val view = View(mContext)
        biometricView.setBackgroundView(view)
        view.performClick()

        verify(callback).onAction(eq(AuthBiometricView.Callback.ACTION_USER_CANCELED))
    }

    @Test
    fun testBackgroundClicked_afterAuthenticated_neverSendsUserCanceled() {
        val view = View(mContext)
        biometricView.setBackgroundView(view)
        biometricView.onAuthenticationSucceeded(BiometricAuthenticator.TYPE_FINGERPRINT)
        view.performClick()

        verify(callback, never())
                .onAction(eq(AuthBiometricView.Callback.ACTION_USER_CANCELED))
    }

    @Test
    fun testBackgroundClicked_whenSmallDialog_neverSendsUserCanceled() {
        biometricView.mLayoutParams = AuthDialog.LayoutParams(0, 0)
        biometricView.updateSize(AuthDialog.SIZE_SMALL)
        val view = View(mContext)
        biometricView.setBackgroundView(view)
        view.performClick()

        verify(callback, never()).onAction(eq(AuthBiometricView.Callback.ACTION_USER_CANCELED))
    }

    @Test
    fun testIgnoresUselessHelp() {
        biometricView.mAnimationDurationHideDialog = 10_000
        biometricView.onDialogAnimatedIn()
        waitForIdleSync()

        assertThat(biometricView.isAuthenticating).isTrue()

        val helpText = biometricView.mIndicatorView.text
        biometricView.onHelp(BiometricAuthenticator.TYPE_FINGERPRINT, "")
        waitForIdleSync()

        // text should not change
        assertThat(biometricView.mIndicatorView.text).isEqualTo(helpText)
        verify(callback, never()).onAction(eq(AuthBiometricView.Callback.ACTION_ERROR))
    }

    @Test
    fun testRestoresState() {
        val requireConfirmation = true
        biometricView.mAnimationDurationHideDialog = 10_000
        val failureMessage = "testFailureMessage"
        biometricView.setRequireConfirmation(requireConfirmation)
        biometricView.onAuthenticationFailed(BiometricAuthenticator.TYPE_FACE, failureMessage)
        waitForIdleSync()

        val state = Bundle()
        biometricView.onSaveState(state)
        assertThat(biometricView.mTryAgainButton.visibility).isEqualTo(View.GONE)
        assertThat(state.getInt(AuthDialog.KEY_BIOMETRIC_TRY_AGAIN_VISIBILITY))
            .isEqualTo(View.GONE)
        assertThat(state.getInt(AuthDialog.KEY_BIOMETRIC_STATE))
            .isEqualTo(AuthBiometricView.STATE_ERROR)
        assertThat(biometricView.mIndicatorView.visibility).isEqualTo(View.VISIBLE)
        assertThat(state.getBoolean(AuthDialog.KEY_BIOMETRIC_INDICATOR_ERROR_SHOWING)).isTrue()
        assertThat(biometricView.mIndicatorView.text).isEqualTo(failureMessage)
        assertThat(state.getString(AuthDialog.KEY_BIOMETRIC_INDICATOR_STRING))
            .isEqualTo(failureMessage)

        // TODO: Test dialog size. Should move requireConfirmation to buildBiometricPromptBundle

        // Create new dialog and restore the previous state into it
        biometricView.destroyDialog()
        biometricView = createView()
        biometricView.restoreState(state)
        biometricView.mAnimationDurationHideDialog = 10_000
        biometricView.setRequireConfirmation(requireConfirmation)
        waitForIdleSync()

        assertThat(biometricView.mTryAgainButton.visibility).isEqualTo(View.GONE)
        assertThat(biometricView.mIndicatorView.visibility).isEqualTo(View.VISIBLE)

        // TODO: Test restored text. Currently cannot test this, since it gets restored only after
        // dialog size is known.
    }

    @Test
    fun testCredentialButton_whenDeviceCredentialAllowed() {
        biometricView.destroyDialog()
        biometricView = createView(allowDeviceCredential = true)

        assertThat(biometricView.mUseCredentialButton.visibility).isEqualTo(View.VISIBLE)
        assertThat(biometricView.mNegativeButton.visibility).isEqualTo(View.GONE)

        biometricView.mUseCredentialButton.performClick()
        waitForIdleSync()

        verify(callback).onAction(AuthBiometricView.Callback.ACTION_USE_DEVICE_CREDENTIAL)
    }

    override fun waitForIdleSync() = TestableLooper.get(this).processAllMessages()
}
+0 −324

File deleted.

Preview size limit exceeded, changes collapsed.

Loading