Debian Codegraph @

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

raw
1 /*
2 * Copyright (C) 2021 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.Direction
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 class GameAppHelper @JvmOverloads constructor(
31 instr: Instrumentation,
32 launcherName: String = ActivityOptions.GAME_ACTIVITY_LAUNCHER_NAME,
33 component: FlickerComponentName =
34 ActivityOptions.GAME_ACTIVITY_COMPONENT_NAME.toFlickerComponent(),
35 launcherStrategy: ILauncherStrategy =
36 LauncherStrategyFactory.getInstance(instr).launcherStrategy,
37 ) : StandardAppHelper(instr, launcherName, component, launcherStrategy) {
38
39 /**
40 * Swipes down in the mock game app.
41 *
42 * @return true if the swipe operation is successful.
43 */
44 fun swipeDown(): Boolean {
45 val gameView = uiDevice.wait(
46 Until.findObject(By.res(getPackage(), GAME_APP_VIEW_RES)), WAIT_TIME_MS)
47 require(gameView != null) { "Mock game app view not found." }
48
49 val bound = gameView.getVisibleBounds()
50 return uiDevice.swipe(
51 bound.centerX(), bound.top, bound.centerX(), bound.centerY(), SWIPE_STEPS)
52 }
53
54 /**
55 * Switches to a recent app by quick switch gesture. This function can be used in both portrait
56 * and landscape mode.
57 *
58 * @param wmHelper Helper used to get window region.
59 * @param direction UiAutomator Direction enum to indicate the swipe direction.
60 *
61 * @return true if the swipe operation is successful.
62 */
63 fun switchToPreviousAppByQuickSwitchGesture(
64 wmHelper: WindowManagerStateHelper,
65 direction: Direction
66 ): Boolean {
67 val ratioForScreenBottom = 0.99
68 val fullView = wmHelper.getWindowRegion(component)
69 require(!fullView.isEmpty) { "Target $component view not found." }
70
71 val bound = fullView.bounds
72 val targetYPos = bound.bottom * ratioForScreenBottom
73 val endX = when (direction) {
74 Direction.LEFT -> bound.left
75 Direction.RIGHT -> bound.right
76 else -> {
77 throw IllegalStateException("Only left or right direction is allowed.")
78 }
79 }
80 return uiDevice.swipe(
81 bound.centerX(), targetYPos.toInt(), endX, targetYPos.toInt(), SWIPE_STEPS)
82 }
83
84 /**
85 * Waits for view idel with timeout, then checkes the target object whether visible on screen.
86 *
87 * @param packageName The targe application's package name.
88 * @param identifier The resource id of the target object.
89 * @param timeout The timeout duration in milliseconds.
90 *
91 * @return true if the target object exists.
92 */
93 @JvmOverloads
94 fun isTargetObjVisible(
95 packageName: String,
96 identifier: String,
97 timeout: Long = WAIT_TIME_MS
98 ): Boolean {
99 uiDevice.waitForIdle(timeout)
100 return uiDevice.hasObject(By.res(packageName, identifier))
101 }
102
103 companion object {
104 private const val GAME_APP_VIEW_RES = "container"
105 private const val WAIT_TIME_MS = 3_000L
106 private const val SWIPE_STEPS = 30
107 }
108 }