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.tools.Scenario
20 import android.tools.ScenarioBuilder
21 import android.tools.ScenarioImpl
22 import android.tools.flicker.assertions.AssertionData
23 import android.tools.flicker.assertions.AssertionRunner
24 import android.tools.flicker.assertions.BaseFlickerTest
25 import android.tools.flicker.assertions.SubjectsParser
26 import android.tools.flicker.datastore.CachedAssertionRunner
27 import android.tools.flicker.datastore.CachedResultReader
28 import android.tools.io.Reader
29 import android.tools.traces.TRACE_CONFIG_REQUIRE_CHANGES
30 
31 /** Specification of a flicker test for JUnit ParameterizedRunner class */
32 data class LegacyFlickerTest(
33     private val scenarioBuilder: ScenarioBuilder = ScenarioBuilder(),
<lambda>null34     private val resultReaderProvider: (Scenario) -> Reader = {
35         android.tools.flicker.datastore.CachedResultReader(it, TRACE_CONFIG_REQUIRE_CHANGES)
36     },
<lambda>null37     private val subjectsParserProvider: (Reader) -> SubjectsParser = { SubjectsParser(it) },
<lambda>null38     private val runnerProvider: (Scenario) -> AssertionRunner = {
39         val reader = resultReaderProvider(it)
40         android.tools.flicker.datastore.CachedAssertionRunner(it, reader)
41     }
42 ) : BaseFlickerTest() {
43     var scenario: ScenarioImpl = ScenarioBuilder().createEmptyScenario() as ScenarioImpl
44         private set
45 
toStringnull46     override fun toString(): String = scenario.toString()
47 
48     fun initialize(testClass: String): Scenario {
49         scenario = scenarioBuilder.forClass(testClass).build() as ScenarioImpl
50         return scenario
51     }
52 
53     /** Obtains a reader for the flicker result artifact */
54     val reader: Reader
55         get() = resultReaderProvider(scenario)
56 
doProcessnull57     override fun doProcess(assertion: AssertionData) {
58         require(!scenario.isEmpty) { "Scenario shouldn't be empty" }
59         runnerProvider.invoke(scenario).runAssertion(assertion)?.let { throw it }
60     }
61 }
62