Commit f00584b0 by christosts Committed by Oliver Woodman

Improve FakeClock and AutoAdvancingFakeClock

Issue: #4904
PiperOrigin-RevId: 337047518
parent 41b58d50
......@@ -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.
*
* <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
......
......@@ -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.
*
* <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;
......@@ -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();
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or sign in to comment