Improve FakeClock and AutoAdvancingFakeClock
Issue: #4904 PiperOrigin-RevId: 337047518
This commit is contained in:
parent
41b58d503a
commit
f00584b02a
@ -20,16 +20,28 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
|
|||||||
|
|
||||||
/**
|
/**
|
||||||
* {@link FakeClock} extension which automatically advances time whenever an empty message is
|
* {@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
|
* enqueued at a future time.
|
||||||
* 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.
|
* <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 {
|
public final class AutoAdvancingFakeClock extends FakeClock {
|
||||||
|
|
||||||
private @MonotonicNonNull HandlerWrapper autoAdvancingHandler;
|
private @MonotonicNonNull HandlerWrapper autoAdvancingHandler;
|
||||||
|
|
||||||
|
/** Creates the auto-advancing clock with an initial time of 0. */
|
||||||
public AutoAdvancingFakeClock() {
|
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
|
@Override
|
||||||
|
@ -18,6 +18,7 @@ package com.google.android.exoplayer2.testutil;
|
|||||||
import android.os.Handler.Callback;
|
import android.os.Handler.Callback;
|
||||||
import android.os.Looper;
|
import android.os.Looper;
|
||||||
import android.os.Message;
|
import android.os.Message;
|
||||||
|
import android.os.SystemClock;
|
||||||
import androidx.annotation.GuardedBy;
|
import androidx.annotation.GuardedBy;
|
||||||
import androidx.annotation.Nullable;
|
import androidx.annotation.Nullable;
|
||||||
import com.google.android.exoplayer2.util.Clock;
|
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.ArrayList;
|
||||||
import java.util.List;
|
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 {
|
public class FakeClock implements Clock {
|
||||||
|
|
||||||
private final List<Long> wakeUpTimes;
|
private final List<Long> wakeUpTimes;
|
||||||
@ -57,6 +67,7 @@ public class FakeClock implements Clock {
|
|||||||
this.timeSinceBootMs = initialTimeMs;
|
this.timeSinceBootMs = initialTimeMs;
|
||||||
this.wakeUpTimes = new ArrayList<>();
|
this.wakeUpTimes = new ArrayList<>();
|
||||||
this.handlerMessages = new ArrayList<>();
|
this.handlerMessages = new ArrayList<>();
|
||||||
|
SystemClock.setCurrentTimeMillis(initialTimeMs);
|
||||||
}
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
@ -66,6 +77,7 @@ public class FakeClock implements Clock {
|
|||||||
*/
|
*/
|
||||||
public synchronized void advanceTime(long timeDiffMs) {
|
public synchronized void advanceTime(long timeDiffMs) {
|
||||||
timeSinceBootMs += timeDiffMs;
|
timeSinceBootMs += timeDiffMs;
|
||||||
|
SystemClock.setCurrentTimeMillis(timeSinceBootMs);
|
||||||
for (Long wakeUpTime : wakeUpTimes) {
|
for (Long wakeUpTime : wakeUpTimes) {
|
||||||
if (wakeUpTime <= timeSinceBootMs) {
|
if (wakeUpTime <= timeSinceBootMs) {
|
||||||
notifyAll();
|
notifyAll();
|
||||||
|
Loading…
x
Reference in New Issue
Block a user