Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2020 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.support.test.launcherhelper.ILauncherStrategy
21 import android.support.test.launcherhelper.LauncherStrategyFactory
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 com.android.server.wm.traces.common.FlickerComponentName
27 import com.android.server.wm.traces.parser.toFlickerComponent
28 import com.android.server.wm.traces.parser.windowmanager.WindowManagerStateHelper
29
30 open class ImeAppHelper @JvmOverloads constructor(
31 instr: Instrumentation,
32 launcherName: String = ActivityOptions.IME_ACTIVITY_LAUNCHER_NAME,
33 component: FlickerComponentName =
34 ActivityOptions.IME_ACTIVITY_COMPONENT_NAME.toFlickerComponent(),
35 launcherStrategy: ILauncherStrategy = LauncherStrategyFactory
36 .getInstance(instr)
37 .launcherStrategy
38 ) : StandardAppHelper(instr, launcherName, component, launcherStrategy) {
39 /**
40 * Opens the IME and wait for it to be displayed
41 *
42 * @param device UIDevice instance to interact with the device
43 * @param wmHelper Helper used to wait for WindowManager states
44 */
45 @JvmOverloads
46 open fun openIME(device: UiDevice, wmHelper: WindowManagerStateHelper? = null) {
47 val editText = device.wait(
48 Until.findObject(By.res(getPackage(), "plain_text_input")),
49 FIND_TIMEOUT)
50
51 require(editText != null) {
52 "Text field not found, this usually happens when the device " +
53 "was left in an unknown state (e.g. in split screen)"
54 }
55 editText.click()
56 waitIMEShown(device, wmHelper)
57 }
58
59 protected fun waitIMEShown(
60 device: UiDevice,
61 wmHelper: WindowManagerStateHelper? = null
62 ) {
63 if (wmHelper == null) {
64 device.waitForIdle()
65 } else {
66 wmHelper.waitImeShown()
67 }
68 }
69
70 /**
71 * Opens the IME and wait for it to be gone
72 *
73 * @param device UIDevice instance to interact with the device
74 * @param wmHelper Helper used to wait for WindowManager states
75 */
76 @JvmOverloads
77 open fun closeIME(device: UiDevice, wmHelper: WindowManagerStateHelper? = null) {
78 device.pressBack()
79 // Using only the AccessibilityInfo it is not possible to identify if the IME is active
80 if (wmHelper == null) {
81 device.waitForIdle()
82 } else {
83 wmHelper.waitImeGone()
84 }
85 }
86
87 @JvmOverloads
88 open fun finishActivity(device: UiDevice, wmHelper: WindowManagerStateHelper? = null) {
89 val finishButton = device.wait(
90 Until.findObject(By.res(getPackage(), "finish_activity_btn")),
91 FIND_TIMEOUT)
92 require(finishButton != null) {
93 "Finish activity button not found, probably IME activity is not on the screen ?"
94 }
95 finishButton.click()
96 if (wmHelper == null) {
97 device.waitForIdle()
98 } else {
99 wmHelper.waitForActivityRemoved(component)
100 }
101 }
102 }