• 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.common.flicker.assertors
18 
19 import android.tools.common.flicker.ScenarioInstance
20 import android.tools.common.flicker.assertions.AssertionData
21 import android.tools.common.flicker.assertions.FlickerTest
22 import android.tools.common.flicker.assertions.ServiceFlickerTest
23 import android.tools.common.flicker.assertions.SubjectsParser
24 
25 /** Base class for a FaaS assertion */
26 abstract class AssertionTemplate(name: String? = null) {
27     protected open val name =
28         this::class.simpleName
29             ?: name ?: error("Must provide a name to assertions when using anonymous classes.")
30     val id
31         get() = AssertionId(name)
32 
qualifiedAssertionNamenull33     fun qualifiedAssertionName(scenarioInstance: ScenarioInstance): String =
34         "${scenarioInstance.type}::$name"
35 
36     /** Evaluates assertions */
37     abstract fun doEvaluate(scenarioInstance: ScenarioInstance, flicker: FlickerTest)
38 
39     fun createAssertions(scenarioInstance: ScenarioInstance): Collection<AssertionData> {
40         val flicker = ServiceFlickerTest()
41 
42         val mainBlockAssertions = mutableListOf<AssertionData>()
43         try {
44             doEvaluate(scenarioInstance, flicker)
45         } catch (e: Throwable) {
46             // Any failure that occurred outside of the Flicker assertion blocks
47             mainBlockAssertions.add(
48                 object : AssertionData {
49                     override fun checkAssertion(run: SubjectsParser) {
50                         throw e
51                     }
52                 }
53             )
54         }
55 
56         return flicker.assertions + mainBlockAssertions
57     }
58 
equalsnull59     override fun equals(other: Any?): Boolean {
60         if (other == null) {
61             return false
62         }
63         // Ensure both assertions are instances of the same class.
64         return this::class == other::class
65     }
66 
hashCodenull67     override fun hashCode(): Int {
68         return id.hashCode()
69     }
70 }
71 
72 data class Assertion(
73     val flickerAssertions: List<AssertionData>,
74     val genericAssertions: List<Throwable>
75 )
76