xref: /aosp_15_r20/platform_testing/libraries/flicker/src/android/tools/flicker/subject/wm/WindowStateSubject.kt (revision dd0948b35e70be4c0246aabd6c72554a5eb8b22a)
1 /*
2  * Copyright (C) 2023 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 android.tools.flicker.subject.wm
18 
19 import android.tools.Timestamp
20 import android.tools.flicker.assertions.Fact
21 import android.tools.flicker.subject.FlickerSubject
22 import android.tools.flicker.subject.region.RegionSubject
23 import android.tools.function.AssertionPredicate
24 import android.tools.io.Reader
25 import android.tools.traces.wm.WindowState
26 
27 /**
28  * Subject for [WindowState] objects, used to make assertions over behaviors that occur on a single
29  * [WindowState] of a WM state.
30  *
31  * To make assertions over a layer from a state it is recommended to create a subject using
32  * [WindowManagerStateSubject.windowState](windowStateName)
33  *
34  * Alternatively, it is also possible to use [WindowStateSubject](myWindow).
35  *
36  * Example:
37  * ```
38  *    val trace = WindowManagerTraceParser().parse(myTraceFile)
39  *    val subject = WindowManagerTraceSubject(trace).first()
40  *        .windowState("ValidWindow")
41  *        .exists()
42  *        { myCustomAssertion(this) }
43  * ```
44  */
45 class WindowStateSubject
46 @JvmOverloads
47 constructor(
48     override val timestamp: Timestamp,
49     val windowState: WindowState,
50     override val reader: Reader? = null,
51 ) : FlickerSubject() {
52     val isVisible: Boolean = windowState.isVisible
53     val isInvisible: Boolean = !windowState.isVisible
54     val name: String = windowState.name
55     val frame: RegionSubject
56         get() = RegionSubject(windowState.frame, timestamp, reader)
57 
58     override val selfFacts = listOf(Fact("Window title", windowState.title))
59 
60     /** If the [windowState] exists, executes a custom [assertion] on the current subject */
<lambda>null61     operator fun invoke(assertion: AssertionPredicate<WindowState>): WindowStateSubject = apply {
62         assertion.verify(this.windowState)
63     }
64 
toStringnull65     override fun toString(): String {
66         return "WindowState:$name"
67     }
68 }
69