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

Commit 114ea0ab authored by William Xiao's avatar William Xiao
Browse files

Fix hub lockscreen shortcut showing up before first unlock

isCommunalAvailable also takes into consideration settings state for
the glanceable hub ("Widgets on lock screen")

Fixes: 383183096
Test: atest GlanceableHubQuickAffordanceConfigTest
      also verified manually on device
Flag: com.android.systemui.glanceable_hub_v2
Change-Id: I56840bf9344010aaa1ceb96bb6d8c2df7557b88d
parent 4a1d29e8
Loading
Loading
Loading
Loading
+37 −37
Original line number Diff line number Diff line
@@ -24,21 +24,19 @@ import androidx.test.filters.SmallTest
import com.android.systemui.Flags
import com.android.systemui.SysuiTestCase
import com.android.systemui.communal.data.repository.communalSceneRepository
import com.android.systemui.communal.domain.interactor.communalInteractor
import com.android.systemui.communal.domain.interactor.communalSettingsInteractor
import com.android.systemui.communal.domain.interactor.setCommunalV2Available
import com.android.systemui.communal.domain.interactor.setCommunalV2Enabled
import com.android.systemui.communal.shared.model.CommunalScenes
import com.android.systemui.coroutines.collectLastValue
import com.android.systemui.flags.andSceneContainer
import com.android.systemui.kosmos.testScope
import com.android.systemui.flags.parameterizeSceneContainerFlag
import com.android.systemui.keyguard.data.repository.fakeKeyguardRepository
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.collectLastValue
import com.android.systemui.kosmos.runCurrent
import com.android.systemui.kosmos.runTest
import com.android.systemui.scene.data.repository.sceneContainerRepository
import com.android.systemui.scene.domain.interactor.sceneInteractor
import com.android.systemui.scene.shared.model.Scenes
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.Before
import org.junit.Test
import org.junit.runner.RunWith
@@ -47,14 +45,11 @@ import platform.test.runner.parameterized.ParameterizedAndroidJunit4
import platform.test.runner.parameterized.Parameters

@SmallTest
@OptIn(ExperimentalCoroutinesApi::class)
@EnableFlags(Flags.FLAG_GLANCEABLE_HUB_SHORTCUT_BUTTON, Flags.FLAG_GLANCEABLE_HUB_V2)
@EnableFlags(Flags.FLAG_GLANCEABLE_HUB_V2)
@RunWith(ParameterizedAndroidJunit4::class)
class GlanceableHubQuickAffordanceConfigTest(flags: FlagsParameterization?) : SysuiTestCase() {
    private val kosmos = testKosmos()
    private val testScope = kosmos.testScope

    private lateinit var underTest: GlanceableHubQuickAffordanceConfig
    private val Kosmos.underTest by Kosmos.Fixture { glanceableHubQuickAffordanceConfig }

    init {
        mSetFlagsRule.setFlagsParameterization(flags!!)
@@ -64,20 +59,16 @@ class GlanceableHubQuickAffordanceConfigTest(flags: FlagsParameterization?) : Sy
    fun setUp() {
        MockitoAnnotations.initMocks(this)

        underTest =
            GlanceableHubQuickAffordanceConfig(
                context = context,
                communalInteractor = kosmos.communalInteractor,
                communalSceneRepository = kosmos.communalSceneRepository,
                communalSettingsInteractor = kosmos.communalSettingsInteractor,
                sceneInteractor = kosmos.sceneInteractor,
            )
        // Access the class immediately so that flows are instantiated.
        // GlanceableHubQuickAffordanceConfig accesses StateFlow.value directly so we need the flows
        // to start flowing before runCurrent is called in the tests.
        kosmos.underTest
    }

    @Test
    fun lockscreenState_whenGlanceableHubEnabled_returnsVisible() =
        testScope.runTest {
            kosmos.setCommunalV2Enabled(true)
        kosmos.runTest {
            kosmos.setCommunalV2Available(true)
            runCurrent()

            val lockScreenState by collectLastValue(underTest.lockScreenState)
@@ -88,8 +79,21 @@ class GlanceableHubQuickAffordanceConfigTest(flags: FlagsParameterization?) : Sy

    @Test
    fun lockscreenState_whenGlanceableHubDisabled_returnsHidden() =
        testScope.runTest {
            kosmos.setCommunalV2Enabled(false)
        kosmos.runTest {
            setCommunalV2Enabled(false)
            val lockScreenState by collectLastValue(underTest.lockScreenState)
            runCurrent()

            assertThat(lockScreenState)
                .isEqualTo(KeyguardQuickAffordanceConfig.LockScreenState.Hidden)
        }

    @Test
    fun lockscreenState_whenGlanceableHubNotAvailable_returnsHidden() =
        kosmos.runTest {
            // Hub is enabled, but not available.
            setCommunalV2Enabled(true)
            fakeKeyguardRepository.setKeyguardShowing(false)
            val lockScreenState by collectLastValue(underTest.lockScreenState)
            runCurrent()

@@ -99,8 +103,8 @@ class GlanceableHubQuickAffordanceConfigTest(flags: FlagsParameterization?) : Sy

    @Test
    fun pickerScreenState_whenGlanceableHubEnabled_returnsDefault() =
        testScope.runTest {
            kosmos.setCommunalV2Enabled(true)
        kosmos.runTest {
            setCommunalV2Enabled(true)
            runCurrent()

            assertThat(underTest.getPickerScreenState())
@@ -109,8 +113,8 @@ class GlanceableHubQuickAffordanceConfigTest(flags: FlagsParameterization?) : Sy

    @Test
    fun pickerScreenState_whenGlanceableHubDisabled_returnsDisabled() =
        testScope.runTest {
            kosmos.setCommunalV2Enabled(false)
        kosmos.runTest {
            setCommunalV2Enabled(false)
            runCurrent()

            assertThat(
@@ -122,7 +126,7 @@ class GlanceableHubQuickAffordanceConfigTest(flags: FlagsParameterization?) : Sy
    @Test
    @DisableFlags(Flags.FLAG_SCENE_CONTAINER)
    fun onTriggered_changesSceneToCommunal() =
        testScope.runTest {
        kosmos.runTest {
            underTest.onTriggered(expandable = null)
            runCurrent()

@@ -133,7 +137,7 @@ class GlanceableHubQuickAffordanceConfigTest(flags: FlagsParameterization?) : Sy
    @Test
    @EnableFlags(Flags.FLAG_SCENE_CONTAINER)
    fun testTransitionToGlanceableHub_sceneContainer() =
        testScope.runTest {
        kosmos.runTest {
            underTest.onTriggered(expandable = null)
            runCurrent()

@@ -145,11 +149,7 @@ class GlanceableHubQuickAffordanceConfigTest(flags: FlagsParameterization?) : Sy
        @JvmStatic
        @Parameters(name = "{0}")
        fun getParams(): List<FlagsParameterization> {
            return FlagsParameterization.allCombinationsOf(
                    Flags.FLAG_GLANCEABLE_HUB_SHORTCUT_BUTTON,
                    Flags.FLAG_GLANCEABLE_HUB_V2,
                )
                .andSceneContainer()
            return parameterizeSceneContainerFlag()
        }
    }
}
+6 −7
Original line number Diff line number Diff line
@@ -36,7 +36,7 @@ import com.android.systemui.scene.shared.flag.SceneContainerFlag
import com.android.systemui.scene.shared.model.Scenes
import javax.inject.Inject
import kotlinx.coroutines.flow.Flow
import kotlinx.coroutines.flow.flow
import kotlinx.coroutines.flow.map

/** Lockscreen affordance that opens the glanceable hub. */
@SysUISingleton
@@ -60,13 +60,13 @@ constructor(
        get() = R.drawable.ic_widgets

    override val lockScreenState: Flow<KeyguardQuickAffordanceConfig.LockScreenState>
        get() = flow {
            emit(
        get() =
            communalInteractor.isCommunalAvailable.map { available ->
                if (!communalSettingsInteractor.isV2FlagEnabled()) {
                    Log.i(TAG, "Button hidden on lockscreen: flag not enabled.")
                    KeyguardQuickAffordanceConfig.LockScreenState.Hidden
                } else if (!communalInteractor.isCommunalEnabled.value) {
                    Log.i(TAG, "Button hidden on lockscreen: hub not enabled in settings.")
                } else if (!available) {
                    Log.i(TAG, "Button hidden on lockscreen: hub not available.")
                    KeyguardQuickAffordanceConfig.LockScreenState.Hidden
                } else {
                    KeyguardQuickAffordanceConfig.LockScreenState.Visible(
@@ -77,7 +77,6 @@ constructor(
                            )
                    )
                }
            )
            }

    override suspend fun getPickerScreenState(): KeyguardQuickAffordanceConfig.PickerScreenState {
+2 −6
Original line number Diff line number Diff line
@@ -99,10 +99,6 @@ suspend fun Kosmos.setCommunalAvailable(available: Boolean) {
}

suspend fun Kosmos.setCommunalV2Available(available: Boolean) {
    setCommunalV2ConfigEnabled(true)
    setCommunalEnabled(available)
    with(fakeKeyguardRepository) {
        setIsEncryptedOrLockdown(!available)
        setKeyguardShowing(available)
    }
    setCommunalV2ConfigEnabled(available)
    setCommunalAvailable(available)
}
+35 −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.keyguard.data.quickaffordance

import android.content.applicationContext
import com.android.systemui.communal.data.repository.communalSceneRepository
import com.android.systemui.communal.domain.interactor.communalInteractor
import com.android.systemui.communal.domain.interactor.communalSettingsInteractor
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.scene.domain.interactor.sceneInteractor

val Kosmos.glanceableHubQuickAffordanceConfig by
    Kosmos.Fixture {
        GlanceableHubQuickAffordanceConfig(
            context = applicationContext,
            communalInteractor = communalInteractor,
            communalSceneRepository = communalSceneRepository,
            communalSettingsInteractor = communalSettingsInteractor,
            sceneInteractor = sceneInteractor,
        )
    }