Debian Codegraph @

tests/FlickerTests/src/com/android/server/wm/flicker/CommonAssertions.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 @file:JvmName("CommonAssertions")
18 package com.android.server.wm.flicker
19
20 import com.android.server.wm.flicker.helpers.WindowUtils
21 import com.android.server.wm.flicker.traces.region.RegionSubject
22 import com.android.server.wm.traces.common.FlickerComponentName
23
24 val LAUNCHER_COMPONENT = FlickerComponentName(
25 "com.google.android.apps.nexuslauncher",
26 "com.google.android.apps.nexuslauncher.NexusLauncherActivity"
27 )
28
29 /**
30 * Checks that [FlickerComponentName.STATUS_BAR] window is visible and above the app windows in
31 * all WM trace entries
32 */
33 fun FlickerTestParameter.statusBarWindowIsVisible() {
34 assertWm {
35 this.isAboveAppWindowVisible(FlickerComponentName.STATUS_BAR)
36 }
37 }
38
39 /**
40 * Checks that [FlickerComponentName.NAV_BAR] window is visible and above the app windows in
41 * all WM trace entries
42 */
43 fun FlickerTestParameter.navBarWindowIsVisible() {
44 assertWm {
45 this.isAboveAppWindowVisible(FlickerComponentName.NAV_BAR)
46 }
47 }
48
49 /**
50 * If [allStates] is true, checks if the stack space of all displays is fully covered
51 * by any visible layer, during the whole transitions
52 *
53 * Otherwise, checks if the stack space of all displays is fully covered
54 * by any visible layer, at the start and end of the transition
55 *
56 * @param allStates if all states should be checked, othersie, just initial and final
57 */
58 @JvmOverloads
59 fun FlickerTestParameter.entireScreenCovered(allStates: Boolean = true) {
60 if (allStates) {
61 assertLayers {
62 this.invoke("entireScreenCovered") { entry ->
63 entry.entry.displays.forEach { display ->
64 entry.visibleRegion().coversAtLeast(display.layerStackSpace)
65 }
66 }
67 }
68 } else {
69 assertLayersStart {
70 this.entry.displays.forEach { display ->
71 this.visibleRegion().coversAtLeast(display.layerStackSpace)
72 }
73 }
74 assertLayersEnd {
75 this.entry.displays.forEach { display ->
76 this.visibleRegion().coversAtLeast(display.layerStackSpace)
77 }
78 }
79 }
80 }
81
82 /**
83 * Checks that [FlickerComponentName.NAV_BAR] layer is visible at the start and end of the SF
84 * trace
85 */
86 fun FlickerTestParameter.navBarLayerIsVisible() {
87 assertLayersStart {
88 this.isVisible(FlickerComponentName.NAV_BAR)
89 }
90 assertLayersEnd {
91 this.isVisible(FlickerComponentName.NAV_BAR)
92 }
93 }
94
95 /**
96 * Checks that [FlickerComponentName.STATUS_BAR] layer is visible at the start and end of the SF
97 * trace
98 */
99 fun FlickerTestParameter.statusBarLayerIsVisible() {
100 assertLayersStart {
101 this.isVisible(FlickerComponentName.STATUS_BAR)
102 }
103 assertLayersEnd {
104 this.isVisible(FlickerComponentName.STATUS_BAR)
105 }
106 }
107
108 /**
109 * Asserts that the [FlickerComponentName.NAV_BAR] layer is at the correct position at the start
110 * of the SF trace
111 */
112 fun FlickerTestParameter.navBarLayerPositionStart() {
113 assertLayersStart {
114 val display = this.entry.displays.minByOrNull { it.id }
115 ?: throw RuntimeException("There is no display!")
116 this.visibleRegion(FlickerComponentName.NAV_BAR)
117 .coversExactly(WindowUtils.getNavigationBarPosition(display, isGesturalNavigation))
118 }
119 }
120
121 /**
122 * Asserts that the [FlickerComponentName.NAV_BAR] layer is at the correct position at the end
123 * of the SF trace
124 */
125 fun FlickerTestParameter.navBarLayerPositionEnd() {
126 assertLayersEnd {
127 val display = this.entry.displays.minByOrNull { it.id }
128 ?: throw RuntimeException("There is no display!")
129 this.visibleRegion(FlickerComponentName.NAV_BAR)
130 .coversExactly(WindowUtils.getNavigationBarPosition(display, isGesturalNavigation))
131 }
132 }
133
134 /**
135 * Asserts that the [FlickerComponentName.NAV_BAR] layer is at the correct position at the start
136 * and end of the SF trace
137 */
138 fun FlickerTestParameter.navBarLayerRotatesAndScales() {
139 navBarLayerPositionStart()
140 navBarLayerPositionEnd()
141 }
142
143 /**
144 * Asserts that the [FlickerComponentName.STATUS_BAR] layer is at the correct position at the start
145 * of the SF trace
146 */
147 fun FlickerTestParameter.statusBarLayerPositionStart() {
148 assertLayersStart {
149 val display = this.entry.displays.minByOrNull { it.id }
150 ?: throw RuntimeException("There is no display!")
151 this.visibleRegion(FlickerComponentName.STATUS_BAR)
152 .coversExactly(WindowUtils.getStatusBarPosition(display))
153 }
154 }
155
156 /**
157 * Asserts that the [FlickerComponentName.STATUS_BAR] layer is at the correct position at the end
158 * of the SF trace
159 */
160 fun FlickerTestParameter.statusBarLayerPositionEnd() {
161 assertLayersEnd {
162 val display = this.entry.displays.minByOrNull { it.id }
163 ?: throw RuntimeException("There is no display!")
164 this.visibleRegion(FlickerComponentName.STATUS_BAR)
165 .coversExactly(WindowUtils.getStatusBarPosition(display))
166 }
167 }
168
169 /**
170 * Asserts that the [FlickerComponentName.STATUS_BAR] layer is at the correct position at the start
171 * and end of the SF trace
172 */
173 fun FlickerTestParameter.statusBarLayerRotatesScales() {
174 statusBarLayerPositionStart()
175 statusBarLayerPositionEnd()
176 }
177
178 /**
179 * Asserts that the visibleRegion of the [FlickerComponentName.SNAPSHOT] layer can cover
180 * the visibleRegion of the given app component exactly
181 */
182 fun FlickerTestParameter.snapshotStartingWindowLayerCoversExactlyOnApp(
183 component: FlickerComponentName) {
184 assertLayers {
185 invoke("snapshotStartingWindowLayerCoversExactlyOnApp") {
186 val snapshotLayers = it.subjects.filter { subject ->
187 subject.name.contains(
188 FlickerComponentName.SNAPSHOT.toLayerName()) && subject.isVisible
189 }
190 // Verify the size of snapshotRegion covers appVisibleRegion exactly in animation.
191 if (snapshotLayers.isNotEmpty()) {
192 val visibleAreas = snapshotLayers.mapNotNull { snapshotLayer ->
193 snapshotLayer.layer?.visibleRegion
194 }.toTypedArray()
195 val snapshotRegion = RegionSubject.assertThat(visibleAreas, this, timestamp)
196 val appVisibleRegion = it.visibleRegion(component)
197 if (snapshotRegion.region.isNotEmpty) {
198 snapshotRegion.coversExactly(appVisibleRegion.region)
199 }
200 }
201 }
202 }
203 }
204
205 /**
206 * Asserts that:
207 * [originalLayer] is visible at the start of the trace
208 * [originalLayer] becomes invisible during the trace and (in the same entry) [newLayer]
209 * becomes visible
210 * [newLayer] remains visible until the end of the trace
211 *
212 * @param originalLayer Layer that should be visible at the start
213 * @param newLayer Layer that should be visible at the end
214 * @param ignoreEntriesWithRotationLayer If entries with a visible rotation layer should be ignored
215 * when checking the transition. If true we will not fail the assertion if a rotation layer is
216 * visible to fill the gap between the [originalLayer] being visible and the [newLayer] being
217 * visible.
218 * @param ignoreSnapshot If the snapshot layer should be ignored during the transition
219 * (useful mostly for app launch)
220 * @param ignoreSplashscreen If the splashscreen layer should be ignored during the transition.
221 * If true then we will allow for a splashscreen to be shown before the layer is shown,
222 * otherwise we won't and the layer must appear immediately.
223 */
224 fun FlickerTestParameter.replacesLayer(
225 originalLayer: FlickerComponentName,
226 newLayer: FlickerComponentName,
227 ignoreEntriesWithRotationLayer: Boolean = false,
228 ignoreSnapshot: Boolean = false,
229 ignoreSplashscreen: Boolean = true
230 ) {
231 assertLayers {
232 val assertion = this.isVisible(originalLayer)
233
234 if (ignoreEntriesWithRotationLayer) {
235 assertion.then().isVisible(FlickerComponentName.ROTATION, isOptional = true)
236 }
237 if (ignoreSnapshot) {
238 assertion.then().isVisible(FlickerComponentName.SNAPSHOT, isOptional = true)
239 }
240 if (ignoreSplashscreen) {
241 assertion.then().isSplashScreenVisibleFor(newLayer, isOptional = true)
242 }
243
244 assertion.then().isVisible(newLayer)
245 }
246
247 assertLayersStart {
248 this.isVisible(originalLayer)
249 .isInvisible(newLayer)
250 }
251
252 assertLayersEnd {
253 this.isInvisible(originalLayer)
254 .isVisible(newLayer)
255 }
256 }