From dc2e05430b05805208492bb4305448179a72e569 Mon Sep 17 00:00:00 2001 From: ganfra Date: Wed, 8 Nov 2023 11:53:01 +0100 Subject: [PATCH] Lockscreen : should fix tests --- .../lockscreen/impl/unlock/PinUnlockHelper.kt | 53 +++++++++++++++++++ .../impl/unlock/PinUnlockPresenter.kt | 29 +--------- .../impl/unlock/PinUnlockPresenterTest.kt | 20 ++----- 3 files changed, 59 insertions(+), 43 deletions(-) create mode 100644 features/lockscreen/impl/src/main/kotlin/io/element/android/features/lockscreen/impl/unlock/PinUnlockHelper.kt diff --git a/features/lockscreen/impl/src/main/kotlin/io/element/android/features/lockscreen/impl/unlock/PinUnlockHelper.kt b/features/lockscreen/impl/src/main/kotlin/io/element/android/features/lockscreen/impl/unlock/PinUnlockHelper.kt new file mode 100644 index 0000000000..72d29b4481 --- /dev/null +++ b/features/lockscreen/impl/src/main/kotlin/io/element/android/features/lockscreen/impl/unlock/PinUnlockHelper.kt @@ -0,0 +1,53 @@ +/* + * Copyright (c) 2023 New Vector Ltd + * + * 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 io.element.android.features.lockscreen.impl.unlock + +import androidx.compose.runtime.Composable +import androidx.compose.runtime.DisposableEffect +import io.element.android.features.lockscreen.impl.biometric.BiometricUnlockManager +import io.element.android.features.lockscreen.impl.biometric.DefaultBiometricUnlockCallback +import io.element.android.features.lockscreen.impl.pin.DefaultPinCodeManagerCallback +import io.element.android.features.lockscreen.impl.pin.PinCodeManager +import javax.inject.Inject + +class PinUnlockHelper @Inject constructor( + private val biometricUnlockManager: BiometricUnlockManager, + private val pinCodeManager: PinCodeManager +) { + + @Composable + fun OnUnlockEffect(onUnlock: () -> Unit) { + DisposableEffect(Unit) { + val biometricUnlockCallback = object : DefaultBiometricUnlockCallback() { + override fun onBiometricUnlockSuccess() { + onUnlock() + } + } + val pinCodeVerifiedCallback = object : DefaultPinCodeManagerCallback() { + override fun onPinCodeVerified() { + onUnlock() + } + } + biometricUnlockManager.addCallback(biometricUnlockCallback) + pinCodeManager.addCallback(pinCodeVerifiedCallback) + onDispose { + biometricUnlockManager.removeCallback(biometricUnlockCallback) + pinCodeManager.removeCallback(pinCodeVerifiedCallback) + } + } + } +} diff --git a/features/lockscreen/impl/src/main/kotlin/io/element/android/features/lockscreen/impl/unlock/PinUnlockPresenter.kt b/features/lockscreen/impl/src/main/kotlin/io/element/android/features/lockscreen/impl/unlock/PinUnlockPresenter.kt index b2d1135963..1b5ce80085 100644 --- a/features/lockscreen/impl/src/main/kotlin/io/element/android/features/lockscreen/impl/unlock/PinUnlockPresenter.kt +++ b/features/lockscreen/impl/src/main/kotlin/io/element/android/features/lockscreen/impl/unlock/PinUnlockPresenter.kt @@ -17,7 +17,6 @@ package io.element.android.features.lockscreen.impl.unlock import androidx.compose.runtime.Composable -import androidx.compose.runtime.DisposableEffect import androidx.compose.runtime.LaunchedEffect import androidx.compose.runtime.MutableState import androidx.compose.runtime.getValue @@ -27,8 +26,6 @@ import androidx.compose.runtime.saveable.rememberSaveable import androidx.compose.runtime.setValue import io.element.android.features.lockscreen.impl.biometric.BiometricUnlock import io.element.android.features.lockscreen.impl.biometric.BiometricUnlockManager -import io.element.android.features.lockscreen.impl.biometric.DefaultBiometricUnlockCallback -import io.element.android.features.lockscreen.impl.pin.DefaultPinCodeManagerCallback import io.element.android.features.lockscreen.impl.pin.PinCodeManager import io.element.android.features.lockscreen.impl.pin.model.PinEntry import io.element.android.features.lockscreen.impl.unlock.keypad.PinKeypadModel @@ -46,6 +43,7 @@ class PinUnlockPresenter @Inject constructor( private val biometricUnlockManager: BiometricUnlockManager, private val matrixClient: MatrixClient, private val coroutineScope: CoroutineScope, + private val pinUnlockHelper: PinUnlockHelper, ) : Presenter { @Composable @@ -98,8 +96,7 @@ class PinUnlockPresenter @Inject constructor( showSignOutPrompt = true } } - - OnUnlockEffect { + pinUnlockHelper.OnUnlockEffect { isUnlocked.value = true } @@ -142,28 +139,6 @@ class PinUnlockPresenter @Inject constructor( ) } - @Composable - private fun OnUnlockEffect(onUnlock: () -> Unit) { - DisposableEffect(Unit) { - val biometricUnlockCallback = object : DefaultBiometricUnlockCallback() { - override fun onBiometricUnlockSuccess() { - onUnlock() - } - } - val pinCodeVerifiedCallback = object : DefaultPinCodeManagerCallback() { - override fun onPinCodeVerified() { - onUnlock() - } - } - biometricUnlockManager.addCallback(biometricUnlockCallback) - pinCodeManager.addCallback(pinCodeVerifiedCallback) - onDispose { - biometricUnlockManager.removeCallback(biometricUnlockCallback) - pinCodeManager.removeCallback(pinCodeVerifiedCallback) - } - } - } - private fun Async.isComplete(): Boolean { return dataOrNull()?.isComplete().orFalse() } diff --git a/features/lockscreen/impl/src/test/kotlin/io/element/android/features/lockscreen/impl/unlock/PinUnlockPresenterTest.kt b/features/lockscreen/impl/src/test/kotlin/io/element/android/features/lockscreen/impl/unlock/PinUnlockPresenterTest.kt index 95ded7617c..745b1c9629 100644 --- a/features/lockscreen/impl/src/test/kotlin/io/element/android/features/lockscreen/impl/unlock/PinUnlockPresenterTest.kt +++ b/features/lockscreen/impl/src/test/kotlin/io/element/android/features/lockscreen/impl/unlock/PinUnlockPresenterTest.kt @@ -32,7 +32,6 @@ import io.element.android.libraries.architecture.Async import io.element.android.libraries.matrix.test.FakeMatrixClient import io.element.android.tests.testutils.awaitLastSequentialItem import io.element.android.tests.testutils.consumeItemsUntilPredicate -import kotlinx.coroutines.CompletableDeferred import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.test.runTest import org.junit.Test @@ -44,13 +43,7 @@ class PinUnlockPresenterTest { @Test fun `present - success verify flow`() = runTest { - val pinCodeVerified = CompletableDeferred() - val callback = object : DefaultPinCodeManagerCallback() { - override fun onPinCodeCreated() { - pinCodeVerified.complete(Unit) - } - } - val presenter = createPinUnlockPresenter(this, callback = callback) + val presenter = createPinUnlockPresenter(this) moleculeFlow(RecompositionMode.Immediate) { presenter.present() }.test { @@ -58,6 +51,7 @@ class PinUnlockPresenterTest { assertThat(state.pinEntry).isInstanceOf(Async.Uninitialized::class.java) assertThat(state.showWrongPinTitle).isFalse() assertThat(state.showSignOutPrompt).isFalse() + assertThat(state.isUnlocked).isFalse() assertThat(state.signOutAction).isInstanceOf(Async.Uninitialized::class.java) assertThat(state.remainingAttempts).isInstanceOf(Async.Uninitialized::class.java) } @@ -77,20 +71,14 @@ class PinUnlockPresenterTest { } awaitLastSequentialItem().also { state -> state.pinEntry.assertText(completePin) + assertThat(state.isUnlocked).isTrue() } - pinCodeVerified.await() } } @Test fun `present - failure verify flow`() = runTest { - val pinCodeVerified = CompletableDeferred() - val callback = object : DefaultPinCodeManagerCallback() { - override fun onPinCodeCreated() { - pinCodeVerified.complete(Unit) - } - } - val presenter = createPinUnlockPresenter(this, callback = callback) + val presenter = createPinUnlockPresenter(this) moleculeFlow(RecompositionMode.Immediate) { presenter.present() }.test {