Skip to content

Commit 54af229

Browse files
committed
Add unit test for PinUnlockState
1 parent 6b12d45 commit 54af229

File tree

2 files changed

+84
-6
lines changed

2 files changed

+84
-6
lines changed

features/lockscreen/impl/src/main/kotlin/io/element/android/features/lockscreen/impl/unlock/PinUnlockStateProvider.kt

+8-6
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,19 @@ open class PinUnlockStateProvider : PreviewParameterProvider<PinUnlockState> {
2323
aPinUnlockState(showWrongPinTitle = true),
2424
aPinUnlockState(showSignOutPrompt = true),
2525
aPinUnlockState(showBiometricUnlock = false),
26-
aPinUnlockState(showSignOutPrompt = true, remainingAttempts = 0),
26+
aPinUnlockState(showSignOutPrompt = true, remainingAttempts = AsyncData.Success(0)),
2727
aPinUnlockState(signOutAction = AsyncAction.Loading),
28-
aPinUnlockState(biometricUnlockResult = BiometricAuthenticator.AuthenticationResult.Failure(
29-
BiometricUnlockError(BiometricPrompt.ERROR_LOCKOUT, "Biometric auth disabled")
30-
)),
28+
aPinUnlockState(
29+
biometricUnlockResult = BiometricAuthenticator.AuthenticationResult.Failure(
30+
BiometricUnlockError(BiometricPrompt.ERROR_LOCKOUT, "Biometric auth disabled")
31+
)
32+
),
3133
)
3234
}
3335

3436
fun aPinUnlockState(
3537
pinEntry: PinEntry = PinEntry.createEmpty(4),
36-
remainingAttempts: Int = 3,
38+
remainingAttempts: AsyncData<Int> = AsyncData.Success(3),
3739
showWrongPinTitle: Boolean = false,
3840
showSignOutPrompt: Boolean = false,
3941
showBiometricUnlock: Boolean = true,
@@ -43,7 +45,7 @@ fun aPinUnlockState(
4345
) = PinUnlockState(
4446
pinEntry = AsyncData.Success(pinEntry),
4547
showWrongPinTitle = showWrongPinTitle,
46-
remainingAttempts = AsyncData.Success(remainingAttempts),
48+
remainingAttempts = remainingAttempts,
4749
showSignOutPrompt = showSignOutPrompt,
4850
showBiometricUnlock = showBiometricUnlock,
4951
signOutAction = signOutAction,
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
/*
2+
* Copyright 2025 New Vector Ltd.
3+
*
4+
* SPDX-License-Identifier: AGPL-3.0-only OR LicenseRef-Element-Commercial
5+
* Please see LICENSE files in the repository root for full details.
6+
*/
7+
8+
package io.element.android.features.lockscreen.impl.unlock
9+
10+
import androidx.biometric.BiometricPrompt
11+
import com.google.common.truth.Truth.assertThat
12+
import io.element.android.features.lockscreen.impl.biometric.BiometricAuthenticator
13+
import io.element.android.features.lockscreen.impl.biometric.BiometricUnlockError
14+
import io.element.android.libraries.architecture.AsyncData
15+
import org.junit.Test
16+
17+
class PinUnlockStateTest {
18+
@Test
19+
fun `isSignOutPromptCancellable should have expected values`() {
20+
assertThat(aPinUnlockState(remainingAttempts = AsyncData.Uninitialized).isSignOutPromptCancellable).isTrue()
21+
assertThat(aPinUnlockState(remainingAttempts = AsyncData.Success(1)).isSignOutPromptCancellable).isTrue()
22+
assertThat(aPinUnlockState(remainingAttempts = AsyncData.Success(0)).isSignOutPromptCancellable).isFalse()
23+
}
24+
25+
@Test
26+
fun `biometricUnlockErrorMessage and showBiometricUnlockError should have expected values`() {
27+
listOf(
28+
null,
29+
BiometricAuthenticator.AuthenticationResult.Failure(),
30+
BiometricAuthenticator.AuthenticationResult.Success,
31+
).forEach { biometricUnlockResult ->
32+
aPinUnlockState(
33+
biometricUnlockResult = biometricUnlockResult,
34+
).let {
35+
assertThat(it.biometricUnlockErrorMessage).isNull()
36+
assertThat(it.showBiometricUnlockError).isFalse()
37+
}
38+
}
39+
listOf(
40+
BiometricPrompt.ERROR_HW_UNAVAILABLE,
41+
BiometricPrompt.ERROR_UNABLE_TO_PROCESS,
42+
BiometricPrompt.ERROR_TIMEOUT,
43+
BiometricPrompt.ERROR_NO_SPACE,
44+
BiometricPrompt.ERROR_CANCELED,
45+
BiometricPrompt.ERROR_VENDOR,
46+
BiometricPrompt.ERROR_USER_CANCELED,
47+
BiometricPrompt.ERROR_NO_BIOMETRICS,
48+
BiometricPrompt.ERROR_HW_NOT_PRESENT,
49+
BiometricPrompt.ERROR_NEGATIVE_BUTTON,
50+
BiometricPrompt.ERROR_NO_DEVICE_CREDENTIAL,
51+
BiometricPrompt.ERROR_SECURITY_UPDATE_REQUIRED,
52+
).forEach { code ->
53+
aPinUnlockState(
54+
biometricUnlockResult = BiometricAuthenticator.AuthenticationResult.Failure(
55+
error = BiometricUnlockError(code, "Error message")
56+
),
57+
).let {
58+
assertThat(it.biometricUnlockErrorMessage).isNull()
59+
assertThat(it.showBiometricUnlockError).isFalse()
60+
}
61+
}
62+
listOf(
63+
BiometricPrompt.ERROR_LOCKOUT,
64+
BiometricPrompt.ERROR_LOCKOUT_PERMANENT,
65+
).forEach { code ->
66+
aPinUnlockState(
67+
biometricUnlockResult = BiometricAuthenticator.AuthenticationResult.Failure(
68+
error = BiometricUnlockError(code, "Error message")
69+
),
70+
).let {
71+
assertThat(it.biometricUnlockErrorMessage).isEqualTo("Error message")
72+
assertThat(it.showBiometricUnlockError).isTrue()
73+
}
74+
}
75+
}
76+
}

0 commit comments

Comments
 (0)