1 /* <lambda>null2 * Copyright (C) 2021 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 com.android.server.wm.flicker.traces.region 18 19 import com.android.server.wm.flicker.assertions.FlickerSubject 20 import com.android.server.wm.flicker.traces.FlickerFailureStrategy 21 import com.android.server.wm.flicker.traces.FlickerTraceSubject 22 import com.android.server.wm.traces.common.FlickerComponentName 23 import com.android.server.wm.traces.common.Rect 24 import com.android.server.wm.traces.common.region.Region 25 import com.android.server.wm.traces.common.region.RegionTrace 26 import com.google.common.truth.FailureMetadata 27 import com.google.common.truth.StandardSubjectBuilder 28 import com.google.common.truth.Subject.Factory 29 30 class RegionTraceSubject( 31 fm: FailureMetadata, 32 val trace: RegionTrace, 33 override val parent: FlickerSubject? 34 ) : FlickerTraceSubject<RegionSubject>(fm, trace) { 35 36 private val components: Array<out FlickerComponentName> = trace.components 37 38 override val subjects by lazy { 39 trace.entries.map { RegionSubject.assertThat(it, this, it.timestamp) } 40 } 41 42 private val componentsAsString get() = 43 if (components.isEmpty()) { 44 "<any>" 45 } else { 46 "[" + components.joinToString() + "]" 47 } 48 49 /** 50 * Asserts that the visible area covered by any element in the state covers at most 51 * [testRegion], that is, if the area of no elements cover any point outside of [testRegion]. 52 * 53 * @param testRegion Expected covered area 54 */ 55 fun coversAtMost( 56 testRegion: Region 57 ): RegionTraceSubject = apply { 58 addAssertion("coversAtMost($testRegion, $componentsAsString") { 59 it.coversAtMost(testRegion) 60 } 61 } 62 63 /** 64 * Asserts that the visible area covered by any element in the state covers at most 65 * [testRegion], that is, if the area of no elements cover any point outside of [testRegion]. 66 * 67 * @param testRegion Expected covered area 68 */ 69 fun coversAtMost( 70 testRegion: Rect 71 ): RegionTraceSubject = this.coversAtMost(testRegion) 72 73 /** 74 * Asserts that the visible area covered by any element in the state covers at least 75 * [testRegion], that is, if the area of its elements visible region covers each point in 76 * the region. 77 * 78 * @param testRegion Expected covered area 79 */ 80 fun coversAtLeast( 81 testRegion: Region 82 ): RegionTraceSubject = apply { 83 addAssertion("coversAtLeast($testRegion, $componentsAsString)") { 84 it.coversAtLeast(testRegion) 85 } 86 } 87 88 /** 89 * Asserts that the visible area covered by any element in the state covers at least 90 * [testRegion], that is, if the area of its elements visible region covers each point in 91 * the region. 92 * 93 * @param testRegion Expected covered area 94 */ 95 fun coversAtLeast( 96 testRegion: Rect 97 ): RegionTraceSubject = this.coversAtLeast(Region.from(testRegion)) 98 99 /** 100 * Asserts that the visible region of the trace entries is exactly [expectedVisibleRegion]. 101 * 102 * @param expectedVisibleRegion Expected visible region of the layer 103 */ 104 fun coversExactly( 105 expectedVisibleRegion: Region 106 ): RegionTraceSubject = apply { 107 addAssertion("coversExactly($expectedVisibleRegion, $componentsAsString)") { 108 it.coversExactly(expectedVisibleRegion) 109 } 110 } 111 112 companion object { 113 /** 114 * Boiler-plate Subject.Factory for RegionTraceSubject 115 */ 116 private fun getFactory( 117 parent: FlickerSubject? 118 ): Factory<RegionTraceSubject, RegionTrace> = 119 Factory { fm, subject -> RegionTraceSubject(fm, subject, parent) } 120 121 /** 122 * Creates a [RegionTraceSubject] representing a trace of the visible region of a 123 * window or layer which can be used to make assertions. 124 * 125 * @param trace The region trace to assert on 126 */ 127 @JvmStatic 128 @JvmOverloads 129 fun assertThat( 130 trace: RegionTrace, 131 parent: FlickerSubject? = null 132 ): RegionTraceSubject { 133 val strategy = FlickerFailureStrategy() 134 val subject = StandardSubjectBuilder.forCustomFailureStrategy(strategy) 135 .about(getFactory(parent)) 136 .that(trace) as RegionTraceSubject 137 strategy.init(subject) 138 return subject 139 } 140 141 /** 142 * Static method for getting the subject factory (for use with assertAbout()) 143 */ 144 @JvmStatic 145 fun entries( 146 parent: FlickerSubject? 147 ): Factory<RegionTraceSubject, RegionTrace> = getFactory(parent) 148 } 149 }