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.common.flicker.subject.region 18 19 import android.tools.common.datatypes.Rect 20 import android.tools.common.datatypes.Region 21 import android.tools.common.flicker.subject.FlickerTraceSubject 22 import android.tools.common.io.Reader 23 import android.tools.common.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} */ isHigherOrEqualnull42 override fun isHigherOrEqual(other: Rect): RegionTraceSubject = 43 isHigherOrEqual(Region.from(other)) 44 45 /** {@inheritDoc} */ 46 override fun isHigherOrEqual(other: Region): RegionTraceSubject = apply { 47 addAssertion("isHigherOrEqual($other, $componentsAsString)") { it.isHigherOrEqual(other) } 48 } 49 50 /** {@inheritDoc} */ isLowerOrEqualnull51 override fun isLowerOrEqual(other: Rect): RegionTraceSubject = 52 isLowerOrEqual(Region.from(other)) 53 54 /** {@inheritDoc} */ 55 override fun isLowerOrEqual(other: Region): RegionTraceSubject = apply { 56 addAssertion("isLowerOrEqual($other, $componentsAsString)") { it.isLowerOrEqual(other) } 57 } 58 59 /** {@inheritDoc} */ <lambda>null60 override fun isToTheRight(other: Region): RegionTraceSubject = apply { 61 addAssertion("isToTheRight($other, $componentsAsString)") { it.isToTheRight(other) } 62 } 63 64 /** {@inheritDoc} */ isHighernull65 override fun isHigher(other: Rect): RegionTraceSubject = isHigher(Region.from(other)) 66 67 /** {@inheritDoc} */ 68 override fun isHigher(other: Region): RegionTraceSubject = apply { 69 addAssertion("isHigher($other, $componentsAsString)") { it.isHigher(other) } 70 } 71 72 /** {@inheritDoc} */ isLowernull73 override fun isLower(other: Rect): RegionTraceSubject = isLower(Region.from(other)) 74 75 /** {@inheritDoc} */ 76 override fun isLower(other: Region): RegionTraceSubject = apply { 77 addAssertion("isLower($other, $componentsAsString)") { it.isLower(other) } 78 } 79 80 /** {@inheritDoc} */ <lambda>null81 override fun coversAtMost(other: Region): RegionTraceSubject = apply { 82 addAssertion("coversAtMost($other, $componentsAsString") { it.coversAtMost(other) } 83 } 84 85 /** {@inheritDoc} */ coversAtMostnull86 override fun coversAtMost(other: Rect): RegionTraceSubject = 87 this.coversAtMost(Region.from(other)) 88 89 /** {@inheritDoc} */ 90 override fun notBiggerThan(other: Region): RegionTraceSubject = apply { 91 addAssertion("notBiggerThan($other, $componentsAsString") { it.notBiggerThan(other) } 92 } 93 94 /** {@inheritDoc} */ <lambda>null95 override fun isToTheRightBottom(other: Region, threshold: Int): RegionTraceSubject = apply { 96 addAssertion("isToTheRightBottom($other, $componentsAsString") { 97 it.isToTheRightBottom(other, threshold) 98 } 99 } 100 101 /** {@inheritDoc} */ <lambda>null102 override fun coversAtLeast(other: Region): RegionTraceSubject = apply { 103 addAssertion("coversAtLeast($other, $componentsAsString)") { it.coversAtLeast(other) } 104 } 105 106 /** {@inheritDoc} */ coversAtLeastnull107 override fun coversAtLeast(other: Rect): RegionTraceSubject = 108 this.coversAtLeast(Region.from(other)) 109 110 /** {@inheritDoc} */ 111 override fun coversExactly(other: Region): RegionTraceSubject = apply { 112 addAssertion("coversExactly($other, $componentsAsString)") { it.coversExactly(other) } 113 } 114 115 /** {@inheritDoc} */ coversExactlynull116 override fun coversExactly(other: Rect): RegionTraceSubject = apply { 117 addAssertion("coversExactly($other, $componentsAsString") { it.coversExactly(other) } 118 } 119 120 /** {@inheritDoc} */ <lambda>null121 override fun overlaps(other: Region): RegionTraceSubject = apply { 122 addAssertion("overlaps($other, $componentsAsString") { it.overlaps(other) } 123 } 124 125 /** {@inheritDoc} */ overlapsnull126 override fun overlaps(other: Rect): RegionTraceSubject = overlaps(Region.from(other)) 127 128 /** {@inheritDoc} */ 129 override fun notOverlaps(other: Region): RegionTraceSubject = apply { 130 addAssertion("notOverlaps($other, $componentsAsString") { it.notOverlaps(other) } 131 } 132 133 /** {@inheritDoc} */ notOverlapsnull134 override fun notOverlaps(other: Rect): RegionTraceSubject = notOverlaps(Region.from(other)) 135 136 fun isSameAspectRatio(other: Region): RegionTraceSubject = 137 isSameAspectRatio(other, threshold = 0.1) 138 139 /** {@inheritDoc} */ 140 override fun isSameAspectRatio(other: Region, threshold: Double): RegionTraceSubject = apply { 141 addAssertion("isSameAspectRatio($other, $componentsAsString") { 142 it.isSameAspectRatio(other, threshold) 143 } 144 } 145 } 146