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.assertors.assertions
18 
19 import android.tools.flicker.ScenarioInstance
20 import android.tools.flicker.assertions.FlickerTest
21 import android.tools.flicker.assertors.ComponentTemplate
22 import android.tools.traces.component.ComponentNameMatcher
23 import android.tools.traces.component.IComponentNameMatcher
24 
25 val ANY_MATCH_COMPONENT = ComponentTemplate("ANY") { _ -> ComponentNameMatcher("", "") }
26 
27 class FocusChanges(
28     private val fromComponent: ComponentTemplate = ANY_MATCH_COMPONENT,
29     private val toComponent: ComponentTemplate = ANY_MATCH_COMPONENT
30 ) : AssertionTemplateWithComponent(fromComponent, toComponent) {
31 
32     // TODO: Make parent call this when appropriate
doEvaluatenull33     override fun doEvaluate(scenarioInstance: ScenarioInstance, flicker: FlickerTest) {
34         val layersTrace = scenarioInstance.reader.readLayersTrace()
35         val fromComponent = fromComponent.build(scenarioInstance)
36         val toComponent = toComponent.build(scenarioInstance)
37 
38         val fromPackage =
39             if (fromComponent is IComponentNameMatcher) {
40                 fromComponent.packageName
41             } else {
42                 requireNotNull(layersTrace) { "Missing layers trace" }
43                 layersTrace.entries
44                     .flatMap { it.flattenedLayers }
45                     .first { fromComponent.layerMatchesAnyOf(it) }
46                     .packageName
47             }
48 
49         val toPackage =
50             if (toComponent is IComponentNameMatcher) {
51                 toComponent.packageName
52             } else {
53                 requireNotNull(layersTrace) { "Missing layers trace" }
54                 layersTrace.entries
55                     .flatMap { it.flattenedLayers }
56                     .first { toComponent.layerMatchesAnyOf(it) }
57                     .packageName
58             }
59 
60         flicker.assertEventLog { focusChanges(fromPackage, toPackage) }
61     }
62 }
63