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

Commit 993197d2 authored by Andreas Miko's avatar Andreas Miko
Browse files

Consolidate isInTransitionWhere()

Bug: 349785004
Flag: com.android.systemui.scene_container
Test: Added unit test
Change-Id: I49ec149204f2ad0b6d6fa0819492fed2b50bddbe
parent 9d7bbd00
Loading
Loading
Loading
Loading
+43 −88
Original line number Diff line number Diff line
@@ -318,7 +318,7 @@ class KeyguardTransitionInteractorTest : SysuiTestCase() {
    @Test
    fun isInTransitionToAnyState() =
        testScope.runTest {
            val inTransition by collectValues(underTest.isInTransitionToAnyState)
            val inTransition by collectValues(underTest.isInTransition)

            assertEquals(
                listOf(
@@ -373,10 +373,51 @@ class KeyguardTransitionInteractorTest : SysuiTestCase() {
            )
        }

    @Test
    @EnableSceneContainer
    fun isInTransition_withScene() =
        testScope.runTest {
            val inTransition by collectValues(underTest.isInTransition)

            assertEquals(
                listOf(
                    false,
                    true, // The repo is seeded with a transition from OFF to LOCKSCREEN.
                    false,
                ),
                inTransition
            )

            kosmos.setSceneTransition(Transition(Scenes.Gone, Scenes.Bouncer))

            assertEquals(
                listOf(
                    false,
                    true,
                    false,
                    true,
                ),
                inTransition
            )

            kosmos.setSceneTransition(Idle(Scenes.Bouncer))

            assertEquals(
                listOf(
                    false,
                    true,
                    false,
                    true,
                    false,
                ),
                inTransition
            )
        }

    @Test
    fun isInTransitionToAnyState_finishedStateIsStartedStateAfterCancels() =
        testScope.runTest {
            val inTransition by collectValues(underTest.isInTransitionToAnyState)
            val inTransition by collectValues(underTest.isInTransition)

            assertEquals(
                listOf(
@@ -730,92 +771,6 @@ class KeyguardTransitionInteractorTest : SysuiTestCase() {
                )
        }

    @Test
    fun isInTransitionFromStateWhere() =
        testScope.runTest {
            val results by collectValues(underTest.isInTransitionFromStateWhere { it == DOZING })

            sendSteps(
                TransitionStep(AOD, DOZING, 0f, STARTED),
                TransitionStep(AOD, DOZING, 0.5f, RUNNING),
                TransitionStep(AOD, DOZING, 1f, FINISHED),
            )

            assertThat(results)
                .isEqualTo(
                    listOf(
                        false,
                    )
                )

            sendSteps(
                TransitionStep(DOZING, GONE, 0f, STARTED),
            )

            assertThat(results)
                .isEqualTo(
                    listOf(
                        false,
                        true,
                    )
                )

            sendSteps(
                TransitionStep(DOZING, GONE, 0f, RUNNING),
            )

            assertThat(results)
                .isEqualTo(
                    listOf(
                        false,
                        true,
                    )
                )

            sendSteps(
                TransitionStep(DOZING, GONE, 0f, FINISHED),
            )

            assertThat(results)
                .isEqualTo(
                    listOf(
                        false,
                        true,
                        false,
                    )
                )

            sendSteps(
                TransitionStep(GONE, DOZING, 0f, STARTED),
                TransitionStep(GONE, DOZING, 0f, RUNNING),
                TransitionStep(GONE, DOZING, 1f, FINISHED),
            )

            assertThat(results)
                .isEqualTo(
                    listOf(
                        false,
                        true,
                        false,
                    )
                )

            sendSteps(
                TransitionStep(DOZING, GONE, 0f, STARTED),
                TransitionStep(DOZING, GONE, 0f, RUNNING),
            )

            assertThat(results)
                .isEqualTo(
                    listOf(
                        false,
                        true,
                        false,
                        true,
                    )
                )
        }

    @Test
    fun isInTransitionWhere() =
        testScope.runTest {
+8 −0
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import com.android.compose.animation.scene.ObservableTransitionState
import com.android.compose.animation.scene.SceneKey
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.deviceentry.domain.interactor.deviceUnlockedInteractor
import com.android.systemui.flags.EnableSceneContainer
import com.android.systemui.keyguard.data.repository.fakeDeviceEntryFingerprintAuthRepository
import com.android.systemui.keyguard.domain.interactor.keyguardEnabledInteractor
@@ -62,6 +63,13 @@ class SceneInteractorTest : SysuiTestCase() {

    private val underTest = kosmos.sceneInteractor

    init {
        // Init lazy Fixtures. Accessing them once makes sure that the singletons are initialized
        // and therefore starts to collect StateFlows eagerly (when there are any).
        kosmos.deviceUnlockedInteractor
        kosmos.keyguardEnabledInteractor
    }

    @Test
    fun allSceneKeys() {
        assertThat(underTest.allSceneKeys()).isEqualTo(kosmos.sceneKeys)
+1 −1
Original line number Diff line number Diff line
@@ -397,7 +397,7 @@ constructor(
            Pair(keyguardRepository.isKeyguardGoingAway.isFalse(), "keyguardNotGoingAway"),
            Pair(
                keyguardTransitionInteractor
                    .isInTransitionToStateWhere(KeyguardState::deviceIsAsleepInState)
                    .isInTransitionWhere(toStatePredicate = KeyguardState::deviceIsAsleepInState)
                    .isFalse(),
                "deviceNotTransitioningToAsleepState"
            ),
+18 −49
Original line number Diff line number Diff line
@@ -50,6 +50,7 @@ import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.SharedFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filter
import kotlinx.coroutines.flow.map
@@ -75,7 +76,7 @@ constructor(
    private val fromAlternateBouncerTransitionInteractor:
        dagger.Lazy<FromAlternateBouncerTransitionInteractor>,
    private val fromDozingTransitionInteractor: dagger.Lazy<FromDozingTransitionInteractor>,
    private val sceneInteractor: dagger.Lazy<SceneInteractor>,
    private val sceneInteractor: SceneInteractor,
) {
    private val transitionMap = mutableMapOf<Edge.StateToState, MutableSharedFlow<TransitionStep>>()

@@ -194,7 +195,7 @@ constructor(
                fun SceneKey?.isLockscreenOrNull() = this == Scenes.Lockscreen || this == null

                return@filter (fromScene.isLockscreenOrNull() && toScene.isLockscreenOrNull()) ||
                    sceneInteractor.get().transitionState.value.isTransitioning(fromScene, toScene)
                    sceneInteractor.transitionState.value.isTransitioning(fromScene, toScene)
            }
        } else {
            flow
@@ -228,7 +229,7 @@ constructor(
        stateWithoutSceneContainer: KeyguardState,
    ): Flow<Float> {
        return if (SceneContainerFlag.isEnabled) {
            sceneInteractor.get().transitionProgress(scene)
            sceneInteractor.transitionProgress(scene)
        } else {
            transitionValue(stateWithoutSceneContainer)
        }
@@ -408,8 +409,14 @@ constructor(
    internal val currentTransitionInfoInternal: StateFlow<TransitionInfo> =
        repository.currentTransitionInfoInternal

    /** Whether we've currently STARTED a transition and haven't yet FINISHED it. */
    val isInTransitionToAnyState = isInTransitionWhere({ true }, { true })
    val isInTransition =
        combine(
            isInTransitionWhere({ true }, { true }),
            sceneInteractor.transitionState,
        ) { isKeyguardTransitioning, sceneTransitionState ->
            isKeyguardTransitioning ||
                (SceneContainerFlag.isEnabled && sceneTransitionState.isTransitioning())
        }

    /**
     * Called to start a transition that will ultimately dismiss the keyguard from the current
@@ -448,7 +455,7 @@ constructor(
    fun isInTransition(edge: Edge, edgeWithoutSceneContainer: Edge? = null): Flow<Boolean> {
        return if (SceneContainerFlag.isEnabled) {
                if (edge.isSceneWildcardEdge()) {
                    sceneInteractor.get().transitionState.map {
                    sceneInteractor.transitionState.map {
                        when (edge) {
                            is Edge.StateToState ->
                                throw IllegalStateException("Should not be reachable.")
@@ -468,30 +475,6 @@ constructor(
            .distinctUntilChanged()
    }

    /**
     * Whether we're in a transition to a [KeyguardState] that matches the given predicate, but
     * haven't yet completed it.
     *
     * If you only care about a single state, instead use the optimized [isInTransition].
     */
    fun isInTransitionToStateWhere(
        stateMatcher: (KeyguardState) -> Boolean,
    ): Flow<Boolean> {
        return isInTransitionWhere(fromStatePredicate = { true }, toStatePredicate = stateMatcher)
    }

    /**
     * Whether we're in a transition out of a [KeyguardState] that matches the given predicate, but
     * haven't yet completed it.
     *
     * If you only care about a single state, instead use the optimized [isInTransition].
     */
    fun isInTransitionFromStateWhere(
        stateMatcher: (KeyguardState) -> Boolean,
    ): Flow<Boolean> {
        return isInTransitionWhere(fromStatePredicate = stateMatcher, toStatePredicate = { true })
    }

    /**
     * Whether we're in a transition between two [KeyguardState]s that match the given predicates,
     * but haven't yet completed it.
@@ -500,27 +483,15 @@ constructor(
     * [isInTransition].
     */
    fun isInTransitionWhere(
        fromStatePredicate: (KeyguardState) -> Boolean,
        toStatePredicate: (KeyguardState) -> Boolean,
    ): Flow<Boolean> {
        return isInTransitionWhere { from, to -> fromStatePredicate(from) && toStatePredicate(to) }
    }

    /**
     * Whether we're in a transition between two [KeyguardState]s that match the given predicates,
     * but haven't yet completed it.
     *
     * If you only care about a single state for both from and to, instead use the optimized
     * [isInTransition].
     */
    private fun isInTransitionWhere(
        fromToStatePredicate: (KeyguardState, KeyguardState) -> Boolean
        fromStatePredicate: (KeyguardState) -> Boolean = { true },
        toStatePredicate: (KeyguardState) -> Boolean = { true },
    ): Flow<Boolean> {
        return repository.transitions
            .filter { it.transitionState != TransitionState.CANCELED }
            .mapLatest {
                it.transitionState != TransitionState.FINISHED &&
                    fromToStatePredicate(it.from, it.to)
                    fromStatePredicate(it.from) &&
                    toStatePredicate(it.to)
            }
            .distinctUntilChanged()
    }
@@ -532,9 +503,7 @@ constructor(

    fun isFinishedIn(scene: SceneKey, stateWithoutSceneContainer: KeyguardState): Flow<Boolean> {
        return if (SceneContainerFlag.isEnabled) {
            sceneInteractor
                .get()
                .transitionState
            sceneInteractor.transitionState
                .map { it.isIdle(scene) || it.isTransitioning(from = scene) }
                .distinctUntilChanged()
        } else {
+5 −5
Original line number Diff line number Diff line
@@ -129,7 +129,7 @@ constructor(
                    }
                }
            } else {
                transitionInteractor.isInTransitionToAnyState.flatMapLatest { isInTransition ->
                transitionInteractor.isInTransition.flatMapLatest { isInTransition ->
                    if (!isInTransition) {
                        defaultSurfaceBehindVisibility
                    } else {
@@ -206,11 +206,11 @@ constructor(
            transitionInteractor.currentKeyguardState
                .sample(transitionInteractor.startedStepWithPrecedingStep, ::Pair)
                .map { (currentState, startedWithPrev) ->
                    val startedFromStep = startedWithPrev?.previousValue
                    val startedStep = startedWithPrev?.newValue
                    val startedFromStep = startedWithPrev.previousValue
                    val startedStep = startedWithPrev.newValue
                    val returningToGoneAfterCancellation =
                        startedStep?.to == KeyguardState.GONE &&
                            startedFromStep?.transitionState == TransitionState.CANCELED &&
                        startedStep.to == KeyguardState.GONE &&
                            startedFromStep.transitionState == TransitionState.CANCELED &&
                            startedFromStep.from == KeyguardState.GONE

                    if (!returningToGoneAfterCancellation) {
Loading