• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2024 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.systemui.keyboard.shortcut.data.repository
18 
19 import android.content.Context
20 import android.content.Intent
21 import android.hardware.input.FakeInputManager
22 import android.view.KeyboardShortcutGroup
23 import android.view.WindowManager
24 import android.view.WindowManager.KeyboardShortcutsReceiver
25 import com.android.systemui.broadcast.FakeBroadcastDispatcher
26 import com.android.systemui.keyboard.shortcut.ShortcutHelperCoreStartable
27 import com.android.systemui.keyguard.data.repository.FakeCommandQueue
28 import com.android.systemui.plugins.ActivityStarter
29 import com.android.systemui.util.mockito.any
30 import com.android.systemui.util.mockito.whenever
31 import org.mockito.kotlin.eq
32 
33 class ShortcutHelperTestHelper(
34     coreStartable: ShortcutHelperCoreStartable,
35     private val context: Context,
36     private val fakeBroadcastDispatcher: FakeBroadcastDispatcher,
37     private val fakeCommandQueue: FakeCommandQueue,
38     private val fakeInputManager: FakeInputManager,
39     private val activityStarter: ActivityStarter,
40     windowManager: WindowManager,
41 ) {
42     private var imeShortcuts: List<KeyboardShortcutGroup> = emptyList()
43     private var currentAppsShortcuts: List<KeyboardShortcutGroup> = emptyList()
44 
45     init {
<lambda>null46         whenever(windowManager.requestImeKeyboardShortcuts(any(), any())).thenAnswer {
47             val keyboardShortcutReceiver = it.getArgument<KeyboardShortcutsReceiver>(0)
48             keyboardShortcutReceiver.onKeyboardShortcutsReceived(imeShortcuts)
49         }
<lambda>null50         whenever(windowManager.requestAppKeyboardShortcuts(any(), any())).thenAnswer {
51             val keyboardShortcutReceiver = it.getArgument<KeyboardShortcutsReceiver>(0)
52             keyboardShortcutReceiver.onKeyboardShortcutsReceived(currentAppsShortcuts)
53         }
<lambda>null54         whenever(activityStarter.dismissKeyguardThenExecute(any(), any(), eq(true))).then {
55             (it.arguments[0] as ActivityStarter.OnDismissAction).onDismiss()
56         }
57         coreStartable.start()
58     }
59 
60     /**
61      * Use this method to set what ime shortcuts should be returned from windowManager in tests. By
62      * default windowManager.requestImeKeyboardShortcuts will return emptyList. See init block.
63      */
setImeShortcutsnull64     fun setImeShortcuts(imeShortcuts: List<KeyboardShortcutGroup>) {
65         this.imeShortcuts = imeShortcuts
66     }
67 
68     /**
69      * Use this method to set what current app shortcuts should be returned from windowManager in
70      * tests. By default [WindowManager.requestAppKeyboardShortcuts] will return emptyList.
71      */
setCurrentAppsShortcutsnull72     fun setCurrentAppsShortcuts(currentAppShortcuts: List<KeyboardShortcutGroup>) {
73         this.currentAppsShortcuts = currentAppShortcuts
74     }
75 
hideThroughCloseSystemDialogsnull76     fun hideThroughCloseSystemDialogs() {
77         fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly(
78             context,
79             Intent(Intent.ACTION_CLOSE_SYSTEM_DIALOGS),
80         )
81     }
82 
hideFromActivitynull83     fun hideFromActivity() {
84         fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly(
85             context,
86             Intent(Intent.ACTION_DISMISS_KEYBOARD_SHORTCUTS),
87         )
88     }
89 
showFromActivitynull90     fun showFromActivity() {
91         fakeBroadcastDispatcher.sendIntentToMatchingReceiversOnly(
92             context,
93             Intent(Intent.ACTION_SHOW_KEYBOARD_SHORTCUTS),
94         )
95     }
96 
togglenull97     fun toggle(deviceId: Int) {
98         fakeInputManager.addPhysicalKeyboardIfNotPresent(deviceId)
99         fakeCommandQueue.doForEachCallback { it.toggleKeyboardShortcutsMenu(deviceId) }
100     }
101 
hideForSystemnull102     fun hideForSystem() {
103         fakeCommandQueue.doForEachCallback { it.dismissKeyboardShortcutsMenu() }
104     }
105 }
106