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