1 /*
<lambda>null2  * 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.flicker.config.appclose
18 
19 import android.tools.flicker.ScenarioInstance
20 import android.tools.flicker.assertors.ComponentTemplate
21 import android.tools.flicker.isAppTransitionChange
22 import android.tools.traces.component.FullComponentIdMatcher
23 import android.tools.traces.component.IComponentMatcher
24 import android.tools.traces.surfaceflinger.LayersTrace
25 import android.tools.traces.wm.Transition
26 import android.tools.traces.wm.TransitionType
27 import android.tools.traces.wm.WindowManagerTrace
28 
29 object Components {
30     val CLOSING_APPS =
31         ComponentTemplate("CLOSING_APP(S)") { scenarioInstance: ScenarioInstance ->
32             closingAppFrom(
33                 scenarioInstance.associatedTransition ?: error("Missing associated transition"),
34                 scenarioInstance.reader.readLayersTrace(),
35                 scenarioInstance.reader.readWmTrace()
36             )
37         }
38 
39     private fun closingAppFrom(
40         transition: Transition,
41         layersTrace: LayersTrace?,
42         wmTrace: WindowManagerTrace?
43     ): IComponentMatcher {
44         val targetChanges =
45             transition.changes.filter {
46                 (it.transitMode == TransitionType.CLOSE ||
47                     it.transitMode == TransitionType.TO_BACK) &&
48                     isAppTransitionChange(it, layersTrace, wmTrace)
49             }
50 
51         val closingAppMatchers =
52             targetChanges.map { FullComponentIdMatcher(it.windowId, it.layerId) }
53 
54         return closingAppMatchers.reduce<IComponentMatcher, IComponentMatcher> { acc, matcher ->
55             acc.or(matcher)
56         }
57     }
58 }
59