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

Commit 9e58a42d authored by burakov's avatar burakov
Browse files

[flexiglass] Always round up the remaining throttle milliseconds.

This fixes a bug where we show the user there are 0 seconds remaining
before they can try to re-authenticate during throttling.

Bonus: since we only store this value in the model for presenting it to
the user, we should store it in seconds directly rather than converting
from milliseconds when formatting the message (which happens every
second during throttling).

Fix: 314308690
Bug: 314757822
Test: Unit tests updated and still pass.
Flag: ACONFIG com.android.systemui.scene_container DEVELOPMENT
Change-Id: Ifbf4708b687c037877784b29427c56ee2f20ab37
parent 516fdd0b
Loading
Loading
Loading
Loading
+5 −11
Original line number Diff line number Diff line
@@ -27,8 +27,6 @@ import com.android.systemui.authentication.shared.model.AuthenticationThrottling
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.scene.SceneTestUtils
import com.google.common.truth.Truth.assertThat
import kotlin.time.Duration.Companion.milliseconds
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.advanceTimeBy
import kotlinx.coroutines.test.runCurrent
@@ -335,7 +333,7 @@ class AuthenticationInteractorTest : SysuiTestCase() {
                    AuthenticationThrottlingModel(
                        failedAttemptCount =
                            FakeAuthenticationRepository.MAX_FAILED_AUTH_TRIES_BEFORE_THROTTLING,
                        remainingMs = FakeAuthenticationRepository.THROTTLE_DURATION_MS,
                        remainingSeconds = FakeAuthenticationRepository.THROTTLE_DURATION_SECONDS,
                    )
                )

@@ -347,15 +345,12 @@ class AuthenticationInteractorTest : SysuiTestCase() {
                    AuthenticationThrottlingModel(
                        failedAttemptCount =
                            FakeAuthenticationRepository.MAX_FAILED_AUTH_TRIES_BEFORE_THROTTLING,
                        remainingMs = FakeAuthenticationRepository.THROTTLE_DURATION_MS,
                        remainingSeconds = FakeAuthenticationRepository.THROTTLE_DURATION_SECONDS,
                    )
                )

            // Move the clock forward to ALMOST skip the throttling, leaving one second to go:
            val throttleTimeoutSec =
                FakeAuthenticationRepository.THROTTLE_DURATION_MS.milliseconds.inWholeSeconds
                    .toInt()
            repeat(throttleTimeoutSec - 1) { time ->
            repeat(FakeAuthenticationRepository.THROTTLE_DURATION_SECONDS - 1) { time ->
                advanceTimeBy(1000)
                assertThat(throttling)
                    .isEqualTo(
@@ -363,9 +358,8 @@ class AuthenticationInteractorTest : SysuiTestCase() {
                            failedAttemptCount =
                                FakeAuthenticationRepository
                                    .MAX_FAILED_AUTH_TRIES_BEFORE_THROTTLING,
                            remainingMs =
                                ((throttleTimeoutSec - (time + 1)).seconds.inWholeMilliseconds)
                                    .toInt(),
                            remainingSeconds =
                                FakeAuthenticationRepository.THROTTLE_DURATION_SECONDS - (time + 1),
                        )
                    )
            }
+2 −4
Original line number Diff line number Diff line
@@ -29,7 +29,6 @@ import com.android.systemui.keyguard.domain.interactor.KeyguardFaceAuthInteracto
import com.android.systemui.res.R
import com.android.systemui.scene.SceneTestUtils
import com.google.common.truth.Truth.assertThat
import kotlin.math.ceil
import kotlin.time.Duration.Companion.milliseconds
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.advanceTimeBy
@@ -268,7 +267,7 @@ class BouncerInteractorTest : SysuiTestCase() {
                    AuthenticationThrottlingModel(
                        failedAttemptCount =
                            FakeAuthenticationRepository.MAX_FAILED_AUTH_TRIES_BEFORE_THROTTLING,
                        remainingMs = FakeAuthenticationRepository.THROTTLE_DURATION_MS,
                        remainingSeconds = FakeAuthenticationRepository.THROTTLE_DURATION_SECONDS,
                    )
                )
            assertTryAgainMessage(
@@ -286,8 +285,7 @@ class BouncerInteractorTest : SysuiTestCase() {
                    .toInt()
            )

            throttling?.remainingMs?.let { remainingMs ->
                val seconds = ceil(remainingMs / 1000f).toInt()
            throttling?.remainingSeconds?.let { seconds ->
                repeat(seconds) { time ->
                    advanceTimeBy(1000)
                    val remainingTimeSec = seconds - time - 1
+7 −2
Original line number Diff line number Diff line
@@ -26,6 +26,7 @@ import com.android.systemui.flags.Flags
import com.android.systemui.scene.SceneTestUtils
import com.google.common.truth.Truth.assertThat
import com.google.common.truth.Truth.assertWithMessage
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.emptyFlow
import kotlinx.coroutines.flow.flatMapLatest
@@ -144,7 +145,9 @@ class BouncerViewModelTest : SysuiTestCase() {
            }
            assertThat(message?.isUpdateAnimated).isFalse()

            throttling?.remainingMs?.let { remainingMs -> advanceTimeBy(remainingMs.toLong()) }
            throttling?.remainingSeconds?.let { remainingSeconds ->
                advanceTimeBy(remainingSeconds.seconds.inWholeMilliseconds)
            }
            assertThat(message?.isUpdateAnimated).isTrue()
        }

@@ -167,7 +170,9 @@ class BouncerViewModelTest : SysuiTestCase() {
            }
            assertThat(isInputEnabled).isFalse()

            throttling?.remainingMs?.let { milliseconds -> advanceTimeBy(milliseconds.toLong()) }
            throttling?.remainingSeconds?.let { remainingSeconds ->
                advanceTimeBy(remainingSeconds.seconds.inWholeMilliseconds)
            }
            assertThat(isInputEnabled).isTrue()
        }

+3 −3
Original line number Diff line number Diff line
@@ -335,12 +335,12 @@ class PasswordBouncerViewModelTest : SysuiTestCase() {
            repeat(failedAttemptCount) {
                authenticationRepository.reportAuthenticationAttempt(false)
            }
            val remainingTimeMs = 30_000
            authenticationRepository.setThrottleDuration(remainingTimeMs)
            val remainingTimeSeconds = 30
            authenticationRepository.setThrottleDuration(remainingTimeSeconds * 1000)
            authenticationRepository.throttling.value =
                AuthenticationThrottlingModel(
                    failedAttemptCount = failedAttemptCount,
                    remainingMs = remainingTimeMs,
                    remainingSeconds = remainingTimeSeconds,
                )
        } else {
            authenticationRepository.reportAuthenticationAttempt(true)
+2 −1
Original line number Diff line number Diff line
@@ -29,6 +29,7 @@ import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.user.data.repository.UserRepository
import com.android.systemui.util.time.SystemClock
import javax.inject.Inject
import kotlin.math.ceil
import kotlin.math.max
import kotlin.time.Duration.Companion.seconds
import kotlinx.coroutines.CoroutineDispatcher
@@ -283,7 +284,7 @@ constructor(
                if (remainingMs > 0) {
                    AuthenticationThrottlingModel(
                        failedAttemptCount = failedAttemptCount.await(),
                        remainingMs = remainingMs.toInt(),
                        remainingSeconds = ceil(remainingMs / 1000f).toInt(),
                    )
                } else {
                    null // Throttling ended.
Loading