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.tools.Rotation
21 import android.tools.helpers.FIND_TIMEOUT
22 import android.tools.helpers.IME_PACKAGE
23 import android.tools.traces.component.ComponentNameMatcher
24 import android.tools.traces.component.IComponentMatcher
25 import android.tools.traces.parsers.WindowManagerStateHelper
26 import android.tools.traces.parsers.toFlickerComponent
27 import android.view.WindowInsets.Type.ime
28 import android.view.WindowInsets.Type.navigationBars
29 import android.view.WindowInsets.Type.statusBars
30 import androidx.test.uiautomator.By
31 import androidx.test.uiautomator.Until
32 import com.android.server.wm.flicker.testapp.ActivityOptions
33 import java.util.regex.Pattern
34 
35 class ImeShownOnAppStartHelper
36 @JvmOverloads
37 constructor(
38     instr: Instrumentation,
39     private val rotation: Rotation,
40     private val imePackageName: String = IME_PACKAGE,
41     launcherName: String = ActivityOptions.Ime.AutoFocusActivity.LABEL,
42     component: ComponentNameMatcher =
43         ActivityOptions.Ime.AutoFocusActivity.COMPONENT.toFlickerComponent()
44 ) : ImeAppHelper(instr, launcherName, component) {
openIMEnull45     override fun openIME(wmHelper: WindowManagerStateHelper) {
46         // do nothing (the app is focused automatically)
47         waitIMEShown(wmHelper)
48     }
49 
launchViaIntentnull50     override fun launchViaIntent(
51         wmHelper: WindowManagerStateHelper,
52         launchedAppComponentMatcherOverride: IComponentMatcher?,
53         action: String?,
54         stringExtras: Map<String, String>,
55         waitConditionsBuilder: WindowManagerStateHelper.StateSyncBuilder
56     ) {
57         super.launchViaIntent(
58             wmHelper,
59             launchedAppComponentMatcherOverride,
60             action,
61             stringExtras,
62             waitConditionsBuilder
63         )
64         waitIMEShown(wmHelper)
65     }
66 
opennull67     override fun open() {
68         val expectedPackage =
69             if (rotation.isRotated()) {
70                 imePackageName
71             } else {
72                 packageName
73             }
74         open(expectedPackage)
75     }
76 
dismissDialognull77     fun dismissDialog(wmHelper: WindowManagerStateHelper) {
78         val dialog = uiDevice.wait(Until.findObject(By.text("Dialog for test")), FIND_TIMEOUT)
79 
80         // Pressing back key to dismiss the dialog
81         if (dialog != null) {
82             uiDevice.pressBack()
83             wmHelper.StateSyncBuilder().withAppTransitionIdle().waitForAndVerify()
84         }
85     }
86 
getInsetsVisibleFromDialognull87     fun getInsetsVisibleFromDialog(type: Int): Boolean {
88         val insetsVisibilityTextView =
89             uiDevice.wait(Until.findObject(By.res("android:id/text1")), FIND_TIMEOUT)
90         if (insetsVisibilityTextView != null) {
91             val visibility = insetsVisibilityTextView.text.toString()
92             val matcher =
93                 when (type) {
94                     ime() -> {
95                         Pattern.compile("IME\\: (VISIBLE|INVISIBLE)").matcher(visibility)
96                     }
97                     statusBars() -> {
98                         Pattern.compile("StatusBar\\: (VISIBLE|INVISIBLE)").matcher(visibility)
99                     }
100                     navigationBars() -> {
101                         Pattern.compile("NavBar\\: (VISIBLE|INVISIBLE)").matcher(visibility)
102                     }
103                     else -> null
104                 }
105             if (matcher != null && matcher.find()) {
106                 return matcher.group(1) == "VISIBLE"
107             }
108         }
109         return false
110     }
111 }
112