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.systemui.SysuiTestCase
23 import com.google.common.truth.Truth.assertThat
24 import org.junit.Rule
25 import org.junit.Test
26 import org.junit.runner.RunWith
27 
28 /**
29  * This test class validates that two tests' animators are isolated from each other when using the
30  * same animator test rule. This is a test to prevent future instances of b/275602127.
31  */
32 @RunWith(AndroidTestingRunner::class)
33 @SmallTest
34 @RunWithLooper
35 class AnimatorTestRuleIsolationTest : SysuiTestCase() {
36 
37     @get:Rule val animatorTestRule = AnimatorTestRule(this)
38 
39     @Test
testAnull40     fun testA() {
41         // GIVEN global state is reset at the start of the test
42         didTouchA = false
43         didTouchB = false
44         // WHEN starting 2 animations of different durations, and setting didTouchA at the end
45         ObjectAnimator.ofFloat(0f, 1f).apply {
46             duration = 100
47             doOnEnd { didTouchA = true }
48             start()
49         }
50         ObjectAnimator.ofFloat(0f, 1f).apply {
51             duration = 150
52             doOnEnd { didTouchA = true }
53             start()
54         }
55         // WHEN when you advance time so that only one of the animations has ended
56         animatorTestRule.advanceTimeBy(100)
57         // VERIFY we did indeed end the current animation
58         assertThat(didTouchA).isTrue()
59         // VERIFY advancing the animator did NOT cause testB's animator to end
60         assertThat(didTouchB).isFalse()
61     }
62 
63     @Test
testBnull64     fun testB() {
65         // GIVEN global state is reset at the start of the test
66         didTouchA = false
67         didTouchB = false
68         // WHEN starting 2 animations of different durations, and setting didTouchB at the end
69         ObjectAnimator.ofFloat(0f, 1f).apply {
70             duration = 100
71             doOnEnd { didTouchB = true }
72             start()
73         }
74         ObjectAnimator.ofFloat(0f, 1f).apply {
75             duration = 150
76             doOnEnd { didTouchB = true }
77             start()
78         }
79         animatorTestRule.advanceTimeBy(100)
80         // VERIFY advancing the animator did NOT cause testA's animator to end
81         assertThat(didTouchA).isFalse()
82         // VERIFY we did indeed end the current animation
83         assertThat(didTouchB).isTrue()
84     }
85 
86     companion object {
87         var didTouchA = false
88         var didTouchB = false
89     }
90 }
91