1 package android.animation; 2 3 import static com.google.common.truth.Truth.assertThat; 4 5 import android.animation.Animator.AnimatorListener; 6 import androidx.test.core.app.ActivityScenario; 7 import androidx.test.espresso.Espresso; 8 import androidx.test.ext.junit.runners.AndroidJUnit4; 9 import java.lang.reflect.Modifier; 10 import java.util.concurrent.CountDownLatch; 11 import java.util.concurrent.TimeUnit; 12 import org.junit.Test; 13 import org.junit.runner.RunWith; 14 import org.robolectric.testapp.ActivityWithoutTheme; 15 16 /** Compatibility test for animation-related logic. */ 17 @RunWith(AndroidJUnit4.class) 18 public final class AnimationTest { 19 20 @Test propertyValuesHolder()21 public void propertyValuesHolder() throws Exception { 22 PropertyValuesHolder pVHolder = PropertyValuesHolder.ofFloat("x", 100f, 150f); 23 PropertyBag object = new PropertyBag(); 24 assertThat(Modifier.isPrivate(PropertyBag.class.getModifiers())).isTrue(); 25 ObjectAnimator objAnimator = ObjectAnimator.ofPropertyValuesHolder(object, pVHolder); 26 objAnimator.setDuration(500); 27 AnimatorSet animatorSet = new AnimatorSet(); 28 animatorSet.play(objAnimator); 29 30 CountDownLatch countDownLatch = new CountDownLatch(1); 31 objAnimator.addListener( 32 new AnimatorListener() { 33 @Override 34 public void onAnimationEnd(Animator a) { 35 countDownLatch.countDown(); 36 } 37 38 @Override 39 public void onAnimationStart(Animator a) {} 40 41 @Override 42 public void onAnimationCancel(Animator a) {} 43 44 @Override 45 public void onAnimationRepeat(Animator a) {} 46 }); 47 48 // In Android animations can only run on Looper threads. 49 try (ActivityScenario<ActivityWithoutTheme> scenario = 50 ActivityScenario.launch(ActivityWithoutTheme.class)) { 51 scenario.onActivity( 52 activity -> { 53 animatorSet.start(); 54 }); 55 } 56 57 Espresso.onIdle(); 58 countDownLatch.await(5, TimeUnit.SECONDS); 59 assertThat(object.x).isEqualTo(150f); 60 } 61 62 /** Private class with a public member. */ 63 @SuppressWarnings("unused") 64 private static class PropertyBag { 65 public float x; 66 setX(float x)67 public void setX(float x) { 68 this.x = x; 69 } 70 } 71 } 72