1 /* <lambda>null2 * Copyright (C) 2024 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 18 19 import android.tools.Rotation 20 import android.tools.Timestamp 21 import android.tools.flicker.assertions.ScenarioAssertion 22 import android.tools.flicker.assertions.ScenarioAssertionImpl 23 import android.tools.flicker.config.FlickerConfigEntry 24 import android.tools.flicker.extractors.TraceSlice 25 import android.tools.flicker.extractors.Utils 26 import android.tools.io.Reader 27 import android.tools.traces.events.ICujType 28 import android.tools.traces.wm.Transition 29 import android.tools.withTracing 30 31 import java.lang.IllegalStateException 32 33 data class ScenarioInstanceImpl 34 @JvmOverloads 35 constructor( 36 override val config: FlickerConfigEntry, 37 override val startRotation: Rotation, 38 override val endRotation: Rotation, 39 val startTimestamp: Timestamp, 40 val endTimestamp: Timestamp, 41 override val reader: Reader, 42 val associatedCuj: ICujType? = null, 43 override val associatedTransition: Transition? = null, 44 ) : ScenarioInstance { 45 // b/227752705 46 override val navBarMode 47 get() = error("Unsupported") 48 49 override val key = "${config.scenarioId.name}_${startRotation}_$endRotation" 50 51 override val description = key 52 53 override val isEmpty = false 54 55 override fun <T> getConfigValue(key: String): T? = null 56 57 override fun generateAssertions(): Collection<ScenarioAssertion> = 58 withTracing("generateAssertions") { 59 val assertionExtraData = 60 mutableMapOf<String, String>().apply { 61 this["Scenario Start"] = startTimestamp.toString() 62 this["Scenario End"] = endTimestamp.toString() 63 this["Associated CUJ"] = associatedCuj.toString() 64 this["Associated Transition"] = associatedTransition.toString() 65 } 66 67 config.assertions.map { (template, stabilityGroup) -> 68 ScenarioAssertionImpl( 69 template.qualifiedAssertionName(this), 70 reader, 71 template.createAssertions(this), 72 stabilityGroup, 73 assertionExtraData, 74 ) 75 } 76 } 77 78 override fun toString() = key 79 80 companion object { 81 @JvmStatic 82 fun fromSlice( 83 traceSlice: TraceSlice, 84 reader: Reader, 85 config: FlickerConfigEntry, 86 ): ScenarioInstanceImpl { 87 try { 88 val layersTrace = reader.readLayersTrace() ?: error("Missing layers trace") 89 val startTimestamp = traceSlice.startTimestamp 90 val endTimestamp = traceSlice.endTimestamp 91 92 val displayAtStart = 93 Utils.getOnDisplayFor( 94 layersTrace.getFirstEntryWithOnDisplayAfter(startTimestamp)) 95 val displayAtEnd = 96 Utils.getOnDisplayFor(layersTrace.getLastEntryWithOnDisplayBefore(endTimestamp)) 97 98 return ScenarioInstanceImpl( 99 config, 100 startRotation = displayAtStart.transform.getRotation(), 101 endRotation = displayAtEnd.transform.getRotation(), 102 startTimestamp = startTimestamp, 103 endTimestamp = endTimestamp, 104 associatedCuj = traceSlice.associatedCuj, 105 associatedTransition = traceSlice.associatedTransition, 106 reader = reader.slice(startTimestamp, endTimestamp), 107 ) 108 } catch (e: Exception) { 109 throw IllegalStateException( 110 "Failed to create scenario (${config.scenarioId.name}) instance from slice: " 111 + traceSlice, 112 e, 113 ) 114 } 115 } 116 } 117 } 118