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 18 19 import android.tools.flicker.AssertionInvocationGroup 20 import android.tools.flicker.assertors.AssertionId 21 import android.tools.flicker.assertors.AssertionTemplate 22 import android.tools.flicker.extractors.ScenarioExtractor 23 24 internal class FlickerConfigImpl : FlickerConfig { 25 private val registry = mutableMapOf<ScenarioId, FlickerConfigEntry>() 26 27 override fun use(flickerConfigEntries: Collection<FlickerConfigEntry>): FlickerConfig = apply { 28 for (config in flickerConfigEntries) { 29 if (!config.enabled) { 30 continue 31 } 32 registerScenario(config.scenarioId, config.extractor, config.assertions) 33 } 34 } 35 36 override fun registerScenario( 37 scenario: ScenarioId, 38 extractor: ScenarioExtractor, 39 assertions: Map<AssertionTemplate, AssertionInvocationGroup>?, 40 ) = apply { 41 require(!registry.containsKey(scenario)) { 42 "${this::class.simpleName} already has a registered scenario with name '$scenario'." 43 } 44 45 registry[scenario] = FlickerConfigEntry(scenario, extractor, assertions ?: emptyMap()) 46 } 47 48 override fun unregisterScenario(scenario: ScenarioId) = apply { 49 val entry = registry[scenario] ?: error("No scenario named '$scenario' registered.") 50 registry.remove(scenario) 51 } 52 53 override fun registerAssertions( 54 scenario: ScenarioId, 55 vararg assertions: AssertionTemplate, 56 stabilityGroup: AssertionInvocationGroup, 57 ) = apply { 58 val entry = registry[scenario] 59 require(entry != null) { "No scenario named '$scenario' registered." } 60 61 assertions.forEach { assertion -> 62 require(entry.assertions.keys.none { it.id == assertion.id }) { 63 "Assertion with id '${assertion.id.name}' already present for scenario " + 64 "'${scenario.name}'." 65 } 66 67 registry[scenario] = 68 FlickerConfigEntry( 69 entry.scenarioId, 70 entry.extractor, 71 entry.assertions.toMutableMap().apply { this[assertion] = stabilityGroup }, 72 entry.enabled, 73 ) 74 } 75 } 76 77 override fun getEntries(): Collection<FlickerConfigEntry> { 78 return registry.values 79 } 80 81 override fun overrideAssertionStabilityGroup( 82 scenario: ScenarioId, 83 assertionId: AssertionId, 84 stabilityGroup: AssertionInvocationGroup, 85 ) = apply { 86 val entry = registry[scenario] 87 require(entry != null) { "No scenario named '$scenario' registered." } 88 89 val targetAssertion = 90 entry.assertions.keys.firstOrNull { it.id == assertionId } 91 ?: error( 92 "No assertion with id $assertionId present in registry for scenario $scenario." 93 ) 94 95 registry[scenario] = 96 FlickerConfigEntry( 97 entry.scenarioId, 98 entry.extractor, 99 entry.assertions.toMutableMap().apply { this[targetAssertion] = stabilityGroup }, 100 entry.enabled, 101 ) 102 } 103 104 override fun unregisterAssertion(scenario: ScenarioId, assertionId: AssertionId) = apply { 105 val entry = registry[scenario] 106 require(entry != null) { "No scenario named '$scenario' registered." } 107 108 val targetAssertion = 109 entry.assertions.keys.firstOrNull { it.id == assertionId } 110 ?: error( 111 "No assertion with id $assertionId present in registry for scenario $scenario." 112 ) 113 114 registry[scenario] = 115 FlickerConfigEntry( 116 entry.scenarioId, 117 entry.extractor, 118 entry.assertions.toMutableMap().apply { this.remove(targetAssertion) }, 119 entry.enabled, 120 ) 121 } 122 } 123