Skip to content

Commit

Permalink
Improve FakeClock and AutoAdvancingFakeClock
Browse files Browse the repository at this point in the history
Issue: #4904
PiperOrigin-RevId: 337047518
  • Loading branch information
christosts authored and ojw28 committed Oct 17, 2020
1 parent 41b58d5 commit f00584b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 5 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,16 +20,28 @@

/**
* {@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.
*
* <p>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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,14 +18,24 @@
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;
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.
*
* <p>All timed messages sent by a {@link #createHandler(Looper, Callback) Handler} created from
* this clock are governed by the clock's time.
*
* <p>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<Long> wakeUpTimes;
Expand Down Expand Up @@ -57,6 +67,7 @@ public FakeClock(long bootTimeMs, long initialTimeMs) {
this.timeSinceBootMs = initialTimeMs;
this.wakeUpTimes = new ArrayList<>();
this.handlerMessages = new ArrayList<>();
SystemClock.setCurrentTimeMillis(initialTimeMs);
}

/**
Expand All @@ -66,6 +77,7 @@ public FakeClock(long bootTimeMs, long initialTimeMs) {
*/
public synchronized void advanceTime(long timeDiffMs) {
timeSinceBootMs += timeDiffMs;
SystemClock.setCurrentTimeMillis(timeSinceBootMs);
for (Long wakeUpTime : wakeUpTimes) {
if (wakeUpTime <= timeSinceBootMs) {
notifyAll();
Expand Down

0 comments on commit f00584b

Please sign in to comment.