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

Commit db09257c authored by Josh Tsuji's avatar Josh Tsuji
Browse files

Remove ugly hack now that we're using newer mockito.

Several tests were failing due to the hack not correctly testing transitions, so fix those as well.

Fixes: 331799060
Test: better now
Flag: NA
Change-Id: Ie8d9d297e69b59d994865f410a660745d426e9ee
parent 31d18e5b
Loading
Loading
Loading
Loading
+6 −14
Original line number Original line Diff line number Diff line
@@ -224,14 +224,7 @@ sealed class TransitionInteractor(
    ) {
    ) {
        if (!KeyguardWmStateRefactor.isEnabled) {
        if (!KeyguardWmStateRefactor.isEnabled) {
            scope.launch {
            scope.launch {
                keyguardInteractor.onCameraLaunchDetected
                keyguardInteractor.onCameraLaunchDetected.filterRelevantKeyguardState().collect {
                    .sample(transitionInteractor.finishedKeyguardState)
                    .collect { finishedKeyguardState ->
                        // Other keyguard state transitions may trigger on the first power button
                        // push,
                        // so use the last finishedKeyguardState to determine the overriding FROM
                        // state
                        if (finishedKeyguardState == fromState) {
                    startTransitionTo(
                    startTransitionTo(
                        toState = KeyguardState.OCCLUDED,
                        toState = KeyguardState.OCCLUDED,
                        modeOnCanceled = TransitionModeOnCanceled.RESET,
                        modeOnCanceled = TransitionModeOnCanceled.RESET,
@@ -240,7 +233,6 @@ sealed class TransitionInteractor(
            }
            }
        }
        }
    }
    }
    }


    /**
    /**
     * Whether we're in the KeyguardState relevant to this From*TransitionInteractor (which we know
     * Whether we're in the KeyguardState relevant to this From*TransitionInteractor (which we know
+16 −36
Original line number Original line Diff line number Diff line
@@ -754,35 +754,6 @@ class KeyguardTransitionScenariosTest : SysuiTestCase() {
            coroutineContext.cancelChildren()
            coroutineContext.cancelChildren()
        }
        }


    @Test
    fun goneToDreamingLockscreenHosted() =
        testScope.runTest {
            // GIVEN a device that is not dreaming or dozing
            keyguardRepository.setDreamingWithOverlay(false)
            keyguardRepository.setDozeTransitionModel(
                DozeTransitionModel(from = DozeStateModel.DOZE, to = DozeStateModel.FINISH)
            )
            runCurrent()

            // GIVEN a prior transition has run to GONE
            runTransitionAndSetWakefulness(KeyguardState.LOCKSCREEN, KeyguardState.GONE)

            // WHEN the device begins to dream with the lockscreen hosted dream
            keyguardRepository.setDreamingWithOverlay(true)
            keyguardRepository.setIsActiveDreamLockscreenHosted(true)
            advanceTimeBy(100L)

            assertThat(transitionRepository)
                .startedTransition(
                    to = KeyguardState.DREAMING_LOCKSCREEN_HOSTED,
                    from = KeyguardState.GONE,
                    ownerName = "FromGoneTransitionInteractor",
                    animatorAssertion = { it.isNotNull() }
                )

            coroutineContext.cancelChildren()
        }

    @Test
    @Test
    fun goneToGlanceableHub() =
    fun goneToGlanceableHub() =
        testScope.runTest {
        testScope.runTest {
@@ -1244,9 +1215,15 @@ class KeyguardTransitionScenariosTest : SysuiTestCase() {
            keyguardRepository.setKeyguardOccluded(true)
            keyguardRepository.setKeyguardOccluded(true)
            runCurrent()
            runCurrent()


            // GIVEN device is docked
            // GIVEN device is docked/communal is available
            dockManager.setIsDocked(true)
            dockManager.setIsDocked(true)
            dockManager.setDockEvent(DockManager.STATE_DOCKED)
            dockManager.setDockEvent(DockManager.STATE_DOCKED)
            val idleTransitionState =
                MutableStateFlow<ObservableTransitionState>(
                    ObservableTransitionState.Idle(CommunalScenes.Communal)
                )
            communalInteractor.setTransitionState(idleTransitionState)
            runCurrent()


            // WHEN occlusion ends
            // WHEN occlusion ends
            keyguardRepository.setKeyguardOccluded(false)
            keyguardRepository.setKeyguardOccluded(false)
@@ -1372,6 +1349,8 @@ class KeyguardTransitionScenariosTest : SysuiTestCase() {


            // WHEN the keyguard is occluded and device wakes up and is no longer dreaming
            // WHEN the keyguard is occluded and device wakes up and is no longer dreaming
            keyguardRepository.setDreaming(false)
            keyguardRepository.setDreaming(false)
            testScheduler.advanceTimeBy(150) // The dreaming signal is debounced.
            runCurrent()
            keyguardRepository.setKeyguardOccluded(true)
            keyguardRepository.setKeyguardOccluded(true)
            powerInteractor.setAwakeForTest()
            powerInteractor.setAwakeForTest()
            runCurrent()
            runCurrent()
@@ -1379,7 +1358,7 @@ class KeyguardTransitionScenariosTest : SysuiTestCase() {
            // THEN a transition to OCCLUDED should occur
            // THEN a transition to OCCLUDED should occur
            assertThat(transitionRepository)
            assertThat(transitionRepository)
                .startedTransition(
                .startedTransition(
                    ownerName = "FromDreamingTransitionInteractor",
                    ownerName = "FromDreamingTransitionInteractor(Occluded but no longer dreaming)",
                    from = KeyguardState.DREAMING,
                    from = KeyguardState.DREAMING,
                    to = KeyguardState.OCCLUDED,
                    to = KeyguardState.OCCLUDED,
                    animatorAssertion = { it.isNotNull() },
                    animatorAssertion = { it.isNotNull() },
@@ -1516,7 +1495,7 @@ class KeyguardTransitionScenariosTest : SysuiTestCase() {
            // THEN a transition to OCCLUDED should occur
            // THEN a transition to OCCLUDED should occur
            assertThat(transitionRepository)
            assertThat(transitionRepository)
                .startedTransition(
                .startedTransition(
                    ownerName = "FromAodTransitionInteractor",
                    ownerName = "FromAodTransitionInteractor(isOccluded = true)",
                    from = KeyguardState.AOD,
                    from = KeyguardState.AOD,
                    to = KeyguardState.OCCLUDED,
                    to = KeyguardState.OCCLUDED,
                    animatorAssertion = { it.isNotNull() },
                    animatorAssertion = { it.isNotNull() },
@@ -1555,7 +1534,8 @@ class KeyguardTransitionScenariosTest : SysuiTestCase() {
            runTransitionAndSetWakefulness(KeyguardState.AOD, KeyguardState.LOCKSCREEN)
            runTransitionAndSetWakefulness(KeyguardState.AOD, KeyguardState.LOCKSCREEN)
            runCurrent()
            runCurrent()


            // WHEN the device begins to sleep (first power button press)...
            // WHEN the device begins to sleep (first power button press), which starts
            // LS -> DOZING...
            powerInteractor.setAsleepForTest()
            powerInteractor.setAsleepForTest()
            runCurrent()
            runCurrent()
            reset(transitionRepository)
            reset(transitionRepository)
@@ -1568,11 +1548,11 @@ class KeyguardTransitionScenariosTest : SysuiTestCase() {
            }
            }
            runCurrent()
            runCurrent()


            // THEN a transition from LOCKSCREEN => OCCLUDED should occur
            // THEN a transition from DOZING => OCCLUDED should occur
            assertThat(transitionRepository)
            assertThat(transitionRepository)
                .startedTransition(
                .startedTransition(
                    ownerName = "FromLockscreenTransitionInteractor",
                    ownerName = "FromDozingTransitionInteractor",
                    from = KeyguardState.LOCKSCREEN,
                    from = KeyguardState.DOZING,
                    to = KeyguardState.OCCLUDED,
                    to = KeyguardState.OCCLUDED,
                    animatorAssertion = { it.isNotNull() },
                    animatorAssertion = { it.isNotNull() },
                )
                )
+7 −18
Original line number Original line Diff line number Diff line
@@ -21,6 +21,8 @@ import com.android.systemui.keyguard.data.repository.KeyguardTransitionRepositor
import com.android.systemui.keyguard.shared.model.KeyguardState
import com.android.systemui.keyguard.shared.model.KeyguardState
import com.android.systemui.keyguard.shared.model.TransitionInfo
import com.android.systemui.keyguard.shared.model.TransitionInfo
import com.android.systemui.keyguard.shared.model.TransitionModeOnCanceled
import com.android.systemui.keyguard.shared.model.TransitionModeOnCanceled
import com.android.systemui.util.mockito.any
import com.android.systemui.util.mockito.withArgCaptor
import com.google.common.truth.FailureMetadata
import com.google.common.truth.FailureMetadata
import com.google.common.truth.Subject
import com.google.common.truth.Subject
import com.google.common.truth.Truth
import com.google.common.truth.Truth
@@ -28,6 +30,8 @@ import com.google.common.truth.Truth.assertAbout
import junit.framework.Assert.assertEquals
import junit.framework.Assert.assertEquals
import kotlin.test.fail
import kotlin.test.fail
import org.mockito.Mockito
import org.mockito.Mockito
import org.mockito.Mockito.never
import org.mockito.Mockito.verify


/** [Subject] used to make assertions about a [Mockito.spy] KeyguardTransitionRepository. */
/** [Subject] used to make assertions about a [Mockito.spy] KeyguardTransitionRepository. */
class KeyguardTransitionRepositorySpySubject
class KeyguardTransitionRepositorySpySubject
@@ -75,34 +79,19 @@ private constructor(
        animatorAssertion: (Subject) -> Unit,
        animatorAssertion: (Subject) -> Unit,
        modeOnCanceled: TransitionModeOnCanceled? = null,
        modeOnCanceled: TransitionModeOnCanceled? = null,
    ) {
    ) {
        // TODO(b/331799060): Remove this workaround once atest supports mocking suspend functions.
        withArgCaptor<TransitionInfo> { verify(repository).startTransition(capture()) }
        Mockito.mockingDetails(repository).invocations.forEach { invocation ->
            .also { transitionInfo ->
            if (invocation.method.equals(KeyguardTransitionRepository::startTransition.name)) {
                val transitionInfo = invocation.arguments.firstOrNull() as TransitionInfo
                assertEquals(to, transitionInfo.to)
                assertEquals(to, transitionInfo.to)
                animatorAssertion.invoke(Truth.assertThat(transitionInfo.animator))
                animatorAssertion.invoke(Truth.assertThat(transitionInfo.animator))
                from?.let { assertEquals(it, transitionInfo.from) }
                from?.let { assertEquals(it, transitionInfo.from) }
                ownerName?.let { assertEquals(it, transitionInfo.ownerName) }
                ownerName?.let { assertEquals(it, transitionInfo.ownerName) }
                modeOnCanceled?.let { assertEquals(it, transitionInfo.modeOnCanceled) }
                modeOnCanceled?.let { assertEquals(it, transitionInfo.modeOnCanceled) }
                invocation.markVerified()
            }
            }
            }
    }
    }


    /** Verifies that [KeyguardTransitionRepository.startTransition] was never called. */
    /** Verifies that [KeyguardTransitionRepository.startTransition] was never called. */
    suspend fun noTransitionsStarted() {
    suspend fun noTransitionsStarted() {
        // TODO(b/331799060): Remove this workaround once atest supports mocking suspend functions.
        verify(repository, never()).startTransition(any())
        Mockito.mockingDetails(repository).invocations.forEach {
            if (
                it.method.equals(KeyguardTransitionRepository::startTransition.name) &&
                    !it.isVerified
            ) {
                fail(
                    "Expected no transitions started, however this transition was started: " +
                        it.arguments.firstOrNull()
                )
            }
        }
    }
    }


    companion object {
    companion object {