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

Commit 8c442701 authored by Juan Sebastian Martinez's avatar Juan Sebastian Martinez
Browse files

Ensuring MagneticRowListeners are cleared after each test.

For tests, we use a delegate that allows to test the cancelling calls of
real animators. We also add a cleanup @After method to make sure all
listeners are cleared (by calling MagneticRowManager#reset).

Test: MagneticRowManagerImplTest
Flag: com.android.systemui.magnetic_notification_swipes
Bug: 395364166
Change-Id: I3ed647b6d6848bea22b604624d8f56932b8e7de0
parent aaa454ed
Loading
Loading
Loading
Loading
+39 −24
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import com.android.systemui.testKosmos
import com.google.android.msdl.data.model.MSDLToken
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.runTest
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
@@ -49,7 +50,7 @@ class MagneticNotificationRowManagerImplTest : SysuiTestCase() {
    private val sectionsManager = mock<NotificationSectionsManager>()
    private val msdlPlayer = kosmos.fakeMSDLPlayer
    private var canRowBeDismissed = true
    private var magneticAnimationsCancelled = false
    private var magneticAnimationsCancelled = MutableList(childrenNumber) { false }

    private val underTest = kosmos.magneticNotificationRowManagerImpl

@@ -64,8 +65,10 @@ class MagneticNotificationRowManagerImplTest : SysuiTestCase() {
            NotificationTestHelper(mContext, mDependency, kosmos.testableLooper, featureFlags)
        children = notificationTestHelper.createGroup(childrenNumber).childrenContainer
        swipedRow = children.attachedChildren[childrenNumber / 2]
        configureMagneticRowListener(swipedRow)
        magneticAnimationsCancelled = false
        children.attachedChildren.forEachIndexed { index, row ->
            row.magneticRowListener = row.magneticRowListener.asTestableListener(index)
        }
        magneticAnimationsCancelled.replaceAll { false }
    }

    @Test
@@ -259,14 +262,14 @@ class MagneticNotificationRowManagerImplTest : SysuiTestCase() {
            underTest.onMagneticInteractionEnd(swipedRow, velocity = null)

            // THEN magnetic animations are cancelled
            assertThat(magneticAnimationsCancelled).isTrue()
            assertThat(magneticAnimationsCancelled[childrenNumber / 2]).isTrue()
        }

    @Test
    fun onMagneticInteractionEnd_forMagneticNeighbor_cancelsMagneticAnimations() =
        kosmos.testScope.runTest {
            val neighborRow = children.attachedChildren[childrenNumber / 2 - 1]
            configureMagneticRowListener(neighborRow)
            val neighborIndex = childrenNumber / 2 - 1
            val neighborRow = children.attachedChildren[neighborIndex]

            // GIVEN that targets are set
            setTargets()
@@ -275,7 +278,13 @@ class MagneticNotificationRowManagerImplTest : SysuiTestCase() {
            underTest.onMagneticInteractionEnd(neighborRow, null)

            // THEN magnetic animations are cancelled
            assertThat(magneticAnimationsCancelled).isTrue()
            assertThat(magneticAnimationsCancelled[neighborIndex]).isTrue()
        }

    @After
    fun tearDown() {
        // We reset the manager so that all MagneticRowListener can cancel all animations
        underTest.reset()
    }

    private fun setDetachedState() {
@@ -302,27 +311,33 @@ class MagneticNotificationRowManagerImplTest : SysuiTestCase() {
            originalTranslation *
            MagneticNotificationRowManagerImpl.MAGNETIC_REDUCTION

    private fun configureMagneticRowListener(row: ExpandableNotificationRow) {
        val listener =
            object : MagneticRowListener {
    private fun MagneticRowListener.asTestableListener(rowIndex: Int): MagneticRowListener {
        val delegate = this
        return object : MagneticRowListener {
            override fun setMagneticTranslation(translation: Float) {
                    row.translation = translation
                delegate.setMagneticTranslation(translation)
            }

            override fun triggerMagneticForce(
                endTranslation: Float,
                springForce: SpringForce,
                startVelocity: Float,
                ) {}
            ) {
                delegate.triggerMagneticForce(endTranslation, springForce, startVelocity)
            }

            override fun cancelMagneticAnimations() {
                    magneticAnimationsCancelled = true
                magneticAnimationsCancelled[rowIndex] = true
                delegate.cancelMagneticAnimations()
            }

                override fun cancelTranslationAnimations() {}
            override fun cancelTranslationAnimations() {
                delegate.cancelTranslationAnimations()
            }

                override fun canRowBeDismissed(): Boolean = canRowBeDismissed
            override fun canRowBeDismissed(): Boolean {
                return canRowBeDismissed
            }
        }
        row.magneticRowListener = listener
    }
}