• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 androidx.annotation.VisibleForTesting
20 import com.android.server.wm.flicker.traces.FlickerSubjectException
21 import com.google.common.truth.Fact
22 import com.google.common.truth.FailureMetadata
23 import com.google.common.truth.StandardSubjectBuilder
24 import com.google.common.truth.Subject
25 
26 /**
27  * Base subject for flicker assertions
28  */
29 abstract class FlickerSubject(
30     protected val fm: FailureMetadata,
31     data: Any?
32 ) : Subject(fm, data) {
33     @VisibleForTesting
34     abstract val timestamp: Long
35     protected abstract val parent: FlickerSubject?
36 
37     protected abstract val selfFacts: List<Fact>
38     val completeFacts: List<Fact> get() {
39         val facts = selfFacts.toMutableList()
<lambda>null40         parent?.run {
41             val ancestorFacts = this.completeFacts
42             facts.addAll(ancestorFacts)
43         }
44         return facts
45     }
46 
47     /**
48      * Fails an assertion on a subject
49      *
50      * @param reason for the failure
51      */
<lambda>null52     open fun fail(reason: List<Fact>): FlickerSubject = apply {
53         require(reason.isNotEmpty()) { "Failure should contain at least 1 fact" }
54         val facts = reason.drop(1).toTypedArray()
55         failWithoutActual(reason.first(), *facts)
56     }
57 
<lambda>null58     fun fail(reason: Fact, vararg rest: Fact): FlickerSubject = apply {
59         val facts = mutableListOf(reason)
60             .also { it.addAll(rest) }
61         fail(facts)
62     }
63 
64     /**
65      * Fails an assertion on a subject
66      *
67      * @param reason for the failure
68      */
<lambda>null69     fun fail(reason: Fact): FlickerSubject = apply {
70         fail(listOf(reason))
71     }
72 
73     /**
74      * Fails an assertion on a subject
75      *
76      * @param reason for the failure
77      */
<lambda>null78     fun fail(reason: String): FlickerSubject = apply {
79         fail(Fact.fact("Reason", reason))
80     }
81 
82     /**
83      * Fails an assertion on a subject
84      *
85      * @param reason for the failure
86      * @param value for the failure
87      */
<lambda>null88     fun fail(reason: String, value: Any): FlickerSubject = apply {
89         fail(Fact.fact(reason, value))
90     }
91 
92     /**
93      * Fails an assertion on a subject
94      *
95      * @param reason for the failure
96      */
failnull97     fun fail(reason: Throwable) {
98         if (reason is FlickerSubjectException) {
99             throw reason
100         } else {
101             throw FlickerSubjectException(this, reason)
102         }
103     }
104 
105     /**
106      * Function to make external assertions using the subjects
107      * Necessary because check is protected and final in the Truth library
108      */
verifynull109     fun verify(message: String): StandardSubjectBuilder = check(message)
110 
111     companion object {
112         @VisibleForTesting
113         @JvmStatic
114         val ASSERTION_TAG = "Assertion"
115     }
116 }