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

Commit 93fd7e40 authored by Behnam Heydarshahi's avatar Behnam Heydarshahi Committed by Android (Google) Code Review
Browse files

Merge changes Ia3a15efb,If2e96ca6,If699b5fd into main

* changes:
  Add Flashlight Slider UiEvent
  Add level to flashlight tile
  Flashlight Strength Slider & Dialog UI
parents d9a2774c b8eac375
Loading
Loading
Loading
Loading
+81 −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.flashlight.ui.dialog

import android.platform.test.annotations.EnableFlags
import android.testing.TestableLooper
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.animation.Expandable
import com.android.systemui.animation.mockDialogTransitionAnimator
import com.android.systemui.kosmos.runTest
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers.anyBoolean
import org.mockito.kotlin.any
import org.mockito.kotlin.mock
import org.mockito.kotlin.never
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever

@SmallTest
@EnableFlags(com.android.systemui.Flags.FLAG_FLASHLIGHT_STRENGTH)
@RunWith(AndroidJUnit4::class)
@TestableLooper.RunWithLooper(setAsMainLooper = true)
class FlashlightDialogDelegateTest : SysuiTestCase() {

    val kosmos = testKosmos()

    @Test
    fun createAndShowDialog_whenNoExpandable_dialogIsShowing() =
        kosmos.runTest {
            val underTest = kosmos.flashlightDialogDelegate

            val dialog = underTest.createDialog()

            assertThat(dialog.isShowing).isFalse()

            underTest.showDialog()

            assertThat(dialog.isShowing).isTrue()
        }

    @Test
    fun showDialog_withExpandable_animates() =
        kosmos.runTest {
            val underTest = flashlightDialogDelegateWithMockAnimator
            val expandable = mock<Expandable> {}
            whenever(expandable.dialogTransitionController(any())).thenReturn(mock())

            underTest.showDialog(expandable)

            verify(mockDialogTransitionAnimator).show(any(), any(), anyBoolean())
        }

    @Test
    fun showDialog_withoutExpandable_doesNotAnimate() =
        kosmos.runTest {
            val underTest = flashlightDialogDelegateWithMockAnimator

            underTest.showDialog()

            verify(mockDialogTransitionAnimator, never()).show(any(), any(), anyBoolean())
        }
}
+204 −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.flashlight.ui.viewmodel

import android.hardware.camera2.CameraManager.TorchCallback
import android.platform.test.annotations.EnableFlags
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.internal.logging.uiEventLoggerFake
import com.android.systemui.SysuiTestCase
import com.android.systemui.camera.cameraManager
import com.android.systemui.flashlight.data.repository.startFlashlightRepository
import com.android.systemui.flashlight.domain.interactor.flashlightInteractor
import com.android.systemui.flashlight.shared.logger.FlashlightUiEvent
import com.android.systemui.flashlight.shared.model.FlashlightModel
import com.android.systemui.kosmos.runCurrent
import com.android.systemui.kosmos.runTest
import com.android.systemui.kosmos.testScope
import com.android.systemui.lifecycle.activateIn
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentCaptor
import org.mockito.ArgumentMatchers.any
import org.mockito.kotlin.verify

@SmallTest
@EnableFlags(com.android.systemui.Flags.FLAG_FLASHLIGHT_STRENGTH)
@RunWith(AndroidJUnit4::class)
class FlashlightSliderViewModelTest : SysuiTestCase() {

    val kosmos = testKosmos()
    val underTest = kosmos.flashlightSlicerViewModelFactory.create()

    @Before
    fun setUp() {
        kosmos.startFlashlightRepository(true)
        underTest.activateIn(kosmos.testScope)
    }

    @Test
    fun doNothing_initiallyNullAndThenLoadsInitialState() =
        kosmos.runTest {
            assertThat(underTest.currentFlashlightLevel).isNull()
            runCurrent()
            assertThat(underTest.currentFlashlightLevel)
                .isEqualTo(FlashlightModel.Available.Level(false, DEFAULT_LEVEL, MAX_LEVEL))
        }

    @Test
    fun setLevel_updatesStateAndLogsUiEvents() =
        kosmos.runTest {
            runCurrent()
            assertThat(underTest.currentFlashlightLevel!!.level).isEqualTo(DEFAULT_LEVEL)

            underTest.setFlashlightLevel(1)
            runCurrent()

            assertThat(underTest.currentFlashlightLevel).isNotNull()
            assertThat(underTest.currentFlashlightLevel!!.level).isEqualTo(1)

            assertThat(uiEventLoggerFake.logs.size).isEqualTo(1)
            assertThat(uiEventLoggerFake.eventId(0))
                .isEqualTo(FlashlightUiEvent.FLASHLIGHT_SLIDER_SET_LEVEL.id)
            assertThat(uiEventLoggerFake.logs[0].position).isEqualTo(1) // value 1 logged

            underTest.setFlashlightLevel(2)
            runCurrent()

            assertThat(underTest.currentFlashlightLevel!!.level).isEqualTo(2)

            assertThat(uiEventLoggerFake.logs.size).isEqualTo(2)
            assertThat(uiEventLoggerFake.eventId(1))
                .isEqualTo(FlashlightUiEvent.FLASHLIGHT_SLIDER_SET_LEVEL.id)
            assertThat(uiEventLoggerFake.logs[1].position).isEqualTo(2)
        }

    @Test
    fun setLevel0_stateDisablesAtDefaultLevel() =
        kosmos.runTest {
            underTest.setFlashlightLevel(0)
            runCurrent()

            val actualLevel = underTest.currentFlashlightLevel!!.level
            val enabled = underTest.currentFlashlightLevel!!.enabled

            assertThat(actualLevel).isEqualTo(DEFAULT_LEVEL)
            assertThat(enabled).isEqualTo(false)
        }

    @Test(expected = IllegalArgumentException::class)
    fun setLevelBelowZero_stateUnchanged() =
        kosmos.runTest {
            runCurrent()

            val originalState = underTest.currentFlashlightLevel!!

            underTest.setFlashlightLevel(-1)
            runCurrent()

            assertThat(underTest.currentFlashlightLevel).isEqualTo(originalState)
        }

    @Test
    fun setLevelMax_stateMax() =
        kosmos.runTest {
            runCurrent()

            underTest.setFlashlightLevel(MAX_LEVEL)
            runCurrent()

            assertThat(underTest.currentFlashlightLevel!!.level).isEqualTo(MAX_LEVEL)
        }

    @Test
    fun setLevel_whenCameraInUse_levelRemainsUnchanged() =
        kosmos.runTest {
            val torchCallbackCaptor = ArgumentCaptor.forClass(TorchCallback::class.java)
            runCurrent()
            verify(cameraManager).registerTorchCallback(any(), torchCallbackCaptor.capture())

            underTest.setFlashlightLevel(1)
            runCurrent()
            assertThat(underTest.currentFlashlightLevel!!.level).isEqualTo(1)

            torchCallbackCaptor.value.onTorchModeUnavailable(DEFAULT_ID)
            runCurrent()

            underTest.setFlashlightLevel(2)
            runCurrent()
            assertThat(underTest.currentFlashlightLevel!!.level).isEqualTo(1)
        }

    @Test(expected = IllegalArgumentException::class)
    fun setLevelAboveMax_stateUnchanged() =
        kosmos.runTest {
            runCurrent()
            val originalState = underTest.currentFlashlightLevel!!

            underTest.setFlashlightLevel(MAX_LEVEL + 1)
            runCurrent()

            assertThat(underTest.currentFlashlightLevel).isEqualTo(originalState)
        }

    @Test
    fun updateInteractor_updatesState() =
        kosmos.runTest {
            flashlightInteractor.setEnabled(true)
            runCurrent()

            assertThat(underTest.currentFlashlightLevel!!.enabled).isEqualTo(true)
            assertThat(underTest.currentFlashlightLevel!!.level).isEqualTo(DEFAULT_LEVEL)
            assertThat(underTest.currentFlashlightLevel!!.max).isEqualTo(MAX_LEVEL)

            flashlightInteractor.setLevel(1)
            runCurrent()

            assertThat(underTest.currentFlashlightLevel!!.enabled).isEqualTo(true)
            assertThat(underTest.currentFlashlightLevel!!.level).isEqualTo(1)

            flashlightInteractor.setLevel(2)
            runCurrent()

            assertThat(underTest.currentFlashlightLevel!!.enabled).isEqualTo(true)
            assertThat(underTest.currentFlashlightLevel!!.level).isEqualTo(2)

            // instead it can disable the flashlight
            flashlightInteractor.setEnabled(false)
            runCurrent()

            assertThat(underTest.currentFlashlightLevel!!.enabled).isEqualTo(false)
            assertThat(underTest.currentFlashlightLevel!!.level).isEqualTo(DEFAULT_LEVEL)

            // can set level at max
            flashlightInteractor.setLevel(MAX_LEVEL)
            runCurrent()

            assertThat(underTest.currentFlashlightLevel!!.enabled).isEqualTo(true)
            assertThat(underTest.currentFlashlightLevel!!.level).isEqualTo(MAX_LEVEL)
        }

    private companion object {
        const val MAX_LEVEL = 45
        const val DEFAULT_LEVEL = 21
        const val DEFAULT_ID = "ID"
    }
}
+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

File changed.

Preview size limit exceeded, changes collapsed.

Loading