1 /* 2 * Copyright (C) 2021 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 com.android.server.wm.flicker 18 19 import org.junit.rules.TestRule 20 import org.junit.runner.Description 21 import org.junit.runners.model.Statement 22 23 /** 24 * Execute the transitions of a flicker test using JUnit rules and statements. 25 * 26 * Allow for easier reuse of test rules 27 */ 28 class TransitionRunnerWithRules(private val setupRules: TestRule) : TransitionRunner() { 29 private var result: FlickerResult? = null 30 buildTransitionRulenull31 private fun buildTransitionRule(flicker: Flicker): Statement { 32 return object : Statement() { 33 override fun evaluate() { 34 result = runTransition(flicker) 35 } 36 } 37 } 38 runTransitionnull39 private fun runTransition(flicker: Flicker): FlickerResult { 40 return super.run(flicker) 41 } 42 buildTransitionChainnull43 private fun buildTransitionChain(flicker: Flicker): Statement { 44 val transitionRule = buildTransitionRule(flicker) 45 return setupRules.apply(transitionRule, Description.EMPTY) 46 } 47 cleanUpnull48 override fun cleanUp() { 49 super.cleanUp() 50 result = null 51 } 52 53 /** 54 * Runs the actual setup, transitions and teardown defined in [flicker] 55 * 56 * @param flicker test specification 57 */ runnull58 override fun run(flicker: Flicker): FlickerResult { 59 try { 60 val transitionChain = buildTransitionChain(flicker) 61 transitionChain.evaluate() 62 return result ?: error("Transition did not run") 63 } finally { 64 cleanUp() 65 } 66 } 67 } 68