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

Commit d75970a6 authored by Caitlin Shkuratov's avatar Caitlin Shkuratov
Browse files

[Notif] Update HeadsUpManagerImplTest to use `underTest` everywhere.

This way, the individual tests don't need to manually create
HeadsUpManagerImpl each time.

Bug: 378142453
Flag: EXEMPT refactor
Test: atest HeadsUpManagerImplTest
Change-Id: I8b9623c084e19b360d1187e09569420d859d8357
parent 807995d9
Loading
Loading
Loading
Loading
+70 −95
Original line number Diff line number Diff line
@@ -21,9 +21,7 @@ import android.platform.test.flag.junit.FlagsParameterization
import android.testing.TestableLooper.RunWithLooper
import android.view.accessibility.accessibilityManagerWrapper
import androidx.test.filters.SmallTest
import com.android.internal.logging.UiEventLogger
import com.android.internal.logging.uiEventLoggerFake
import com.android.systemui.dump.DumpManager
import com.android.systemui.dump.dumpManager
import com.android.systemui.flags.andSceneContainer
import com.android.systemui.kosmos.runTest
@@ -31,18 +29,13 @@ import com.android.systemui.kosmos.testScope
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.log.logcatLogBuffer
import com.android.systemui.res.R
import com.android.systemui.shade.domain.interactor.ShadeInteractor
import com.android.systemui.statusbar.FakeStatusBarStateController
import com.android.systemui.statusbar.NotificationShadeWindowController
import com.android.systemui.shade.domain.interactor.shadeInteractor
import com.android.systemui.shade.shadeTestUtil
import com.android.systemui.statusbar.StatusBarState
import com.android.systemui.statusbar.notification.collection.provider.VisualStabilityProvider
import com.android.systemui.statusbar.notification.collection.provider.visualStabilityProvider
import com.android.systemui.statusbar.notification.collection.render.GroupMembershipManager
import com.android.systemui.statusbar.notification.shared.NotificationThrottleHun
import com.android.systemui.statusbar.phone.ConfigurationControllerImpl
import com.android.systemui.statusbar.phone.KeyguardBypassController
import com.android.systemui.statusbar.phone.keyguardBypassController
import com.android.systemui.statusbar.policy.AccessibilityManagerWrapper
import com.android.systemui.statusbar.policy.configurationController
import com.android.systemui.statusbar.sysuiStatusBarStateController
import com.android.systemui.testKosmos
@@ -50,15 +43,11 @@ import com.android.systemui.util.concurrency.mockExecutorHandler
import com.android.systemui.util.kotlin.JavaAdapter
import com.google.common.truth.Truth.assertThat
import junit.framework.Assert
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Ignore
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
import org.mockito.kotlin.mock
import org.mockito.kotlin.whenever
import platform.test.runner.parameterized.ParameterizedAndroidJunit4
@@ -76,33 +65,12 @@ class HeadsUpManagerImplTest(flags: FlagsParameterization) : HeadsUpManagerImplO

    private val mGroupManager = mock<GroupMembershipManager>()
    private val mBgHandler = mock<Handler>()
    private val mShadeInteractor = mock<ShadeInteractor>()

    val statusBarStateController = kosmos.sysuiStatusBarStateController
    private val mJavaAdapter: JavaAdapter = JavaAdapter(testScope.backgroundScope)

    private lateinit var mAvalancheController: AvalancheController

    private fun createHeadsUpManagerPhone(): HeadsUpManagerImpl {
        return HeadsUpManagerImpl(
            mContext,
            mHeadsUpManagerLogger,
            statusBarStateController,
            kosmos.keyguardBypassController,
            mGroupManager,
            kosmos.visualStabilityProvider,
            kosmos.configurationController,
            mockExecutorHandler(mExecutor),
            mGlobalSettings,
            mSystemClock,
            mExecutor,
            kosmos.accessibilityManagerWrapper,
            kosmos.uiEventLoggerFake,
            mJavaAdapter,
            mShadeInteractor,
            mAvalancheController,
        )
    }
    private lateinit var underTest: HeadsUpManagerImpl

    @Before
    fun setUp() {
@@ -124,8 +92,6 @@ class HeadsUpManagerImplTest(flags: FlagsParameterization) : HeadsUpManagerImplO
            )
        }

        whenever(mShadeInteractor.isAnyExpanded).thenReturn(MutableStateFlow(false))
        whenever(mShadeInteractor.isQsExpanded).thenReturn(MutableStateFlow(false))
        whenever(kosmos.keyguardBypassController.bypassEnabled).thenReturn(false)
        kosmos.visualStabilityProvider.isReorderingAllowed = true
        mAvalancheController =
@@ -135,79 +101,92 @@ class HeadsUpManagerImplTest(flags: FlagsParameterization) : HeadsUpManagerImplO
                mHeadsUpManagerLogger,
                mBgHandler,
            )
        underTest =
            HeadsUpManagerImpl(
                mContext,
                mHeadsUpManagerLogger,
                statusBarStateController,
                kosmos.keyguardBypassController,
                mGroupManager,
                kosmos.visualStabilityProvider,
                kosmos.configurationController,
                mockExecutorHandler(mExecutor),
                mGlobalSettings,
                mSystemClock,
                mExecutor,
                kosmos.accessibilityManagerWrapper,
                kosmos.uiEventLoggerFake,
                mJavaAdapter,
                kosmos.shadeInteractor,
                mAvalancheController,
            )
    }

    @Test
    fun testSnooze() {
        val hmp: HeadsUpManager = createHeadsUpManagerPhone()
        val entry = HeadsUpManagerTestUtil.createEntry(/* id= */ 0, mContext)
        hmp.showNotification(entry)
        hmp.snooze()
        Assert.assertTrue(hmp.isSnoozed(entry.sbn.packageName))
        underTest.showNotification(entry)
        underTest.snooze()
        Assert.assertTrue(underTest.isSnoozed(entry.sbn.packageName))
    }

    @Test
    fun testSwipedOutNotification() {
        val hmp: HeadsUpManager = createHeadsUpManagerPhone()
        val entry = HeadsUpManagerTestUtil.createEntry(/* id= */ 0, mContext)
        hmp.showNotification(entry)
        hmp.addSwipedOutNotification(entry.key)
        underTest.showNotification(entry)
        underTest.addSwipedOutNotification(entry.key)

        // Remove should succeed because the notification is swiped out
        val removedImmediately =
            hmp.removeNotification(
            underTest.removeNotification(
                entry.key,
                /* releaseImmediately= */ false,
                /* reason= */ "swipe out",
            )
        Assert.assertTrue(removedImmediately)
        Assert.assertFalse(hmp.isHeadsUpEntry(entry.key))
        Assert.assertFalse(underTest.isHeadsUpEntry(entry.key))
    }

    @Test
    fun testCanRemoveImmediately_swipedOut() {
        val hmp: HeadsUpManager = createHeadsUpManagerPhone()
        val entry = HeadsUpManagerTestUtil.createEntry(/* id= */ 0, mContext)
        hmp.showNotification(entry)
        hmp.addSwipedOutNotification(entry.key)
        underTest.showNotification(entry)
        underTest.addSwipedOutNotification(entry.key)

        // Notification is swiped so it can be immediately removed.
        Assert.assertTrue(hmp.canRemoveImmediately(entry.key))
        Assert.assertTrue(underTest.canRemoveImmediately(entry.key))
    }

    @Ignore("b/141538055")
    @Test
    fun testCanRemoveImmediately_notTopEntry() {
        val hmp: HeadsUpManager = createHeadsUpManagerPhone()
        val earlierEntry = HeadsUpManagerTestUtil.createEntry(/* id= */ 0, mContext)
        val laterEntry = HeadsUpManagerTestUtil.createEntry(/* id= */ 1, mContext)
        laterEntry.row = mRow
        hmp.showNotification(earlierEntry)
        hmp.showNotification(laterEntry)
        underTest.showNotification(earlierEntry)
        underTest.showNotification(laterEntry)

        // Notification is "behind" a higher priority notification so we can remove it immediately.
        Assert.assertTrue(hmp.canRemoveImmediately(earlierEntry.key))
        Assert.assertTrue(underTest.canRemoveImmediately(earlierEntry.key))
    }

    @Test
    fun testExtendHeadsUp() {
        val hmp = createHeadsUpManagerPhone()
        val entry = HeadsUpManagerTestUtil.createEntry(/* id= */ 0, mContext)
        hmp.showNotification(entry)
        hmp.extendHeadsUp()
        underTest.showNotification(entry)
        underTest.extendHeadsUp()
        mSystemClock.advanceTime(((TEST_AUTO_DISMISS_TIME + TEST_EXTENSION_TIME) / 2).toLong())
        Assert.assertTrue(hmp.isHeadsUpEntry(entry.key))
        Assert.assertTrue(underTest.isHeadsUpEntry(entry.key))
    }

    @Test
    @EnableFlags(NotificationThrottleHun.FLAG_NAME)
    fun testShowNotification_removeWhenReorderingAllowedTrue() {
        kosmos.visualStabilityProvider.isReorderingAllowed = true
        val hmp = createHeadsUpManagerPhone()

        val notifEntry = HeadsUpManagerTestUtil.createEntry(/* id= */ 0, mContext)
        hmp.showNotification(notifEntry)
        assertThat(hmp.mEntriesToRemoveWhenReorderingAllowed.contains(notifEntry)).isTrue()
        underTest.showNotification(notifEntry)
        assertThat(underTest.mEntriesToRemoveWhenReorderingAllowed.contains(notifEntry)).isTrue()
    }

    class TestAnimationStateHandler : AnimationStateHandler {
@@ -218,25 +197,23 @@ class HeadsUpManagerImplTest(flags: FlagsParameterization) : HeadsUpManagerImplO
    @EnableFlags(NotificationThrottleHun.FLAG_NAME)
    fun testReorderingAllowed_clearsListOfEntriesToRemove() {
        kosmos.visualStabilityProvider.isReorderingAllowed = true
        val hmp = createHeadsUpManagerPhone()

        val notifEntry = HeadsUpManagerTestUtil.createEntry(/* id= */ 0, mContext)
        hmp.showNotification(notifEntry)
        assertThat(hmp.mEntriesToRemoveWhenReorderingAllowed.contains(notifEntry)).isTrue()
        underTest.showNotification(notifEntry)
        assertThat(underTest.mEntriesToRemoveWhenReorderingAllowed.contains(notifEntry)).isTrue()

        hmp.setAnimationStateHandler(TestAnimationStateHandler())
        hmp.mOnReorderingAllowedListener.onReorderingAllowed()
        assertThat(hmp.mEntriesToRemoveWhenReorderingAllowed.isEmpty()).isTrue()
        underTest.setAnimationStateHandler(TestAnimationStateHandler())
        underTest.mOnReorderingAllowedListener.onReorderingAllowed()
        assertThat(underTest.mEntriesToRemoveWhenReorderingAllowed.isEmpty()).isTrue()
    }

    @Test
    @EnableFlags(NotificationThrottleHun.FLAG_NAME)
    fun testShowNotification_reorderNotAllowed_seenInShadeTrue() {
        kosmos.visualStabilityProvider.isReorderingAllowed = false
        val hmp = createHeadsUpManagerPhone()

        val notifEntry = HeadsUpManagerTestUtil.createEntry(/* id= */ 0, mContext)
        hmp.showNotification(notifEntry)
        underTest.showNotification(notifEntry)
        assertThat(notifEntry.isSeenInShade).isTrue()
    }

@@ -244,95 +221,91 @@ class HeadsUpManagerImplTest(flags: FlagsParameterization) : HeadsUpManagerImplO
    @EnableFlags(NotificationThrottleHun.FLAG_NAME)
    fun testShowNotification_reorderAllowed_seenInShadeFalse() {
        kosmos.visualStabilityProvider.isReorderingAllowed = true
        val hmp = createHeadsUpManagerPhone()

        val notifEntry = HeadsUpManagerTestUtil.createEntry(/* id= */ 0, mContext)
        hmp.showNotification(notifEntry)
        underTest.showNotification(notifEntry)
        assertThat(notifEntry.isSeenInShade).isFalse()
    }

    @Test
    fun testShouldHeadsUpBecomePinned_noFSI_false() =
        kosmos.runTest {
            val hum = createHeadsUpManagerPhone()
            statusBarStateController.setState(StatusBarState.KEYGUARD)

            val entry = HeadsUpManagerTestUtil.createEntry(/* id= */ 0, mContext)

            Assert.assertFalse(hum.shouldHeadsUpBecomePinned(entry))
            Assert.assertFalse(underTest.shouldHeadsUpBecomePinned(entry))
        }

    @Test
    fun testShouldHeadsUpBecomePinned_hasFSI_notUnpinned_true() =
        kosmos.runTest {
            val hum = createHeadsUpManagerPhone()
            statusBarStateController.setState(StatusBarState.KEYGUARD)

            val notifEntry =
                HeadsUpManagerTestUtil.createFullScreenIntentEntry(/* id= */ 0, mContext)

            // Add notifEntry to ANM mAlertEntries map and make it NOT unpinned
            hum.showNotification(notifEntry)
            underTest.showNotification(notifEntry)

            val headsUpEntry = hum.getHeadsUpEntry(notifEntry.key)
            val headsUpEntry = underTest.getHeadsUpEntry(notifEntry.key)
            headsUpEntry!!.mWasUnpinned = false

            Assert.assertTrue(hum.shouldHeadsUpBecomePinned(notifEntry))
            Assert.assertTrue(underTest.shouldHeadsUpBecomePinned(notifEntry))
        }

    @Test
    fun testShouldHeadsUpBecomePinned_wasUnpinned_false() =
        kosmos.runTest {
            val hum = createHeadsUpManagerPhone()
            statusBarStateController.setState(StatusBarState.KEYGUARD)

            val notifEntry =
                HeadsUpManagerTestUtil.createFullScreenIntentEntry(/* id= */ 0, mContext)

            // Add notifEntry to ANM mAlertEntries map and make it unpinned
            hum.showNotification(notifEntry)
            underTest.showNotification(notifEntry)

            val headsUpEntry = hum.getHeadsUpEntry(notifEntry.key)
            val headsUpEntry = underTest.getHeadsUpEntry(notifEntry.key)
            headsUpEntry!!.mWasUnpinned = true

            Assert.assertFalse(hum.shouldHeadsUpBecomePinned(notifEntry))
            Assert.assertFalse(underTest.shouldHeadsUpBecomePinned(notifEntry))
        }

    @Test
    fun shouldHeadsUpBecomePinned_shadeNotExpanded_true() =
        kosmos.runTest {
            // GIVEN
            whenever(mShadeInteractor.isAnyFullyExpanded).thenReturn(MutableStateFlow(false))
            val hmp = createHeadsUpManagerPhone()
            shadeTestUtil.setShadeExpansion(0f)
            // TODO(b/381869885): Determine why we need both of these ShadeTestUtil calls.
            shadeTestUtil.setLegacyExpandedOrAwaitingInputTransfer(false)

            val entry = HeadsUpManagerTestUtil.createEntry(/* id= */ 0, mContext)
            statusBarStateController.setState(StatusBarState.SHADE)

            // THEN
            Assert.assertTrue(hmp.shouldHeadsUpBecomePinned(entry))
            Assert.assertTrue(underTest.shouldHeadsUpBecomePinned(entry))
        }

    @Test
    fun shouldHeadsUpBecomePinned_shadeLocked_false() =
        kosmos.runTest {
            // GIVEN
            val hmp = createHeadsUpManagerPhone()
            val entry = HeadsUpManagerTestUtil.createEntry(/* id= */ 0, mContext)
            statusBarStateController.setState(StatusBarState.SHADE_LOCKED)

            // THEN
            Assert.assertFalse(hmp.shouldHeadsUpBecomePinned(entry))
            Assert.assertFalse(underTest.shouldHeadsUpBecomePinned(entry))
        }

    @Test
    fun shouldHeadsUpBecomePinned_shadeUnknown_false() =
        kosmos.runTest {
            // GIVEN
            val hmp = createHeadsUpManagerPhone()
            val entry = HeadsUpManagerTestUtil.createEntry(/* id= */ 0, mContext)
            statusBarStateController.setState(1207)

            // THEN
            Assert.assertFalse(hmp.shouldHeadsUpBecomePinned(entry))
            Assert.assertFalse(underTest.shouldHeadsUpBecomePinned(entry))
        }

    @Test
@@ -340,12 +313,12 @@ class HeadsUpManagerImplTest(flags: FlagsParameterization) : HeadsUpManagerImplO
        kosmos.runTest {
            // GIVEN
            whenever(keyguardBypassController.bypassEnabled).thenReturn(true)
            val hmp = createHeadsUpManagerPhone()

            val entry = HeadsUpManagerTestUtil.createEntry(/* id= */ 0, mContext)
            statusBarStateController.setState(StatusBarState.KEYGUARD)

            // THEN
            Assert.assertTrue(hmp.shouldHeadsUpBecomePinned(entry))
            Assert.assertTrue(underTest.shouldHeadsUpBecomePinned(entry))
        }

    @Test
@@ -353,25 +326,27 @@ class HeadsUpManagerImplTest(flags: FlagsParameterization) : HeadsUpManagerImplO
        kosmos.runTest {
            // GIVEN
            whenever(keyguardBypassController.bypassEnabled).thenReturn(false)
            val hmp = createHeadsUpManagerPhone()

            val entry = HeadsUpManagerTestUtil.createEntry(/* id= */ 0, mContext)
            statusBarStateController.setState(StatusBarState.KEYGUARD)

            // THEN
            Assert.assertFalse(hmp.shouldHeadsUpBecomePinned(entry))
            Assert.assertFalse(underTest.shouldHeadsUpBecomePinned(entry))
        }

    @Test
    fun shouldHeadsUpBecomePinned_shadeExpanded_false() =
        kosmos.runTest {
            // GIVEN
            whenever(mShadeInteractor.isAnyExpanded).thenReturn(MutableStateFlow(true))
            val hmp = createHeadsUpManagerPhone()
            shadeTestUtil.setShadeExpansion(1f)
            // TODO(b/381869885): Determine why we need both of these ShadeTestUtil calls.
            shadeTestUtil.setLegacyExpandedOrAwaitingInputTransfer(true)

            val entry = HeadsUpManagerTestUtil.createEntry(/* id= */ 0, mContext)
            statusBarStateController.setState(StatusBarState.SHADE)

            // THEN
            Assert.assertFalse(hmp.shouldHeadsUpBecomePinned(entry))
            Assert.assertFalse(underTest.shouldHeadsUpBecomePinned(entry))
        }

    companion object {