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.junit
18 
19 import android.app.Instrumentation
20 import android.device.collectors.util.SendToInstrumentation
21 import android.os.Bundle
22 import android.tools.Cache
23 import android.tools.flicker.AssertionInvocationGroup
24 import android.tools.flicker.FlickerServiceResultsCollector.Companion.getKeyForAssertionResult
25 import android.tools.flicker.assertions.AssertionResult
26 import android.tools.flicker.assertions.ScenarioAssertion
27 import java.lang.reflect.Method
28 import org.junit.Assume
29 import org.junit.runner.Description
30 
31 class FlickerServiceCachedTestCase(
32     private val assertion: ScenarioAssertion,
33     method: Method,
34     private val skipNonBlocking: Boolean,
35     private val isLast: Boolean,
36     injectedBy: IFlickerJUnitDecorator,
37     private val instrumentation: Instrumentation,
38     paramString: String = "",
39 ) : InjectedTestCase(method, "FaaS_$assertion$paramString", injectedBy) {
executenull40     override fun execute(description: Description) {
41         try {
42             val result = assertion.execute()
43 
44             if (result.status == AssertionResult.Status.ASSUMPTION_VIOLATION) {
45                 throw result.assumptionViolations.first()
46             }
47 
48             val metricBundle = Bundle()
49 
50             metricBundle.putString(
51                 getKeyForAssertionResult(result),
52                 if (result.status == AssertionResult.Status.PASS) "0" else "1"
53             )
54             SendToInstrumentation.sendBundle(instrumentation, metricBundle)
55 
56             Assume.assumeTrue(
57                 "FaaS Test was non blocking - skipped",
58                 !skipNonBlocking || result.stabilityGroup == AssertionInvocationGroup.BLOCKING
59             )
60             result.assertionErrors.firstOrNull()?.let { throw it }
61         } catch (e: Throwable) {
62             throw e
63         } finally {
64             if (isLast) {
65                 Cache.clear()
66             }
67         }
68     }
69 }
70