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

Commit e48b88bc authored by Behnam Heydarshahi's avatar Behnam Heydarshahi
Browse files

Add level to flashlight tile

Flag: com.android.systemui.flashlight_strength
Fixes: 399470157
Fixes: 399465570
Fixes: 399492164
Fixes: 399468001
Test: atest FlashlightTileWithLevelTest
Test: atest FlashlightTileMapperTest FlashlightTileDataInteractorTest FlashlightTileUserActionInteractorTest
Change-Id: If2e96ca6ed1d841210f307fbe1b8f646b0292816
parent 1cea9b2d
Loading
Loading
Loading
Loading
+2 −0
Original line number Diff line number Diff line
package com.android.systemui.qs.tiles

import android.os.Handler
import android.platform.test.annotations.DisableFlags
import android.platform.test.flag.junit.FlagsParameterization
import android.platform.test.flag.junit.FlagsParameterization.allCombinationsOf
import android.testing.TestableLooper
@@ -33,6 +34,7 @@ import platform.test.runner.parameterized.Parameters

@RunWith(ParameterizedAndroidJunit4::class)
@TestableLooper.RunWithLooper(setAsMainLooper = true)
@DisableFlags(com.android.systemui.Flags.FLAG_FLASHLIGHT_STRENGTH)
@SmallTest
class FlashlightTileTest(flags: FlagsParameterization) : SysuiTestCase() {

+319 −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.qs.tiles

import android.os.Handler
import android.platform.test.annotations.EnableFlags
import android.platform.test.flag.junit.FlagsParameterization
import android.platform.test.flag.junit.FlagsParameterization.allCombinationsOf
import android.service.quicksettings.Tile
import android.testing.TestableLooper
import androidx.test.filters.SmallTest
import com.android.internal.logging.MetricsLogger
import com.android.internal.logging.testing.UiEventLoggerFake
import com.android.systemui.InstanceIdSequenceFake
import com.android.systemui.SysuiTestCase
import com.android.systemui.classifier.FalsingManagerFake
import com.android.systemui.flashlight.data.repository.startFlashlightRepository
import com.android.systemui.flashlight.domain.interactor.flashlightInteractor
import com.android.systemui.flashlight.shared.model.FlashlightModel
import com.android.systemui.kosmos.runCurrent
import com.android.systemui.kosmos.runTest
import com.android.systemui.plugins.ActivityStarter
import com.android.systemui.plugins.qs.QSTile
import com.android.systemui.plugins.statusbar.StatusBarStateController
import com.android.systemui.qs.QSHost
import com.android.systemui.qs.QsEventLogger
import com.android.systemui.qs.QsEventLoggerFake
import com.android.systemui.qs.flags.QSComposeFragment
import com.android.systemui.qs.logging.QSLogger
import com.android.systemui.qs.pipeline.shared.TileSpec
import com.android.systemui.qs.tileimpl.QSTileImpl.DrawableIconWithRes
import com.android.systemui.qs.tiles.base.domain.model.QSTileInput
import com.android.systemui.qs.tiles.base.shared.model.FakeQSTileConfigProvider
import com.android.systemui.qs.tiles.base.shared.model.QSTileConfigProvider
import com.android.systemui.qs.tiles.base.shared.model.QSTileUserAction
import com.android.systemui.qs.tiles.impl.flashlight.domain.interactor.FlashlightTileUserActionInteractor
import com.android.systemui.qs.tiles.impl.flashlight.domain.interactor.flashlightTileDataInteractor
import com.android.systemui.qs.tiles.impl.flashlight.ui.mapper.flashlightTileMapper
import com.android.systemui.res.R
import com.android.systemui.statusbar.policy.PolicyModule
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentCaptor
import org.mockito.Captor
import org.mockito.Mock
import org.mockito.MockitoAnnotations
import org.mockito.kotlin.capture
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
import platform.test.runner.parameterized.ParameterizedAndroidJunit4
import platform.test.runner.parameterized.Parameters

@RunWith(ParameterizedAndroidJunit4::class)
@TestableLooper.RunWithLooper(setAsMainLooper = true)
@EnableFlags(com.android.systemui.Flags.FLAG_FLASHLIGHT_STRENGTH)
@SmallTest
class FlashlightTileWithLevelTest(flags: FlagsParameterization) : SysuiTestCase() {
    private val kosmos = testKosmos()

    @Captor private lateinit var inputCaptor: ArgumentCaptor<QSTileInput<FlashlightModel>>

    @Mock private lateinit var qsLogger: QSLogger

    @Mock private lateinit var qsHost: QSHost

    @Mock private lateinit var metricsLogger: MetricsLogger

    @Mock private lateinit var statusBarStateController: StatusBarStateController

    @Mock private lateinit var activityStarter: ActivityStarter

    @Mock private lateinit var uiEventLogger: QsEventLogger

    @Mock private lateinit var mockUserActionInteractor: FlashlightTileUserActionInteractor

    private val falsingManager = FalsingManagerFake()
    private lateinit var testableLooper: TestableLooper
    private lateinit var underTest: FlashlightTileWithLevel

    init {
        mSetFlagsRule.setFlagsParameterization(flags)
    }

    @Before
    fun setUp() {
        MockitoAnnotations.openMocks(this)
        testableLooper = TestableLooper.get(this)

        whenever(qsHost.context).thenReturn(mContext)

        underTest =
            FlashlightTileWithLevel(
                qsHost,
                uiEventLogger,
                testableLooper.looper,
                Handler(testableLooper.looper),
                falsingManager,
                metricsLogger,
                statusBarStateController,
                activityStarter,
                qsLogger,
                createAndPopulateQsTileConfigProvider(),
                kosmos.flashlightTileDataInteractor,
                mockUserActionInteractor,
                kosmos.flashlightTileMapper,
            )

        underTest.initialize()
        underTest.setListening(Object(), true)

        testableLooper.processAllMessages()
    }

    @After
    fun tearDown() {
        underTest.destroy()
        testableLooper.processAllMessages()
    }

    @Test
    fun testIcon_whenFlashlightEnabled_isOnState() =
        kosmos.runTest {
            startFlashlightRepository(true)

            flashlightInteractor.setEnabled(true)
            runCurrent()

            val state = QSTile.BooleanState()

            underTest.handleUpdateState(state, /* arg= */ null)

            val resId = R.drawable.qs_flashlight_icon_on
            assertThat(state.icon)
                .isEqualTo(DrawableIconWithRes(mContext.getDrawable(resId), resId))
        }

    @Test
    fun testIcon_whenFlashlightDisabled_isOffState() =
        kosmos.runTest {
            startFlashlightRepository(true)

            flashlightInteractor.setEnabled(false)
            runCurrent()

            val state = QSTile.BooleanState()

            underTest.handleUpdateState(state, /* arg= */ null)

            val resId = R.drawable.qs_flashlight_icon_off
            assertThat(state.icon)
                .isEqualTo(DrawableIconWithRes(mContext.getDrawable(resId), resId))
        }

    @Test
    fun testIcon_whenFlashlightUnavailablePermanently_isOffState() =
        kosmos.runTest {
            startFlashlightRepository(false)
            runCurrent()
            val state = QSTile.BooleanState()

            underTest.handleUpdateState(state, /* arg= */ null)
            runCurrent()

            val resId = R.drawable.qs_flashlight_icon_off
            assertThat(state.icon)
                .isEqualTo(DrawableIconWithRes(mContext.getDrawable(resId), resId))
        }

    @Test
    fun stateUpdatesOnChange() =
        kosmos.runTest {
            startFlashlightRepository(true)

            runCurrent()
            testableLooper.processAllMessages()
            assertThat(underTest.state.state).isEqualTo(Tile.STATE_INACTIVE)

            flashlightInteractor.setEnabled(true)
            runCurrent()
            testableLooper.processAllMessages()

            assertThat(underTest.state.state).isEqualTo(Tile.STATE_ACTIVE)
        }

    @Test
    fun handleUpdateState_withNull_updatesState() =
        kosmos.runTest {
            startFlashlightRepository(true)

            val tileState =
                QSTile.BooleanState().apply {
                    state = Tile.STATE_INACTIVE
                    secondaryLabel = "Old secondary label to be overwritten"
                }
            flashlightInteractor.setLevel(MAX_LEVEL)
            runCurrent()

            underTest.handleUpdateState(tileState, null)

            runCurrent()

            assertThat(tileState.state).isEqualTo(Tile.STATE_ACTIVE)
            assertThat(tileState.secondaryLabel).isEqualTo("100%")
        }

    @Test
    fun click_delegatesToUserActionInteractorClick() =
        kosmos.runTest {
            runCurrent()
            testableLooper.processAllMessages()

            underTest.click(null)
            runCurrent()
            testableLooper.processAllMessages()

            verify(mockUserActionInteractor).handleInput(capture(inputCaptor))

            val action = inputCaptor.value.action

            assertThat(action).isInstanceOf(QSTileUserAction.Click::class.java)
        }

    @Test
    fun secondaryClick_delegatesToUserActionInteractorToggleClick() =
        kosmos.runTest {
            runCurrent()
            testableLooper.processAllMessages()

            underTest.secondaryClick(null)
            runCurrent()
            testableLooper.processAllMessages()

            verify(mockUserActionInteractor).handleInput(capture(inputCaptor))

            val action = inputCaptor.value.action

            assertThat(action).isInstanceOf(QSTileUserAction.ToggleClick::class.java)
        }

    @Test
    fun longClick_delegatesToUserActionInteractorLongClick() =
        kosmos.runTest {
            runCurrent()
            testableLooper.processAllMessages()

            underTest.longClick(null)
            runCurrent()
            testableLooper.processAllMessages()

            verify(mockUserActionInteractor).handleInput(capture(inputCaptor))

            val action = inputCaptor.value.action

            assertThat(action).isInstanceOf(QSTileUserAction.LongClick::class.java)
        }

    @Test
    fun isAvailable_matchesDataInteractor() =
        kosmos.runTest {
            startFlashlightRepository(true)

            runCurrent()
            testableLooper.processAllMessages()

            assertThat(underTest.isAvailable).isTrue()
            assertThat(flashlightTileDataInteractor.isAvailable()).isTrue()
        }

    @Test
    fun isNotAvailable_matchesDataInteractor() =
        kosmos.runTest {
            startFlashlightRepository(false)

            runCurrent()
            testableLooper.processAllMessages()

            assertThat(underTest.isAvailable).isFalse()
            assertThat(flashlightTileDataInteractor.isAvailable()).isFalse()
        }

    companion object {
        @JvmStatic
        @Parameters(name = "{0}")
        fun getParams(): List<FlagsParameterization> {
            return allCombinationsOf(QSComposeFragment.FLAG_NAME)
        }

        private const val MAX_LEVEL = 45

        private val FLASHLIGHT_TILE_SPEC = TileSpec.create(PolicyModule.FLASHLIGHT_TILE_SPEC)

        private fun createAndPopulateQsTileConfigProvider(): QSTileConfigProvider {
            val logger =
                QsEventLoggerFake(UiEventLoggerFake(), InstanceIdSequenceFake(Int.MAX_VALUE))

            return FakeQSTileConfigProvider().apply {
                putConfig(FLASHLIGHT_TILE_SPEC, PolicyModule.provideFlashlightTileConfig(logger))
            }
        }
    }
}
+146 −40
Original line number Diff line number Diff line
@@ -16,41 +16,62 @@

package com.android.systemui.qs.tiles.impl.flashlight.domain.interactor

import android.content.packageManager
import android.content.pm.PackageManager
import android.os.UserHandle
import android.platform.test.annotations.DisableFlags
import android.platform.test.annotations.EnableFlags
import android.platform.test.annotations.EnabledOnRavenwood
import android.testing.LeakCheck
import android.util.Log
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.coroutines.collectValues
import com.android.systemui.flashlight.data.repository.startFlashlightRepository
import com.android.systemui.flashlight.domain.interactor.flashlightInteractor
import com.android.systemui.flashlight.shared.model.FlashlightModel
import com.android.systemui.kosmos.collectLastValue
import com.android.systemui.kosmos.collectValues
import com.android.systemui.kosmos.runCurrent
import com.android.systemui.kosmos.runTest
import com.android.systemui.qs.tiles.base.domain.model.DataUpdateTrigger
import com.android.systemui.qs.tiles.impl.flashlight.domain.model.FlashlightTileModel
import com.android.systemui.utils.leaks.FakeFlashlightController
import com.android.systemui.statusbar.policy.fakeFlashlightController
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.flow.flowOf
import kotlinx.coroutines.test.runCurrent
import kotlinx.coroutines.test.runTest
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.eq
import org.mockito.kotlin.times
import org.mockito.kotlin.verify

@SmallTest
@EnabledOnRavenwood
@RunWith(AndroidJUnit4::class)
class FlashlightTileDataInteractorTest : SysuiTestCase() {
    private lateinit var controller: FakeFlashlightController
    private lateinit var underTest: FlashlightTileDataInteractor
    private val kosmos = testKosmos()
    private val controller = kosmos.fakeFlashlightController
    private val underTest = kosmos.flashlightTileDataInteractor

    @Before
    fun setup() {
        controller = FakeFlashlightController(LeakCheck())
        underTest = FlashlightTileDataInteractor(controller)
    @DisableFlags(com.android.systemui.Flags.FLAG_FLASHLIGHT_STRENGTH)
    @Test
    fun withFlagOff_availabilityOnMatchesController() = runTest {
        controller.hasFlashlight = true

        runCurrent()
        val availability by collectLastValue(underTest.availability(TEST_USER))

        assertThat(availability).isTrue()
    }

    @EnableFlags(com.android.systemui.Flags.FLAG_FLASHLIGHT_STRENGTH)
    @Test
    fun availabilityOnMatchesController() = runTest {
        controller.hasFlashlight = true
    fun withFlagOn_availabilityOnMatchesInteractor() =
        kosmos.runTest {
            startFlashlightRepository(true)

            runCurrent()
            val availability by collectLastValue(underTest.availability(TEST_USER))
@@ -58,8 +79,21 @@ class FlashlightTileDataInteractorTest : SysuiTestCase() {
            assertThat(availability).isTrue()
        }

    @EnableFlags(com.android.systemui.Flags.FLAG_FLASHLIGHT_STRENGTH)
    @Test
    fun withFlagOn_availabilityOnMatchesInteractorDeviceSupportsFlashlight() =
        kosmos.runTest {
            startFlashlightRepository(true)

            runCurrent()
            val availability by collectLastValue(underTest.availability(TEST_USER))

            assertThat(availability).isEqualTo(flashlightInteractor.deviceSupportsFlashlight)
        }

    @DisableFlags(com.android.systemui.Flags.FLAG_FLASHLIGHT_STRENGTH)
    @Test
    fun availabilityOffMatchesController() = runTest {
    fun withFlagOff_availabilityOffMatchesController() = runTest {
        controller.hasFlashlight = false

        runCurrent()
@@ -68,9 +102,51 @@ class FlashlightTileDataInteractorTest : SysuiTestCase() {
        assertThat(availability).isFalse()
    }

    @EnableFlags(com.android.systemui.Flags.FLAG_FLASHLIGHT_STRENGTH)
    @Test
    fun withFlagOn_availabilityOffMatchesInteractor() =
        kosmos.runTest {
            startFlashlightRepository(false)

            runCurrent()
            verify(packageManager, times(1))
                .hasSystemFeature(eq(PackageManager.FEATURE_CAMERA_FLASH))

            val flowValues: List<FlashlightModel> by
                collectValues(
                    underTest.tileData(TEST_USER, flowOf(DataUpdateTrigger.InitialRequest))
                )

            flowValues.forEach {
                Log.d(
                    "FLASHLIGHT_DATA",
                    "withFlagOn_availabilityOffMatchesInteractorDeviceSupportsFlashlight: $it",
                )
            }

            val availability by collectLastValue(underTest.availability(TEST_USER))
            runCurrent()

            assertThat(availability).isNotNull()
            assertThat(availability).isFalse()
        }

    @EnableFlags(com.android.systemui.Flags.FLAG_FLASHLIGHT_STRENGTH)
    @Test
    fun withFlagOn_availabilityOffMatchesInteractorDeviceSupportsFlashlight() =
        kosmos.runTest {
            startFlashlightRepository(false)

            runCurrent()
            val availability by collectLastValue(underTest.availability(TEST_USER))

            assertThat(availability).isEqualTo(flashlightInteractor.deviceSupportsFlashlight)
        }

    @DisableFlags(com.android.systemui.Flags.FLAG_FLASHLIGHT_STRENGTH)
    @Test
    fun isEnabledDataMatchesControllerWhenAvailable() = runTest {
        val flowValues: List<FlashlightTileModel> by
    fun withFlagOff_isEnabledDataMatchesControllerWhenAvailable() = runTest {
        val flowValues: List<FlashlightModel> by
            collectValues(underTest.tileData(TEST_USER, flowOf(DataUpdateTrigger.InitialRequest)))

        runCurrent()
@@ -80,37 +156,67 @@ class FlashlightTileDataInteractorTest : SysuiTestCase() {
        runCurrent()

        assertThat(flowValues.size).isEqualTo(4) // 2 from setup(), 2 from this test
        assertThat(
                flowValues.filterIsInstance<FlashlightTileModel.FlashlightAvailable>().map {
                    it.isEnabled
        assertThat(flowValues.filterIsInstance<FlashlightModel.Available>().map { it.enabled })
            .containsExactly(false, false, true, false)
            .inOrder()
    }

    @EnableFlags(com.android.systemui.Flags.FLAG_FLASHLIGHT_STRENGTH)
    @Test
    fun withFlagOn_isEnabledDataMatchesInteractorWhenAvailable() =
        kosmos.runTest {
            val flowValues: List<FlashlightModel> by
                collectValues(
                    underTest.tileData(TEST_USER, flowOf(DataUpdateTrigger.InitialRequest))
                )
            .containsExactly(false, false, true, false)

            startFlashlightRepository(true)

            runCurrent()
            flashlightInteractor.setEnabled(true)
            runCurrent()
            flashlightInteractor.setEnabled(false)
            runCurrent()

            assertThat(flowValues.size).isEqualTo(4) // 2 from setup(), 2 from this test
            assertThat(flowValues.filterIsInstance<FlashlightModel.Available>().map { it.enabled })
                .containsExactly(false, true, false)
                .inOrder()
        }

    /**
     * Simulates the scenario of changes in flashlight tile availability when camera is initially
     * closed, then opened, and closed again.
     */
    @EnableFlags(com.android.systemui.Flags.FLAG_FLASHLIGHT_STRENGTH)
    @Test
    fun availabilityDataMatchesControllerAvailability() = runTest {
        val flowValues: List<FlashlightTileModel> by
            collectValues(underTest.tileData(TEST_USER, flowOf(DataUpdateTrigger.InitialRequest)))
    fun withFlagOn_dataMatchesInteractorLevel() =
        kosmos.runTest {
            val flowValues: List<FlashlightModel> by
                collectValues(
                    underTest.tileData(TEST_USER, flowOf(DataUpdateTrigger.InitialRequest))
                )
            startFlashlightRepository(true)

            runCurrent()
        controller.onFlashlightAvailabilityChanged(false)
            flashlightInteractor.setLevel(1)
            runCurrent()
        controller.onFlashlightAvailabilityChanged(true)
            flashlightInteractor.setLevel(2)
            runCurrent()

        assertThat(flowValues.size).isEqualTo(4) // 2 from setup + 2 from this test
        assertThat(flowValues.map { it is FlashlightTileModel.FlashlightAvailable })
            .containsExactly(true, true, false, true)
            flowValues.forEach {
                Log.d("FlashlightDataInteractorTest", "dataMatchesInteractorLevel: $it")
            }

            assertThat(flowValues.size).isEqualTo(4) // loading, 0, 1, 2
            assertThat(flowValues.map { it is FlashlightModel.Available.Level })
                .containsExactly(false, true, true, true)
                .inOrder()
            assertThat(
                    flowValues.filterIsInstance<FlashlightModel.Available.Level>().map { it.level }
                )
                .containsExactly(DEFAULT_LEVEL, 1, 2)
                .inOrder()
        }

    private companion object {
        val TEST_USER = UserHandle.of(1)!!
        const val DEFAULT_LEVEL = 21
    }
}
+232 −25

File changed.

Preview size limit exceeded, changes collapsed.

+0 −133

File deleted.

Preview size limit exceeded, changes collapsed.

Loading