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

Commit 61f7ea96 authored by Ioana Alexandru's avatar Ioana Alexandru Committed by Android (Google) Code Review
Browse files

Merge "Use unconfined test dispatcher in ZenModeInteractorTest" into main

parents f6e78f2d bf27a0b8
Loading
Loading
Loading
Loading
+58 −110
Original line number Diff line number Diff line
@@ -36,8 +36,9 @@ import com.android.settingslib.notification.modes.TestModeBuilder
import com.android.settingslib.notification.modes.TestModeBuilder.MANUAL_DND
import com.android.settingslib.volume.shared.model.AudioStream
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.kosmos.testScope
import com.android.systemui.kosmos.collectLastValue
import com.android.systemui.kosmos.runTest
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.shared.settings.data.repository.secureSettingsRepository
import com.android.systemui.statusbar.notification.emptyshade.shared.ModesEmptyShadeFix
import com.android.systemui.statusbar.policy.data.repository.fakeDeviceProvisioningRepository
@@ -45,18 +46,13 @@ import com.android.systemui.statusbar.policy.data.repository.fakeZenModeReposito
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import java.time.Duration
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith

@OptIn(ExperimentalCoroutinesApi::class)
@RunWith(AndroidJUnit4::class)
@SmallTest
class ZenModeInteractorTest : SysuiTestCase() {
    private val kosmos = testKosmos()
    private val testScope = kosmos.testScope
    private val kosmos = testKosmos().useUnconfinedTestDispatcher()
    private val zenModeRepository = kosmos.fakeZenModeRepository
    private val settingsRepository = kosmos.secureSettingsRepository
    private val deviceProvisioningRepository = kosmos.fakeDeviceProvisioningRepository
@@ -65,166 +61,136 @@ class ZenModeInteractorTest : SysuiTestCase() {

    @Test
    fun isZenAvailable_off() =
        testScope.runTest {
        kosmos.runTest {
            val isZenAvailable by collectLastValue(underTest.isZenAvailable)
            deviceProvisioningRepository.setDeviceProvisioned(false)
            runCurrent()

            assertThat(isZenAvailable).isFalse()
        }

    @Test
    fun isZenAvailable_on() =
        testScope.runTest {
        kosmos.runTest {
            val isZenAvailable by collectLastValue(underTest.isZenAvailable)
            deviceProvisioningRepository.setDeviceProvisioned(true)
            runCurrent()

            assertThat(isZenAvailable).isTrue()
        }

    @Test
    fun isZenModeEnabled_off() =
        testScope.runTest {
        kosmos.runTest {
            val enabled by collectLastValue(underTest.isZenModeEnabled)

            zenModeRepository.updateZenMode(Settings.Global.ZEN_MODE_OFF)
            runCurrent()

            assertThat(enabled).isFalse()
        }

    @Test
    fun isZenModeEnabled_alarms() =
        testScope.runTest {
        kosmos.runTest {
            val enabled by collectLastValue(underTest.isZenModeEnabled)

            zenModeRepository.updateZenMode(Settings.Global.ZEN_MODE_ALARMS)
            runCurrent()

            assertThat(enabled).isTrue()
        }

    @Test
    fun isZenModeEnabled_importantInterruptions() =
        testScope.runTest {
        kosmos.runTest {
            val enabled by collectLastValue(underTest.isZenModeEnabled)

            zenModeRepository.updateZenMode(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS)
            runCurrent()

            assertThat(enabled).isTrue()
        }

    @Test
    fun isZenModeEnabled_noInterruptions() =
        testScope.runTest {
        kosmos.runTest {
            val enabled by collectLastValue(underTest.isZenModeEnabled)

            zenModeRepository.updateZenMode(Settings.Global.ZEN_MODE_NO_INTERRUPTIONS)
            runCurrent()

            assertThat(enabled).isTrue()
        }

    @Test
    fun testIsZenModeEnabled_unknown() =
        testScope.runTest {
        kosmos.runTest {
            val enabled by collectLastValue(underTest.isZenModeEnabled)

            // this should fail if we ever add another zen mode type
            zenModeRepository.updateZenMode(4)
            runCurrent()

            assertThat(enabled).isFalse()
        }

    @Test
    fun areNotificationsHiddenInShade_noPolicy() =
        testScope.runTest {
        kosmos.runTest {
            val hidden by collectLastValue(underTest.areNotificationsHiddenInShade)

            zenModeRepository.updateNotificationPolicy(null)
            zenModeRepository.updateZenMode(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS)
            runCurrent()

            assertThat(hidden).isFalse()
        }

    @Test
    fun areNotificationsHiddenInShade_zenOffShadeSuppressed() =
        testScope.runTest {
        kosmos.runTest {
            val hidden by collectLastValue(underTest.areNotificationsHiddenInShade)

            zenModeRepository.updateNotificationPolicy(
                suppressedVisualEffects = Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST
            )
            zenModeRepository.updateZenMode(Settings.Global.ZEN_MODE_OFF)
            runCurrent()

            assertThat(hidden).isFalse()
        }

    @Test
    fun areNotificationsHiddenInShade_zenOnShadeNotSuppressed() =
        testScope.runTest {
        kosmos.runTest {
            val hidden by collectLastValue(underTest.areNotificationsHiddenInShade)

            zenModeRepository.updateNotificationPolicy(
                suppressedVisualEffects = Policy.SUPPRESSED_EFFECT_STATUS_BAR
            )
            zenModeRepository.updateZenMode(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS)
            runCurrent()

            assertThat(hidden).isFalse()
        }

    @Test
    fun areNotificationsHiddenInShade_zenOnShadeSuppressed() =
        testScope.runTest {
        kosmos.runTest {
            val hidden by collectLastValue(underTest.areNotificationsHiddenInShade)

            zenModeRepository.updateNotificationPolicy(
                suppressedVisualEffects = Policy.SUPPRESSED_EFFECT_NOTIFICATION_LIST
            )
            zenModeRepository.updateZenMode(Settings.Global.ZEN_MODE_IMPORTANT_INTERRUPTIONS)
            runCurrent()

            assertThat(hidden).isTrue()
        }

    @Test
    fun shouldAskForZenDuration_falseForNonManualDnd() =
        testScope.runTest {
        kosmos.runTest {
            settingsRepository.setInt(ZEN_DURATION, ZEN_DURATION_PROMPT)
            runCurrent()

            assertThat(underTest.shouldAskForZenDuration(TestModeBuilder.EXAMPLE)).isFalse()
        }

    @Test
    fun shouldAskForZenDuration_changesWithSetting() =
        testScope.runTest {
        kosmos.runTest {
            val manualDnd = TestModeBuilder().makeManualDnd().setActive(true).build()

            settingsRepository.setInt(ZEN_DURATION, ZEN_DURATION_FOREVER)
            runCurrent()

            assertThat(underTest.shouldAskForZenDuration(manualDnd)).isFalse()

            settingsRepository.setInt(ZEN_DURATION, ZEN_DURATION_PROMPT)
            runCurrent()

            assertThat(underTest.shouldAskForZenDuration(manualDnd)).isTrue()
        }

    @Test
    fun activateMode_nonManualDnd() =
        testScope.runTest {
        kosmos.runTest {
            val mode = TestModeBuilder().setActive(false).build()
            zenModeRepository.addModes(listOf(mode))
            settingsRepository.setInt(ZEN_DURATION, 60)
            runCurrent()

            underTest.activateMode(mode)
            assertThat(zenModeRepository.getMode(mode.id)?.isActive).isTrue()
@@ -233,16 +199,14 @@ class ZenModeInteractorTest : SysuiTestCase() {

    @Test
    fun activateMode_usesCorrectDuration() =
        testScope.runTest {
        kosmos.runTest {
            settingsRepository.setInt(ZEN_DURATION, ZEN_DURATION_FOREVER)
            runCurrent()

            underTest.activateMode(MANUAL_DND)
            assertThat(zenModeRepository.getModeActiveDuration(MANUAL_DND.id)).isNull()

            zenModeRepository.deactivateMode(MANUAL_DND)
            settingsRepository.setInt(ZEN_DURATION, 60)
            runCurrent()

            underTest.activateMode(MANUAL_DND)
            assertThat(zenModeRepository.getModeActiveDuration(MANUAL_DND.id))
@@ -251,7 +215,7 @@ class ZenModeInteractorTest : SysuiTestCase() {

    @Test
    fun deactivateAllModes_updatesCorrectModes() =
        testScope.runTest {
        kosmos.runTest {
            zenModeRepository.activateMode(MANUAL_DND)
            zenModeRepository.addModes(
                listOf(
@@ -267,41 +231,36 @@ class ZenModeInteractorTest : SysuiTestCase() {

    @Test
    fun activeModes_computesMainActiveMode() =
        testScope.runTest {
        kosmos.runTest {
            val activeModes by collectLastValue(underTest.activeModes)

            zenModeRepository.addMode(id = "Bedtime", type = AutomaticZenRule.TYPE_BEDTIME)
            zenModeRepository.addMode(id = "Other", type = AutomaticZenRule.TYPE_OTHER)

            runCurrent()
            assertThat(activeModes?.modeNames).hasSize(0)
            assertThat(activeModes?.mainMode).isNull()

            zenModeRepository.activateMode("Other")
            runCurrent()
            assertThat(activeModes?.modeNames).containsExactly("Mode Other")
            assertThat(activeModes?.mainMode?.name).isEqualTo("Mode Other")

            zenModeRepository.activateMode("Bedtime")
            runCurrent()
            assertThat(activeModes?.modeNames)
                .containsExactly("Mode Bedtime", "Mode Other")
                .inOrder()
            assertThat(activeModes?.mainMode?.name).isEqualTo("Mode Bedtime")

            zenModeRepository.deactivateMode("Other")
            runCurrent()
            assertThat(activeModes?.modeNames).containsExactly("Mode Bedtime")
            assertThat(activeModes?.mainMode?.name).isEqualTo("Mode Bedtime")

            zenModeRepository.deactivateMode("Bedtime")
            runCurrent()
            assertThat(activeModes?.modeNames).hasSize(0)
            assertThat(activeModes?.mainMode).isNull()
        }

    @Test
    fun getActiveModes_computesMainActiveMode() = runTest {
    fun getActiveModes_computesMainActiveMode() =
        kosmos.runTest {
            zenModeRepository.addMode(id = "Bedtime", type = AutomaticZenRule.TYPE_BEDTIME)
            zenModeRepository.addMode(id = "Other", type = AutomaticZenRule.TYPE_OTHER)

@@ -316,7 +275,9 @@ class ZenModeInteractorTest : SysuiTestCase() {

            zenModeRepository.activateMode("Bedtime")
            activeModes = underTest.getActiveModes()
        assertThat(activeModes.modeNames).containsExactly("Mode Bedtime", "Mode Other").inOrder()
            assertThat(activeModes.modeNames)
                .containsExactly("Mode Bedtime", "Mode Other")
                .inOrder()
            assertThat(activeModes.mainMode?.name).isEqualTo("Mode Bedtime")

            zenModeRepository.deactivateMode("Other")
@@ -332,7 +293,7 @@ class ZenModeInteractorTest : SysuiTestCase() {

    @Test
    fun mainActiveMode_flows() =
        testScope.runTest {
        kosmos.runTest {
            val mainActiveMode by collectLastValue(underTest.mainActiveMode)

            zenModeRepository.addModes(
@@ -355,51 +316,42 @@ class ZenModeInteractorTest : SysuiTestCase() {
                        .build(),
                )
            )

            runCurrent()
            assertThat(mainActiveMode).isNull()

            zenModeRepository.activateMode("Other")
            runCurrent()
            assertThat(mainActiveMode?.name).isEqualTo("Mode Other")
            assertThat(mainActiveMode?.icon?.key?.resId)
                .isEqualTo(R.drawable.ic_zen_mode_type_other)

            zenModeRepository.activateMode("Bedtime")
            runCurrent()
            assertThat(mainActiveMode?.name).isEqualTo("Mode Bedtime")
            assertThat(mainActiveMode?.icon?.key?.resId)
                .isEqualTo(R.drawable.ic_zen_mode_type_bedtime)

            zenModeRepository.deactivateMode("Other")
            runCurrent()
            assertThat(mainActiveMode?.name).isEqualTo("Mode Bedtime")
            assertThat(mainActiveMode?.icon?.key?.resId)
                .isEqualTo(R.drawable.ic_zen_mode_type_bedtime)

            zenModeRepository.deactivateMode("Bedtime")
            runCurrent()
            assertThat(mainActiveMode).isNull()
        }

    @Test
    @EnableFlags(Flags.FLAG_MODES_UI)
    fun dndMode_flows() =
        testScope.runTest {
        kosmos.runTest {
            val dndMode by collectLastValue(underTest.dndMode)

            assertThat(dndMode!!.isActive).isFalse()

            zenModeRepository.activateMode(MANUAL_DND)
            runCurrent()

            assertThat(dndMode!!.isActive).isTrue()
        }

    @Test
    @EnableFlags(Flags.FLAG_MODES_UI)
    fun activeModesBlockingMedia_hasModesWithPolicyBlockingMedia() =
        testScope.runTest {
        kosmos.runTest {
            val blockingMedia by
                collectLastValue(
                    underTest.activeModesBlockingStream(AudioStream(AudioManager.STREAM_MUSIC))
@@ -429,7 +381,6 @@ class ZenModeInteractorTest : SysuiTestCase() {
                        .build(),
                )
            )
            runCurrent()

            assertThat(blockingMedia!!.mainMode!!.name).isEqualTo("Blocks media, Active")
            assertThat(blockingMedia!!.modeNames)
@@ -440,7 +391,7 @@ class ZenModeInteractorTest : SysuiTestCase() {
    @Test
    @EnableFlags(Flags.FLAG_MODES_UI)
    fun activeModesBlockingAlarms_hasModesWithPolicyBlockingAlarms() =
        testScope.runTest {
        kosmos.runTest {
            val blockingAlarms by
                collectLastValue(
                    underTest.activeModesBlockingStream(AudioStream(AudioManager.STREAM_ALARM))
@@ -470,7 +421,6 @@ class ZenModeInteractorTest : SysuiTestCase() {
                        .build(),
                )
            )
            runCurrent()

            assertThat(blockingAlarms!!.mainMode!!.name).isEqualTo("Blocks alarms, Active")
            assertThat(blockingAlarms!!.modeNames)
@@ -481,7 +431,7 @@ class ZenModeInteractorTest : SysuiTestCase() {
    @Test
    @EnableFlags(Flags.FLAG_MODES_UI)
    fun activeModesBlockingAlarms_hasModesWithPolicyBlockingSystem() =
        testScope.runTest {
        kosmos.runTest {
            val blockingSystem by
                collectLastValue(
                    underTest.activeModesBlockingStream(AudioStream(AudioManager.STREAM_SYSTEM))
@@ -511,7 +461,6 @@ class ZenModeInteractorTest : SysuiTestCase() {
                        .build(),
                )
            )
            runCurrent()

            assertThat(blockingSystem!!.mainMode!!.name).isEqualTo("Blocks system, Active")
            assertThat(blockingSystem!!.modeNames)
@@ -522,7 +471,7 @@ class ZenModeInteractorTest : SysuiTestCase() {
    @Test
    @EnableFlags(ModesEmptyShadeFix.FLAG_NAME, Flags.FLAG_MODES_UI, Flags.FLAG_MODES_API)
    fun modesHidingNotifications_onlyIncludesModesWithNotifListSuppression() =
        testScope.runTest {
        kosmos.runTest {
            val modesHidingNotifications by collectLastValue(underTest.modesHidingNotifications)

            zenModeRepository.addModes(
@@ -554,7 +503,6 @@ class ZenModeInteractorTest : SysuiTestCase() {
                        .build(),
                )
            )
            runCurrent()

            assertThat(modesHidingNotifications?.map { it.name })
                .containsExactly("Has list suppression 1", "Has list suppression 2")