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

Commit 4929e21c authored by Treehugger Robot's avatar Treehugger Robot Committed by Android (Google) Code Review
Browse files

Merge changes Id5ff5381,I347e54a7 into main

* changes:
  Fixed Peristent search in shortcut helper
  Shortcut helper should not show up on Locked Screen
parents 87e94275 bb45bda7
Loading
Loading
Loading
Loading
+82 −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.keyboard.shortcut

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.keyboard.shortcut.shared.model.ShortcutHelperState
import com.android.systemui.kosmos.testScope
import com.android.systemui.plugins.ActivityStarter
import com.android.systemui.plugins.activityStarter
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.runTest
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.kotlin.any
import org.mockito.kotlin.doNothing
import org.mockito.kotlin.eq
import org.mockito.kotlin.whenever

@SmallTest
@RunWith(AndroidJUnit4::class)
@OptIn(ExperimentalCoroutinesApi::class)
class ShortcutHelperCoreStartableTest : SysuiTestCase() {
    private val kosmos = testKosmos()
    private val repo = kosmos.shortcutHelperStateRepository
    private val helper = kosmos.shortcutHelperTestHelper
    private val testScope = kosmos.testScope
    private val activityStarter = kosmos.activityStarter

    @Test
    fun shortcutHelperState_whenToggled_doesNotBecomeActive_ifDeviceIsLocked() {
        testScope.runTest {
            assumedKeyguardIsNotDismissed()

            val state by collectLastValue(repo.state)
            helper.toggle(deviceId = 456)

            assertThat(state).isEqualTo(ShortcutHelperState.Inactive)
        }
    }

    @Test
    fun shortcutHelperState_whenToggled_becomesActive_ifDeviceIsUnlocked() {
        testScope.runTest {
            assumeKeyguardIsDismissed()

            val state by collectLastValue(repo.state)
            helper.toggle(deviceId = 456)

            assertThat(state).isEqualTo(ShortcutHelperState.Active(deviceId = 456))
        }
    }

    private fun assumeKeyguardIsDismissed(){
        whenever(activityStarter.dismissKeyguardThenExecute(any(), any(), eq(true))).then {
            (it.arguments[0] as ActivityStarter.OnDismissAction).onDismiss()
        }
    }

    private fun assumedKeyguardIsNotDismissed(){
        // Do nothing, simulating keyguard not being dismissed and action not being not executed
        doNothing().whenever(activityStarter).dismissKeyguardThenExecute(any(), any(), eq(true))
    }
}
+25 −5
Original line number Diff line number Diff line
@@ -55,15 +55,15 @@ import com.android.systemui.keyboard.shortcut.shortcutHelperViewModel
import com.android.systemui.keyboard.shortcut.ui.model.IconSource
import com.android.systemui.keyboard.shortcut.ui.model.ShortcutCategoryUi
import com.android.systemui.keyboard.shortcut.ui.model.ShortcutsUiState
import com.android.systemui.kosmos.Kosmos
import com.android.systemui.kosmos.testCase
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.kosmos.testScope
import com.android.systemui.kosmos.useUnconfinedTestDispatcher
import com.android.systemui.model.sysUiState
import com.android.systemui.settings.FakeUserTracker
import com.android.systemui.settings.fakeUserTracker
import com.android.systemui.settings.userTracker
import com.android.systemui.shared.system.QuickStepContract.SYSUI_STATE_SHORTCUT_HELPER_SHOWING
import com.android.systemui.testKosmos
import com.google.common.truth.Truth.assertThat
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.test.UnconfinedTestDispatcher
@@ -89,8 +89,7 @@ class ShortcutHelperViewModelTest : SysuiTestCase() {
    private val mockApplicationInfo: ApplicationInfo = mock()

    private val kosmos =
        Kosmos().also {
            it.testCase = this
        testKosmos().useUnconfinedTestDispatcher().also {
            it.testDispatcher = UnconfinedTestDispatcher()
            it.shortcutHelperSystemShortcutsSource = fakeSystemSource
            it.shortcutHelperMultiTaskingShortcutsSource = fakeMultiTaskingSource
@@ -108,7 +107,6 @@ class ShortcutHelperViewModelTest : SysuiTestCase() {
    private val inputManager = kosmos.fakeInputManager.inputManager
    private val viewModel = kosmos.shortcutHelperViewModel


    @Before
    fun setUp() {
        fakeSystemSource.setGroups(TestShortcuts.systemGroups)
@@ -433,6 +431,28 @@ class ShortcutHelperViewModelTest : SysuiTestCase() {
            assertThat(activeUiState.shouldShowResetButton).isTrue()
        }

    @Test
    fun shortcutsUiState_searchQuery_isResetAfterHelperIsClosedAndReOpened() =
        testScope.runTest{
            val uiState by collectLastValue(viewModel.shortcutsUiState)

            openHelperAndSearchForFooString()
            assertThat((uiState as? ShortcutsUiState.Active)?.searchQuery).isEqualTo("foo")

            closeAndReopenShortcutHelper()
            assertThat((uiState as? ShortcutsUiState.Active)?.searchQuery).isEqualTo("")
        }

    private fun openHelperAndSearchForFooString(){
        testHelper.showFromActivity()
        viewModel.onSearchQueryChanged("foo")
    }

    private fun closeAndReopenShortcutHelper() {
        viewModel.onViewClosed()
        testHelper.showFromActivity()
    }

    private fun groupWithShortcutLabels(
        vararg shortcutLabels: String,
        groupLabel: String = FIRST_SIMPLE_GROUP_LABEL,
+34 −15
Original line number Diff line number Diff line
@@ -26,34 +26,34 @@ import com.android.systemui.broadcast.BroadcastDispatcher
import com.android.systemui.dagger.SysUISingleton
import com.android.systemui.dagger.qualifiers.Background
import com.android.systemui.keyboard.shortcut.data.repository.ShortcutHelperStateRepository
import com.android.systemui.plugins.ActivityStarter
import com.android.systemui.statusbar.CommandQueue
import javax.inject.Inject
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.launch
import javax.inject.Inject


@SysUISingleton
class ShortcutHelperCoreStartable
@Inject constructor(
@Inject
constructor(
    private val commandQueue: CommandQueue,
    private val broadcastDispatcher: BroadcastDispatcher,
    private val stateRepository: ShortcutHelperStateRepository,
    private val activityStarter: ActivityStarter,
    @Background private val backgroundScope: CoroutineScope,
) : CoreStartable {
    override fun start() {
        registerBroadcastReceiver(
            action = Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS,
            onReceive = {
                backgroundScope.launch { stateRepository.show() }
            }
            onReceive = { showShortcutHelper() },
        )
        registerBroadcastReceiver(
            action = Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS,
            onReceive = { stateRepository.hide() }
            onReceive = { stateRepository.hide() },
        )
        registerBroadcastReceiver(
            action = Intent.ACTION_CLOSE_SYSTEM_DIALOGS,
            onReceive = { stateRepository.hide() }
            onReceive = { stateRepository.hide() },
        )
        commandQueue.addCallback(
            object : CommandQueue.Callbacks {
@@ -62,7 +62,7 @@ class ShortcutHelperCoreStartable
                }

                override fun toggleKeyboardShortcutsMenu(deviceId: Int) {
                    backgroundScope.launch { stateRepository.toggle(deviceId) }
                    toggleShortcutHelper(deviceId)
                }
            }
        )
@@ -81,4 +81,23 @@ class ShortcutHelperCoreStartable
            user = UserHandle.ALL,
        )
    }

    private fun showShortcutHelper() {
        dismissKeyguardThenPerformShortcutHelperAction { stateRepository.show() }
    }

    private fun toggleShortcutHelper(deviceId: Int? = null) {
        dismissKeyguardThenPerformShortcutHelperAction { stateRepository.toggle(deviceId) }
    }

    private fun dismissKeyguardThenPerformShortcutHelperAction(action: suspend () -> Unit) {
        activityStarter.dismissKeyguardThenExecute(
            /* action= */ {
                backgroundScope.launch { action() }
                false
            },
            /* cancel= */ {},
            /* afterKeyguardGone= */ true,
        )
    }
}
+6 −1
Original line number Diff line number Diff line
@@ -129,7 +129,7 @@ constructor(
                    val iconDrawable =
                        userContext.packageManager.getApplicationIcon(type.packageName)
                    IconSource(painter = DrawablePainter(drawable = iconDrawable))
                } catch (e: NameNotFoundException) {
                } catch (_: NameNotFoundException) {
                    Log.w(
                        "ShortcutHelperViewModel",
                        "Package not found when retrieving icon for ${type.packageName}",
@@ -234,6 +234,7 @@ constructor(

    fun onViewClosed() {
        stateInteractor.onViewClosed()
        resetSearchQuery()
    }

    fun onViewOpened() {
@@ -243,4 +244,8 @@ constructor(
    fun onSearchQueryChanged(query: String) {
        searchQuery.value = query
    }

    private fun resetSearchQuery(){
        searchQuery.value = ""
    }
}
+12 −14
Original line number Diff line number Diff line
@@ -53,6 +53,7 @@ import com.android.systemui.kosmos.backgroundScope
import com.android.systemui.kosmos.testDispatcher
import com.android.systemui.kosmos.testScope
import com.android.systemui.model.sysUiState
import com.android.systemui.plugins.activityStarter
import com.android.systemui.settings.displayTracker
import com.android.systemui.settings.userTracker
import com.android.systemui.statusbar.phone.systemUIDialogFactory
@@ -67,12 +68,7 @@ var Kosmos.shortcutHelperMultiTaskingShortcutsSource: KeyboardShortcutGroupsSour
    Kosmos.Fixture { MultitaskingShortcutsSource(mainResources, applicationContext) }

val Kosmos.shortcutHelperStateRepository by
    Kosmos.Fixture {
        ShortcutHelperStateRepository(
            fakeInputManager.inputManager,
            testDispatcher,
        )
    }
    Kosmos.Fixture { ShortcutHelperStateRepository(fakeInputManager.inputManager, testDispatcher) }

var Kosmos.shortcutHelperInputShortcutsSource: KeyboardShortcutGroupsSource by
    Kosmos.Fixture {
@@ -156,6 +152,7 @@ val Kosmos.shortcutHelperCoreStartable by
            fakeCommandQueue,
            broadcastDispatcher,
            shortcutHelperStateRepository,
            activityStarter,
            testScope,
        )
    }
@@ -168,6 +165,7 @@ val Kosmos.shortcutHelperTestHelper by
            broadcastDispatcher,
            fakeCommandQueue,
            fakeInputManager,
            activityStarter,
            windowManager,
        )
    }
Loading