|
| 1 | +/* |
| 2 | + * Copyright 2026 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 com.google.android.material.animation; |
| 17 | + |
| 18 | +import static com.google.common.truth.Truth.assertThat; |
| 19 | +import static org.mockito.Mockito.mock; |
| 20 | +import static org.mockito.Mockito.never; |
| 21 | +import static org.mockito.Mockito.verify; |
| 22 | + |
| 23 | +import android.animation.ValueAnimator; |
| 24 | +import android.content.Context; |
| 25 | +import android.view.View; |
| 26 | +import androidx.dynamicanimation.animation.SpringAnimation; |
| 27 | +import androidx.dynamicanimation.animation.SpringForce; |
| 28 | +import androidx.test.core.app.ApplicationProvider; |
| 29 | +import org.junit.Before; |
| 30 | +import org.junit.Test; |
| 31 | +import org.junit.runner.RunWith; |
| 32 | +import org.robolectric.RobolectricTestRunner; |
| 33 | +import org.robolectric.shadows.ShadowLooper; |
| 34 | + |
| 35 | +@RunWith(RobolectricTestRunner.class) |
| 36 | +public class AnimationCoordinatorTest { |
| 37 | + |
| 38 | + private Context context; |
| 39 | + private AnimationCoordinator coordinator; |
| 40 | + private AnimationCoordinator.Listener mockListener; |
| 41 | + |
| 42 | + @Before |
| 43 | + public void setUp() { |
| 44 | + context = ApplicationProvider.getApplicationContext(); |
| 45 | + coordinator = new AnimationCoordinator(); |
| 46 | + mockListener = mock(AnimationCoordinator.Listener.class); |
| 47 | + coordinator.addListener(mockListener); |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + public void start_noAnimations_callsListenersImmediately() { |
| 52 | + coordinator.start(); |
| 53 | + ShadowLooper.idleMainLooper(); |
| 54 | + |
| 55 | + verify(mockListener).onAnimationsStart(); |
| 56 | + verify(mockListener).onAnimationsEnd(); |
| 57 | + } |
| 58 | + |
| 59 | + @Test |
| 60 | + public void start_onlyDurationAnimations_callsListeners() { |
| 61 | + ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f); |
| 62 | + animator.setDuration(100); |
| 63 | + coordinator.addAnimator(animator); |
| 64 | + |
| 65 | + coordinator.start(); |
| 66 | + ShadowLooper.idleMainLooper(); |
| 67 | + |
| 68 | + verify(mockListener).onAnimationsStart(); |
| 69 | + verify(mockListener).onAnimationsEnd(); |
| 70 | + } |
| 71 | + |
| 72 | + @Test |
| 73 | + public void start_onlySpringAnimations_callsListeners() { |
| 74 | + View testView = new View(context); |
| 75 | + SpringAnimation springAnimation = |
| 76 | + new SpringAnimation(testView, SpringAnimation.ALPHA) |
| 77 | + .setSpring(new SpringForce(1f).setStiffness(0.01f).setDampingRatio(1f)); |
| 78 | + coordinator.addDynamicAnimation(springAnimation); |
| 79 | + |
| 80 | + coordinator.start(); |
| 81 | + ShadowLooper.idleMainLooper(); |
| 82 | + |
| 83 | + verify(mockListener).onAnimationsStart(); |
| 84 | + verify(mockListener).onAnimationsEnd(); |
| 85 | + } |
| 86 | + |
| 87 | + @Test |
| 88 | + public void start_mixedAnimations_callsListeners() { |
| 89 | + ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f); |
| 90 | + animator.setDuration(100); |
| 91 | + coordinator.addAnimator(animator); |
| 92 | + |
| 93 | + View testView = new View(context); |
| 94 | + SpringAnimation springAnimation = |
| 95 | + new SpringAnimation(testView, SpringAnimation.ALPHA) |
| 96 | + .setSpring(new SpringForce(1f).setStiffness(0.01f).setDampingRatio(1f)); |
| 97 | + coordinator.addDynamicAnimation(springAnimation); |
| 98 | + |
| 99 | + coordinator.start(); |
| 100 | + ShadowLooper.idleMainLooper(); |
| 101 | + |
| 102 | + verify(mockListener).onAnimationsStart(); |
| 103 | + verify(mockListener).onAnimationsEnd(); |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + public void start_calledTwice_onlyRunsOnce() { |
| 108 | + ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f); |
| 109 | + animator.setDuration(100); |
| 110 | + coordinator.addAnimator(animator); |
| 111 | + |
| 112 | + coordinator.start(); |
| 113 | + coordinator.start(); // Second call should be ignored |
| 114 | + ShadowLooper.idleMainLooper(); |
| 115 | + |
| 116 | + verify(mockListener).onAnimationsStart(); |
| 117 | + verify(mockListener).onAnimationsEnd(); |
| 118 | + } |
| 119 | + |
| 120 | + @Test |
| 121 | + public void removeListener_preventsCallbacks() { |
| 122 | + coordinator.removeListener(mockListener); |
| 123 | + coordinator.start(); |
| 124 | + ShadowLooper.idleMainLooper(); |
| 125 | + |
| 126 | + verify(mockListener, never()).onAnimationsStart(); |
| 127 | + verify(mockListener, never()).onAnimationsEnd(); |
| 128 | + } |
| 129 | + |
| 130 | + @Test |
| 131 | + public void clear_removesAnimationsAndListeners() { |
| 132 | + ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f); |
| 133 | + animator.setDuration(100); |
| 134 | + coordinator.addAnimator(animator); |
| 135 | + |
| 136 | + coordinator.clear(); |
| 137 | + coordinator.start(); |
| 138 | + ShadowLooper.idleMainLooper(); |
| 139 | + |
| 140 | + verify(mockListener, never()).onAnimationsStart(); |
| 141 | + verify(mockListener, never()).onAnimationsEnd(); |
| 142 | + } |
| 143 | + |
| 144 | + @Test |
| 145 | + public void clear_whileRunning_cancelsAndJumpsToEnd() { |
| 146 | + ValueAnimator animator = ValueAnimator.ofFloat(0f, 1f); |
| 147 | + animator.setDuration(100); |
| 148 | + coordinator.addAnimator(animator); |
| 149 | + |
| 150 | + View testView = new View(context); |
| 151 | + SpringAnimation springAnimation = |
| 152 | + new SpringAnimation(testView, SpringAnimation.ALPHA) |
| 153 | + .setSpring(new SpringForce(1f).setStiffness(0.01f).setDampingRatio(1f)); |
| 154 | + coordinator.addDynamicAnimation(springAnimation); |
| 155 | + |
| 156 | + coordinator.start(); |
| 157 | + |
| 158 | + // Verify animations are running |
| 159 | + assertThat(animator.isRunning()).isTrue(); |
| 160 | + assertThat(springAnimation.isRunning()).isTrue(); |
| 161 | + |
| 162 | + coordinator.clear(); |
| 163 | + ShadowLooper.idleMainLooper(); |
| 164 | + |
| 165 | + // Verify animations are stopped and jumped to end |
| 166 | + assertThat(animator.isRunning()).isFalse(); |
| 167 | + assertThat((float) animator.getAnimatedValue()).isEqualTo(1f); |
| 168 | + assertThat(springAnimation.isRunning()).isFalse(); |
| 169 | + |
| 170 | + // Verify listeners are not called after clear |
| 171 | + ShadowLooper.idleMainLooper(); |
| 172 | + verify(mockListener).onAnimationsStart(); |
| 173 | + verify(mockListener, never()).onAnimationsEnd(); |
| 174 | + } |
| 175 | +} |
0 commit comments