• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.CujType
28 import android.tools.traces.wm.Transition
29 import android.tools.withTracing
30 
31 data class ScenarioInstanceImpl(
32     override val config: FlickerConfigEntry,
33     override val startRotation: Rotation,
34     override val endRotation: Rotation,
35     val startTimestamp: Timestamp,
36     val endTimestamp: Timestamp,
37     override val reader: Reader,
38     val associatedCuj: CujType? = null,
39     override val associatedTransition: Transition? = null,
40 ) : ScenarioInstance {
41     // b/227752705
42     override val navBarMode
43         get() = error("Unsupported")
44 
45     override val key = "${config.scenarioId.name}_${startRotation}_$endRotation"
46 
47     override val description = key
48 
49     override val isEmpty = false
50 
51     override fun <T> getConfigValue(key: String): T? = null
52 
53     override fun generateAssertions(): Collection<ScenarioAssertion> =
54         withTracing("generateAssertions") {
55             val assertionExtraData =
56                 mutableMapOf<String, String>().apply {
57                     this["Scenario Start"] = startTimestamp.toString()
58                     this["Scenario End"] = endTimestamp.toString()
59                     this["Associated CUJ"] = associatedCuj.toString()
60                     this["Associated Transition"] = associatedTransition.toString()
61                 }
62 
63             config.assertions.map { (template, stabilityGroup) ->
64                 ScenarioAssertionImpl(
65                     template.qualifiedAssertionName(this),
66                     reader,
67                     template.createAssertions(this),
68                     stabilityGroup,
69                     assertionExtraData
70                 )
71             }
72         }
73 
74     override fun toString() = key
75 
76     companion object {
77         fun fromSlice(
78             traceSlice: TraceSlice,
79             reader: Reader,
80             config: FlickerConfigEntry
81         ): ScenarioInstanceImpl {
82             val layersTrace = reader.readLayersTrace() ?: error("Missing layers trace")
83             val startTimestamp = traceSlice.startTimestamp
84             val endTimestamp = traceSlice.endTimestamp
85 
86             val displayAtStart =
87                 Utils.getOnDisplayFor(layersTrace.getFirstEntryWithOnDisplayAfter(startTimestamp))
88             val displayAtEnd =
89                 Utils.getOnDisplayFor(layersTrace.getLastEntryWithOnDisplayBefore(endTimestamp))
90 
91             return ScenarioInstanceImpl(
92                 config,
93                 startRotation = displayAtStart.transform.getRotation(),
94                 endRotation = displayAtEnd.transform.getRotation(),
95                 startTimestamp = startTimestamp,
96                 endTimestamp = endTimestamp,
97                 associatedCuj = traceSlice.associatedCuj,
98                 associatedTransition = traceSlice.associatedTransition,
99                 reader = reader.slice(startTimestamp, endTimestamp)
100             )
101         }
102     }
103 }
104