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.region
18 
19 import android.graphics.Rect
20 import android.graphics.Region
21 import android.tools.flicker.subject.FlickerTraceSubject
22 import android.tools.io.Reader
23 import android.tools.traces.region.RegionTrace
24 
25 /**
26  * Subject for [RegionTrace] objects, used to make assertions over behaviors that occur on a
27  * sequence of regions.
28  */
29 class RegionTraceSubject(val trace: RegionTrace, override val reader: Reader? = null) :
30     FlickerTraceSubject<RegionSubject>(), IRegionSubject {
31 
<lambda>null32     override val subjects by lazy { trace.entries.map { RegionSubject(it, it.timestamp, reader) } }
33 
34     private val componentsAsString =
35         if (trace.components == null) {
36             "<any>"
37         } else {
38             "[${trace.components}]"
39         }
40 
41     /** {@inheritDoc} */
thennull42     override fun then(): RegionTraceSubject {
43         return super.then() as RegionTraceSubject
44     }
45 
46     /** {@inheritDoc} */
isHigherOrEqualnull47     override fun isHigherOrEqual(other: Rect): RegionTraceSubject = isHigherOrEqual(Region(other))
48 
49     /** {@inheritDoc} */
50     override fun isHigherOrEqual(other: Region): RegionTraceSubject = apply {
51         addAssertion("isHigherOrEqual($other, $componentsAsString)") { it.isHigherOrEqual(other) }
52     }
53 
54     /** {@inheritDoc} */
isLowerOrEqualnull55     override fun isLowerOrEqual(other: Rect): RegionTraceSubject = isLowerOrEqual(Region(other))
56 
57     /** {@inheritDoc} */
58     override fun isLowerOrEqual(other: Region): RegionTraceSubject = apply {
59         addAssertion("isLowerOrEqual($other, $componentsAsString)") { it.isLowerOrEqual(other) }
60     }
61 
62     /** {@inheritDoc} */
<lambda>null63     override fun isToTheRight(other: Region): RegionTraceSubject = apply {
64         addAssertion("isToTheRight($other, $componentsAsString)") { it.isToTheRight(other) }
65     }
66 
67     /** {@inheritDoc} */
isHighernull68     override fun isHigher(other: Rect): RegionTraceSubject = isHigher(Region(other))
69 
70     /** {@inheritDoc} */
71     override fun isHigher(other: Region): RegionTraceSubject = apply {
72         addAssertion("isHigher($other, $componentsAsString)") { it.isHigher(other) }
73     }
74 
75     /** {@inheritDoc} */
isLowernull76     override fun isLower(other: Rect): RegionTraceSubject = isLower(Region(other))
77 
78     /** {@inheritDoc} */
79     override fun isLower(other: Region): RegionTraceSubject = apply {
80         addAssertion("isLower($other, $componentsAsString)") { it.isLower(other) }
81     }
82 
83     /** {@inheritDoc} */
<lambda>null84     override fun coversAtMost(other: Region): RegionTraceSubject = apply {
85         addAssertion("coversAtMost($other, $componentsAsString") { it.coversAtMost(other) }
86     }
87 
88     /** {@inheritDoc} */
coversAtMostnull89     override fun coversAtMost(other: Rect): RegionTraceSubject = this.coversAtMost(Region(other))
90 
91     /** {@inheritDoc} */
92     override fun notBiggerThan(other: Region): RegionTraceSubject = apply {
93         addAssertion("notBiggerThan($other, $componentsAsString") { it.notBiggerThan(other) }
94     }
95 
96     /** {@inheritDoc} */
<lambda>null97     override fun isToTheRightBottom(other: Region, threshold: Int): RegionTraceSubject = apply {
98         addAssertion("isToTheRightBottom($other, $componentsAsString") {
99             it.isToTheRightBottom(other, threshold)
100         }
101     }
102 
103     /** {@inheritDoc} */
<lambda>null104     override fun regionsCenterPointInside(other: Rect): RegionTraceSubject = apply {
105         addAssertion("regionsCenterPointInside($other, $componentsAsString") {
106             it.regionsCenterPointInside(other)
107         }
108     }
109 
110     /** {@inheritDoc} */
<lambda>null111     override fun coversAtLeast(other: Region): RegionTraceSubject = apply {
112         addAssertion("coversAtLeast($other, $componentsAsString)") { it.coversAtLeast(other) }
113     }
114 
115     /** {@inheritDoc} */
coversAtLeastnull116     override fun coversAtLeast(other: Rect): RegionTraceSubject = this.coversAtLeast(Region(other))
117 
118     /** {@inheritDoc} */
119     override fun coversExactly(other: Region): RegionTraceSubject = apply {
120         addAssertion("coversExactly($other, $componentsAsString)") { it.coversExactly(other) }
121     }
122 
123     /** {@inheritDoc} */
coversExactlynull124     override fun coversExactly(other: Rect): RegionTraceSubject = apply {
125         addAssertion("coversExactly($other, $componentsAsString") { it.coversExactly(other) }
126     }
127 
128     /** {@inheritDoc} */
<lambda>null129     override fun overlaps(other: Region): RegionTraceSubject = apply {
130         addAssertion("overlaps($other, $componentsAsString") { it.overlaps(other) }
131     }
132 
133     /** {@inheritDoc} */
overlapsnull134     override fun overlaps(other: Rect): RegionTraceSubject = overlaps(Region(other))
135 
136     /** {@inheritDoc} */
137     override fun notOverlaps(other: Region): RegionTraceSubject = apply {
138         addAssertion("notOverlaps($other, $componentsAsString") { it.notOverlaps(other) }
139     }
140 
141     /** {@inheritDoc} */
notOverlapsnull142     override fun notOverlaps(other: Rect): RegionTraceSubject = notOverlaps(Region(other))
143 
144     fun isSameAspectRatio(other: Region): RegionTraceSubject =
145         isSameAspectRatio(other, threshold = 0.1)
146 
147     /** {@inheritDoc} */
148     override fun isSameAspectRatio(other: Region, threshold: Double): RegionTraceSubject = apply {
149         addAssertion("isSameAspectRatio($other, $componentsAsString") {
150             it.isSameAspectRatio(other, threshold)
151         }
152     }
153 
<lambda>null154     override fun hasSameLeftPosition(displayRect: Rect): RegionTraceSubject = apply {
155         addAssertion("hasSameLeftPosition($displayRect") { it.hasSameLeftPosition(displayRect) }
156     }
157 
<lambda>null158     override fun hasSameBottomPosition(displayRect: Rect): RegionTraceSubject = apply {
159         addAssertion("hasSameBottomPosition($displayRect") { it.hasSameBottomPosition(displayRect) }
160     }
161 
<lambda>null162     override fun hasSameRightPosition(displayRect: Rect): RegionTraceSubject = apply {
163         addAssertion("hasSameRightPosition($displayRect") { it.hasSameRightPosition(displayRect) }
164     }
165 
<lambda>null166     override fun hasSameTopPosition(displayRect: Rect): RegionTraceSubject = apply {
167         addAssertion("hasSameTopPosition($displayRect") { it.hasSameTopPosition(displayRect) }
168     }
169 }
170