• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * 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.legacy
18 
19 import android.annotation.SuppressLint
20 import android.tools.CleanFlickerEnvironmentRuleWithDataStore
21 import android.tools.ScenarioBuilder
22 import android.tools.flicker.assertions.FlickerTest
23 import android.tools.io.TraceType
24 import android.tools.newTestCachedResultWriter
25 import android.tools.testutils.TEST_SCENARIO
26 import android.tools.testutils.TestTraces
27 import android.tools.testutils.assertExceptionMessage
28 import android.tools.testutils.assertThrows
29 import android.tools.traces.TRACE_CONFIG_REQUIRE_CHANGES
30 import android.tools.traces.io.ResultReader
31 import com.google.common.truth.Truth
32 import java.io.File
33 import kotlin.io.path.name
34 import org.junit.Before
35 import org.junit.Rule
36 import org.junit.Test
37 
38 /** Tests for [FlickerTest] */
39 @SuppressLint("VisibleForTests")
40 class LegacyFlickerTestTest {
41     private var executionCount = 0
42     @Rule @JvmField val envCleanup = CleanFlickerEnvironmentRuleWithDataStore()
43 
44     @Before
setupnull45     fun setup() {
46         executionCount = 0
47     }
48 
49     @Test
failsWithoutScenarionull50     fun failsWithoutScenario() {
51         val actual = LegacyFlickerTest()
52         val failure =
53             assertThrows<IllegalArgumentException> { actual.assertLayers { executionCount++ } }
54         assertExceptionMessage(failure, "Scenario shouldn't be empty")
55         Truth.assertWithMessage("Executed").that(executionCount).isEqualTo(0)
56     }
57 
58     @Test
executesLayersnull59     fun executesLayers() {
60         val predicate: (FlickerTest) -> Unit = { it.assertLayers { executionCount++ } }
61         doWriteTraceExecuteAssertionAndVerify(
62             TraceType.SF,
63             predicate,
64             TestTraces.LayerTrace.FILE,
65             expectedExecutionCount = 2,
66         )
67     }
68 
69     @Test
executesLayerStartnull70     fun executesLayerStart() {
71         val predicate: (FlickerTest) -> Unit = { it.assertLayersStart { executionCount++ } }
72         doWriteTraceExecuteAssertionAndVerify(
73             TraceType.SF,
74             predicate,
75             TestTraces.LayerTrace.FILE,
76             expectedExecutionCount = 2,
77         )
78     }
79 
80     @Test
executesLayerEndnull81     fun executesLayerEnd() {
82         val predicate: (FlickerTest) -> Unit = { it.assertLayersEnd { executionCount++ } }
83         doWriteTraceExecuteAssertionAndVerify(
84             TraceType.SF,
85             predicate,
86             TestTraces.LayerTrace.FILE,
87             expectedExecutionCount = 2,
88         )
89     }
90 
91     @Test
doesNotExecuteLayersWithoutTracenull92     fun doesNotExecuteLayersWithoutTrace() {
93         val predicate: (FlickerTest) -> Unit = { it.assertLayers { executionCount++ } }
94         doExecuteAssertionWithoutTraceAndVerifyNotExecuted(TraceType.SF, predicate)
95     }
96 
97     @Test
doesNotExecuteLayersStartWithoutTracenull98     fun doesNotExecuteLayersStartWithoutTrace() {
99         val predicate: (FlickerTest) -> Unit = { it.assertLayersStart { executionCount++ } }
100         doExecuteAssertionWithoutTraceAndVerifyNotExecuted(TraceType.SF, predicate)
101     }
102 
103     @Test
doesNotExecuteLayersEndWithoutTracenull104     fun doesNotExecuteLayersEndWithoutTrace() {
105         val predicate: (FlickerTest) -> Unit = { it.assertLayersEnd { executionCount++ } }
106         doExecuteAssertionWithoutTraceAndVerifyNotExecuted(TraceType.SF, predicate)
107     }
108 
109     @Test
doesNotExecuteLayerTagWithoutTagnull110     fun doesNotExecuteLayerTagWithoutTag() {
111         val predicate: (FlickerTest) -> Unit = { it.assertLayersTag("tag") { executionCount++ } }
112         doExecuteAssertionWithoutTraceAndVerifyNotExecuted(TraceType.SF, predicate)
113     }
114 
115     @Test
executesWmnull116     fun executesWm() {
117         val predicate: (FlickerTest) -> Unit = { it.assertWm { executionCount++ } }
118         doWriteTraceExecuteAssertionAndVerify(
119             if (android.tracing.Flags.perfettoWmTracing()) TraceType.PERFETTO else TraceType.WM,
120             predicate,
121             if (android.tracing.Flags.perfettoWmTracing()) TestTraces.WMTrace.FILE
122             else TestTraces.LegacyWMTrace.FILE,
123             expectedExecutionCount = 2,
124         )
125     }
126 
127     @Test
executesWmStartnull128     fun executesWmStart() {
129         val predicate: (FlickerTest) -> Unit = { it.assertWmStart { executionCount++ } }
130         doWriteTraceExecuteAssertionAndVerify(
131             if (android.tracing.Flags.perfettoWmTracing()) TraceType.PERFETTO else TraceType.WM,
132             predicate,
133             if (android.tracing.Flags.perfettoWmTracing()) TestTraces.WMTrace.FILE
134             else TestTraces.LegacyWMTrace.FILE,
135             expectedExecutionCount = 2,
136         )
137     }
138 
139     @Test
executesWmEndnull140     fun executesWmEnd() {
141         val predicate: (FlickerTest) -> Unit = { it.assertWmEnd { executionCount++ } }
142         doWriteTraceExecuteAssertionAndVerify(
143             if (android.tracing.Flags.perfettoWmTracing()) TraceType.PERFETTO else TraceType.WM,
144             predicate,
145             if (android.tracing.Flags.perfettoWmTracing()) TestTraces.WMTrace.FILE
146             else TestTraces.LegacyWMTrace.FILE,
147             expectedExecutionCount = 2,
148         )
149     }
150 
151     @Test
doesNotExecuteWmWithoutTracenull152     fun doesNotExecuteWmWithoutTrace() {
153         val predicate: (FlickerTest) -> Unit = { it.assertWm { executionCount++ } }
154         doExecuteAssertionWithoutTraceAndVerifyNotExecuted(
155             if (android.tracing.Flags.perfettoWmTracing()) TraceType.PERFETTO else TraceType.WM,
156             predicate,
157         )
158     }
159 
160     @Test
doesNotExecuteWmStartWithoutTracenull161     fun doesNotExecuteWmStartWithoutTrace() {
162         val predicate: (FlickerTest) -> Unit = { it.assertWmStart { executionCount++ } }
163         doExecuteAssertionWithoutTraceAndVerifyNotExecuted(
164             if (android.tracing.Flags.perfettoWmTracing()) TraceType.PERFETTO else TraceType.WM,
165             predicate,
166         )
167     }
168 
169     @Test
doesNotExecuteWmEndWithoutTracenull170     fun doesNotExecuteWmEndWithoutTrace() {
171         val predicate: (FlickerTest) -> Unit = { it.assertWmEnd { executionCount++ } }
172         doExecuteAssertionWithoutTraceAndVerifyNotExecuted(
173             if (android.tracing.Flags.perfettoWmTracing()) TraceType.PERFETTO else TraceType.WM,
174             predicate,
175         )
176     }
177 
178     @Test
doesNotExecuteWmTagWithoutTagnull179     fun doesNotExecuteWmTagWithoutTag() {
180         val predicate: (FlickerTest) -> Unit = { it.assertWmTag("tag") { executionCount++ } }
181         doWriteTraceExecuteAssertionAndVerify(
182             if (android.tracing.Flags.perfettoWmTracing()) TraceType.PERFETTO else TraceType.WM,
183             predicate,
184             if (android.tracing.Flags.perfettoWmTracing()) TestTraces.WMTrace.FILE
185             else TestTraces.LegacyWMTrace.FILE,
186             expectedExecutionCount = 0,
187         )
188     }
189 
190     @Test
executesEventLognull191     fun executesEventLog() {
192         val predicate: (FlickerTest) -> Unit = { it.assertEventLog { executionCount++ } }
193         doWriteTraceExecuteAssertionAndVerify(
194             TraceType.EVENT_LOG,
195             predicate,
196             TestTraces.EventLog.FILE,
197             expectedExecutionCount = 2,
198         )
199     }
200 
201     @Test
doesNotExecuteEventLogWithoutEventLognull202     fun doesNotExecuteEventLogWithoutEventLog() {
203         val predicate: (FlickerTest) -> Unit = { it.assertEventLog { executionCount++ } }
204         val scenarioName = kotlin.io.path.createTempFile().name
205         val scenario = ScenarioBuilder().forClass(scenarioName).build()
206         newTestCachedResultWriter(scenario).write()
207         val flickerWrapper = LegacyFlickerTest()
208         flickerWrapper.initialize(scenarioName)
209         // Each assertion is executed independently and not cached, only Flicker as a Service
210         // assertions are cached
211         predicate.invoke(flickerWrapper)
212         predicate.invoke(flickerWrapper)
213 
214         Truth.assertWithMessage("Executed").that(executionCount).isEqualTo(0)
215     }
216 
doExecuteAssertionWithoutTraceAndVerifyNotExecutednull217     private fun doExecuteAssertionWithoutTraceAndVerifyNotExecuted(
218         traceType: TraceType,
219         predicate: (FlickerTest) -> Unit,
220     ) =
221         doWriteTraceExecuteAssertionAndVerify(
222             traceType,
223             predicate,
224             file = null,
225             expectedExecutionCount = 0,
226         )
227 
228     private fun doWriteTraceExecuteAssertionAndVerify(
229         traceType: TraceType,
230         predicate: (FlickerTest) -> Unit,
231         file: File?,
232         expectedExecutionCount: Int,
233     ) {
234         val writer = newTestCachedResultWriter()
235         if (file != null) {
236             writer.addTraceResult(traceType, file)
237         }
238         writer.write()
239         val flickerWrapper =
240             LegacyFlickerTest(
241                 resultReaderProvider = {
242                     android.tools.flicker.datastore.CachedResultReader(
243                         it,
244                         TRACE_CONFIG_REQUIRE_CHANGES,
245                         reader =
246                             ResultReader(
247                                 android.tools.flicker.datastore.DataStore.getResult(it),
248                                 TRACE_CONFIG_REQUIRE_CHANGES,
249                             ),
250                     )
251                 }
252             )
253         flickerWrapper.initialize(TEST_SCENARIO.testClass)
254         // Each assertion is executed independently and not cached, only Flicker as a Service
255         // assertions are cached
256         predicate.invoke(flickerWrapper)
257         predicate.invoke(flickerWrapper)
258 
259         Truth.assertWithMessage("Executed").that(executionCount).isEqualTo(expectedExecutionCount)
260     }
261 }
262