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

Commit a3c8e1d9 authored by Ale Nijamkin's avatar Ale Nijamkin
Browse files

[flexiglass] Fixes DeviceEntryHapticsInteractorTest

A race condition between when the class under test was instantiated
(pulling in all of its dependencies) and when, later, the biometric face
auth bypass was set as a config resource override caused the bug.

The fix is to delay instantiation of the class under test and to
modernize the test to use Kosmos.runTest.

Fix: 437436426
Test: test passes when run both instrumented and deviceless
Flag: EXEMPT test fix only
Change-Id: I75b0c41dc6cd4cbf464855c45a10fe5b5bd1acec
parent 748f1058
Loading
Loading
Loading
Loading
+48 −46
Original line number Diff line number Diff line
@@ -45,7 +45,11 @@ import com.android.systemui.keyguard.shared.model.BiometricUnlockMode
import com.android.systemui.keyguard.shared.model.BiometricUnlockSource
import com.android.systemui.keyguard.shared.model.FailFingerprintAuthenticationStatus
import com.android.systemui.keyguard.shared.model.SuccessFingerprintAuthenticationStatus
import com.android.systemui.kosmos.testScope
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.advanceTimeBy
import com.android.systemui.kosmos.collectLastValue
import com.android.systemui.kosmos.runCurrent
import com.android.systemui.kosmos.runTest
import com.android.systemui.power.data.repository.powerRepository
import com.android.systemui.power.shared.model.WakeSleepReason
import com.android.systemui.power.shared.model.WakefulnessState
@@ -58,7 +62,6 @@ import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.TestScope
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
@@ -72,9 +75,6 @@ import org.mockito.kotlin.whenever
@RunWith(AndroidJUnit4::class)
class DeviceEntryHapticsInteractorTest : SysuiTestCase() {
    private val kosmos = testKosmos()
    private val testScope = kosmos.testScope

    private lateinit var underTest: DeviceEntryHapticsInteractor

    @Before
    fun setup() {
@@ -98,14 +98,14 @@ class DeviceEntryHapticsInteractorTest : SysuiTestCase() {
            kosmos.keyguardBouncerRepository.setAlternateVisible(false)
            kosmos.sceneInteractor.changeScene(Scenes.Lockscreen, "reason")
        }
        underTest = kosmos.deviceEntryHapticsInteractor
    }

    @DisableSceneContainer
    @Test
    fun nonPowerButtonFPS_vibrateSuccess() =
        testScope.runTest {
            val playSuccessHaptic by collectLastValue(underTest.playSuccessHapticOnDeviceEntry)
        kosmos.runTest {
            val playSuccessHaptic by
                collectLastValue(deviceEntryHapticsInteractor.playSuccessHapticOnDeviceEntry)
            enrollFingerprint(FingerprintSensorType.UDFPS_ULTRASONIC)
            runCurrent()
            enterDeviceFromFingerprintUnlockLegacy()
@@ -115,8 +115,9 @@ class DeviceEntryHapticsInteractorTest : SysuiTestCase() {
    @DisableSceneContainer
    @Test
    fun powerButtonFPS_vibrateSuccess() =
        testScope.runTest {
            val playSuccessHaptic by collectLastValue(underTest.playSuccessHapticOnDeviceEntry)
        kosmos.runTest {
            val playSuccessHaptic by
                collectLastValue(deviceEntryHapticsInteractor.playSuccessHapticOnDeviceEntry)
            enrollFingerprint(FingerprintSensorType.POWER_BUTTON)
            kosmos.fakeKeyEventRepository.setPowerButtonDown(false)

@@ -132,8 +133,9 @@ class DeviceEntryHapticsInteractorTest : SysuiTestCase() {
    @DisableSceneContainer
    @Test
    fun powerButtonFPS_powerDown_doNotVibrateSuccess() =
        testScope.runTest {
            val playSuccessHaptic by collectLastValue(underTest.playSuccessHapticOnDeviceEntry)
        kosmos.runTest {
            val playSuccessHaptic by
                collectLastValue(deviceEntryHapticsInteractor.playSuccessHapticOnDeviceEntry)
            enrollFingerprint(FingerprintSensorType.POWER_BUTTON)
            kosmos.fakeKeyEventRepository.setPowerButtonDown(true) // power button is currently DOWN

@@ -149,8 +151,9 @@ class DeviceEntryHapticsInteractorTest : SysuiTestCase() {
    @DisableSceneContainer
    @Test
    fun powerButtonFPS_powerButtonRecentlyPressed_doNotVibrateSuccess() =
        testScope.runTest {
            val playSuccessHaptic by collectLastValue(underTest.playSuccessHapticOnDeviceEntry)
        kosmos.runTest {
            val playSuccessHaptic by
                collectLastValue(deviceEntryHapticsInteractor.playSuccessHapticOnDeviceEntry)
            enrollFingerprint(FingerprintSensorType.POWER_BUTTON)
            kosmos.fakeKeyEventRepository.setPowerButtonDown(false)

@@ -165,8 +168,8 @@ class DeviceEntryHapticsInteractorTest : SysuiTestCase() {

    @Test
    fun nonPowerButtonFPS_vibrateError() =
        testScope.runTest {
            val playErrorHaptic by collectLastValue(underTest.playErrorHaptic)
        kosmos.runTest {
            val playErrorHaptic by collectLastValue(deviceEntryHapticsInteractor.playErrorHaptic)
            enrollFingerprint(FingerprintSensorType.UDFPS_ULTRASONIC)
            runCurrent()
            fingerprintFailure()
@@ -175,8 +178,8 @@ class DeviceEntryHapticsInteractorTest : SysuiTestCase() {

    @Test
    fun nonPowerButtonFPS_coExFaceFailure_doNotVibrateError() =
        testScope.runTest {
            val playErrorHaptic by collectLastValue(underTest.playErrorHaptic)
        kosmos.runTest {
            val playErrorHaptic by collectLastValue(deviceEntryHapticsInteractor.playErrorHaptic)
            enrollFingerprint(FingerprintSensorType.UDFPS_ULTRASONIC)
            enrollFace()
            runCurrent()
@@ -186,8 +189,8 @@ class DeviceEntryHapticsInteractorTest : SysuiTestCase() {

    @Test
    fun powerButtonFPS_vibrateError() =
        testScope.runTest {
            val playErrorHaptic by collectLastValue(underTest.playErrorHaptic)
        kosmos.runTest {
            val playErrorHaptic by collectLastValue(deviceEntryHapticsInteractor.playErrorHaptic)
            enrollFingerprint(FingerprintSensorType.POWER_BUTTON)
            runCurrent()
            fingerprintFailure()
@@ -196,8 +199,8 @@ class DeviceEntryHapticsInteractorTest : SysuiTestCase() {

    @Test
    fun powerButtonFPS_powerDown_doNotVibrateError() =
        testScope.runTest {
            val playErrorHaptic by collectLastValue(underTest.playErrorHaptic)
        kosmos.runTest {
            val playErrorHaptic by collectLastValue(deviceEntryHapticsInteractor.playErrorHaptic)
            enrollFingerprint(FingerprintSensorType.POWER_BUTTON)
            kosmos.fakeKeyEventRepository.setPowerButtonDown(true)
            runCurrent()
@@ -208,10 +211,10 @@ class DeviceEntryHapticsInteractorTest : SysuiTestCase() {
    @EnableSceneContainer
    @Test
    fun playSuccessHaptic_onDeviceEntryFromUdfps() =
        testScope.runTest {
        kosmos.runTest {
            kosmos.configureKeyguardBypass(isBypassAvailable = false)
            underTest = kosmos.deviceEntryHapticsInteractor
            val playSuccessHaptic by collectLastValue(underTest.playSuccessHapticOnDeviceEntry)
            val playSuccessHaptic by
                collectLastValue(deviceEntryHapticsInteractor.playSuccessHapticOnDeviceEntry)
            enrollFingerprint(FingerprintSensorType.UDFPS_ULTRASONIC)
            runCurrent()
            configureDeviceEntryFromBiometricSource(isFpUnlock = true)
@@ -222,10 +225,10 @@ class DeviceEntryHapticsInteractorTest : SysuiTestCase() {
    @EnableSceneContainer
    @Test
    fun playSuccessHaptic_onDeviceEntryFromSfps() =
        testScope.runTest {
        kosmos.runTest {
            kosmos.configureKeyguardBypass(isBypassAvailable = false)
            underTest = kosmos.deviceEntryHapticsInteractor
            val playSuccessHaptic by collectLastValue(underTest.playSuccessHapticOnDeviceEntry)
            val playSuccessHaptic by
                collectLastValue(deviceEntryHapticsInteractor.playSuccessHapticOnDeviceEntry)
            enrollFingerprint(FingerprintSensorType.POWER_BUTTON)
            kosmos.fakeKeyEventRepository.setPowerButtonDown(false)

@@ -242,11 +245,11 @@ class DeviceEntryHapticsInteractorTest : SysuiTestCase() {
    @EnableSceneContainer
    @Test
    fun playSuccessHaptic_onDeviceEntryFromFaceAuth() =
        testScope.runTest {
        kosmos.runTest {
            enrollFace()
            kosmos.configureKeyguardBypass(isBypassAvailable = true)
            underTest = kosmos.deviceEntryHapticsInteractor
            val playSuccessHaptic by collectLastValue(underTest.playSuccessHapticOnDeviceEntry)
            val playSuccessHaptic by
                collectLastValue(deviceEntryHapticsInteractor.playSuccessHapticOnDeviceEntry)
            configureDeviceEntryFromBiometricSource(isFaceUnlock = true)
            verifyDeviceEntryFromFaceAuth()
            assertThat(playSuccessHaptic).isNotNull()
@@ -256,9 +259,9 @@ class DeviceEntryHapticsInteractorTest : SysuiTestCase() {
    @EnableSceneContainer
    @Test
    fun skipSuccessHaptic_onFaceAuthSuccess_whenBypassDisabled() =
        testScope.runTest {
            underTest = kosmos.deviceEntryHapticsInteractor
            val playSuccessHaptic by collectLastValue(underTest.playSuccessHapticOnDeviceEntry)
        kosmos.runTest {
            val playSuccessHaptic by
                collectLastValue(deviceEntryHapticsInteractor.playSuccessHapticOnDeviceEntry)

            enrollFace()
            kosmos.configureKeyguardBypass(isBypassAvailable = false)
@@ -272,10 +275,10 @@ class DeviceEntryHapticsInteractorTest : SysuiTestCase() {
    @EnableSceneContainer
    @Test
    fun skipSuccessHaptic_onDeviceEntryFromSfps_whenPowerDown() =
        testScope.runTest {
        kosmos.runTest {
            kosmos.configureKeyguardBypass(isBypassAvailable = false)
            underTest = kosmos.deviceEntryHapticsInteractor
            val playSuccessHaptic by collectLastValue(underTest.playSuccessHapticOnDeviceEntry)
            val playSuccessHaptic by
                collectLastValue(deviceEntryHapticsInteractor.playSuccessHapticOnDeviceEntry)
            enrollFingerprint(FingerprintSensorType.POWER_BUTTON)
            // power button is currently DOWN
            kosmos.fakeKeyEventRepository.setPowerButtonDown(true)
@@ -293,10 +296,10 @@ class DeviceEntryHapticsInteractorTest : SysuiTestCase() {
    @EnableSceneContainer
    @Test
    fun skipSuccessHaptic_onDeviceEntryFromSfps_whenPowerButtonRecentlyPressed() =
        testScope.runTest {
        kosmos.runTest {
            kosmos.configureKeyguardBypass(isBypassAvailable = false)
            underTest = kosmos.deviceEntryHapticsInteractor
            val playSuccessHaptic by collectLastValue(underTest.playSuccessHapticOnDeviceEntry)
            val playSuccessHaptic by
                collectLastValue(deviceEntryHapticsInteractor.playSuccessHapticOnDeviceEntry)
            enrollFingerprint(FingerprintSensorType.POWER_BUTTON)
            kosmos.fakeKeyEventRepository.setPowerButtonDown(false)

@@ -313,9 +316,9 @@ class DeviceEntryHapticsInteractorTest : SysuiTestCase() {
    @OptIn(ExperimentalCoroutinesApi::class)
    @Test
    fun playSuccessHaptic_onDeviceEntry_fromDeviceEntryIcon() =
        testScope.runTest {
            underTest = kosmos.deviceEntryHapticsInteractor
            val playSuccessHaptic by collectLastValue(underTest.playSuccessHapticOnDeviceEntry)
        kosmos.runTest {
            val playSuccessHaptic by
                collectLastValue(deviceEntryHapticsInteractor.playSuccessHapticOnDeviceEntry)

            kosmos.fakeKeyguardRepository.setKeyguardDismissible(true)
            runCurrent()
@@ -353,17 +356,16 @@ class DeviceEntryHapticsInteractorTest : SysuiTestCase() {
                )
            }
        }
        underTest = kosmos.deviceEntryHapticsInteractor
    }

    private fun TestScope.verifyDeviceEntryFromFingerprintAuth() {
    private fun Kosmos.verifyDeviceEntryFromFingerprintAuth() {
        val deviceEntryFromBiometricSource by
            collectLastValue(kosmos.deviceEntrySourceInteractor.deviceEntryFromBiometricSource)
        assertThat(deviceEntryFromBiometricSource)
            .isEqualTo(BiometricUnlockSource.FINGERPRINT_SENSOR)
    }

    private fun TestScope.verifyDeviceEntryFromFaceAuth() {
    private fun Kosmos.verifyDeviceEntryFromFaceAuth() {
        val deviceEntryFromBiometricSource by
            collectLastValue(kosmos.deviceEntrySourceInteractor.deviceEntryFromBiometricSource)
        assertThat(deviceEntryFromBiometricSource).isEqualTo(BiometricUnlockSource.FACE_SENSOR)