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.subject
18 
19 import android.tools.Timestamp
20 import android.tools.flicker.assertions.Fact
21 import android.tools.flicker.subject.exceptions.ExceptionMessageBuilder
22 import android.tools.flicker.subject.exceptions.InvalidPropertyException
23 import android.tools.io.Reader
24 
25 /** Subject for flicker checks */
26 data class CheckSubject<T>(
27     private val actualValue: T?,
28     private val timestamp: Timestamp,
29     private val extraFacts: List<Fact>,
30     private val reader: Reader?,
31     private val lazyMessage: () -> String,
32 ) {
33     private val exceptionMessageBuilder: ExceptionMessageBuilder
34         get() {
35             val builder =
36                 ExceptionMessageBuilder().setTimestamp(timestamp).addExtraDescription(extraFacts)
37             if (reader != null) {
38                 builder.setReader(reader)
39             }
40             return builder
41         }
42 
isEqualnull43     fun isEqual(expectedValue: T?) {
44         if (actualValue != expectedValue) {
45             val errorMsgBuilder =
46                 exceptionMessageBuilder
47                     .setMessage(lazyMessage.invoke())
48                     .setExpected(expectedValue)
49                     .setActual(actualValue)
50             throw InvalidPropertyException(errorMsgBuilder)
51         }
52     }
53 
isNotEqualnull54     fun isNotEqual(expectedValue: T?) {
55         if (actualValue == expectedValue) {
56             val errorMsgBuilder =
57                 exceptionMessageBuilder
58                     .setMessage(lazyMessage.invoke())
59                     .setExpected("Different from $expectedValue")
60                     .setActual(actualValue)
61             throw InvalidPropertyException(errorMsgBuilder)
62         }
63     }
64 
isNullnull65     fun isNull() {
66         if (actualValue != null) {
67             val errorMsgBuilder =
68                 exceptionMessageBuilder
69                     .setMessage(lazyMessage.invoke())
70                     .setExpected(null)
71                     .setActual(actualValue)
72             throw InvalidPropertyException(errorMsgBuilder)
73         }
74     }
75 
isNotNullnull76     fun isNotNull() {
77         if (actualValue == null) {
78             val errorMsgBuilder =
79                 exceptionMessageBuilder
80                     .setMessage(lazyMessage.invoke())
81                     .setExpected("Not null")
82                     .setActual(null)
83             throw InvalidPropertyException(errorMsgBuilder)
84         }
85     }
86 
isLowernull87     fun isLower(expectedValue: T?) {
88         if (
89             actualValue == null ||
90                 expectedValue == null ||
91                 (actualValue as Comparable<T>) >= expectedValue
92         ) {
93             val errorMsgBuilder =
94                 exceptionMessageBuilder
95                     .setMessage(lazyMessage.invoke())
96                     .setExpected("Lower than $expectedValue")
97                     .setActual(actualValue)
98             throw InvalidPropertyException(errorMsgBuilder)
99         }
100     }
101 
isLowerOrEqualnull102     fun isLowerOrEqual(expectedValue: T?) {
103         if (
104             actualValue == null ||
105                 expectedValue == null ||
106                 (actualValue as Comparable<T>) > expectedValue
107         ) {
108             val errorMsgBuilder =
109                 exceptionMessageBuilder
110                     .setMessage(lazyMessage.invoke())
111                     .setExpected("Lower or equal to $expectedValue")
112                     .setActual(actualValue)
113             throw InvalidPropertyException(errorMsgBuilder)
114         }
115     }
116 
isGreaternull117     fun isGreater(expectedValue: T) {
118         if (
119             actualValue == null ||
120                 expectedValue == null ||
121                 (actualValue as Comparable<T>) <= expectedValue
122         ) {
123             val errorMsgBuilder =
124                 exceptionMessageBuilder
125                     .setMessage(lazyMessage.invoke())
126                     .setExpected("Greater than $expectedValue")
127                     .setActual(actualValue)
128             throw InvalidPropertyException(errorMsgBuilder)
129         }
130     }
131 
isGreaterOrEqualnull132     fun isGreaterOrEqual(expectedValue: T) {
133         if (
134             actualValue == null ||
135                 expectedValue == null ||
136                 (actualValue as Comparable<T>) < expectedValue
137         ) {
138             val errorMsgBuilder =
139                 exceptionMessageBuilder
140                     .setMessage(lazyMessage.invoke())
141                     .setExpected("Greater or equal to $expectedValue")
142                     .setActual(actualValue)
143             throw InvalidPropertyException(errorMsgBuilder)
144         }
145     }
146 
containsnull147     fun <U> contains(expectedValue: U) {
148         if (actualValue !is List<*> || !(actualValue as List<U>).contains(expectedValue)) {
149             val errorMsgBuilder =
150                 exceptionMessageBuilder
151                     .setMessage(lazyMessage.invoke())
152                     .setExpected("Contain $expectedValue")
153                     .setActual(actualValue)
154             throw InvalidPropertyException(errorMsgBuilder)
155         }
156     }
157 }
158