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

Commit 86b74a09 authored by Ioana Alexandru's avatar Ioana Alexandru
Browse files

Include manual DND in FakeZenModeRepository by default

This matches the real behavior more closely. Updated tests that were
assuming that the modes list is empty by default, or adding DND
manually.

Updated TestModeBuilder as well so it only has one static (inactive by
default) MANUAL_DND, to simplify references to it and its ID. The main
reason we had both an ACTIVE and INACTIVE version of this previously was
because setActive() used to not work correctly for manual DND, but now
it does, so we can just set it explicitly when needed. Also removed the
static dndMode method because it's cleaner to use makeDndMode and set
fields on that explicitly where needed.

Bug: 382497909
Flag: TEST_ONLY
Test: ZenModeInteractorTest
Change-Id: I939db3a4e22812d0b85705abb2b077e47623cb3b
parent 54bd6475
Loading
Loading
Loading
Loading
+7 −3
Original line number Diff line number Diff line
@@ -37,7 +37,8 @@ class FakeZenModeRepository : ZenModeRepository {
    override val globalZenMode: StateFlow<Int>
        get() = mutableZenMode.asStateFlow()

    private val mutableModesFlow: MutableStateFlow<List<ZenMode>> = MutableStateFlow(listOf())
    private val mutableModesFlow: MutableStateFlow<List<ZenMode>> =
        MutableStateFlow(listOf(TestModeBuilder.MANUAL_DND))
    override val modes: Flow<List<ZenMode>>
        get() = mutableModesFlow.asStateFlow()

@@ -65,8 +66,11 @@ class FakeZenModeRepository : ZenModeRepository {
        mutableModesFlow.value += mode
    }

    fun addMode(id: String, @AutomaticZenRule.Type type: Int = AutomaticZenRule.TYPE_UNKNOWN,
        active: Boolean = false) {
    fun addMode(
        id: String,
        @AutomaticZenRule.Type type: Int = AutomaticZenRule.TYPE_UNKNOWN,
        active: Boolean = false,
    ) {
        mutableModesFlow.value += newMode(id, type, active)
    }

+1 −17
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@ import android.service.notification.ZenModeConfig;
import android.service.notification.ZenPolicy;

import androidx.annotation.DrawableRes;
import androidx.annotation.NonNull;
import androidx.annotation.Nullable;

import java.util.Random;
@@ -44,22 +43,7 @@ public class TestModeBuilder {
    private boolean mIsManualDnd;

    public static final ZenMode EXAMPLE = new TestModeBuilder().build();

    public static final ZenMode MANUAL_DND_ACTIVE = manualDnd(
            INTERRUPTION_FILTER_PRIORITY, true);

    public static final ZenMode MANUAL_DND_INACTIVE = manualDnd(
            INTERRUPTION_FILTER_PRIORITY, false);

    @NonNull
    public static ZenMode manualDnd(@NotificationManager.InterruptionFilter int filter,
            boolean isActive) {
        return new TestModeBuilder()
                .makeManualDnd()
                .setInterruptionFilter(filter)
                .setActive(isActive)
                .build();
    }
    public static final ZenMode MANUAL_DND = new TestModeBuilder().makeManualDnd().build();

    public TestModeBuilder() {
        // Reasonable defaults
+38 −12
Original line number Diff line number Diff line
@@ -87,7 +87,7 @@ public class ZenModeTest {

    @Test
    public void testBasicMethods_manualDnd() {
        ZenMode manualMode = TestModeBuilder.MANUAL_DND_INACTIVE;
        ZenMode manualMode = TestModeBuilder.MANUAL_DND;

        assertThat(manualMode.getId()).isEqualTo(ZenMode.MANUAL_DND_MODE_ID);
        assertThat(manualMode.isManualDnd()).isTrue();
@@ -271,7 +271,7 @@ public class ZenModeTest {

    @Test
    public void setInterruptionFilter_manualDnd_throws() {
        ZenMode manualDnd = TestModeBuilder.MANUAL_DND_INACTIVE;
        ZenMode manualDnd = TestModeBuilder.MANUAL_DND;

        assertThrows(IllegalStateException.class,
                () -> manualDnd.setInterruptionFilter(INTERRUPTION_FILTER_ALL));
@@ -280,24 +280,46 @@ public class ZenModeTest {
    @Test
    public void canEditPolicy_onlyFalseForSpecialDnd() {
        assertThat(TestModeBuilder.EXAMPLE.canEditPolicy()).isTrue();
        assertThat(TestModeBuilder.MANUAL_DND_ACTIVE.canEditPolicy()).isTrue();
        assertThat(TestModeBuilder.MANUAL_DND_INACTIVE.canEditPolicy()).isTrue();

        ZenMode dndWithAlarms = TestModeBuilder.manualDnd(INTERRUPTION_FILTER_ALARMS, true);
        ZenMode inactiveDnd = new TestModeBuilder().makeManualDnd().setActive(false).build();
        assertThat(inactiveDnd.canEditPolicy()).isTrue();

        ZenMode activeDnd = new TestModeBuilder().makeManualDnd().setActive(true).build();
        assertThat(activeDnd.canEditPolicy()).isTrue();

        ZenMode dndWithAlarms = new TestModeBuilder()
                .makeManualDnd()
                .setInterruptionFilter(INTERRUPTION_FILTER_ALARMS)
                .setActive(true)
                .build();
        assertThat(dndWithAlarms.canEditPolicy()).isFalse();
        ZenMode dndWithNone = TestModeBuilder.manualDnd(INTERRUPTION_FILTER_NONE, true);

        ZenMode dndWithNone = new TestModeBuilder()
                .makeManualDnd()
                .setInterruptionFilter(INTERRUPTION_FILTER_NONE)
                .setActive(true)
                .build();
        assertThat(dndWithNone.canEditPolicy()).isFalse();

        // Note: Backend will never return an inactive manual mode with custom filter.
        ZenMode badDndWithAlarms = TestModeBuilder.manualDnd(INTERRUPTION_FILTER_ALARMS, false);
        ZenMode badDndWithAlarms = new TestModeBuilder()
                .makeManualDnd()
                .setInterruptionFilter(INTERRUPTION_FILTER_ALARMS)
                .setActive(false)
                .build();
        assertThat(badDndWithAlarms.canEditPolicy()).isFalse();
        ZenMode badDndWithNone = TestModeBuilder.manualDnd(INTERRUPTION_FILTER_NONE, false);

        ZenMode badDndWithNone = new TestModeBuilder()
                .makeManualDnd()
                .setInterruptionFilter(INTERRUPTION_FILTER_NONE)
                .setActive(false)
                .build();
        assertThat(badDndWithNone.canEditPolicy()).isFalse();
    }

    @Test
    public void canEditPolicy_whenTrue_allowsSettingPolicyAndEffects() {
        ZenMode normalDnd = TestModeBuilder.manualDnd(INTERRUPTION_FILTER_PRIORITY, true);
        ZenMode normalDnd = new TestModeBuilder().makeManualDnd().setActive(true).build();

        assertThat(normalDnd.canEditPolicy()).isTrue();

@@ -313,7 +335,11 @@ public class ZenModeTest {

    @Test
    public void canEditPolicy_whenFalse_preventsSettingFilterPolicyOrEffects() {
        ZenMode specialDnd = TestModeBuilder.manualDnd(INTERRUPTION_FILTER_ALARMS, true);
        ZenMode specialDnd = new TestModeBuilder()
                .makeManualDnd()
                .setInterruptionFilter(INTERRUPTION_FILTER_ALARMS)
                .setActive(true)
                .build();

        assertThat(specialDnd.canEditPolicy()).isFalse();
        assertThrows(IllegalStateException.class,
@@ -324,7 +350,7 @@ public class ZenModeTest {

    @Test
    public void comparator_prioritizes() {
        ZenMode manualDnd = TestModeBuilder.MANUAL_DND_INACTIVE;
        ZenMode manualDnd = TestModeBuilder.MANUAL_DND;
        ZenMode driving1 = new TestModeBuilder().setName("b1").setType(TYPE_DRIVING).build();
        ZenMode driving2 = new TestModeBuilder().setName("b2").setType(TYPE_DRIVING).build();
        ZenMode bedtime1 = new TestModeBuilder().setName("c1").setType(TYPE_BEDTIME).build();
@@ -403,7 +429,7 @@ public class ZenModeTest {

    @Test
    public void getIconKey_manualDnd_isDndIcon() {
        ZenIcon.Key iconKey = TestModeBuilder.MANUAL_DND_INACTIVE.getIconKey();
        ZenIcon.Key iconKey = TestModeBuilder.MANUAL_DND.getIconKey();

        assertThat(iconKey.resPackage()).isNull();
        assertThat(iconKey.resId()).isEqualTo(
+3 −10
Original line number Diff line number Diff line
@@ -28,7 +28,7 @@ import android.provider.Settings.Secure.ZEN_DURATION_PROMPT
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.settingslib.notification.modes.EnableZenModeDialog
import com.android.settingslib.notification.modes.TestModeBuilder
import com.android.settingslib.notification.modes.TestModeBuilder.MANUAL_DND
import com.android.systemui.SysuiTestCase
import com.android.systemui.animation.Expandable
import com.android.systemui.common.shared.model.ContentDescription
@@ -187,7 +187,7 @@ class DoNotDisturbQuickAffordanceConfigTest : SysuiTestCase() {
        testScope.runTest {
            val currentModes by collectLastValue(zenModeRepository.modes)

            zenModeRepository.addMode(TestModeBuilder.MANUAL_DND_ACTIVE)
            zenModeRepository.activateMode(MANUAL_DND)
            secureSettingsRepository.setInt(Settings.Secure.ZEN_DURATION, -2)
            collectLastValue(underTest.lockScreenState)
            runCurrent()
@@ -233,7 +233,6 @@ class DoNotDisturbQuickAffordanceConfigTest : SysuiTestCase() {
        testScope.runTest {
            val currentModes by collectLastValue(zenModeRepository.modes)

            zenModeRepository.addMode(TestModeBuilder.MANUAL_DND_INACTIVE)
            secureSettingsRepository.setInt(Settings.Secure.ZEN_DURATION, ZEN_DURATION_FOREVER)
            collectLastValue(underTest.lockScreenState)
            runCurrent()
@@ -278,7 +277,6 @@ class DoNotDisturbQuickAffordanceConfigTest : SysuiTestCase() {
    fun onTriggered_dndModeIsOff_settingNotFOREVERorPROMPT_dndWithDuration() =
        testScope.runTest {
            val currentModes by collectLastValue(zenModeRepository.modes)
            zenModeRepository.addMode(TestModeBuilder.MANUAL_DND_INACTIVE)
            secureSettingsRepository.setInt(Settings.Secure.ZEN_DURATION, -900)
            runCurrent()

@@ -323,7 +321,6 @@ class DoNotDisturbQuickAffordanceConfigTest : SysuiTestCase() {
    fun onTriggered_dndModeIsOff_settingIsPROMPT_showDialog() =
        testScope.runTest {
            val expandable: Expandable = mock()
            zenModeRepository.addMode(TestModeBuilder.MANUAL_DND_INACTIVE)
            secureSettingsRepository.setInt(Settings.Secure.ZEN_DURATION, ZEN_DURATION_PROMPT)
            whenever(enableZenModeDialog.createDialog()).thenReturn(mock())
            collectLastValue(underTest.lockScreenState)
@@ -405,10 +402,6 @@ class DoNotDisturbQuickAffordanceConfigTest : SysuiTestCase() {
        testScope.runTest {
            val lockScreenState by collectLastValue(underTest.lockScreenState)

            val manualDnd = TestModeBuilder.MANUAL_DND_INACTIVE
            zenModeRepository.addMode(manualDnd)
            runCurrent()

            assertThat(lockScreenState)
                .isEqualTo(
                    KeyguardQuickAffordanceConfig.LockScreenState.Visible(
@@ -420,7 +413,7 @@ class DoNotDisturbQuickAffordanceConfigTest : SysuiTestCase() {
                    )
                )

            zenModeRepository.activateMode(manualDnd)
            zenModeRepository.activateMode(MANUAL_DND)
            runCurrent()

            assertThat(lockScreenState)
+3 −3
Original line number Diff line number Diff line
@@ -24,6 +24,7 @@ import android.provider.Settings
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.settingslib.notification.modes.TestModeBuilder
import com.android.settingslib.notification.modes.TestModeBuilder.MANUAL_DND
import com.android.systemui.Flags
import com.android.systemui.SysuiTestCase
import com.android.systemui.animation.Expandable
@@ -87,9 +88,9 @@ class ModesTileUserActionInteractorTest : SysuiTestCase() {
        testScope.runTest {
            val activeModes by collectLastValue(zenModeInteractor.activeModes)

            zenModeRepository.activateMode(MANUAL_DND)
            zenModeRepository.addModes(
                listOf(
                    TestModeBuilder.MANUAL_DND_ACTIVE,
                    TestModeBuilder().setName("Mode 1").setActive(true).build(),
                    TestModeBuilder().setName("Mode 2").setActive(true).build(),
                )
@@ -111,7 +112,7 @@ class ModesTileUserActionInteractorTest : SysuiTestCase() {
        testScope.runTest {
            val dndMode by collectLastValue(zenModeInteractor.dndMode)

            zenModeRepository.addMode(TestModeBuilder.MANUAL_DND_ACTIVE)
            zenModeRepository.activateMode(MANUAL_DND)
            assertThat(dndMode?.isActive).isTrue()

            underTest.handleInput(
@@ -127,7 +128,6 @@ class ModesTileUserActionInteractorTest : SysuiTestCase() {
        testScope.runTest {
            val dndMode by collectLastValue(zenModeInteractor.dndMode)

            zenModeRepository.addMode(TestModeBuilder.MANUAL_DND_INACTIVE)
            assertThat(dndMode?.isActive).isFalse()

            underTest.handleInput(
Loading