From f00584b02aab4120f8d08266b1c2088f47691890 Mon Sep 17 00:00:00 2001 From: christosts Date: Wed, 14 Oct 2020 10:20:17 +0100 Subject: [PATCH] Improve FakeClock and AutoAdvancingFakeClock Issue: #4904 PiperOrigin-RevId: 337047518 --- .../testutil/AutoAdvancingFakeClock.java | 20 +++++++++++++++---- .../exoplayer2/testutil/FakeClock.java | 14 ++++++++++++- 2 files changed, 29 insertions(+), 5 deletions(-) diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/AutoAdvancingFakeClock.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/AutoAdvancingFakeClock.java index a7d5e132d5..7bef854ba9 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/AutoAdvancingFakeClock.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/AutoAdvancingFakeClock.java @@ -20,16 +20,28 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull; /** * {@link FakeClock} extension which automatically advances time whenever an empty message is - * enqueued at a future time. The clock time is advanced to the time of the message. Only the first - * Handler sending messages at a future time will be allowed to advance time to ensure there is only - * one "time master". This should usually be the Handler of the internal playback loop. + * enqueued at a future time. + * + *

The clock time is advanced to the time of enqueued empty messages. Only the first Handler + * sending messages at a future time will be allowed to advance time to ensure there is only one + * primary time source. This should usually be the Handler of the internal playback loop. */ public final class AutoAdvancingFakeClock extends FakeClock { private @MonotonicNonNull HandlerWrapper autoAdvancingHandler; + /** Creates the auto-advancing clock with an initial time of 0. */ public AutoAdvancingFakeClock() { - super(/* initialTimeMs= */ 0); + this(/* initialTimeMs= */ 0); + } + + /** + * Creates the auto-advancing clock. + * + * @param initialTimeMs The initial time of the clock in milliseconds. + */ + public AutoAdvancingFakeClock(long initialTimeMs) { + super(initialTimeMs); } @Override diff --git a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeClock.java b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeClock.java index ee30b90355..077b690b34 100644 --- a/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeClock.java +++ b/testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeClock.java @@ -18,6 +18,7 @@ package com.google.android.exoplayer2.testutil; import android.os.Handler.Callback; import android.os.Looper; import android.os.Message; +import android.os.SystemClock; import androidx.annotation.GuardedBy; import androidx.annotation.Nullable; import com.google.android.exoplayer2.util.Clock; @@ -25,7 +26,16 @@ import com.google.android.exoplayer2.util.HandlerWrapper; import java.util.ArrayList; import java.util.List; -/** Fake {@link Clock} implementation independent of {@link android.os.SystemClock}. */ +/** + * Fake {@link Clock} implementation that allows to {@link #advanceTime(long) advance the time} + * manually to trigger pending timed messages. + * + *

All timed messages sent by a {@link #createHandler(Looper, Callback) Handler} created from + * this clock are governed by the clock's time. + * + *

The clock also sets the time of the {@link SystemClock} to match the {@link #elapsedRealtime() + * clock's time}. + */ public class FakeClock implements Clock { private final List wakeUpTimes; @@ -57,6 +67,7 @@ public class FakeClock implements Clock { this.timeSinceBootMs = initialTimeMs; this.wakeUpTimes = new ArrayList<>(); this.handlerMessages = new ArrayList<>(); + SystemClock.setCurrentTimeMillis(initialTimeMs); } /** @@ -66,6 +77,7 @@ public class FakeClock implements Clock { */ public synchronized void advanceTime(long timeDiffMs) { timeSinceBootMs += timeDiffMs; + SystemClock.setCurrentTimeMillis(timeSinceBootMs); for (Long wakeUpTime : wakeUpTimes) { if (wakeUpTime <= timeSinceBootMs) { notifyAll();