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

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

Merge "Check if the stream can be disabled by the zen mode" into main

parents d3bee4d9 0986cd43
Loading
Loading
Loading
Loading
+47 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.systemui.volume.dialog.domain.interactor

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.kosmos.collectLastValue
import com.android.systemui.kosmos.runTest
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.testKosmos
import com.android.systemui.volume.dialog.domain.model.VolumeDialogEventModel
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith

@SmallTest
@RunWith(AndroidJUnit4::class)
class VolumeDialogCallbacksInteractorTest : SysuiTestCase() {

    private val kosmos = testKosmos().apply { useUnconfinedTestDispatcher() }

    private val underTest: VolumeDialogCallbacksInteractor by lazy {
        kosmos.volumeDialogCallbacksInteractor
    }

    @Test
    fun initialEvent_isSubscribedToEvents() =
        kosmos.runTest {
            val event by collectLastValue(underTest.event)
            assertThat(event).isInstanceOf(VolumeDialogEventModel.SubscribedToEvents::class.java)
        }
}
+81 −41
Original line number Diff line number Diff line
@@ -16,60 +16,62 @@

package com.android.systemui.volume.dialog.sliders.domain.interactor

import android.media.AudioManager
import android.service.notification.ZenPolicy
import android.testing.TestableLooper
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.settingslib.notification.modes.TestModeBuilder
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.plugins.VolumeDialogController
import com.android.systemui.plugins.fakeVolumeDialogController
import com.android.systemui.statusbar.policy.data.repository.zenModeRepository
import com.android.systemui.testKosmos
import com.android.systemui.volume.dialog.sliders.domain.model.VolumeDialogSliderType
import com.android.systemui.volume.dialog.sliders.domain.model.volumeDialogSliderType
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith

@OptIn(ExperimentalCoroutinesApi::class)
@SmallTest
@RunWith(AndroidJUnit4::class)
@TestableLooper.RunWithLooper
class VolumeDialogSliderInteractorTest : SysuiTestCase() {

    private val kosmos = testKosmos()

    private lateinit var underTest: VolumeDialogSliderInteractor
    private val kosmos =
        testKosmos().apply {
            useUnconfinedTestDispatcher()
            zenModeRepository.addMode(
                TestModeBuilder()
                    .setName("Blocks media, Active")
                    .setZenPolicy(ZenPolicy.Builder().allowMedia(false).build())
                    .setActive(true)
                    .build()
            )
        }

    @Before
    fun setUp() {
        underTest = kosmos.volumeDialogSliderInteractor
    private val underTest: VolumeDialogSliderInteractor by lazy {
        kosmos.volumeDialogSliderInteractor
    }

    @Test
    fun settingStreamVolume_setsActiveStream() =
        with(kosmos) {
            testScope.runTest {
                runCurrent()
        kosmos.runTest {
            // initialize the stream model
            fakeVolumeDialogController.setStreamVolume(volumeDialogSliderType.audioStream, 0)

            val sliderModel by collectLastValue(underTest.slider)
            underTest.setStreamVolume(1)
                runCurrent()

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

    @Test
    fun streamVolumeIs_minMaxAreEnforced() =
        with(kosmos) {
            testScope.runTest {
                runCurrent()
        kosmos.runTest {
            fakeVolumeDialogController.updateState {
                states.put(
                    volumeDialogSliderType.audioStream,
@@ -82,9 +84,47 @@ class VolumeDialogSliderInteractorTest : SysuiTestCase() {
            }

            val sliderModel by collectLastValue(underTest.slider)
                runCurrent()

            assertThat(sliderModel!!.level).isEqualTo(1)
        }

    @Test
    fun streamCantBeBlockedByZenMode_isDisabledByZenMode_false() =
        kosmos.runTest {
            volumeDialogSliderType = VolumeDialogSliderType.Stream(AudioManager.STREAM_VOICE_CALL)

            val isDisabledByZenMode by collectLastValue(underTest.isDisabledByZenMode)

            assertThat(isDisabledByZenMode).isFalse()
        }

    @Test
    fun remoteMediaStream_zenModeRestrictive_IsNotDisabledByZenMode() =
        kosmos.runTest {
            volumeDialogSliderType = VolumeDialogSliderType.RemoteMediaStream(0)

            val isDisabledByZenMode by collectLastValue(underTest.isDisabledByZenMode)

            assertThat(isDisabledByZenMode).isFalse()
        }

    @Test
    fun audioSharingStream_zenModeRestrictive_IsNotDisabledByZenMode() =
        kosmos.runTest {
            volumeDialogSliderType = VolumeDialogSliderType.AudioSharingStream(0)

            val isDisabledByZenMode by collectLastValue(underTest.isDisabledByZenMode)

            assertThat(isDisabledByZenMode).isFalse()
        }

    @Test
    fun streamBlockedByZenMode_isDisabledByZenMode_true() =
        kosmos.runTest {
            volumeDialogSliderType = VolumeDialogSliderType.Stream(AudioManager.STREAM_MUSIC)

            val isDisabledByZenMode by collectLastValue(underTest.isDisabledByZenMode)

            assertThat(isDisabledByZenMode).isTrue()
        }
}
+3 −1
Original line number Diff line number Diff line
@@ -25,6 +25,7 @@ import com.android.systemui.volume.dialog.dagger.scope.VolumeDialogPluginScope
import com.android.systemui.volume.dialog.domain.model.VolumeDialogEventModel
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.channels.BufferOverflow
import kotlinx.coroutines.channels.ProducerScope
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
@@ -54,9 +55,10 @@ constructor(
        callbackFlow {
                val producer = VolumeDialogEventModelProducer(this)
                volumeDialogController.addCallback(producer, bgHandler)
                send(VolumeDialogEventModel.SubscribedToEvents)
                awaitClose { volumeDialogController.removeCallback(producer) }
            }
            .buffer(BUFFER_CAPACITY)
            .buffer(capacity = BUFFER_CAPACITY, onBufferOverflow = BufferOverflow.DROP_OLDEST)
            .shareIn(replay = 0, scope = coroutineScope, started = SharingStarted.WhileSubscribed())

    private class VolumeDialogEventModelProducer(
+3 −2
Original line number Diff line number Diff line
@@ -31,7 +31,6 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.launchIn
import kotlinx.coroutines.flow.onEach
import kotlinx.coroutines.flow.onStart

/**
 * Exposes [VolumeDialogController.getState] in the [volumeDialogState].
@@ -65,12 +64,14 @@ constructor(
                    is VolumeDialogEventModel.ShowSafetyWarning -> {
                        setSafetyWarning(VolumeDialogSafetyWarningModel.Visible(event.flags))
                    }
                    is VolumeDialogEventModel.SubscribedToEvents -> {
                        volumeDialogController.getState()
                    }
                    else -> {
                        // do nothing
                    }
                }
            }
            .onStart { volumeDialogController.getState() }
            .launchIn(coroutineScope)
    }

+7 −0
Original line number Diff line number Diff line
@@ -52,4 +52,11 @@ sealed interface VolumeDialogEventModel {
        VolumeDialogEventModel

    data object VolumeChangedFromKey : VolumeDialogEventModel

    /**
     * Signals that the
     * [com.android.systemui.volume.dialog.domain.interactor.VolumeDialogCallbacksInteractor] is
     * ready to process the events.
     */
    data object SubscribedToEvents : VolumeDialogEventModel
}
Loading