1 /* 2 * 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 com.android.systemui.animation 18 19 import java.util.function.Consumer 20 import org.junit.rules.RuleChain 21 import org.junit.rules.TestRule 22 import org.junit.runner.Description 23 import org.junit.runners.model.Statement 24 25 /** 26 * A rule that wraps both [androidx.core.animation.AnimatorTestRule] and 27 * [android.animation.AnimatorTestRule] such that the clocks of the two animation handlers can be 28 * advanced together. 29 */ 30 class AnimatorTestRule : TestRule { 31 private val androidxRule = androidx.core.animation.AnimatorTestRule() 32 private val platformRule = android.animation.AnimatorTestRule() 33 private val advanceAndroidXTimeBy = 34 Consumer<Long> { timeDelta -> androidxRule.advanceTimeBy(timeDelta) } 35 36 /** 37 * Chain is for simplicity not to force a particular order; order should not matter, because 38 * each rule affects a different AnimationHandler classes, and no callbacks to code under test 39 * should be triggered by these rules 40 */ 41 private val ruleChain = RuleChain.emptyRuleChain().around(androidxRule).around(platformRule) 42 43 override fun apply(base: Statement, description: Description): Statement = 44 ruleChain.apply(base, description) 45 46 /** 47 * Advances the animation clock by the given amount of delta in milliseconds. This call will 48 * produce an animation frame to all the ongoing animations. 49 * 50 * @param timeDelta the amount of milliseconds to advance 51 */ 52 fun advanceTimeBy(timeDelta: Long) { 53 // NOTE: To avoid errors with order, we have to ensure that we advance the time within both 54 // rules before either rule does its frame output. Failing to do this could cause the 55 // animation from one to start later than the other. 56 platformRule.advanceTimeBy(timeDelta, advanceAndroidXTimeBy) 57 } 58 } 59