• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.device.flicker.assertions
18 
19 import android.tools.common.flicker.assertions.AssertionData
20 import android.tools.common.flicker.assertions.AssertionRunner
21 import android.tools.common.flicker.assertions.ReaderAssertionRunner
22 import android.tools.common.io.Reader
23 import android.tools.common.io.RunStatus
24 
25 /**
26  * Helper class to run an assertions
27  *
28  * @param resultReader helper class to read the flicker artifact
29  * @param innerRunner helper class to run the assertion
30  */
31 abstract class BaseAssertionRunner(
32     private val resultReader: Reader,
33     private val innerRunner: AssertionRunner = ReaderAssertionRunner(resultReader)
<lambda>null34 ) : AssertionRunner by innerRunner {
35     protected abstract fun doUpdateStatus(newStatus: RunStatus)
36 
37     override fun runAssertion(assertion: AssertionData): Throwable? =
38         innerRunner.runAssertion(assertion).also { updateResultStatus(it) }
39 
40     private fun updateResultStatus(error: Throwable?) {
41         val newStatus =
42             if (error == null) RunStatus.ASSERTION_SUCCESS else RunStatus.ASSERTION_FAILED
43 
44         if (resultReader.isFailure || resultReader.runStatus == newStatus) return
45 
46         doUpdateStatus(newStatus)
47     }
48 }
49