AnimatorTestRule


public final class AnimatorTestRule implements TestRule


JUnit TestRule that can be used to run Animators without actually waiting for the duration of the animation. This also helps the test to be written in a deterministic manner. Create an instance of AnimatorTestRule and specify it as a org.junit.ClassRule of the test class. Use advanceTimeBy to advance animators that have been started. Note that advanceTimeBy should be called from the same thread you have used to start the animator.

@SmallTest
@RunWith(AndroidJUnit4.class)
public class SampleAnimatorTest {

    @ClassRule
    public static AnimatorTestRule sAnimatorTestRule = new AnimatorTestRule();

    @UiThreadTest
    @Test
    public void sample() {
        final ValueAnimator animator = ValueAnimator.ofInt(0, 1000);
        animator.setDuration(1000L);
        assertThat(animator.getAnimatedValue(), is(0));
        animator.start();
        sAnimatorTestRule.advanceTimeBy(500L);
        assertThat(animator.getAnimatedValue(), is(500));
    }
}

Summary

Public constructors

Public methods

void
advanceTimeBy(long timeDelta)

Advances the animation clock by the given amount of delta in milliseconds.

@NonNull Statement
apply(@NonNull Statement base, @NonNull Description description)
long

Returns the current time in milliseconds tracked by AnimationHandler.

Public constructors

AnimatorTestRule

Added in 1.0.0
public AnimatorTestRule()

Public methods

advanceTimeBy

Added in 1.0.0
public void advanceTimeBy(long timeDelta)

Advances the animation clock by the given amount of delta in milliseconds. This call will produce an animation frame to all the ongoing animations. This method needs to be called on the same thread as start.

Parameters
long timeDelta

the amount of milliseconds to advance

apply

Added in 1.0.0
public @NonNull Statement apply(@NonNull Statement base, @NonNull Description description)

getCurrentTime

Added in 1.0.0
public long getCurrentTime()

Returns the current time in milliseconds tracked by AnimationHandler. Note that this is a different time than the time tracked by SystemClock This method needs to be called on the same thread as start.