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.wm.shell.flicker.helpers
18 
19 import android.app.Instrumentation
20 import android.content.pm.PackageManager.FEATURE_LEANBACK
21 import android.content.pm.PackageManager.FEATURE_LEANBACK_ONLY
22 import android.os.SystemProperties
23 import android.support.test.launcherhelper.LauncherStrategyFactory
24 import android.util.Log
25 import androidx.test.uiautomator.By
26 import androidx.test.uiautomator.UiObject2
27 import androidx.test.uiautomator.Until
28 import com.android.compatibility.common.util.SystemUtil
29 import com.android.server.wm.flicker.helpers.StandardAppHelper
30 import com.android.server.wm.traces.common.FlickerComponentName
31 import java.io.IOException
32 
33 abstract class BaseAppHelper(
34     instrumentation: Instrumentation,
35     launcherName: String,
36     component: FlickerComponentName
37 ) : StandardAppHelper(
38     instrumentation,
39     launcherName,
40     component,
41     LauncherStrategyFactory.getInstance(instrumentation).launcherStrategy
42 ) {
43     private val appSelector = By.pkg(component.packageName).depth(0)
44 
45     protected val isTelevision: Boolean
46         get() = context.packageManager.run {
47             hasSystemFeature(FEATURE_LEANBACK) || hasSystemFeature(FEATURE_LEANBACK_ONLY)
48         }
49 
50     val defaultWindowName: String
51         get() = component.toWindowName()
52 
53     val ui: UiObject2?
54         get() = uiDevice.findObject(appSelector)
55 
56     fun waitUntilClosed(): Boolean {
57         return uiDevice.wait(Until.gone(appSelector), APP_CLOSE_WAIT_TIME_MS)
58     }
59 
60     companion object {
61         private const val APP_CLOSE_WAIT_TIME_MS = 3_000L
62 
63         fun isShellTransitionsEnabled() =
64                 SystemProperties.getBoolean("persist.debug.shell_transit", false)
65 
66         fun executeShellCommand(instrumentation: Instrumentation, cmd: String) {
67             try {
68                 SystemUtil.runShellCommand(instrumentation, cmd)
69             } catch (e: IOException) {
70                 Log.e("BaseAppHelper", "executeShellCommand error! $e")
71             }
72         }
73     }
74 }
75