# AnimatorTestRule

# AnimatorTestRule

Artifact:[androidx.core:core-animation-testing](https://developer.android.com/jetpack/androidx/releases/core)  
[View Source](https://cs.android.com/search?q=file:androidx/core/animation/AnimatorTestRule.java+class:androidx.core.animation.AnimatorTestRule)  
Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/core#1.0.0)

*** ** * ** ***

Kotlin\|[Java](https://developer.android.com/reference/androidx/core/animation/AnimatorTestRule "View this page in Java")

<br />

```
class AnimatorTestRule : TestRule
```

<br />

*** ** * ** ***

JUnit[TestRule](https://junit.org/junit4/javadoc/4.12/org/junit/rules/TestRule.html)that can be used to run[Animator](https://developer.android.com/reference/kotlin/androidx/core/animation/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[org.junit.ClassRule](https://junit.org/junit4/javadoc/4.12/org/junit/ClassRule.html)of the test class. Use[advanceTimeBy](https://developer.android.com/reference/kotlin/androidx/core/animation/AnimatorTestRule#advanceTimeBy(long))to advance animators that have been started. Note that[advanceTimeBy](https://developer.android.com/reference/kotlin/androidx/core/animation/AnimatorTestRule#advanceTimeBy(long))should be called from the same thread you have used to start the animator.  

```kotlin
@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                                                       |
|------------------------------------------------------------------------------------------------------------------------------------|
| [AnimatorTestRule](https://developer.android.com/reference/kotlin/androidx/core/animation/AnimatorTestRule#AnimatorTestRule())`()` |

|                                   ### Public functions                                    |
|-------------------------------------------------------------------------------------------|------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|
| [Unit](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-unit/index.html)             | [advanceTimeBy](https://developer.android.com/reference/kotlin/androidx/core/animation/AnimatorTestRule#advanceTimeBy(long))`(timeDelta: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)`)` Advances the animation clock by the given amount of delta in milliseconds.                                                                              |
| [Statement](https://junit.org/junit4/javadoc/4.12/org/junit/runners/model/Statement.html) | [apply](https://developer.android.com/reference/kotlin/androidx/core/animation/AnimatorTestRule#apply(org.junit.runners.model.Statement,org.junit.runner.Description))`(base: `[Statement](https://junit.org/junit4/javadoc/4.12/org/junit/runners/model/Statement.html)`, description: `[Description](https://junit.org/junit4/javadoc/4.12/org/junit/runner/Description.html)`)` |
| [Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html)             | [getCurrentTime](https://developer.android.com/reference/kotlin/androidx/core/animation/AnimatorTestRule#getCurrentTime())`()` Returns the current time in milliseconds tracked by AnimationHandler.                                                                                                                                                                               |

## Public constructors

### AnimatorTestRule

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/core#1.0.0)  

```
AnimatorTestRule()
```  

## Public functions

### advanceTimeBy

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/core#1.0.0)  

```
fun advanceTimeBy(timeDelta: Long): Unit
```

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](https://developer.android.com/reference/kotlin/androidx/core/animation/Animator#start()).  

|                                         Parameters                                         |
|--------------------------------------------------------------------------------------------|---------------------------------------|
| `timeDelta: `[Long](https://kotlinlang.org/api/core/kotlin-stdlib/kotlin/-long/index.html) | the amount of milliseconds to advance |

### apply

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/core#1.0.0)  

```
fun apply(base: Statement, description: Description): Statement
```  

### getCurrentTime

Added in[1.0.0](https://developer.android.com/jetpack/androidx/releases/core#1.0.0)  

```
fun getCurrentTime(): Long
```

Returns the current time in milliseconds tracked by AnimationHandler. Note that this is a different time than the time tracked by[SystemClock](https://developer.android.com/reference/android/os/SystemClock.html)This method needs to be called on the same thread as[start](https://developer.android.com/reference/kotlin/androidx/core/animation/Animator#start()).