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

Commit d0bc6945 authored by Bryce Lee's avatar Bryce Lee Committed by Android (Google) Code Review
Browse files

Merge "Debounce dreaming signal in LowLightBehaviorCoreStartable." into main

parents ef37b7a9 383655e3
Loading
Loading
Loading
Loading
+51 −14
Original line number Diff line number Diff line
@@ -32,6 +32,7 @@ import com.android.systemui.keyguard.domain.interactor.keyguardInteractor
import com.android.systemui.keyguard.shared.model.DozeStateModel
import com.android.systemui.keyguard.shared.model.DozeTransitionModel
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.advanceTimeBy
import com.android.systemui.kosmos.backgroundScope
import com.android.systemui.kosmos.runCurrent
import com.android.systemui.kosmos.runTest
@@ -61,6 +62,7 @@ import com.android.systemui.user.domain.interactor.selectedUserInteractor
import com.android.systemui.user.domain.interactor.userLockedInteractor
import com.android.systemui.util.settings.fakeSettings
import com.google.common.truth.Truth.assertThat
import kotlin.time.Duration.Companion.milliseconds
import kotlinx.coroutines.runBlocking
import org.junit.Before
import org.junit.Test
@@ -120,10 +122,25 @@ class LowLightBehaviorCoreStartableTest : SysuiTestCase() {
        )
    }

    private fun Kosmos.debounce() {
        advanceTimeBy(
            (LowLightBehaviorCoreStartable.DREAM_STATE_DEBOUNCE_DURATION_MS + 1).milliseconds
        )
    }

    private fun Kosmos.start() {
        underTest.start()
        debounce()
    }

    private fun Kosmos.setUserUnlocked(unlocked: Boolean) {
        fakeUserRepository.setUserUnlocked(selectedUserInteractor.getSelectedUserId(), unlocked)
    }

    private fun Kosmos.setDreaming(dreaming: Boolean) {
        fakeKeyguardRepository.setDreaming(dreaming)
    }

    private fun Kosmos.setAllowLowLightWhenLocked(allowed: Boolean) {
        whenever(mockResources.getBoolean(R.bool.config_allowLowLightBehaviorWhenLocked))
            .thenReturn(allowed)
@@ -170,7 +187,7 @@ class LowLightBehaviorCoreStartableTest : SysuiTestCase() {
    @Test
    fun testSetAmbientLowLightWhenInLowLight() =
        kosmos.runTest {
            underTest.start()
            start()

            // Turn on screen
            setDisplayOn(true)
@@ -184,18 +201,38 @@ class LowLightBehaviorCoreStartableTest : SysuiTestCase() {
        kosmos.runTest {
            setUserUnlocked(false)
            setAllowLowLightWhenLocked(true)
            underTest.start()
            start()
            setDisplayOn(true)

            assertThat(action.activationCount).isEqualTo(1)
        }

    @Test
    fun testDreamDebounce() =
        kosmos.runTest {
            kosmos.fakeKeyguardRepository.setKeyguardShowing(false)
            setUserUnlocked(true)
            setDreamEnabled(true)
            setAllowLowLightWhenLocked(true)
            setDisplayOn(true)
            setLowLightFromSensor(true)
            start()
            setDreaming(true)
            assertThat(action.activationCount).isEqualTo(0)
            debounce()
            assertThat(action.activationCount).isEqualTo(1)
            setDreaming(false)
            assertThat(action.cancellationCount).isEqualTo(0)
            debounce()
            assertThat(action.cancellationCount).isEqualTo(1)
        }

    @Test
    fun testWhenDisallowLowLightBehaviorWhenLockedAndUserLocked_LowLightBehaviorDoesNotActivate() =
        kosmos.runTest {
            setUserUnlocked(false)
            setAllowLowLightWhenLocked(false)
            underTest.start()
            start()
            setDisplayOn(true)

            assertThat(action.activationCount).isEqualTo(0)
@@ -205,7 +242,7 @@ class LowLightBehaviorCoreStartableTest : SysuiTestCase() {
    fun testSetAmbientLowLightWhenDisabledInLowLight() =
        kosmos.runTest {
            lowLightSettingsRepository.setLowLightDisplayBehaviorEnabled(false)
            underTest.start()
            start()

            // Turn on screen
            setDisplayOn(true)
@@ -221,7 +258,7 @@ class LowLightBehaviorCoreStartableTest : SysuiTestCase() {
            setDisplayOn(true)
            setLowLightFromSensor(true)

            underTest.start()
            start()

            assertThat(action.cancellationCount).isEqualTo(0)
            assertThat(action.activationCount).isEqualTo(1)
@@ -233,7 +270,7 @@ class LowLightBehaviorCoreStartableTest : SysuiTestCase() {
    @Test
    fun testStopMonitorLowLightConditionsWhenScreenTurnsOff() =
        kosmos.runTest {
            underTest.start()
            start()

            setDisplayOn(true)
            assertThat(ambientLightModeMonitor.fake.started).isTrue()
@@ -246,7 +283,7 @@ class LowLightBehaviorCoreStartableTest : SysuiTestCase() {
    @Test
    fun testStopMonitorLowLightConditionsWhenMonitorSwapped() =
        kosmos.runTest {
            underTest.start()
            start()

            setDisplayOn(true)
            assertThat(ambientLightModeMonitor.fake.started).isTrue()
@@ -261,7 +298,7 @@ class LowLightBehaviorCoreStartableTest : SysuiTestCase() {
    @Test
    fun testStopMonitorLowLightConditionsWhenDreamDisabled() =
        kosmos.runTest {
            underTest.start()
            start()

            setDisplayOn(true)
            setDreamEnabled(true)
@@ -278,7 +315,7 @@ class LowLightBehaviorCoreStartableTest : SysuiTestCase() {
        kosmos.runTest {
            setDisplayOn(true)

            underTest.start()
            start()
            assertThat(ambientLightModeMonitor.fake.started).isTrue()
        }

@@ -292,7 +329,7 @@ class LowLightBehaviorCoreStartableTest : SysuiTestCase() {

            setDisplayOn(true)

            underTest.start()
            start()
            assertThat(ambientLightModeMonitor.fake.started).isTrue()
        }

@@ -311,7 +348,7 @@ class LowLightBehaviorCoreStartableTest : SysuiTestCase() {
                DozeTransitionModel(from = DozeStateModel.UNINITIALIZED, to = DozeStateModel.DOZE)
            )

            underTest.start()
            start()
            assertThat(ambientLightModeMonitor.fake.started).isTrue()
        }

@@ -330,7 +367,7 @@ class LowLightBehaviorCoreStartableTest : SysuiTestCase() {
                DozeTransitionModel(from = DozeStateModel.UNINITIALIZED, to = DozeStateModel.DOZE)
            )

            underTest.start()
            start()
            assertThat(ambientLightModeMonitor.fake.started).isFalse()
        }

@@ -349,7 +386,7 @@ class LowLightBehaviorCoreStartableTest : SysuiTestCase() {
                DozeTransitionModel(from = DozeStateModel.UNINITIALIZED, to = DozeStateModel.DOZE)
            )

            underTest.start()
            start()
            assertThat(ambientLightModeMonitor.fake.started).isFalse()
        }

@@ -368,7 +405,7 @@ class LowLightBehaviorCoreStartableTest : SysuiTestCase() {
            setBatteryPluggedIn(true)
            setDisplayOn(true)

            underTest.start()
            start()
            assertThat(ambientLightModeMonitor.fake.started).isFalse()
        }

+10 −1
Original line number Diff line number Diff line
@@ -17,6 +17,7 @@ package com.android.systemui.lowlight

import android.os.Flags
import android.os.UserHandle
import androidx.annotation.VisibleForTesting
import com.android.internal.logging.UiEventLogger
import com.android.systemui.CoreStartable
import com.android.systemui.common.domain.interactor.BatteryInteractorDeprecated
@@ -46,13 +47,16 @@ import com.android.systemui.util.kotlin.BooleanFlowOperators.not
import com.android.systemui.utils.coroutines.flow.conflatedCallbackFlow
import com.android.systemui.utils.coroutines.flow.flatMapLatestConflated
import javax.inject.Inject
import kotlin.time.Duration.Companion.milliseconds
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.FlowPreview
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.SharingStarted
import kotlinx.coroutines.flow.collectLatest
import kotlinx.coroutines.flow.combine
import kotlinx.coroutines.flow.debounce
import kotlinx.coroutines.flow.distinctUntilChanged
import kotlinx.coroutines.flow.filterNot
import kotlinx.coroutines.flow.flowOf
@@ -157,11 +161,14 @@ constructor(
     * Whether the device is idle (lockscreen showing or dreaming or asleep) and not in doze/AOD, as
     * we do not want to override doze/AOD with lowlight dream.
     */
    @OptIn(FlowPreview::class)
    private val isDeviceIdleAndNotDozing: Flow<Boolean> =
        allOf(
            not(anyDoze),
            anyOf(
                keyguardInteractor.isDreaming,
                keyguardInteractor.isDreaming.debounce(
                    DREAM_STATE_DEBOUNCE_DURATION_MS.milliseconds
                ),
                keyguardInteractor.isKeyguardShowing,
                powerInteractor.isAsleep,
            ),
@@ -224,5 +231,7 @@ constructor(

    companion object {
        private const val TAG = "LowLightBehaviorCoreStartable"

        @VisibleForTesting const val DREAM_STATE_DEBOUNCE_DURATION_MS = 100
    }
}