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

Commit 9561c544 authored by Fabian Kozynski's avatar Fabian Kozynski Committed by Android (Google) Code Review
Browse files

Merge "Refactor brightness stack" into main

parents fb61b5a0 ea190138
Loading
Loading
Loading
Loading
+136 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.brightness.data.repository

import android.content.applicationContext
import android.os.UserManager
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.kosmos.testScope
import com.android.systemui.testKosmos
import com.android.systemui.user.data.repository.fakeUserRepository
import com.android.systemui.user.data.repository.userRepository
import com.android.systemui.util.mockito.any
import com.android.systemui.util.mockito.eq
import com.android.systemui.util.mockito.mock
import com.android.systemui.util.mockito.whenever
import com.android.systemui.utils.PolicyRestriction
import com.android.systemui.utils.UserRestrictionChecker
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.ArgumentMatchers.anyString

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

    private val kosmos = testKosmos()

    private val fakeUserRepository = kosmos.fakeUserRepository

    private val mockUserRestrictionChecker: UserRestrictionChecker = mock {
        whenever(checkIfRestrictionEnforced(any(), anyString(), anyInt())).thenReturn(null)
        whenever(hasBaseUserRestriction(any(), anyString(), anyInt())).thenReturn(false)
    }

    private val underTest =
        with(kosmos) {
            BrightnessPolicyRepositoryImpl(
                userRepository,
                mockUserRestrictionChecker,
                applicationContext,
                testDispatcher,
            )
        }

    @Test
    fun noRestrictionByDefaultForAllUsers() =
        with(kosmos) {
            testScope.runTest {
                val restrictions by collectLastValue(underTest.restrictionPolicy)

                assertThat(restrictions).isEqualTo(PolicyRestriction.NoRestriction)

                fakeUserRepository.asMainUser()

                assertThat(restrictions).isEqualTo(PolicyRestriction.NoRestriction)
            }
        }

    @Test
    fun restrictDefaultUser() =
        with(kosmos) {
            testScope.runTest {
                val enforcedAdmin: EnforcedAdmin =
                    EnforcedAdmin.createDefaultEnforcedAdminWithRestriction(RESTRICTION)

                whenever(
                        mockUserRestrictionChecker.checkIfRestrictionEnforced(
                            any(),
                            eq(RESTRICTION),
                            eq(userRepository.getSelectedUserInfo().id)
                        )
                    )
                    .thenReturn(enforcedAdmin)

                val restrictions by collectLastValue(underTest.restrictionPolicy)

                assertThat(restrictions).isEqualTo(PolicyRestriction.Restricted(enforcedAdmin))

                fakeUserRepository.asMainUser()

                assertThat(restrictions).isEqualTo(PolicyRestriction.NoRestriction)
            }
        }

    @Test
    fun restrictMainUser() =
        with(kosmos) {
            testScope.runTest {
                val enforcedAdmin: EnforcedAdmin =
                    EnforcedAdmin.createDefaultEnforcedAdminWithRestriction(RESTRICTION)

                whenever(
                        mockUserRestrictionChecker.checkIfRestrictionEnforced(
                            any(),
                            eq(RESTRICTION),
                            eq(userRepository.mainUserId)
                        )
                    )
                    .thenReturn(enforcedAdmin)

                val restrictions by collectLastValue(underTest.restrictionPolicy)

                assertThat(restrictions).isEqualTo(PolicyRestriction.NoRestriction)

                fakeUserRepository.asMainUser()

                assertThat(restrictions).isEqualTo(PolicyRestriction.Restricted(enforcedAdmin))
            }
        }

    private companion object {
        val RESTRICTION = UserManager.DISALLOW_CONFIG_BRIGHTNESS
    }
}
+249 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.brightness.data.repository

import android.hardware.display.BrightnessInfo
import android.hardware.display.BrightnessInfo.BRIGHTNESS_MAX_REASON_NONE
import android.hardware.display.BrightnessInfo.HIGH_BRIGHTNESS_MODE_OFF
import android.hardware.display.DisplayManager
import android.hardware.display.DisplayManager.EVENT_FLAG_DISPLAY_BRIGHTNESS
import android.view.Display
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.brightness.data.model.LinearBrightness
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.kosmos.applicationCoroutineScope
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.kosmos.testScope
import com.android.systemui.testKosmos
import com.android.systemui.util.mockito.argumentCaptor
import com.android.systemui.util.mockito.capture
import com.android.systemui.util.mockito.eq
import com.android.systemui.util.mockito.whenever
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.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyFloat
import org.mockito.ArgumentMatchers.anyInt
import org.mockito.Mock
import org.mockito.Mockito.never
import org.mockito.Mockito.verify
import org.mockito.junit.MockitoJUnit
import org.mockito.junit.MockitoRule

@OptIn(ExperimentalCoroutinesApi::class)
@SmallTest
@RunWith(AndroidJUnit4::class)
class ScreenBrightnessDisplayManagerRepositoryTest : SysuiTestCase() {
    @Rule @JvmField val mockitoRule: MockitoRule = MockitoJUnit.rule()

    private val kosmos = testKosmos()

    private var currentBrightnessInfo = BrightnessInfo()

    @Mock private lateinit var displayManager: DisplayManager
    @Mock private lateinit var display: Display

    private val displayId = 0

    private lateinit var underTest: ScreenBrightnessDisplayManagerRepository

    @Before
    fun setUp() {
        underTest =
            ScreenBrightnessDisplayManagerRepository(
                displayId,
                displayManager,
                kosmos.applicationCoroutineScope,
                kosmos.testDispatcher,
            )

        whenever(displayManager.getDisplay(displayId)).thenReturn(display)
        // Using then answer so it will be retrieved in every call
        whenever(display.brightnessInfo).thenAnswer { currentBrightnessInfo }
    }

    @Test
    fun startingBrightnessInfo() =
        with(kosmos) {
            testScope.runTest {
                val brightness by collectLastValue(underTest.linearBrightness)
                val minBrightness by collectLastValue(underTest.minLinearBrightness)
                val maxBrightness by collectLastValue(underTest.maxLinearBrightness)
                runCurrent()

                assertThat(brightness?.floatValue).isEqualTo(currentBrightnessInfo.brightness)
                assertThat(minBrightness?.floatValue)
                    .isEqualTo(currentBrightnessInfo.brightnessMinimum)
                assertThat(maxBrightness?.floatValue)
                    .isEqualTo(currentBrightnessInfo.brightnessMaximum)
            }
        }

    @Test
    fun followsChangingBrightnessInfo() =
        with(kosmos) {
            testScope.runTest {
                val listenerCaptor = argumentCaptor<DisplayManager.DisplayListener>()

                val brightness by collectLastValue(underTest.linearBrightness)
                val minBrightness by collectLastValue(underTest.minLinearBrightness)
                val maxBrightness by collectLastValue(underTest.maxLinearBrightness)
                runCurrent()

                verify(displayManager)
                    .registerDisplayListener(
                        capture(listenerCaptor),
                        eq(null),
                        eq(EVENT_FLAG_DISPLAY_BRIGHTNESS),
                    )

                val newBrightness = BrightnessInfo(0.6f, 0.3f, 0.9f)
                changeBrightnessInfoAndNotify(newBrightness, listenerCaptor.value)

                assertThat(brightness?.floatValue).isEqualTo(currentBrightnessInfo.brightness)
                assertThat(minBrightness?.floatValue)
                    .isEqualTo(currentBrightnessInfo.brightnessMinimum)
                assertThat(maxBrightness?.floatValue)
                    .isEqualTo(currentBrightnessInfo.brightnessMaximum)
            }
        }

    @Test
    fun minMaxWhenNotCollecting() =
        with(kosmos) {
            testScope.runTest {
                currentBrightnessInfo = BrightnessInfo(0.5f, 0.1f, 0.7f)
                val (min, max) = underTest.getMinMaxLinearBrightness()
                assertThat(min.floatValue).isEqualTo(currentBrightnessInfo.brightnessMinimum)
                assertThat(max.floatValue).isEqualTo(currentBrightnessInfo.brightnessMaximum)
            }
        }

    @Test
    fun minMaxWhenCollecting() =
        with(kosmos) {
            testScope.runTest {
                val listenerCaptor = argumentCaptor<DisplayManager.DisplayListener>()

                val brightness by collectLastValue(underTest.linearBrightness)
                runCurrent()

                verify(displayManager)
                    .registerDisplayListener(
                        capture(listenerCaptor),
                        eq(null),
                        eq(EVENT_FLAG_DISPLAY_BRIGHTNESS),
                    )

                changeBrightnessInfoAndNotify(
                    BrightnessInfo(0.5f, 0.1f, 0.7f),
                    listenerCaptor.value
                )
                runCurrent()

                val (min, max) = underTest.getMinMaxLinearBrightness()
                assertThat(min.floatValue).isEqualTo(currentBrightnessInfo.brightnessMinimum)
                assertThat(max.floatValue).isEqualTo(currentBrightnessInfo.brightnessMaximum)
            }
        }

    @Test
    fun setTemporaryBrightness_insideBounds() =
        with(kosmos) {
            testScope.runTest {
                val brightness = 0.3f
                underTest.setTemporaryBrightness(LinearBrightness(brightness))
                runCurrent()

                verify(displayManager).setTemporaryBrightness(displayId, brightness)
                verify(displayManager, never()).setBrightness(anyInt(), anyFloat())
            }
        }

    @Test
    fun setTemporaryBrightness_outsideBounds() =
        with(kosmos) {
            testScope.runTest {
                val brightness = 1.3f
                underTest.setTemporaryBrightness(LinearBrightness(brightness))
                runCurrent()

                verify(displayManager)
                    .setTemporaryBrightness(displayId, currentBrightnessInfo.brightnessMaximum)
                verify(displayManager, never()).setBrightness(anyInt(), anyFloat())
            }
        }

    @Test
    fun setBrightness_insideBounds() =
        with(kosmos) {
            testScope.runTest {
                val brightness = 0.3f
                underTest.setBrightness(LinearBrightness(brightness))
                runCurrent()

                verify(displayManager).setBrightness(displayId, brightness)
                verify(displayManager, never()).setTemporaryBrightness(anyInt(), anyFloat())
            }
        }

    @Test
    fun setBrightness_outsideBounds() =
        with(kosmos) {
            testScope.runTest {
                val brightness = 1.3f
                underTest.setBrightness(LinearBrightness(brightness))
                runCurrent()

                verify(displayManager)
                    .setBrightness(displayId, currentBrightnessInfo.brightnessMaximum)
                verify(displayManager, never()).setTemporaryBrightness(anyInt(), anyFloat())
            }
        }

    private fun changeBrightnessInfoAndNotify(
        newValue: BrightnessInfo,
        listener: DisplayManager.DisplayListener,
    ) {
        currentBrightnessInfo = newValue
        listener.onDisplayChanged(displayId)
    }

    companion object {
        fun BrightnessInfo(
            brightness: Float = 0f,
            minBrightness: Float = 0f,
            maxBrightness: Float = 1f,
        ): BrightnessInfo {
            return BrightnessInfo(
                brightness,
                minBrightness,
                maxBrightness,
                HIGH_BRIGHTNESS_MODE_OFF,
                1f,
                BRIGHTNESS_MAX_REASON_NONE,
            )
        }
    }
}
+116 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.brightness.domain.interactor

import android.content.ComponentName
import android.content.Intent
import android.os.UserHandle
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.settingslib.RestrictedLockUtils
import com.android.settingslib.RestrictedLockUtils.EnforcedAdmin
import com.android.systemui.SysuiTestCase
import com.android.systemui.brightness.data.repository.BrightnessPolicyRepository
import com.android.systemui.brightness.data.repository.brightnessPolicyRepository
import com.android.systemui.brightness.data.repository.fakeBrightnessPolicyRepository
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.kosmos.testScope
import com.android.systemui.plugins.activityStarter
import com.android.systemui.testKosmos
import com.android.systemui.util.mockito.argumentCaptor
import com.android.systemui.util.mockito.capture
import com.android.systemui.util.mockito.eq
import com.android.systemui.utils.PolicyRestriction
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.verify

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

    private val kosmos = testKosmos()

    private val mockActivityStarter = kosmos.activityStarter
    private val fakeBrightnessPolicyEnforcementInteractor = kosmos.fakeBrightnessPolicyRepository

    private val underTest =
        with(kosmos) {
            BrightnessPolicyEnforcementInteractor(
                brightnessPolicyRepository,
                activityStarter,
            )
        }

    @Test
    fun restriction() =
        with(kosmos) {
            testScope.runTest {
                fakeBrightnessPolicyRepository.setCurrentUserUnrestricted()

                val restriction by collectLastValue(underTest.brightnessPolicyRestriction)

                assertThat(restriction).isEqualTo(PolicyRestriction.NoRestriction)

                fakeBrightnessPolicyRepository.setCurrentUserRestricted()

                assertThat(restriction).isInstanceOf(PolicyRestriction.Restricted::class.java)
            }
        }

    @Test
    fun startRestrictionDialog() =
        with(kosmos) {
            testScope.runTest {
                val enforcedAdmin =
                    EnforcedAdmin.createDefaultEnforcedAdminWithRestriction(
                            BrightnessPolicyRepository.RESTRICTION
                        )
                        .apply {
                            component = TEST_COMPONENT
                            user = UserHandle.of(TEST_USER)
                        }

                underTest.startAdminSupportDetailsDialog(
                    PolicyRestriction.Restricted(enforcedAdmin)
                )

                val intentCaptor = argumentCaptor<Intent>()

                verify(mockActivityStarter)
                    .postStartActivityDismissingKeyguard(
                        capture(intentCaptor),
                        eq(0),
                    )

                val expectedIntent =
                    RestrictedLockUtils.getShowAdminSupportDetailsIntent(enforcedAdmin)

                with(intentCaptor.value) {
                    assertThat(action).isEqualTo(expectedIntent.action)
                    assertThat(extras!!.kindofEquals(expectedIntent.extras)).isTrue()
                }
            }
        }

    private companion object {
        val TEST_COMPONENT = ComponentName("pkg", ".cls")
        val TEST_USER = 10
    }
}
+146 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2024 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.brightness.domain.interactor

import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.settingslib.display.BrightnessUtils
import com.android.systemui.SysuiTestCase
import com.android.systemui.brightness.data.model.LinearBrightness
import com.android.systemui.brightness.data.repository.fakeScreenBrightnessRepository
import com.android.systemui.brightness.data.repository.screenBrightnessRepository
import com.android.systemui.brightness.shared.GammaBrightness
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.kosmos.testScope
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith

@OptIn(ExperimentalCoroutinesApi::class)
@SmallTest
@RunWith(AndroidJUnit4::class)
class ScreenBrightnessInteractorTest : SysuiTestCase() {

    private val kosmos = testKosmos()

    private val underTest = ScreenBrightnessInteractor(kosmos.screenBrightnessRepository)

    @Test
    fun gammaBrightness() =
        with(kosmos) {
            testScope.runTest {
                val gammaBrightness by collectLastValue(underTest.gammaBrightness)

                val brightness = 0.3f
                val min = 0f
                val max = 1f

                with(fakeScreenBrightnessRepository) {
                    setBrightness(LinearBrightness(brightness))
                    setMinMaxBrightness(LinearBrightness(min), LinearBrightness(max))
                }
                runCurrent()

                assertThat(gammaBrightness?.value)
                    .isEqualTo(BrightnessUtils.convertLinearToGammaFloat(brightness, min, max))
            }
        }

    @Test
    fun gammaBrightness_constrained() =
        with(kosmos) {
            testScope.runTest {
                val gammaBrightness by collectLastValue(underTest.gammaBrightness)

                val brightness = 0.3f
                val min = 0.2f
                val max = 0.8f

                with(fakeScreenBrightnessRepository) {
                    setBrightness(LinearBrightness(brightness))
                    setMinMaxBrightness(LinearBrightness(min), LinearBrightness(max))
                }
                runCurrent()

                assertThat(gammaBrightness?.value)
                    .isEqualTo(BrightnessUtils.convertLinearToGammaFloat(brightness, min, max))
            }
        }

    @Test
    fun setTemporaryBrightness() =
        with(kosmos) {
            testScope.runTest {
                val temporaryBrightness by
                    collectLastValue(fakeScreenBrightnessRepository.temporaryBrightness)
                val brightness by collectLastValue(underTest.gammaBrightness)

                val gammaBrightness = 30000
                underTest.setTemporaryBrightness(GammaBrightness(gammaBrightness))

                val (min, max) = fakeScreenBrightnessRepository.getMinMaxLinearBrightness()

                val expectedTemporaryBrightness =
                    BrightnessUtils.convertGammaToLinearFloat(
                        gammaBrightness,
                        min.floatValue,
                        max.floatValue
                    )
                assertThat(temporaryBrightness!!.floatValue)
                    .isWithin(1e-5f)
                    .of(expectedTemporaryBrightness)
                assertThat(brightness!!.value).isNotEqualTo(gammaBrightness)
            }
        }

    @Test
    fun setBrightness() =
        with(kosmos) {
            testScope.runTest {
                val brightness by collectLastValue(fakeScreenBrightnessRepository.linearBrightness)

                val gammaBrightness = 30000
                underTest.setBrightness(GammaBrightness(gammaBrightness))

                val (min, max) = fakeScreenBrightnessRepository.getMinMaxLinearBrightness()

                val expectedBrightness =
                    BrightnessUtils.convertGammaToLinearFloat(
                        gammaBrightness,
                        min.floatValue,
                        max.floatValue
                    )
                assertThat(brightness!!.floatValue).isWithin(1e-5f).of(expectedBrightness)
            }
        }

    @Test
    fun maxGammaBrightness() {
        assertThat(underTest.maxGammaBrightness)
            .isEqualTo(GammaBrightness(BrightnessUtils.GAMMA_SPACE_MAX))
    }

    @Test
    fun minGammaBrightness() {
        assertThat(underTest.minGammaBrightness)
            .isEqualTo(GammaBrightness(BrightnessUtils.GAMMA_SPACE_MIN))
    }
}
+167 −0

File added.

Preview size limit exceeded, changes collapsed.

Loading