Skip to content
Toggle navigation
P
Projects
G
Groups
S
Snippets
Help
SDK
/
exoplayer
This project
Loading...
Sign in
Toggle navigation
Go to a project
Project
Repository
Issues
0
Merge Requests
0
Pipelines
Wiki
Snippets
Settings
Activity
Graph
Charts
Create a new issue
Jobs
Commits
Issue Boards
Files
Commits
Branches
Tags
Contributors
Graph
Compare
Charts
Commit
f00584b0
authored
Oct 14, 2020
by
christosts
Committed by
Oliver Woodman
Oct 17, 2020
Browse files
Options
_('Browse Files')
Download
Email Patches
Plain Diff
Improve FakeClock and AutoAdvancingFakeClock
Issue: #4904 PiperOrigin-RevId: 337047518
parent
41b58d50
Show whitespace changes
Inline
Side-by-side
Showing
2 changed files
with
29 additions
and
5 deletions
testutils/src/main/java/com/google/android/exoplayer2/testutil/AutoAdvancingFakeClock.java
testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeClock.java
testutils/src/main/java/com/google/android/exoplayer2/testutil/AutoAdvancingFakeClock.java
View file @
f00584b0
...
@@ -20,16 +20,28 @@ import org.checkerframework.checker.nullness.qual.MonotonicNonNull;
...
@@ -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
...
...
testutils/src/main/java/com/google/android/exoplayer2/testutil/FakeClock.java
View file @
f00584b0
...
@@ -18,6 +18,7 @@ package com.google.android.exoplayer2.testutil;
...
@@ -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;
...
@@ -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 {
...
@@ -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 {
...
@@ -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
();
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment