AnimatorTestRule
public
final
class
AnimatorTestRule
extends Object
implements
TestRule
java.lang.Object
|
↳ |
androidx.core.animation.AnimatorTestRule
|
JUnit TestRule
that can be used to run Animator
s 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 ClassRule
of the test class. Use advanceTimeBy(long)
to advance animators that have been started.
Note that advanceTimeBy(long)
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 methods |
void
|
advanceTimeBy(long timeDelta)
Advances the animation clock by the given amount of delta in milliseconds.
|
Statement
|
apply(Statement base, Description description)
|
long
|
getCurrentTime()
Returns the current time in milliseconds tracked by AnimationHandler.
|
Inherited methods |
From class
java.lang.Object
Object
|
clone()
|
boolean
|
equals(Object arg0)
|
void
|
finalize()
|
final
Class<?>
|
getClass()
|
int
|
hashCode()
|
final
void
|
notify()
|
final
void
|
notifyAll()
|
String
|
toString()
|
final
void
|
wait(long arg0, int arg1)
|
final
void
|
wait(long arg0)
|
final
void
|
wait()
|
|
From interface
org.junit.rules.TestRule
abstract
Statement
|
apply(Statement arg0, Description arg1)
|
|
Public constructors
AnimatorTestRule
public AnimatorTestRule ()
Public methods
advanceTimeBy
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 Animator.start()
.
Parameters |
timeDelta |
long : the amount of milliseconds to advance
|
apply
public Statement apply (Statement base,
Description description)
Parameters |
base |
Statement |
description |
Description |
getCurrentTime
public long getCurrentTime ()
Returns the current time in milliseconds tracked by AnimationHandler. Note that this is a
different time than the time tracked by @{link SystemClock} This method needs to be called on
the same thread as Animator.start()
.