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

Commit 1197d0e1 authored by Alejandro Nijamkin's avatar Alejandro Nijamkin
Browse files

KeyguardTransitionInteractor#transitionValue.

This is a function that returns a flow of all transitions in or out of
the given KeyguardState. Should be useful when a consumer cares about a
specific state but doesn't care about which other state we're move in
from or moving out to.

Bug: 274159734
Flag: N/A
Test: unit test included
Test: actively calling this from the next CL
Change-Id: Iab6a756892a388e306562eae1a18a17e32112417
parent 0628dfba
Loading
Loading
Loading
Loading
+25 −9
Original line number Diff line number Diff line
@@ -39,7 +39,7 @@ import kotlinx.coroutines.flow.merge
class KeyguardTransitionInteractor
@Inject
constructor(
    repository: KeyguardTransitionRepository,
    private val repository: KeyguardTransitionRepository,
) {
    /** (any)->GONE transition information */
    val anyStateToGoneTransition: Flow<TransitionStep> =
@@ -62,10 +62,6 @@ constructor(
    /** LOCKSCREEN->AOD transition information. */
    val lockscreenToAodTransition: Flow<TransitionStep> = repository.transition(LOCKSCREEN, AOD)

    /** LOCKSCREEN->PRIMARY_BOUNCER transition information. */
    val mLockscreenToPrimaryBouncerTransition: Flow<TransitionStep> =
        repository.transition(LOCKSCREEN, PRIMARY_BOUNCER)

    /** LOCKSCREEN->DREAMING transition information. */
    val lockscreenToDreamingTransition: Flow<TransitionStep> =
        repository.transition(LOCKSCREEN, DREAMING)
@@ -92,19 +88,39 @@ constructor(
            lockscreenToAodTransition,
        )

    /* The last [TransitionStep] with a [TransitionState] of STARTED */
    /** The last [TransitionStep] with a [TransitionState] of STARTED */
    val startedKeyguardTransitionStep: Flow<TransitionStep> =
        repository.transitions.filter { step -> step.transitionState == TransitionState.STARTED }

    /* The last [TransitionStep] with a [TransitionState] of CANCELED */
    /** The last [TransitionStep] with a [TransitionState] of CANCELED */
    val canceledKeyguardTransitionStep: Flow<TransitionStep> =
        repository.transitions.filter { step -> step.transitionState == TransitionState.CANCELED }

    /* The last [TransitionStep] with a [TransitionState] of FINISHED */
    /** The last [TransitionStep] with a [TransitionState] of FINISHED */
    val finishedKeyguardTransitionStep: Flow<TransitionStep> =
        repository.transitions.filter { step -> step.transitionState == TransitionState.FINISHED }

    /* The last completed [KeyguardState] transition */
    /** The last completed [KeyguardState] transition */
    val finishedKeyguardState: Flow<KeyguardState> =
        finishedKeyguardTransitionStep.map { step -> step.to }

    /**
     * The amount of transition into or out of the given [KeyguardState].
     *
     * The value will be `0` (or close to `0`, due to float point arithmetic) if not in this step or
     * `1` when fully in the given state.
     */
    fun transitionValue(
        state: KeyguardState,
    ): Flow<Float> {
        return repository.transitions
            .filter { it.from == state || it.to == state }
            .map {
                if (it.from == state) {
                    1 - it.value
                } else {
                    it.value
                }
            }
    }
}
+134 −122
Original line number Diff line number Diff line
@@ -20,9 +20,10 @@ package com.android.systemui.keyguard.domain.interactor
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectValues
import com.android.systemui.keyguard.data.repository.FakeKeyguardTransitionRepository
import com.android.systemui.keyguard.shared.model.KeyguardState
import com.android.systemui.keyguard.shared.model.KeyguardState.AOD
import com.android.systemui.keyguard.shared.model.KeyguardState.DOZING
import com.android.systemui.keyguard.shared.model.KeyguardState.GONE
import com.android.systemui.keyguard.shared.model.KeyguardState.LOCKSCREEN
import com.android.systemui.keyguard.shared.model.TransitionState.FINISHED
@@ -30,9 +31,7 @@ import com.android.systemui.keyguard.shared.model.TransitionState.RUNNING
import com.android.systemui.keyguard.shared.model.TransitionState.STARTED
import com.android.systemui.keyguard.shared.model.TransitionStep
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
@@ -53,19 +52,9 @@ class KeyguardTransitionInteractorTest : SysuiTestCase() {
    }

    @Test
    fun `transition collectors receives only appropriate events`() =
        runTest(UnconfinedTestDispatcher()) {
            var lockscreenToAodSteps = mutableListOf<TransitionStep>()
            val job1 =
                underTest.lockscreenToAodTransition
                    .onEach { lockscreenToAodSteps.add(it) }
                    .launchIn(this)

            var aodToLockscreenSteps = mutableListOf<TransitionStep>()
            val job2 =
                underTest.aodToLockscreenTransition
                    .onEach { aodToLockscreenSteps.add(it) }
                    .launchIn(this)
    fun `transition collectors receives only appropriate events`() = runTest {
        val lockscreenToAodSteps by collectValues(underTest.lockscreenToAodTransition)
        val aodToLockscreenSteps by collectValues(underTest.aodToLockscreenTransition)

        val steps = mutableListOf<TransitionStep>()
        steps.add(TransitionStep(AOD, GONE, 0f, STARTED))
@@ -77,20 +66,18 @@ class KeyguardTransitionInteractorTest : SysuiTestCase() {
        steps.add(TransitionStep(LOCKSCREEN, AOD, 0.1f, RUNNING))
        steps.add(TransitionStep(LOCKSCREEN, AOD, 0.2f, RUNNING))

            steps.forEach { repository.sendTransitionStep(it) }
        steps.forEach {
            repository.sendTransitionStep(it)
            runCurrent()
        }

        assertThat(aodToLockscreenSteps).isEqualTo(steps.subList(2, 5))
        assertThat(lockscreenToAodSteps).isEqualTo(steps.subList(5, 8))

            job1.cancel()
            job2.cancel()
    }

    @Test
    fun dozeAmountTransitionTest() =
        runTest(UnconfinedTestDispatcher()) {
            var dozeAmountSteps = mutableListOf<TransitionStep>()
            val job = underTest.dozeAmountTransition.onEach { dozeAmountSteps.add(it) }.launchIn(this)
    fun dozeAmountTransitionTest() = runTest {
        val dozeAmountSteps by collectValues(underTest.dozeAmountTransition)

        val steps = mutableListOf<TransitionStep>()

@@ -102,7 +89,10 @@ class KeyguardTransitionInteractorTest : SysuiTestCase() {
        steps.add(TransitionStep(LOCKSCREEN, AOD, 0.9f, RUNNING))
        steps.add(TransitionStep(LOCKSCREEN, AOD, 1f, FINISHED))

            steps.forEach { repository.sendTransitionStep(it) }
        steps.forEach {
            repository.sendTransitionStep(it)
            runCurrent()
        }

        assertThat(dozeAmountSteps.subList(0, 3))
            .isEqualTo(
@@ -113,15 +103,11 @@ class KeyguardTransitionInteractorTest : SysuiTestCase() {
                )
            )
        assertThat(dozeAmountSteps.subList(3, 7)).isEqualTo(steps.subList(3, 7))

            job.cancel()
    }

    @Test
    fun keyguardStateTests() =
        runTest(UnconfinedTestDispatcher()) {
            var finishedSteps = mutableListOf<KeyguardState>()
            val job = underTest.finishedKeyguardState.onEach { finishedSteps.add(it) }.launchIn(this)
    fun keyguardStateTests() = runTest {
        val finishedSteps by collectValues(underTest.finishedKeyguardState)

        val steps = mutableListOf<TransitionStep>()

@@ -133,19 +119,17 @@ class KeyguardTransitionInteractorTest : SysuiTestCase() {
        steps.add(TransitionStep(LOCKSCREEN, AOD, 1f, FINISHED))
        steps.add(TransitionStep(AOD, GONE, 1f, STARTED))

            steps.forEach { repository.sendTransitionStep(it) }
        steps.forEach {
            repository.sendTransitionStep(it)
            runCurrent()
        }

        assertThat(finishedSteps).isEqualTo(listOf(LOCKSCREEN, AOD))

            job.cancel()
    }

    @Test
    fun finishedKeyguardTransitionStepTests() =
        runTest(UnconfinedTestDispatcher()) {
            var finishedSteps = mutableListOf<TransitionStep>()
            val job =
                underTest.finishedKeyguardTransitionStep.onEach { finishedSteps.add(it) }.launchIn(this)
    fun finishedKeyguardTransitionStepTests() = runTest {
        val finishedSteps by collectValues(underTest.finishedKeyguardTransitionStep)

        val steps = mutableListOf<TransitionStep>()

@@ -157,19 +141,17 @@ class KeyguardTransitionInteractorTest : SysuiTestCase() {
        steps.add(TransitionStep(LOCKSCREEN, AOD, 1f, FINISHED))
        steps.add(TransitionStep(AOD, GONE, 1f, STARTED))

            steps.forEach { repository.sendTransitionStep(it) }
        steps.forEach {
            repository.sendTransitionStep(it)
            runCurrent()
        }

        assertThat(finishedSteps).isEqualTo(listOf(steps[2], steps[5]))

            job.cancel()
    }

    @Test
    fun startedKeyguardTransitionStepTests() =
        runTest(UnconfinedTestDispatcher()) {
            var startedSteps = mutableListOf<TransitionStep>()
            val job =
                underTest.startedKeyguardTransitionStep.onEach { startedSteps.add(it) }.launchIn(this)
    fun startedKeyguardTransitionStepTests() = runTest {
        val startedSteps by collectValues(underTest.startedKeyguardTransitionStep)

        val steps = mutableListOf<TransitionStep>()

@@ -181,10 +163,40 @@ class KeyguardTransitionInteractorTest : SysuiTestCase() {
        steps.add(TransitionStep(LOCKSCREEN, AOD, 1f, FINISHED))
        steps.add(TransitionStep(AOD, GONE, 1f, STARTED))

            steps.forEach { repository.sendTransitionStep(it) }
        steps.forEach {
            repository.sendTransitionStep(it)
            runCurrent()
        }

        assertThat(startedSteps).isEqualTo(listOf(steps[0], steps[3], steps[6]))
    }

    @Test
    fun transitionValue() = runTest {
        val startedSteps by collectValues(underTest.transitionValue(state = DOZING))

        val toSteps =
            listOf(
                TransitionStep(AOD, DOZING, 0f, STARTED),
                TransitionStep(AOD, DOZING, 0.5f, RUNNING),
                TransitionStep(AOD, DOZING, 1f, FINISHED),
            )
        toSteps.forEach {
            repository.sendTransitionStep(it)
            runCurrent()
        }

        val fromSteps =
            listOf(
                TransitionStep(DOZING, LOCKSCREEN, 0f, STARTED),
                TransitionStep(DOZING, LOCKSCREEN, 0.5f, RUNNING),
                TransitionStep(DOZING, LOCKSCREEN, 1f, FINISHED),
            )
        fromSteps.forEach {
            repository.sendTransitionStep(it)
            runCurrent()
        }

            job.cancel()
        assertThat(startedSteps).isEqualTo(listOf(0f, 0.5f, 1f, 1f, 0.5f, 0f))
    }
}