1 /* <lambda>null2 * 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.FLICKER_TAG 20 import android.tools.flicker.AssertionInvocationGroup 21 import android.tools.flicker.subject.exceptions.FlickerAssertionError 22 import android.tools.io.Reader 23 import android.tools.withTracing 24 import android.util.Log 25 import org.junit.AssumptionViolatedException 26 27 // internal data class but visible for testing 28 data class ScenarioAssertionImpl( 29 private val name: String, 30 private val reader: Reader, 31 private val assertionData: Collection<AssertionData>, 32 override val stabilityGroup: AssertionInvocationGroup, 33 private val assertionExtraData: Map<String, String>, 34 private val assertionRunner: AssertionRunner = ReaderAssertionRunner(reader) 35 ) : ScenarioAssertion { 36 init { 37 require(assertionData.isNotEmpty()) { "Expected at least one assertion data object." } 38 } 39 40 override fun execute(): AssertionResult = 41 withTracing("executeAssertion") { 42 val assertionExceptions = assertionData.map { assertionRunner.runAssertion(it) } 43 44 require( 45 assertionExceptions.all { 46 it == null || it is FlickerAssertionError || it is AssumptionViolatedException 47 } 48 ) { 49 "Expected all assertion exceptions to be " + 50 "FlickerAssertionErrors or AssumptionViolatedExceptions" 51 } 52 53 val assertionErrors = assertionExceptions.filterIsInstance<FlickerAssertionError>() 54 assertionErrors.forEach { assertion -> 55 assertionExtraData.forEach { (key, value) -> 56 assertion.messageBuilder.addExtraDescription(key, value) 57 } 58 } 59 60 AssertionResultImpl( 61 name, 62 assertionData, 63 assumptionViolations = 64 assertionExceptions.filterIsInstance<AssumptionViolatedException>(), 65 assertionErrors, 66 stabilityGroup 67 ) 68 .also { log(it) } 69 } 70 71 override fun toString() = name 72 73 private fun log(result: AssertionResult) { 74 when (result.status) { 75 AssertionResult.Status.ASSUMPTION_VIOLATION -> 76 Log.w( 77 "$FLICKER_TAG-SERVICE", 78 "${result.name} ASSUMPTION VIOLATION :: " + 79 result.assumptionViolations.map { it.message } 80 ) 81 AssertionResult.Status.PASS -> Log.d("$FLICKER_TAG-SERVICE", "${result.name} PASSED") 82 AssertionResult.Status.FAIL -> 83 Log.e( 84 "$FLICKER_TAG-SERVICE", 85 "${result.name} FAILED :: " + result.assertionErrors.map { it.message } 86 ) 87 } 88 } 89 } 90