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