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

Commit ec615d01 authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge "Modes dual-target tile: Activate the last-used manual mode, instead of DND" into main

parents d3e40abc 0467a3bc
Loading
Loading
Loading
Loading
+1 −2
Original line number Diff line number Diff line
@@ -22,7 +22,6 @@ import android.provider.Settings
import com.android.settingslib.notification.modes.TestModeBuilder
import com.android.settingslib.notification.modes.ZenMode
import java.time.Duration
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asStateFlow
@@ -40,7 +39,7 @@ class FakeZenModeRepository : ZenModeRepository {
    private val mutableModesFlow: MutableStateFlow<List<ZenMode>> by lazy {
        MutableStateFlow(listOf(TestModeBuilder.MANUAL_DND))
    }
    override val modes: Flow<List<ZenMode>>
    override val modes: StateFlow<List<ZenMode>>
        get() = mutableModesFlow.asStateFlow()

    override fun getModes(): List<ZenMode> = mutableModesFlow.value
+1 −2
Original line number Diff line number Diff line
@@ -33,7 +33,6 @@ import java.time.Duration
import kotlin.coroutines.CoroutineContext
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.StateFlow
@@ -57,7 +56,7 @@ interface ZenModeRepository {
    val globalZenMode: StateFlow<Int?>

    /** A list of all existing priority modes. */
    val modes: Flow<List<ZenMode>>
    val modes: StateFlow<List<ZenMode>>

    fun getModes(): List<ZenMode>

+2 −0
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import android.testing.TestableLooper.RunWithLooper
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.internal.logging.MetricsLogger
import com.android.settingslib.notification.modes.TestModeBuilder
import com.android.systemui.SysuiTestCase
import com.android.systemui.classifier.FalsingManagerFake
import com.android.systemui.common.shared.model.asIcon
@@ -187,6 +188,7 @@ class ModesTileTest : SysuiTestCase() {
                    isActivated = true,
                    activeModes = listOf("One", "Two"),
                    icon = TestStubDrawable().asIcon(),
                    quickMode = TestModeBuilder.MANUAL_DND,
                )

            underTest.handleUpdateState(tileState, model)
+113 −0
Original line number Diff line number Diff line
@@ -40,6 +40,8 @@ import com.android.systemui.statusbar.policy.data.repository.fakeZenModeReposito
import com.android.systemui.statusbar.policy.domain.interactor.zenModeInteractor
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import java.time.Instant
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.flow.toCollection
import kotlinx.coroutines.test.runCurrent
@@ -50,6 +52,7 @@ import org.junit.runner.RunWith

@SmallTest
@RunWith(AndroidJUnit4::class)
@OptIn(ExperimentalCoroutinesApi::class)
class ModesTileDataInteractorTest : SysuiTestCase() {
    private val kosmos = testKosmos()
    private val testScope = kosmos.testScope
@@ -65,6 +68,7 @@ class ModesTileDataInteractorTest : SysuiTestCase() {
        context.orCreateTestableResources.apply {
            addOverride(MODES_DRAWABLE_ID, MODES_DRAWABLE)
            addOverride(BEDTIME_DRAWABLE_ID, BEDTIME_DRAWABLE)
            addOverride(THEATER_DRAWABLE_ID, THEATER_DRAWABLE)
        }

        val customPackageContext = SysuiTestableContext(context)
@@ -194,6 +198,67 @@ class ModesTileDataInteractorTest : SysuiTestCase() {
            assertThat(tileData?.icon!!.res).isEqualTo(MODES_DRAWABLE_ID)
        }

    @Test
    @EnableFlags(
        Flags.FLAG_MODES_UI,
        Flags.FLAG_MODES_UI_ICONS,
        Flags.FLAG_MODES_UI_TILE_REACTIVATES_LAST,
        com.android.systemui.Flags.FLAG_QS_UI_REFACTOR_COMPOSE_FRAGMENT,
    )
    fun tileData_withPastManualActivation_iconOfMruManualMode() =
        testScope.runTest {
            val tileData by
                collectLastValue(
                    underTest.tileData(TEST_USER, flowOf(DataUpdateTrigger.InitialRequest))
                )

            // Tile starts with the generic Modes icon.
            runCurrent()
            assertThat(tileData?.icon).isEqualTo(MODES_ICON)

            // With modes that were never activated, and no active modes -> Still modes icon
            zenModeRepository.addMode(
                TestModeBuilder()
                    .setId("Manual Mode 1")
                    .setManualInvocationAllowed(true)
                    .setPackage("android")
                    .setIconResId(BEDTIME_DRAWABLE_ID)
                    .build()
            )
            zenModeRepository.addMode(
                TestModeBuilder()
                    .setId("Manual Mode 2")
                    .setManualInvocationAllowed(true)
                    .setPackage("android")
                    .setIconResId(THEATER_DRAWABLE_ID)
                    .build()
            )
            zenModeRepository.addMode(
                TestModeBuilder().setId("Manual Mode 3").setManualInvocationAllowed(true).build()
            )
            runCurrent()
            assertThat(tileData?.icon).isEqualTo(MODES_ICON)

            // With modes that were activated manually -> Icon of the last manually activated mode
            zenModeRepository.updateMode("Manual Mode 3") {
                TestModeBuilder(it).setLastManualActivation(Instant.ofEpochMilli(100)).build()
            }
            zenModeRepository.updateMode("Manual Mode 2") {
                TestModeBuilder(it).setLastManualActivation(Instant.ofEpochMilli(200)).build()
            }
            runCurrent()
            assertThat(tileData?.icon).isEqualTo(THEATER_ICON)

            // With an active mode -> the icon of the active mode, regardless of past activations
            zenModeRepository.addMode(
                id = "Active automatic mode",
                type = AutomaticZenRule.TYPE_BEDTIME,
                active = true,
            )
            runCurrent()
            assertThat(tileData?.icon).isEqualTo(BEDTIME_ICON)
        }

    @Test
    @EnableFlags(Flags.FLAG_MODES_UI)
    @DisableFlags(Flags.FLAG_MODES_UI_ICONS)
@@ -220,6 +285,51 @@ class ModesTileDataInteractorTest : SysuiTestCase() {
            assertThat(tileData?.icon!!.res).isEqualTo(MODES_DRAWABLE_ID)
        }

    @EnableFlags(Flags.FLAG_MODES_UI, Flags.FLAG_MODES_UI_TILE_REACTIVATES_LAST)
    fun tileData_withPastManualActivation_mruManualModeAsQuickMode() =
        testScope.runTest {
            val tileData by
                collectLastValue(
                    underTest.tileData(TEST_USER, flowOf(DataUpdateTrigger.InitialRequest))
                )

            // Default -> DND
            runCurrent()
            assertThat(tileData?.quickMode?.id).isEqualTo(TestModeBuilder.MANUAL_DND.id)

            // With modes that were never activated, and no active modes -> Still DND
            zenModeRepository.addMode(
                TestModeBuilder().setId("Manual Mode 1").setManualInvocationAllowed(true).build()
            )
            zenModeRepository.addMode(
                TestModeBuilder().setId("Manual Mode 2").setManualInvocationAllowed(true).build()
            )
            zenModeRepository.addMode(
                TestModeBuilder().setId("Manual Mode 3").setManualInvocationAllowed(true).build()
            )
            runCurrent()
            assertThat(tileData?.quickMode?.id).isEqualTo(TestModeBuilder.MANUAL_DND.id)

            // With modes that were activated manually -> last manually activated mode
            zenModeRepository.updateMode("Manual Mode 3") {
                TestModeBuilder(it).setLastManualActivation(Instant.ofEpochMilli(100)).build()
            }
            zenModeRepository.updateMode("Manual Mode 2") {
                TestModeBuilder(it).setLastManualActivation(Instant.ofEpochMilli(200)).build()
            }
            runCurrent()
            assertThat(tileData?.quickMode?.id).isEqualTo("Manual Mode 2")

            // Active modes have no effect -> still last manually activated mode
            zenModeRepository.addMode(
                id = "Active mode",
                type = AutomaticZenRule.TYPE_BEDTIME,
                active = true,
            )
            runCurrent()
            assertThat(tileData?.quickMode?.id).isEqualTo("Manual Mode 2")
        }

    @EnableFlags(Flags.FLAG_MODES_UI)
    @Test
    fun getCurrentTileModel_returnsActiveModes() = runTest {
@@ -261,13 +371,16 @@ class ModesTileDataInteractorTest : SysuiTestCase() {
        const val CUSTOM_DRAWABLE_ID = 12345

        const val BEDTIME_DRAWABLE_ID = R.drawable.ic_zen_mode_type_bedtime
        const val THEATER_DRAWABLE_ID = R.drawable.ic_zen_mode_type_theater

        val MODES_DRAWABLE = TestStubDrawable("modes_icon")
        val BEDTIME_DRAWABLE = TestStubDrawable("bedtime")
        val THEATER_DRAWABLE = TestStubDrawable("theater")
        val CUSTOM_DRAWABLE = TestStubDrawable("custom")

        val MODES_ICON = Icon.Loaded(MODES_DRAWABLE, null, MODES_DRAWABLE_ID)
        val BEDTIME_ICON = Icon.Loaded(BEDTIME_DRAWABLE, null, BEDTIME_DRAWABLE_ID)
        val THEATER_ICON = Icon.Loaded(THEATER_DRAWABLE, null, THEATER_DRAWABLE_ID)
        val CUSTOM_ICON = CUSTOM_DRAWABLE.asIcon()
    }
}
+28 −1
Original line number Diff line number Diff line
@@ -17,12 +17,14 @@
package com.android.systemui.qs.tiles.impl.modes.domain.interactor

import android.graphics.drawable.TestStubDrawable
import android.platform.test.annotations.DisableFlags
import android.platform.test.annotations.EnableFlags
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.settingslib.notification.modes.ZenMode
import com.android.systemui.Flags
import com.android.systemui.SysuiTestCase
import com.android.systemui.animation.Expandable
@@ -40,6 +42,7 @@ import com.android.systemui.statusbar.policy.ui.dialog.mockModesDialogDelegate
import com.android.systemui.statusbar.policy.ui.dialog.modesDialogEventLogger
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith
@@ -129,6 +132,7 @@ class ModesTileUserActionInteractorTest : SysuiTestCase() {

    @Test
    @EnableFlags(Flags.FLAG_QS_UI_REFACTOR_COMPOSE_FRAGMENT)
    @DisableFlags(android.app.Flags.FLAG_MODES_UI_TILE_REACTIVATES_LAST)
    fun handleToggleClick_dndInactive_activatesDnd() =
        testScope.runTest {
            val dndMode by collectLastValue(zenModeInteractor.dndMode)
@@ -142,6 +146,24 @@ class ModesTileUserActionInteractorTest : SysuiTestCase() {
            assertThat(dndMode?.isActive).isTrue()
        }

    @Test
    @EnableFlags(
        Flags.FLAG_QS_UI_REFACTOR_COMPOSE_FRAGMENT,
        android.app.Flags.FLAG_MODES_UI_TILE_REACTIVATES_LAST,
    )
    fun handleToggleClick_noModesActive_activatesQuickMode() =
        testScope.runTest {
            val dndMode by collectLastValue(zenModeInteractor.dndMode)
            zenModeRepository.addMode("mode", active = false)
            val model = modelOf(false, emptyList(), quickMode = zenModeRepository.getMode("mode")!!)

            underTest.handleInput(QSTileInputTestKtx.toggleClick(model))

            runCurrent()
            assertThat(zenModeRepository.getMode("mode")?.isActive).isTrue()
            assertThat(dndMode?.isActive).isFalse()
        }

    @Test
    fun handleLongClick_active_opensSettings() = runTest {
        underTest.handleInput(QSTileInputTestKtx.longClick(modelOf(true, listOf("DND"))))
@@ -160,11 +182,16 @@ class ModesTileUserActionInteractorTest : SysuiTestCase() {
        }
    }

    private fun modelOf(isActivated: Boolean, activeModeNames: List<String>): ModesTileModel {
    private fun modelOf(
        isActivated: Boolean,
        activeModeNames: List<String>,
        quickMode: ZenMode? = MANUAL_DND,
    ): ModesTileModel {
        return ModesTileModel(
            isActivated,
            activeModeNames,
            TestStubDrawable("icon").asIcon(res = 123),
            quickMode,
        )
    }
}
Loading