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.keyguard.data.repository.FakeCommandQueue
27 import com.android.systemui.util.mockito.any
28 import com.android.systemui.util.mockito.whenever
29 
30 class ShortcutHelperTestHelper(
31     repo: ShortcutHelperStateRepository,
32     private val context: Context,
33     private val fakeBroadcastDispatcher: FakeBroadcastDispatcher,
34     private val fakeCommandQueue: FakeCommandQueue,
35     private val fakeInputManager: FakeInputManager,
36     windowManager: WindowManager
37 ) {
38 
39     companion object {
40         const val DEFAULT_DEVICE_ID = 123
41     }
42 
43     private var imeShortcuts: List<KeyboardShortcutGroup> = emptyList()
44     private var currentAppsShortcuts: List<KeyboardShortcutGroup> = emptyList()
45 
46     init {
<lambda>null47         whenever(windowManager.requestImeKeyboardShortcuts(any(), any())).thenAnswer {
48             val keyboardShortcutReceiver = it.getArgument<KeyboardShortcutsReceiver>(0)
49             keyboardShortcutReceiver.onKeyboardShortcutsReceived(imeShortcuts)
50             return@thenAnswer Unit
51         }
<lambda>null52         whenever(windowManager.requestAppKeyboardShortcuts(any(), any())).thenAnswer {
53             val keyboardShortcutReceiver = it.getArgument<KeyboardShortcutsReceiver>(0)
54             keyboardShortcutReceiver.onKeyboardShortcutsReceived(currentAppsShortcuts)
55             return@thenAnswer Unit
56         }
57         repo.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