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.datastore
18 
19 import android.tools.Scenario
20 import android.tools.flicker.ScenarioInstance
21 import android.tools.flicker.assertions.ScenarioAssertion
22 import android.tools.traces.io.IResultData
23 import androidx.annotation.VisibleForTesting
24 
25 /** In memory data store for flicker transitions, assertions and results */
26 object DataStore {
27     private var cachedResults = mutableMapOf<Scenario, IResultData>()
28     private var cachedFlickerServiceAssertions =
29         mutableMapOf<Scenario, Map<ScenarioInstance, Collection<ScenarioAssertion>>>()
30 
31     data class Backup(
32         val cachedResults: MutableMap<Scenario, IResultData>,
33         val cachedFlickerServiceAssertions:
34             MutableMap<Scenario, Map<ScenarioInstance, Collection<ScenarioAssertion>>>
35     )
36 
37     @VisibleForTesting
clearnull38     fun clear() {
39         android.tools.flicker.datastore.DataStore.cachedResults = mutableMapOf()
40         android.tools.flicker.datastore.DataStore.cachedFlickerServiceAssertions = mutableMapOf()
41     }
42 
backupnull43     fun backup(): android.tools.flicker.datastore.DataStore.Backup {
44         return android.tools.flicker.datastore.DataStore.Backup(
45             android.tools.flicker.datastore.DataStore.cachedResults.toMutableMap(),
46             android.tools.flicker.datastore.DataStore.cachedFlickerServiceAssertions.toMutableMap()
47         )
48     }
49 
restorenull50     fun restore(backup: android.tools.flicker.datastore.DataStore.Backup) {
51         android.tools.flicker.datastore.DataStore.cachedResults = backup.cachedResults
52         android.tools.flicker.datastore.DataStore.cachedFlickerServiceAssertions =
53             backup.cachedFlickerServiceAssertions
54     }
55 
56     /** @return if the store has results for [scenario] */
containsResultnull57     fun containsResult(scenario: Scenario): Boolean =
58         android.tools.flicker.datastore.DataStore.cachedResults.containsKey(scenario)
59 
60     /**
61      * Adds [result] to the store with [scenario] as id
62      *
63      * @throws IllegalStateException is [scenario] already exists in the data store
64      */
65     fun addResult(scenario: Scenario, result: IResultData) {
66         require(!android.tools.flicker.datastore.DataStore.containsResult(scenario)) {
67             "Result for $scenario already in data store"
68         }
69         android.tools.flicker.datastore.DataStore.cachedResults[scenario] = result
70     }
71 
72     /**
73      * Replaces the old value [scenario] result in the store by [newResult]
74      *
75      * @throws IllegalStateException is [scenario] doesn't exist in the data store
76      */
replaceResultnull77     fun replaceResult(scenario: Scenario, newResult: IResultData) {
78         if (!android.tools.flicker.datastore.DataStore.containsResult(scenario)) {
79             error("Result for $scenario not in data store")
80         }
81         android.tools.flicker.datastore.DataStore.cachedResults[scenario] = newResult
82     }
83 
84     /**
85      * @return the result for [scenario]
86      * @throws IllegalStateException is [scenario] doesn't exist in the data store
87      */
getResultnull88     fun getResult(scenario: Scenario): IResultData =
89         android.tools.flicker.datastore.DataStore.cachedResults[scenario]
90             ?: error("No value for $scenario")
91 
92     /** @return if the store has results for [scenario] */
93     fun containsFlickerServiceResult(scenario: Scenario): Boolean =
94         android.tools.flicker.datastore.DataStore.cachedFlickerServiceAssertions.containsKey(
95             scenario
96         )
97 
98     fun addFlickerServiceAssertions(
99         scenario: Scenario,
100         groupedAssertions: Map<ScenarioInstance, Collection<ScenarioAssertion>>
101     ) {
102         if (android.tools.flicker.datastore.DataStore.containsFlickerServiceResult(scenario)) {
103             error("Result for $scenario already in data store")
104         }
105         android.tools.flicker.datastore.DataStore.cachedFlickerServiceAssertions[scenario] =
106             groupedAssertions
107     }
108 
getFlickerServiceAssertionsnull109     fun getFlickerServiceAssertions(
110         scenario: Scenario
111     ): Map<ScenarioInstance, Collection<ScenarioAssertion>> {
112         return android.tools.flicker.datastore.DataStore.cachedFlickerServiceAssertions[scenario]
113             ?: error("No flicker service results for $scenario")
114     }
115 }
116