diff options
| author | 2023-10-11 21:05:32 +0000 | |
|---|---|---|
| committer | 2023-10-11 21:05:32 +0000 | |
| commit | a4ccd6dbe1fde34e67910a78541a03523b324858 (patch) | |
| tree | 4bc6281fc94370f85ae66bf56cf741649763a016 | |
| parent | ea833fb12d1dee965b885fc9c42183036db1bb14 (diff) | |
| parent | 0d7d665e8e55aec434bac042a6bea71c56270e49 (diff) | |
Merge "Ensure AnimatorTestRule timing is perfectly synchronized." into main
| -rw-r--r-- | packages/SystemUI/tests/utils/src/android/animation/AnimatorTestRule.java | 21 | ||||
| -rw-r--r-- | packages/SystemUI/tests/utils/src/com/android/systemui/animation/AnimatorTestRule.kt | 19 |
2 files changed, 35 insertions, 5 deletions
diff --git a/packages/SystemUI/tests/utils/src/android/animation/AnimatorTestRule.java b/packages/SystemUI/tests/utils/src/android/animation/AnimatorTestRule.java index 41dbc147dfc5..b820ca612bfc 100644 --- a/packages/SystemUI/tests/utils/src/android/animation/AnimatorTestRule.java +++ b/packages/SystemUI/tests/utils/src/android/animation/AnimatorTestRule.java @@ -68,13 +68,26 @@ public final class AnimatorTestRule implements TestRule { private final Object mLock = new Object(); private final TestHandler mTestHandler = new TestHandler(); + private final long mStartTime; + private long mTotalTimeDelta = 0; + + /** + * Construct an AnimatorTestRule with a custom start time. + * @see #AnimatorTestRule() + */ + public AnimatorTestRule(long startTime) { + mStartTime = startTime; + } + /** - * initializing the start time with {@link SystemClock#uptimeMillis()} reduces the discrepancies - * with various internals of classes like ValueAnimator which can sometimes read that clock via + * Construct an AnimatorTestRule with a start time of {@link SystemClock#uptimeMillis()}. + * Initializing the start time with this clock reduces the discrepancies with various internals + * of classes like ValueAnimator which can sometimes read that clock via * {@link android.view.animation.AnimationUtils#currentAnimationTimeMillis()}. */ - private final long mStartTime = SystemClock.uptimeMillis(); - private long mTotalTimeDelta = 0; + public AnimatorTestRule() { + this(SystemClock.uptimeMillis()); + } @NonNull @Override diff --git a/packages/SystemUI/tests/utils/src/com/android/systemui/animation/AnimatorTestRule.kt b/packages/SystemUI/tests/utils/src/com/android/systemui/animation/AnimatorTestRule.kt index 0ced19e3d5f3..ba9c5eda1b63 100644 --- a/packages/SystemUI/tests/utils/src/com/android/systemui/animation/AnimatorTestRule.kt +++ b/packages/SystemUI/tests/utils/src/com/android/systemui/animation/AnimatorTestRule.kt @@ -28,11 +28,21 @@ import org.junit.runners.model.Statement * advanced together. */ class AnimatorTestRule : TestRule { + // Create the androidx rule, which initializes start time to SystemClock.uptimeMillis(), + // then copy that time to the platform rule so that the two clocks are in sync. private val androidxRule = androidx.core.animation.AnimatorTestRule() - private val platformRule = android.animation.AnimatorTestRule() + private val platformRule = android.animation.AnimatorTestRule(androidxRule.startTime) private val advanceAndroidXTimeBy = Consumer<Long> { timeDelta -> androidxRule.advanceTimeBy(timeDelta) } + /** Access the mStartTime field; bypassing the restriction of being on a looper thread. */ + private val androidx.core.animation.AnimatorTestRule.startTime: Long + get() = + javaClass.getDeclaredField("mStartTime").let { field -> + field.isAccessible = true + field.getLong(this) + } + /** * Chain is for simplicity not to force a particular order; order should not matter, because * each rule affects a different AnimationHandler classes, and no callbacks to code under test @@ -55,4 +65,11 @@ class AnimatorTestRule : TestRule { // animation from one to start later than the other. platformRule.advanceTimeBy(timeDelta, advanceAndroidXTimeBy) } + + /** + * Returns the current time in milliseconds tracked by the AnimationHandlers. Note that this is + * a different time than the time tracked by {@link SystemClock}. + */ + val currentTime: Long + get() = androidxRule.currentTime } |