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.assertions
18 
19 import android.tools.Tag
20 import android.tools.flicker.subject.FlickerSubject
21 import android.tools.flicker.subject.FlickerTraceSubject
22 import kotlin.reflect.KClass
23 
24 /**
25  * Helper class to create assertions to execute on a trace or state
26  *
27  * @param stateSubject Type of subject used for state assertions
28  * @param traceSubject Type of subject used for trace assertions
29  */
30 class AssertionDataFactory(
31     stateSubject: KClass<out FlickerSubject>,
32     private val traceSubject: KClass<out FlickerTraceSubject<*>>
33 ) : AssertionStateDataFactory(stateSubject) {
34 
35     /**
36      * Creates an [assertion] to be executed on trace
37      *
38      * @param assertion Assertion predicate
39      */
createTraceAssertionnull40     fun createTraceAssertion(
41         assertion: (FlickerTraceSubject<FlickerSubject>) -> Unit
42     ): AssertionData {
43         val closedAssertion: FlickerTraceSubject<FlickerSubject>.() -> Unit = {
44             require(!hasAssertions()) { "Subject was already used to execute assertions" }
45             assertion(this)
46             forAllEntries()
47         }
48         return AssertionDataImpl(
49             tag = Tag.ALL,
50             expectedSubjectClass = traceSubject,
51             assertion = closedAssertion as FlickerSubject.() -> Unit
52         )
53     }
54 }
55