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

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

Merge "Revert^2 "Migrate FlashlightTile into new architecture"" into main

parents 4ae4c388 e82f24f3
Loading
Loading
Loading
Loading
+97 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.impl.flashlight.domain

import android.graphics.drawable.Drawable
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.common.shared.model.Icon
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.qs.tiles.impl.flashlight.domain.model.FlashlightTileModel
import com.android.systemui.qs.tiles.impl.flashlight.qsFlashlightTileConfig
import com.android.systemui.qs.tiles.viewmodel.QSTileState
import com.android.systemui.res.R
import com.android.systemui.util.mockito.mock
import com.google.common.truth.Truth.assertThat
import junit.framework.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

@SmallTest
@RunWith(AndroidJUnit4::class)
class FlashlightMapperTest : SysuiTestCase() {
    private val kosmos = Kosmos()
    private val qsTileConfig = kosmos.qsFlashlightTileConfig
    private val mapper by lazy { FlashlightMapper(context) }

    @Test
    fun mapsDisabledDataToInactiveState() {
        val tileState: QSTileState = mapper.map(qsTileConfig, FlashlightTileModel(false))

        val actualActivationState = tileState.activationState

        assertEquals(QSTileState.ActivationState.INACTIVE, actualActivationState)
    }

    @Test
    fun mapsEnabledDataToActiveState() {
        val tileState: QSTileState = mapper.map(qsTileConfig, FlashlightTileModel(true))

        val actualActivationState = tileState.activationState
        assertEquals(QSTileState.ActivationState.ACTIVE, actualActivationState)
    }

    @Test
    fun mapsEnabledDataToOnIconState() {
        val fakeDrawable = mock<Drawable>()
        context.orCreateTestableResources.addOverride(
            R.drawable.qs_flashlight_icon_on,
            fakeDrawable
        )
        val expectedIcon = Icon.Loaded(fakeDrawable, null)

        val tileState: QSTileState = mapper.map(qsTileConfig, FlashlightTileModel(true))

        val actualIcon = tileState.icon()
        assertThat(actualIcon).isEqualTo(expectedIcon)
    }

    @Test
    fun mapsDisabledDataToOffIconState() {
        val fakeDrawable = mock<Drawable>()
        context.orCreateTestableResources.addOverride(
            R.drawable.qs_flashlight_icon_off,
            fakeDrawable
        )
        val expectedIcon = Icon.Loaded(fakeDrawable, null)

        val tileState: QSTileState = mapper.map(qsTileConfig, FlashlightTileModel(false))

        val actualIcon = tileState.icon()
        assertThat(actualIcon).isEqualTo(expectedIcon)
    }

    @Test
    fun supportsOnlyClickAction() {
        val dontCare = true
        val tileState: QSTileState = mapper.map(qsTileConfig, FlashlightTileModel(dontCare))

        val supportedActions = tileState.supportedActions
        assertThat(supportedActions).containsExactly(QSTileState.UserAction.CLICK)
    }
}
+88 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.
 */

import android.os.UserHandle
import android.testing.LeakCheck
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.qs.tiles.base.interactor.DataUpdateTrigger
import com.android.systemui.qs.tiles.impl.flashlight.domain.interactor.FlashlightTileDataInteractor
import com.android.systemui.qs.tiles.impl.flashlight.domain.model.FlashlightTileModel
import com.android.systemui.utils.leaks.FakeFlashlightController
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
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

@OptIn(ExperimentalCoroutinesApi::class)
@SmallTest
@RunWith(AndroidJUnit4::class)
class FlashlightTileDataInteractorTest : SysuiTestCase() {
    private lateinit var controller: FakeFlashlightController
    private lateinit var underTest: FlashlightTileDataInteractor

    @Before
    fun setup() {
        controller = FakeFlashlightController(LeakCheck())
        underTest = FlashlightTileDataInteractor(controller)
    }

    @Test
    fun availabilityOnMatchesController() = runTest {
        controller.hasFlashlight = true

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

        assertThat(availability).isTrue()
    }
    @Test
    fun availabilityOffMatchesController() = runTest {
        controller.hasFlashlight = false

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

        assertThat(availability).isFalse()
    }

    @Test
    fun dataMatchesController() = runTest {
        controller.setFlashlight(false)
        val flowValues: List<FlashlightTileModel> by
            collectValues(underTest.tileData(TEST_USER, flowOf(DataUpdateTrigger.InitialRequest)))

        runCurrent()
        controller.setFlashlight(true)
        runCurrent()
        controller.setFlashlight(false)
        runCurrent()

        assertThat(flowValues.size).isEqualTo(3)
        assertThat(flowValues.map { it.isEnabled }).containsExactly(false, true, false).inOrder()
    }

    private companion object {
        val TEST_USER = UserHandle.of(1)!!
    }
}
+67 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.
 */

import android.app.ActivityManager
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.test.filters.SmallTest
import com.android.systemui.SysuiTestCase
import com.android.systemui.qs.tiles.base.interactor.QSTileInputTestKtx.click
import com.android.systemui.qs.tiles.impl.flashlight.domain.interactor.FlashlightTileUserActionInteractor
import com.android.systemui.qs.tiles.impl.flashlight.domain.model.FlashlightTileModel
import com.android.systemui.statusbar.policy.FlashlightController
import com.android.systemui.util.mockito.mock
import kotlinx.coroutines.test.runTest
import org.junit.Assume.assumeFalse
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mock
import org.mockito.Mockito.verify

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

    @Mock private lateinit var controller: FlashlightController

    private lateinit var underTest: FlashlightTileUserActionInteractor

    @Before
    fun setup() {
        controller = mock<FlashlightController>()
        underTest = FlashlightTileUserActionInteractor(controller)
    }

    @Test
    fun handleClickToEnable() = runTest {
        assumeFalse(ActivityManager.isUserAMonkey())
        val stateBeforeClick = false

        underTest.handleInput(click(FlashlightTileModel(stateBeforeClick)))

        verify(controller).setFlashlight(!stateBeforeClick)
    }

    @Test
    fun handleClickToDisable() = runTest {
        assumeFalse(ActivityManager.isUserAMonkey())
        val stateBeforeClick = true

        underTest.handleInput(click(FlashlightTileModel(stateBeforeClick)))

        verify(controller).setFlashlight(!stateBeforeClick)
    }
}
+60 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.impl.flashlight.domain

import android.content.Context
import com.android.systemui.common.shared.model.Icon
import com.android.systemui.qs.tiles.base.interactor.QSTileDataToStateMapper
import com.android.systemui.qs.tiles.impl.flashlight.domain.model.FlashlightTileModel
import com.android.systemui.qs.tiles.viewmodel.QSTileConfig
import com.android.systemui.qs.tiles.viewmodel.QSTileState
import com.android.systemui.res.R
import javax.inject.Inject

/** Maps [FlashlightTileModel] to [QSTileState]. */
class FlashlightMapper @Inject constructor(private val context: Context) :
    QSTileDataToStateMapper<FlashlightTileModel> {

    override fun map(config: QSTileConfig, data: FlashlightTileModel): QSTileState =
        QSTileState.build(context, config.uiConfig) {
            val icon =
                Icon.Loaded(
                    context.resources.getDrawable(
                        if (data.isEnabled) {
                            R.drawable.qs_flashlight_icon_on
                        } else {
                            R.drawable.qs_flashlight_icon_off
                        }
                    ),
                    contentDescription = null
                )
            this.icon = { icon }

            if (data.isEnabled) {
                activationState = QSTileState.ActivationState.ACTIVE
                secondaryLabel = context.resources.getStringArray(R.array.tile_states_flashlight)[2]
            } else {
                activationState = QSTileState.ActivationState.INACTIVE
                secondaryLabel = context.resources.getStringArray(R.array.tile_states_flashlight)[1]
            }
            contentDescription = label
            supportedActions =
                setOf(
                    QSTileState.UserAction.CLICK,
                )
        }
}
+62 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2023 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.impl.flashlight.domain.interactor

import android.os.UserHandle
import com.android.systemui.common.coroutine.ConflatedCallbackFlow.conflatedCallbackFlow
import com.android.systemui.qs.tiles.base.interactor.DataUpdateTrigger
import com.android.systemui.qs.tiles.base.interactor.QSTileDataInteractor
import com.android.systemui.qs.tiles.impl.flashlight.domain.model.FlashlightTileModel
import com.android.systemui.statusbar.policy.FlashlightController
import javax.inject.Inject
import kotlinx.coroutines.channels.awaitClose
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flowOf

/** Observes flashlight state changes providing the [FlashlightTileModel]. */
class FlashlightTileDataInteractor
@Inject
constructor(
    private val flashlightController: FlashlightController,
) : QSTileDataInteractor<FlashlightTileModel> {

    override fun tileData(
        user: UserHandle,
        triggers: Flow<DataUpdateTrigger>
    ): Flow<FlashlightTileModel> = conflatedCallbackFlow {
        val initialValue = flashlightController.isEnabled
        trySend(FlashlightTileModel(initialValue))

        val callback =
            object : FlashlightController.FlashlightListener {
                override fun onFlashlightChanged(enabled: Boolean) {
                    trySend(FlashlightTileModel(enabled))
                }
                override fun onFlashlightError() {
                    trySend(FlashlightTileModel(false))
                }
                override fun onFlashlightAvailabilityChanged(available: Boolean) {
                    trySend(FlashlightTileModel(flashlightController.isEnabled))
                }
            }
        flashlightController.addCallback(callback)
        awaitClose { flashlightController.removeCallback(callback) }
    }

    override fun availability(user: UserHandle): Flow<Boolean> =
        flowOf(flashlightController.hasFlashlight())
}
Loading