• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.annotation.internal.DoNotInstrument;
15 import org.robolectric.testapp.ActivityWithoutTheme;
16 
17 /** Compatibility test for animation-related logic. */
18 @DoNotInstrument
19 @RunWith(AndroidJUnit4.class)
20 public final class AnimationTest {
21 
22   @Test
propertyValuesHolder()23   public void propertyValuesHolder() throws Exception {
24     PropertyValuesHolder pVHolder = PropertyValuesHolder.ofFloat("x", 100f, 150f);
25     PropertyBag object = new PropertyBag();
26     assertThat(Modifier.isPrivate(PropertyBag.class.getModifiers())).isTrue();
27     ObjectAnimator objAnimator = ObjectAnimator.ofPropertyValuesHolder(object, pVHolder);
28     objAnimator.setDuration(500);
29     AnimatorSet animatorSet = new AnimatorSet();
30     animatorSet.play(objAnimator);
31 
32     CountDownLatch countDownLatch = new CountDownLatch(1);
33     objAnimator.addListener(
34         new AnimatorListener() {
35           @Override
36           public void onAnimationEnd(Animator a) {
37             countDownLatch.countDown();
38           }
39 
40           @Override
41           public void onAnimationStart(Animator a) {}
42 
43           @Override
44           public void onAnimationCancel(Animator a) {}
45 
46           @Override
47           public void onAnimationRepeat(Animator a) {}
48         });
49 
50     // In Android animations can only run on Looper threads.
51     try (ActivityScenario<ActivityWithoutTheme> scenario =
52         ActivityScenario.launch(ActivityWithoutTheme.class)) {
53       scenario.onActivity(
54           activity -> {
55             animatorSet.start();
56           });
57     }
58 
59     Espresso.onIdle();
60     countDownLatch.await(5, TimeUnit.SECONDS);
61     assertThat(object.x).isEqualTo(150f);
62   }
63 
64   /** Private class with a public member. */
65   @DoNotInstrument
66   @SuppressWarnings("unused")
67   private static class PropertyBag {
68     public float x;
69 
setX(float x)70     public void setX(float x) {
71       this.x = x;
72     }
73   }
74 }
75