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

Commit 8aaf1c44 authored by Matías Hernández's avatar Matías Hernández
Browse files

Finally fix TestModeBuilder so that it clones DND correctly

Specifically, new TestModeBuiler(dnd).build() will now return a proper DND mode, instead of a half-DND-half-rule hybrid.

Removed workaround in DoNotDisturbQuickAffordanceConfigTest and ClockEventControllerTest to verify it works. Other existing tests still pass.

Fixes: 381413390
Test: atest DoNotDisturbQuickAffordanceConfigTest com.android.settings.notification.modes
Flag: TEST_ONLY
Change-Id: I666e27651b89ce3c2caf190dce6ff2cc8d9b2eda
parent 11f742ec
Loading
Loading
Loading
Loading
+0 −4
Original line number Diff line number Diff line
@@ -74,10 +74,6 @@ class FakeZenModeRepository : ZenModeRepository {
        mutableModesFlow.value = mutableModesFlow.value.filter { it.id != id }
    }

    fun replaceMode(modeId: String, mode: ZenMode) {
        mutableModesFlow.value = (mutableModesFlow.value.filter { it.id != modeId }) + mode
    }

    fun clearModes() {
        mutableModesFlow.value = listOf()
    }
+31 −17
Original line number Diff line number Diff line
@@ -41,32 +41,24 @@ public class TestModeBuilder {
    private String mId;
    private AutomaticZenRule mRule;
    private ZenModeConfig.ZenRule mConfigZenRule;
    private boolean mIsManualDnd;

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

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

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

    @NonNull
    public static ZenMode manualDnd(@NotificationManager.InterruptionFilter int filter,
            boolean isActive) {
        return manualDnd(Uri.EMPTY, filter, isActive);
    }

    private static ZenMode manualDnd(Uri conditionId,
            @NotificationManager.InterruptionFilter int filter, boolean isActive) {
        return ZenMode.manualDndMode(
                new AutomaticZenRule.Builder("Do Not Disturb", conditionId)
        return new TestModeBuilder()
                .makeManualDnd()
                .setInterruptionFilter(filter)
                        .setType(AutomaticZenRule.TYPE_OTHER)
                        .setManualInvocationAllowed(true)
                        .setPackage(SystemZenRules.PACKAGE_ANDROID)
                        .setZenPolicy(new ZenPolicy.Builder().disallowAllSounds().build())
                        .build(),
                isActive);
                .setActive(isActive)
                .build();
    }

    public TestModeBuilder() {
@@ -91,6 +83,10 @@ public class TestModeBuilder {
        mConfigZenRule.enabled = previous.getRule().isEnabled();
        mConfigZenRule.pkg = previous.getRule().getPackageName();
        setActive(previous.isActive());

        if (previous.isManualDnd()) {
            makeManualDnd();
        }
    }

    public TestModeBuilder setId(String id) {
@@ -222,7 +218,25 @@ public class TestModeBuilder {
        return this;
    }

    public TestModeBuilder makeManualDnd() {
        mIsManualDnd = true;
        // Set the "fixed" properties of a DND mode. Other things, such as policy/filter may be set
        // separately or copied from a preexisting DND, so they are not overwritten here.
        setId(ZenMode.MANUAL_DND_MODE_ID);
        setName("Do Not Disturb");
        setType(AutomaticZenRule.TYPE_OTHER);
        setManualInvocationAllowed(true);
        setPackage(SystemZenRules.PACKAGE_ANDROID);
        setConditionId(Uri.EMPTY);
        return this;
    }

    public ZenMode build() {
        if (mIsManualDnd) {
            return ZenMode.manualDndMode(mRule, mConfigZenRule.condition != null
                    && mConfigZenRule.condition.state == Condition.STATE_TRUE);
        } else {
            return new ZenMode(mId, mRule, mConfigZenRule);
        }
    }
}
+3 −3
Original line number Diff line number Diff line
@@ -405,7 +405,8 @@ class DoNotDisturbQuickAffordanceConfigTest : SysuiTestCase() {
        testScope.runTest {
            val lockScreenState by collectLastValue(underTest.lockScreenState)

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

            assertThat(lockScreenState)
@@ -419,8 +420,7 @@ class DoNotDisturbQuickAffordanceConfigTest : SysuiTestCase() {
                    )
                )

            zenModeRepository.removeMode(TestModeBuilder.MANUAL_DND_INACTIVE.id)
            zenModeRepository.addMode(TestModeBuilder.MANUAL_DND_ACTIVE)
            zenModeRepository.activateMode(manualDnd)
            runCurrent()

            assertThat(lockScreenState)
+1 −2
Original line number Diff line number Diff line
@@ -378,8 +378,7 @@ class ZenModeInteractorTest : SysuiTestCase() {

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

            zenModeRepository.removeMode(TestModeBuilder.MANUAL_DND_INACTIVE.id)
            zenModeRepository.addMode(TestModeBuilder.MANUAL_DND_ACTIVE)
            zenModeRepository.activateMode(TestModeBuilder.MANUAL_DND_INACTIVE.id)
            runCurrent()

            assertThat(dndMode!!.isActive).isTrue()
+4 −3
Original line number Diff line number Diff line
@@ -24,7 +24,6 @@ import android.view.ViewTreeObserver
import android.widget.FrameLayout
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.settingslib.notification.modes.TestModeBuilder.MANUAL_DND_ACTIVE
import com.android.settingslib.notification.modes.TestModeBuilder.MANUAL_DND_INACTIVE
import com.android.systemui.Flags as AConfigFlags
import com.android.systemui.SysuiTestCase
@@ -107,6 +106,7 @@ class ClockEventControllerTest : SysuiTestCase() {
    private lateinit var repository: FakeKeyguardRepository
    private val clockBuffers = ClockMessageBuffers(LogcatOnlyMessageBuffer(LogLevel.DEBUG))
    private lateinit var underTest: ClockEventController
    private lateinit var dndModeId: String

    @Mock private lateinit var broadcastDispatcher: BroadcastDispatcher
    @Mock private lateinit var batteryController: BatteryController
@@ -156,6 +156,7 @@ class ClockEventControllerTest : SysuiTestCase() {
        whenever(largeClockController.theme).thenReturn(ThemeConfig(true, null))
        whenever(userTracker.userId).thenReturn(1)

        dndModeId = MANUAL_DND_INACTIVE.id
        zenModeRepository.addMode(MANUAL_DND_INACTIVE)

        repository = FakeKeyguardRepository()
@@ -528,7 +529,7 @@ class ClockEventControllerTest : SysuiTestCase() {
        testScope.runTest {
            underTest.listenForDnd(testScope.backgroundScope)

            zenModeRepository.replaceMode(MANUAL_DND_INACTIVE.id, MANUAL_DND_ACTIVE)
            zenModeRepository.activateMode(dndModeId)
            runCurrent()

            verify(events)
@@ -536,7 +537,7 @@ class ClockEventControllerTest : SysuiTestCase() {
                    eq(ZenData(ZenMode.IMPORTANT_INTERRUPTIONS, R.string::dnd_is_on.name))
                )

            zenModeRepository.replaceMode(MANUAL_DND_ACTIVE.id, MANUAL_DND_INACTIVE)
            zenModeRepository.deactivateMode(dndModeId)
            runCurrent()

            verify(events).onZenDataChanged(eq(ZenData(ZenMode.OFF, R.string::dnd_is_off.name)))