• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.assertions
18 
19 import android.tools.common.Tag
20 import android.tools.common.flicker.subject.FlickerSubject
21 import android.tools.common.flicker.subject.events.EventLogSubject
22 import android.tools.common.flicker.subject.layers.LayerTraceEntrySubject
23 import android.tools.common.flicker.subject.layers.LayersTraceSubject
24 import android.tools.common.flicker.subject.wm.WindowManagerStateSubject
25 import android.tools.common.flicker.subject.wm.WindowManagerTraceSubject
26 import android.tools.common.io.Reader
27 import android.tools.common.traces.events.FocusEvent
28 import android.tools.common.traces.surfaceflinger.LayerTraceEntry
29 import android.tools.common.traces.surfaceflinger.LayersTrace
30 import android.tools.common.traces.wm.WindowManagerState
31 import android.tools.common.traces.wm.WindowManagerTrace
32 import kotlin.reflect.KClass
33 
34 /**
35  * Helper class to read traces from a [resultReader] and parse them into subjects for assertion
36  *
37  * @param resultReader to read the result artifacts
38  */
39 open class SubjectsParser(private val resultReader: Reader) {
getSubjectOfTypenull40     fun getSubjectOfType(
41         tag: String,
42         expectedSubjectClass: KClass<out FlickerSubject>
43     ): FlickerSubject? {
44         return when {
45             tag == Tag.ALL && expectedSubjectClass == WindowManagerTraceSubject::class ->
46                 wmTraceSubject
47             tag == Tag.ALL && expectedSubjectClass == LayersTraceSubject::class ->
48                 layersTraceSubject
49             expectedSubjectClass == EventLogSubject::class -> eventLogSubject
50             expectedSubjectClass == WindowManagerStateSubject::class -> getWmStateSubject(tag)
51             expectedSubjectClass == LayerTraceEntrySubject::class -> getLayerTraceEntrySubject(tag)
52             else -> error("Unknown expected subject type $expectedSubjectClass")
53         }
54     }
55 
56     /** Truth subject that corresponds to a [WindowManagerTrace] */
57     private val wmTraceSubject: WindowManagerTraceSubject?
58         get() = doGetWmTraceSubject()
59 
doGetWmTraceSubjectnull60     protected open fun doGetWmTraceSubject(): WindowManagerTraceSubject? {
61         val trace = resultReader.readWmTrace() ?: return null
62         return WindowManagerTraceSubject(trace, resultReader)
63     }
64 
65     /** Truth subject that corresponds to a [LayersTrace] */
66     private val layersTraceSubject: LayersTraceSubject?
67         get() = doGetLayersTraceSubject()
68 
doGetLayersTraceSubjectnull69     protected open fun doGetLayersTraceSubject(): LayersTraceSubject? {
70         val trace = resultReader.readLayersTrace() ?: return null
71         return LayersTraceSubject(trace, resultReader)
72     }
73 
74     /** Truth subject that corresponds to a [WindowManagerState] */
getWmStateSubjectnull75     private fun getWmStateSubject(tag: String): WindowManagerStateSubject? =
76         doGetWmStateSubject(tag)
77 
78     protected open fun doGetWmStateSubject(tag: String): WindowManagerStateSubject? {
79         return when (tag) {
80             Tag.START -> wmTraceSubject?.subjects?.firstOrNull()
81             Tag.END -> wmTraceSubject?.subjects?.lastOrNull()
82             else -> {
83                 val trace = resultReader.readWmState(tag) ?: return null
84                 WindowManagerStateSubject(trace.entries.first(), resultReader)
85             }
86         }
87     }
88 
89     /** Truth subject that corresponds to a [LayerTraceEntry] */
getLayerTraceEntrySubjectnull90     private fun getLayerTraceEntrySubject(tag: String): LayerTraceEntrySubject? =
91         doGetLayerTraceEntrySubject(tag)
92 
93     protected open fun doGetLayerTraceEntrySubject(tag: String): LayerTraceEntrySubject? {
94         return when (tag) {
95             Tag.START -> layersTraceSubject?.subjects?.firstOrNull()
96             Tag.END -> layersTraceSubject?.subjects?.lastOrNull()
97             else -> {
98                 val trace = resultReader.readLayersDump(tag) ?: return null
99                 return LayersTraceSubject(trace, resultReader).first()
100             }
101         }
102     }
103 
104     /** Truth subject that corresponds to a list of [FocusEvent] */
105     val eventLogSubject: EventLogSubject?
106         get() = doGetEventLogSubject()
107 
doGetEventLogSubjectnull108     protected open fun doGetEventLogSubject(): EventLogSubject? {
109         val trace = resultReader.readEventLogTrace() ?: return null
110         return EventLogSubject(trace, resultReader)
111     }
112 }
113