1 /*
2  * Copyright (C) 2024 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.io.Reader
20 import android.tools.io.RunStatus
21 import android.tools.withTracing
22 
23 /**
24  * Helper class to run an assertions
25  *
26  * @param resultReader helper class to read the flicker artifact
27  * @param innerRunner helper class to run the assertion
28  */
29 abstract class BaseAssertionRunner(
30     private val resultReader: Reader,
31     private val innerRunner: AssertionRunner = ReaderAssertionRunner(resultReader)
<lambda>null32 ) : AssertionRunner by innerRunner {
33     protected abstract fun doUpdateStatus(newStatus: RunStatus)
34 
35     override fun runAssertion(assertion: AssertionData): Throwable? =
36         innerRunner.runAssertion(assertion).also { updateResultStatus(it) }
37 
38     private fun updateResultStatus(error: Throwable?) {
39         val newStatus =
40             if (error == null) RunStatus.ASSERTION_SUCCESS else RunStatus.ASSERTION_FAILED
41 
42         if (resultReader.isFailure || resultReader.runStatus == newStatus) return
43 
44         withTracing("${this::class.simpleName}#updateResultStatus") { doUpdateStatus(newStatus) }
45     }
46 }
47