1 /* 2 * Copyright (C) 2021 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 com.android.server.wm.flicker.assertions 18 19 import com.android.server.wm.flicker.traces.FlickerSubjectException 20 import com.google.common.truth.Fact 21 import com.google.common.truth.FailureMetadata 22 import com.google.common.truth.StandardSubjectBuilder 23 import com.google.common.truth.Subject 24 25 /** 26 * Base subject for flicker assertions 27 */ 28 abstract class FlickerSubject( 29 protected val fm: FailureMetadata, 30 data: Any? 31 ) : Subject(fm, data) { 32 abstract val defaultFacts: String clonenull33 abstract fun clone(): FlickerSubject 34 35 /** 36 * Fails an assertion on a subject 37 * 38 * @param reason for the failure 39 */ 40 open fun fail(reason: List<Fact>): FlickerSubject = apply { 41 require(reason.isNotEmpty()) { "Failure should contain at least 1 fact" } 42 val facts = reason.drop(1).toTypedArray() 43 failWithoutActual(reason.first(), *facts) 44 } 45 <lambda>null46 fun fail(reason: Fact, vararg rest: Fact): FlickerSubject = apply { 47 val facts = mutableListOf(reason) 48 .also { it.addAll(rest) } 49 fail(facts) 50 } 51 52 /** 53 * Fails an assertion on a subject 54 * 55 * @param reason for the failure 56 */ <lambda>null57 fun fail(reason: Fact): FlickerSubject = apply { 58 fail(listOf(reason)) 59 } 60 61 /** 62 * Fails an assertion on a subject 63 * 64 * @param reason for the failure 65 */ <lambda>null66 fun fail(reason: String): FlickerSubject = apply { 67 fail(Fact.fact("Reason", reason)) 68 } 69 70 /** 71 * Fails an assertion on a subject 72 * 73 * @param reason for the failure 74 * @param value for the failure 75 */ <lambda>null76 fun fail(reason: String, value: Any): FlickerSubject = apply { 77 fail(Fact.fact(reason, value)) 78 } 79 80 /** 81 * Fails an assertion on a subject 82 * 83 * @param reason for the failure 84 */ failnull85 fun fail(reason: Throwable) { 86 if (reason is FlickerSubjectException) { 87 throw reason 88 } else { 89 throw FlickerSubjectException(this, reason) 90 } 91 } 92 93 /** 94 * Function to make external assertions using the subjects 95 * Necessary because check is protected and final in the Truth library 96 */ verifynull97 fun verify(message: String): StandardSubjectBuilder = check(message) 98 }