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.io.Reader
24 import android.tools.traces.wm.WindowState
25 
26 /**
27  * Subject for [WindowState] objects, used to make assertions over behaviors that occur on a single
28  * [WindowState] of a WM state.
29  *
30  * To make assertions over a layer from a state it is recommended to create a subject using
31  * [WindowManagerStateSubject.windowState](windowStateName)
32  *
33  * Alternatively, it is also possible to use [WindowStateSubject](myWindow).
34  *
35  * Example:
36  * ```
37  *    val trace = WindowManagerTraceParser().parse(myTraceFile)
38  *    val subject = WindowManagerTraceSubject(trace).first()
39  *        .windowState("ValidWindow")
40  *        .exists()
41  *        { myCustomAssertion(this) }
42  * ```
43  */
44 class WindowStateSubject(
45     override val reader: Reader? = null,
46     override val timestamp: Timestamp,
47     val windowState: WindowState
48 ) : FlickerSubject() {
49     val isVisible: Boolean = windowState.isVisible
50     val isInvisible: Boolean = !windowState.isVisible
51     val name: String = windowState.name
52     val frame: RegionSubject
53         get() = RegionSubject(windowState.frame, timestamp, reader)
54 
55     override val selfFacts = listOf(Fact("Window title", windowState.title))
56 
57     /** If the [windowState] exists, executes a custom [assertion] on the current subject */
<lambda>null58     operator fun invoke(assertion: (WindowState) -> Unit): WindowStateSubject = apply {
59         assertion(this.windowState)
60     }
61 
toStringnull62     override fun toString(): String {
63         return "WindowState:$name"
64     }
65 }
66