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 val unexpectedExceptions = 45 assertionExceptions.filterNot { 46 it == null || 47 it is FlickerAssertionError || 48 it is AssumptionViolatedException || 49 it is IllegalArgumentException 50 } 51 if (unexpectedExceptions.isNotEmpty()) { 52 throw IllegalArgumentException( 53 "Expected all assertion exceptions to be " + 54 "FlickerAssertionErrors or AssumptionViolatedExceptions", 55 unexpectedExceptions.first(), 56 ) 57 } 58 59 val assertionErrors = assertionExceptions.filterIsInstance<FlickerAssertionError>() 60 assertionErrors.forEach { assertion -> 61 assertionExtraData.forEach { (key, value) -> 62 assertion.messageBuilder.addExtraDescription(key, value) 63 } 64 } 65 66 AssertionResultImpl( 67 name, 68 assertionData, 69 assumptionViolations = 70 assertionExceptions.filterIsInstance<AssumptionViolatedException>(), 71 assertionErrors, 72 stabilityGroup, 73 ) 74 .also { log(it) } 75 } 76 77 override fun toString() = name 78 79 private fun log(result: AssertionResult) { 80 when (result.status) { 81 AssertionResult.Status.ASSUMPTION_VIOLATION -> 82 Log.w( 83 "$FLICKER_TAG-SERVICE", 84 "${result.name} ASSUMPTION VIOLATION :: " + 85 result.assumptionViolations.map { it.message }, 86 ) 87 AssertionResult.Status.PASS -> Log.d("$FLICKER_TAG-SERVICE", "${result.name} PASSED") 88 AssertionResult.Status.FAIL -> 89 Log.e( 90 "$FLICKER_TAG-SERVICE", 91 "${result.name} FAILED :: " + result.assertionErrors.map { it.message }, 92 ) 93 } 94 } 95 } 96