Debian Codegraph @

tests/FlickerTests/src/com/android/server/wm/flicker/helpers/AssistantAppHelper.kt

raw
1 /*
2 * Copyright (C) 2022 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.server.wm.flicker.helpers
18
19 import android.app.Instrumentation
20 import android.content.ComponentName
21 import android.provider.Settings
22 import androidx.test.uiautomator.By
23 import androidx.test.uiautomator.UiDevice
24 import androidx.test.uiautomator.Until
25 import com.android.server.wm.flicker.testapp.ActivityOptions
26 import org.junit.Assert.assertNotNull
27
28 class AssistantAppHelper @JvmOverloads constructor(
29 val instr: Instrumentation,
30 val component: ComponentName = ActivityOptions.ASSISTANT_SERVICE_COMPONENT_NAME,
31 ) {
32 protected val uiDevice: UiDevice = UiDevice.getInstance(instr)
33 protected val defaultAssistant: String? = Settings.Secure.getString(
34 instr.targetContext.contentResolver,
35 Settings.Secure.ASSISTANT)
36 protected val defaultVoiceInteractionService: String? = Settings.Secure.getString(
37 instr.targetContext.contentResolver,
38 Settings.Secure.VOICE_INTERACTION_SERVICE)
39
40 fun setDefaultAssistant() {
41 Settings.Secure.putString(
42 instr.targetContext.contentResolver,
43 Settings.Secure.VOICE_INTERACTION_SERVICE,
44 component.flattenToString())
45 Settings.Secure.putString(
46 instr.targetContext.contentResolver,
47 Settings.Secure.ASSISTANT,
48 component.flattenToString())
49 }
50
51 fun resetDefaultAssistant() {
52 Settings.Secure.putString(
53 instr.targetContext.contentResolver,
54 Settings.Secure.VOICE_INTERACTION_SERVICE,
55 defaultVoiceInteractionService)
56 Settings.Secure.putString(
57 instr.targetContext.contentResolver,
58 Settings.Secure.ASSISTANT,
59 defaultAssistant)
60 }
61
62 /**
63 * Open Assistance UI.
64 *
65 * @param longpress open the UI by long pressing power button.
66 * Otherwise open the UI through vioceinteraction shell command directly.
67 */
68 @JvmOverloads
69 fun openUI(longpress: Boolean = false) {
70 if (longpress) {
71 uiDevice.executeShellCommand("input keyevent --longpress KEYCODE_POWER")
72 } else {
73 uiDevice.executeShellCommand("cmd voiceinteraction show")
74 }
75 val ui = uiDevice.wait(
76 Until.findObject(By.res(ActivityOptions.FLICKER_APP_PACKAGE, "vis_frame")),
77 FIND_TIMEOUT)
78 assertNotNull("Can't find Assistant UI after long pressing power button.", ui)
79 }
80 }