• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * 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.device.flicker.datastore
18 
19 import android.tools.common.Scenario
20 import android.tools.common.flicker.ScenarioInstance
21 import android.tools.common.flicker.assertions.ScenarioAssertion
22 import android.tools.device.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         cachedResults = mutableMapOf()
40         cachedFlickerServiceAssertions = mutableMapOf()
41     }
42 
backupnull43     fun backup(): Backup {
44         return Backup(cachedResults.toMutableMap(), cachedFlickerServiceAssertions.toMutableMap())
45     }
46 
restorenull47     fun restore(backup: Backup) {
48         cachedResults = backup.cachedResults
49         cachedFlickerServiceAssertions = backup.cachedFlickerServiceAssertions
50     }
51 
52     /** @return if the store has results for [scenario] */
containsResultnull53     fun containsResult(scenario: Scenario): Boolean = cachedResults.containsKey(scenario)
54 
55     /**
56      * Adds [result] to the store with [scenario] as id
57      *
58      * @throws IllegalStateException is [scenario] already exists in the data store
59      */
60     fun addResult(scenario: Scenario, result: IResultData) {
61         require(!containsResult(scenario)) { "Result for $scenario already in data store" }
62         cachedResults[scenario] = result
63     }
64 
65     /**
66      * Replaces the old value [scenario] result in the store by [newResult]
67      *
68      * @throws IllegalStateException is [scenario] doesn't exist in the data store
69      */
replaceResultnull70     fun replaceResult(scenario: Scenario, newResult: IResultData) {
71         if (!containsResult(scenario)) {
72             error("Result for $scenario not in data store")
73         }
74         cachedResults[scenario] = newResult
75     }
76 
77     /**
78      * @return the result for [scenario]
79      * @throws IllegalStateException is [scenario] doesn't exist in the data store
80      */
getResultnull81     fun getResult(scenario: Scenario): IResultData =
82         cachedResults[scenario] ?: error("No value for $scenario")
83 
84     /** @return if the store has results for [scenario] */
85     fun containsFlickerServiceResult(scenario: Scenario): Boolean =
86         cachedFlickerServiceAssertions.containsKey(scenario)
87 
88     fun addFlickerServiceAssertions(
89         scenario: Scenario,
90         groupedAssertions: Map<ScenarioInstance, Collection<ScenarioAssertion>>
91     ) {
92         if (containsFlickerServiceResult(scenario)) {
93             error("Result for $scenario already in data store")
94         }
95         cachedFlickerServiceAssertions[scenario] = groupedAssertions
96     }
97 
getFlickerServiceAssertionsnull98     fun getFlickerServiceAssertions(
99         scenario: Scenario
100     ): Map<ScenarioInstance, Collection<ScenarioAssertion>> {
101         return cachedFlickerServiceAssertions[scenario]
102             ?: error("No flicker service results for $scenario")
103     }
104 }
105