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 package android.animation 17 18 import android.testing.AndroidTestingRunner 19 import android.testing.TestableLooper.RunWithLooper 20 import androidx.core.animation.doOnEnd 21 import androidx.test.filters.SmallTest 22 import com.android.app.animation.Interpolators 23 import com.android.systemui.SysuiTestCase 24 import com.google.common.truth.Truth.assertThat 25 import org.junit.Rule 26 import org.junit.Test 27 import org.junit.runner.RunWith 28 29 @RunWith(AndroidTestingRunner::class) 30 @SmallTest 31 @RunWithLooper 32 class AnimatorTestRulePrecisionTest : SysuiTestCase() { 33 34 @get:Rule val animatorTestRule = AnimatorTestRule(this) 35 36 var value1: Float = -1f 37 var value2: Float = -1f 38 animateThisnull39 private inline fun animateThis( 40 propertyName: String, 41 duration: Long, 42 startDelay: Long = 0, 43 crossinline onEndAction: (animator: Animator) -> Unit, 44 ) { 45 ObjectAnimator.ofFloat(this, propertyName, 0f, 1f).also { 46 it.interpolator = Interpolators.LINEAR 47 it.duration = duration 48 it.startDelay = startDelay 49 it.doOnEnd(onEndAction) 50 it.start() 51 } 52 } 53 54 @Test testSingleAnimatornull55 fun testSingleAnimator() { 56 var ended = false 57 animateThis("value1", duration = 100) { ended = true } 58 59 assertThat(value1).isEqualTo(0f) 60 assertThat(ended).isFalse() 61 assertThat(AnimationHandler.getAnimationCount()).isEqualTo(1) 62 63 animatorTestRule.advanceTimeBy(50) 64 assertThat(value1).isEqualTo(0.5f) 65 assertThat(ended).isFalse() 66 assertThat(AnimationHandler.getAnimationCount()).isEqualTo(1) 67 68 animatorTestRule.advanceTimeBy(49) 69 assertThat(value1).isEqualTo(0.99f) 70 assertThat(ended).isFalse() 71 assertThat(AnimationHandler.getAnimationCount()).isEqualTo(1) 72 73 animatorTestRule.advanceTimeBy(1) 74 assertThat(value1).isEqualTo(1f) 75 assertThat(ended).isTrue() 76 assertThat(AnimationHandler.getAnimationCount()).isEqualTo(0) 77 } 78 79 @Test testDelayedAnimatornull80 fun testDelayedAnimator() { 81 var ended = false 82 animateThis("value1", duration = 100, startDelay = 50) { ended = true } 83 84 assertThat(value1).isEqualTo(-1f) 85 assertThat(ended).isFalse() 86 assertThat(AnimationHandler.getAnimationCount()).isEqualTo(1) 87 88 animatorTestRule.advanceTimeBy(49) 89 assertThat(value1).isEqualTo(-1f) 90 assertThat(ended).isFalse() 91 assertThat(AnimationHandler.getAnimationCount()).isEqualTo(1) 92 93 animatorTestRule.advanceTimeBy(1) 94 assertThat(value1).isEqualTo(0f) 95 assertThat(ended).isFalse() 96 assertThat(AnimationHandler.getAnimationCount()).isEqualTo(1) 97 98 animatorTestRule.advanceTimeBy(99) 99 assertThat(value1).isEqualTo(0.99f) 100 assertThat(ended).isFalse() 101 assertThat(AnimationHandler.getAnimationCount()).isEqualTo(1) 102 103 animatorTestRule.advanceTimeBy(1) 104 assertThat(value1).isEqualTo(1f) 105 assertThat(ended).isTrue() 106 assertThat(AnimationHandler.getAnimationCount()).isEqualTo(0) 107 } 108 109 @Test testTwoAnimatorsnull110 fun testTwoAnimators() { 111 var ended1 = false 112 var ended2 = false 113 animateThis("value1", duration = 100) { ended1 = true } 114 animateThis("value2", duration = 200) { ended2 = true } 115 assertThat(value1).isEqualTo(0f) 116 assertThat(value2).isEqualTo(0f) 117 assertThat(ended1).isFalse() 118 assertThat(ended2).isFalse() 119 assertThat(AnimationHandler.getAnimationCount()).isEqualTo(2) 120 121 animatorTestRule.advanceTimeBy(99) 122 assertThat(value1).isEqualTo(0.99f) 123 assertThat(value2).isEqualTo(0.495f) 124 assertThat(ended1).isFalse() 125 assertThat(ended2).isFalse() 126 assertThat(AnimationHandler.getAnimationCount()).isEqualTo(2) 127 128 animatorTestRule.advanceTimeBy(1) 129 assertThat(value1).isEqualTo(1f) 130 assertThat(value2).isEqualTo(0.5f) 131 assertThat(ended1).isTrue() 132 assertThat(ended2).isFalse() 133 assertThat(AnimationHandler.getAnimationCount()).isEqualTo(1) 134 135 animatorTestRule.advanceTimeBy(99) 136 assertThat(value1).isEqualTo(1f) 137 assertThat(value2).isEqualTo(0.995f) 138 assertThat(ended1).isTrue() 139 assertThat(ended2).isFalse() 140 assertThat(AnimationHandler.getAnimationCount()).isEqualTo(1) 141 142 animatorTestRule.advanceTimeBy(1) 143 assertThat(value1).isEqualTo(1f) 144 assertThat(value2).isEqualTo(1f) 145 assertThat(ended1).isTrue() 146 assertThat(ended2).isTrue() 147 assertThat(AnimationHandler.getAnimationCount()).isEqualTo(0) 148 } 149 150 @Test testChainedAnimatorsnull151 fun testChainedAnimators() { 152 var ended1 = false 153 var ended2 = false 154 animateThis("value1", duration = 100) { 155 ended1 = true 156 animateThis("value2", duration = 100) { ended2 = true } 157 } 158 159 assertThat(value1).isEqualTo(0f) 160 assertThat(value2).isEqualTo(-1f) 161 assertThat(ended1).isFalse() 162 assertThat(ended2).isFalse() 163 assertThat(AnimationHandler.getAnimationCount()).isEqualTo(1) 164 165 animatorTestRule.advanceTimeBy(99) 166 assertThat(value1).isEqualTo(0.99f) 167 assertThat(value2).isEqualTo(-1f) 168 assertThat(ended1).isFalse() 169 assertThat(ended2).isFalse() 170 assertThat(AnimationHandler.getAnimationCount()).isEqualTo(1) 171 172 animatorTestRule.advanceTimeBy(1) 173 assertThat(value1).isEqualTo(1f) 174 assertThat(value2).isEqualTo(0f) 175 assertThat(ended1).isTrue() 176 assertThat(ended2).isFalse() 177 assertThat(AnimationHandler.getAnimationCount()).isEqualTo(1) 178 179 animatorTestRule.advanceTimeBy(99) 180 assertThat(value1).isEqualTo(1f) 181 assertThat(value2).isEqualTo(0.99f) 182 assertThat(ended1).isTrue() 183 assertThat(ended2).isFalse() 184 assertThat(AnimationHandler.getAnimationCount()).isEqualTo(1) 185 186 animatorTestRule.advanceTimeBy(1) 187 assertThat(value1).isEqualTo(1f) 188 assertThat(value2).isEqualTo(1f) 189 assertThat(ended1).isTrue() 190 assertThat(ended2).isTrue() 191 assertThat(AnimationHandler.getAnimationCount()).isEqualTo(0) 192 } 193 } 194