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.legacy.runner 18 19 import android.app.Instrumentation 20 import android.tools.Scenario 21 import android.tools.flicker.junit.Utils 22 import android.tools.flicker.legacy.FlickerTestData 23 import android.tools.traces.io.ResultWriter 24 import android.tools.traces.parsers.WindowManagerStateHelper 25 import android.tools.withTracing 26 import org.junit.rules.TestRule 27 import org.junit.runner.Description 28 import org.junit.runners.model.Statement 29 30 /** 31 * Test rule to run transition setup and teardown 32 * 33 * @param flicker test definition 34 * @param resultWriter to write 35 * @param scenario to run the transition 36 * @param instrumentation to interact with the device 37 * @param setupCommands to run before the transition 38 * @param teardownCommands to run after the transition 39 * @param wmHelper to stabilize the UI before/after transitions 40 */ 41 class SetupTeardownRule( 42 private val flicker: FlickerTestData, 43 private val resultWriter: ResultWriter, 44 private val scenario: Scenario, 45 private val instrumentation: Instrumentation, 46 private val setupCommands: List<FlickerTestData.() -> Any> = flicker.transitionSetup, 47 private val teardownCommands: List<FlickerTestData.() -> Any> = flicker.transitionTeardown, 48 private val wmHelper: WindowManagerStateHelper = flicker.wmHelper 49 ) : TestRule { applynull50 override fun apply(base: Statement?, description: Description?): Statement { 51 return object : Statement() { 52 override fun evaluate() { 53 try { 54 doRunTransitionSetup(description) 55 base?.evaluate() 56 } finally { 57 doRunTransitionTeardown(description) 58 } 59 } 60 } 61 } 62 doRunTransitionSetupnull63 private fun doRunTransitionSetup(description: Description?) { 64 withTracing("doRunTransitionSetup") { 65 Utils.notifyRunnerProgress(scenario, "Running transition setup for $description") 66 setupCommands.forEach { it.invoke(flicker) } 67 Utils.doWaitForUiStabilize(wmHelper) 68 } 69 } 70 doRunTransitionTeardownnull71 private fun doRunTransitionTeardown(description: Description?) { 72 withTracing("doRunTransitionTeardown") { 73 Utils.notifyRunnerProgress(scenario, "Running transition teardown for $description") 74 teardownCommands.forEach { it.invoke(flicker) } 75 Utils.doWaitForUiStabilize(wmHelper) 76 } 77 } 78 } 79