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

Commit 69bfa998 authored by Yining Liu's avatar Yining Liu
Browse files

Add unit tests for the UpdateSource

Add unit tests to the OriginalUnseenKeyguardCoordinatorTest to test the
onEntryUpdated method by different sources. A seen notification should
only be marked as unseen if it's updated by the App, not the
SystemServer or SystemUi.

Fix: 399173134
Test: OriginalUnseenKeyguardCoordinatorTest
Flag: TEST_ONLY
Change-Id: I2214985d0fdad58263c1cf69278c7e9d6d54e01a
parent 0d377b22
Loading
Loading
Loading
Loading
+126 −0
Original line number Original line Diff line number Diff line
@@ -633,6 +633,132 @@ class OriginalUnseenKeyguardCoordinatorTest(flags: FlagsParameterization) : Sysu
        }
        }
    }
    }


    @Test
    fun seenNotificationOnKeyguardMarkedAsSeenIfUpdatedBySystemServer() {
        // GIVEN: Keyguard is showing, not dozing, unseen notification is present
        keyguardRepository.setKeyguardShowing(true)
        keyguardRepository.setIsDozing(false)
        runKeyguardCoordinatorTest {
            val fakeEntry = NotificationEntryBuilder().build()
            collectionListener.onEntryAdded(fakeEntry)
            keyguardTransitionRepository.sendTransitionSteps(
                from = KeyguardState.AOD,
                to = KeyguardState.LOCKSCREEN,
                this.testScheduler,
            )
            testScheduler.runCurrent()

            // WHEN: five seconds have passed
            testScheduler.advanceTimeBy(5.seconds)
            testScheduler.runCurrent()

            // WHEN: Keyguard is no longer showing
            keyguardRepository.setKeyguardShowing(false)
            kosmos.setTransition(
                sceneTransition = Idle(Scenes.Gone),
                stateTransition = TransitionStep(KeyguardState.LOCKSCREEN, KeyguardState.GONE),
            )

            // WHEN: the notification is updated by the Server
            collectionListener.onEntryUpdated(fakeEntry, UpdateSource.SystemServer)
            testScheduler.runCurrent()

            // WHEN: Keyguard is shown again
            keyguardRepository.setKeyguardShowing(true)
            kosmos.setTransition(
                sceneTransition = Idle(Scenes.Lockscreen),
                stateTransition = TransitionStep(KeyguardState.GONE, KeyguardState.AOD),
            )

            // THEN: The notification is still recognized as "seen" and is filtered out.
            assertThat(unseenFilter.shouldFilterOut(fakeEntry, 0L)).isTrue()
        }
    }

    @Test
    fun seenNotificationOnKeyguardMarkedAsSeenIfUpdatedBySystemUi() {
        // GIVEN: Keyguard is showing, not dozing, unseen notification is present
        keyguardRepository.setKeyguardShowing(true)
        keyguardRepository.setIsDozing(false)
        runKeyguardCoordinatorTest {
            val fakeEntry = NotificationEntryBuilder().build()
            collectionListener.onEntryAdded(fakeEntry)
            keyguardTransitionRepository.sendTransitionSteps(
                from = KeyguardState.AOD,
                to = KeyguardState.LOCKSCREEN,
                this.testScheduler,
            )
            testScheduler.runCurrent()

            // WHEN: five seconds have passed
            testScheduler.advanceTimeBy(5.seconds)
            testScheduler.runCurrent()

            // WHEN: Keyguard is no longer showing
            keyguardRepository.setKeyguardShowing(false)
            kosmos.setTransition(
                sceneTransition = Idle(Scenes.Gone),
                stateTransition = TransitionStep(KeyguardState.LOCKSCREEN, KeyguardState.GONE),
            )

            // WHEN: the notification is updated by the SystemUi
            collectionListener.onEntryUpdated(fakeEntry, UpdateSource.SystemUi)
            testScheduler.runCurrent()

            // WHEN: Keyguard is shown again
            keyguardRepository.setKeyguardShowing(true)
            kosmos.setTransition(
                sceneTransition = Idle(Scenes.Lockscreen),
                stateTransition = TransitionStep(KeyguardState.GONE, KeyguardState.AOD),
            )

            // THEN: The notification is still recognized as "seen" and is filtered out.
            assertThat(unseenFilter.shouldFilterOut(fakeEntry, 0L)).isTrue()
        }
    }

    @Test
    fun seenNotificationOnKeyguardMarkedAsUnseenIfUpdatedByApp() {
        // GIVEN: Keyguard is showing, not dozing, unseen notification is present
        keyguardRepository.setKeyguardShowing(true)
        keyguardRepository.setIsDozing(false)
        runKeyguardCoordinatorTest {
            val fakeEntry = NotificationEntryBuilder().build()
            collectionListener.onEntryAdded(fakeEntry)
            keyguardTransitionRepository.sendTransitionSteps(
                from = KeyguardState.AOD,
                to = KeyguardState.LOCKSCREEN,
                this.testScheduler,
            )
            testScheduler.runCurrent()

            // WHEN: five seconds have passed
            testScheduler.advanceTimeBy(5.seconds)
            testScheduler.runCurrent()

            // WHEN: Keyguard is no longer showing
            keyguardRepository.setKeyguardShowing(false)
            kosmos.setTransition(
                sceneTransition = Idle(Scenes.Gone),
                stateTransition = TransitionStep(KeyguardState.LOCKSCREEN, KeyguardState.GONE),
            )

            // WHEN: the notification is updated by the App
            collectionListener.onEntryUpdated(fakeEntry, UpdateSource.App)
            testScheduler.runCurrent()

            // WHEN: Keyguard is shown again
            keyguardRepository.setKeyguardShowing(true)
            kosmos.setTransition(
                sceneTransition = Idle(Scenes.Lockscreen),
                stateTransition = TransitionStep(KeyguardState.GONE, KeyguardState.AOD),
            )

            // THEN: The notification is now recognized as "unseen" and is not filtered out.
            assertThat(unseenFilter.shouldFilterOut(fakeEntry, 0L)).isFalse()
        }
    }

    private fun runKeyguardCoordinatorTest(
    private fun runKeyguardCoordinatorTest(
        testBlock: suspend KeyguardCoordinatorTestScope.() -> Unit
        testBlock: suspend KeyguardCoordinatorTestScope.() -> Unit
    ) {
    ) {