Class ExoPlayerTestRunner
- java.lang.Object
-
- com.google.android.exoplayer2.testutil.ExoPlayerTestRunner
-
- All Implemented Interfaces:
Player.Listener
,ActionSchedule.Callback
public final class ExoPlayerTestRunner extends Object implements Player.Listener, ActionSchedule.Callback
Helper class to run an ExoPlayer test.
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static class
ExoPlayerTestRunner.Builder
Builder to set-up anExoPlayerTestRunner
.
-
Field Summary
Fields Modifier and Type Field Description static Format
AUDIO_FORMAT
A generic audioFormat
which can be used to set up aFakeMediaSource
.static Format
VIDEO_FORMAT
A generic videoFormat
which can be used to set up aFakeMediaSource
.
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description void
assertNoPositionDiscontinuities()
Asserts thatPlayer.Listener.onPositionDiscontinuity(Player.PositionInfo, Player.PositionInfo, int)
was not called.void
assertPlaybackStatesEqual(Integer... states)
Asserts that the playback states reported byPlayer.Listener.onPlaybackStateChanged(int)
are equal to the provided playback states.void
assertPlayedPeriodIndices(Integer... periodIndices)
Asserts that the indices of played periods is equal to the provided list of periods.void
assertPositionDiscontinuityReasonsEqual(Integer... discontinuityReasons)
Asserts that the discontinuity reasons reported byPlayer.Listener.onPositionDiscontinuity(Player.PositionInfo, Player.PositionInfo, int)
are equal to the provided values.void
assertTimelineChangeReasonsEqual(Integer... reasons)
Asserts that the timeline change reasons reported byPlayer.Listener.onTimelineChanged(Timeline, int)
are equal to the provided timeline change reasons.void
assertTimelinesSame(Timeline... timelines)
Asserts that the timelines reported byPlayer.Listener.onTimelineChanged(Timeline, int)
are the same to the provided timelines.ExoPlayerTestRunner
blockUntilActionScheduleFinished(long timeoutMs)
Blocks the current thread until the action schedule finished.ExoPlayerTestRunner
blockUntilEnded(long timeoutMs)
Blocks the current thread until the test runner finishes.void
onActionScheduleFinished()
Called when action schedule finished executing all its actions.void
onMediaItemTransition(MediaItem mediaItem, @com.google.android.exoplayer2.Player.MediaItemTransitionReason int reason)
Called when playback transitions to a media item or starts repeating a media item according to the currentrepeat mode
.void
onPlaybackStateChanged(@com.google.android.exoplayer2.Player.State int playbackState)
Called when the value returned fromPlayer.getPlaybackState()
changes.void
onPlayerError(PlaybackException error)
Called when an error occurs.void
onPositionDiscontinuity(Player.PositionInfo oldPosition, Player.PositionInfo newPosition, @com.google.android.exoplayer2.Player.DiscontinuityReason int reason)
Called when a position discontinuity occurs.void
onTimelineChanged(Timeline timeline, @com.google.android.exoplayer2.Player.TimelineChangeReason int reason)
Called when the value ofPlayer.getCurrentTimeline()
changes.ExoPlayerTestRunner
start()
Starts the test runner on its own thread.ExoPlayerTestRunner
start(boolean doPrepare)
Starts the test runner on its own thread.-
Methods inherited from class java.lang.Object
clone, equals, finalize, getClass, hashCode, notify, notifyAll, toString, wait, wait, wait
-
Methods inherited from interface com.google.android.exoplayer2.Player.Listener
onAudioAttributesChanged, onAudioSessionIdChanged, onAvailableCommandsChanged, onCues, onCues, onDeviceInfoChanged, onDeviceVolumeChanged, onEvents, onIsLoadingChanged, onIsPlayingChanged, onLoadingChanged, onMaxSeekToPreviousPositionChanged, onMediaMetadataChanged, onMetadata, onPlaybackParametersChanged, onPlaybackSuppressionReasonChanged, onPlayerErrorChanged, onPlayerStateChanged, onPlaylistMetadataChanged, onPlayWhenReadyChanged, onPositionDiscontinuity, onRenderedFirstFrame, onRepeatModeChanged, onSeekBackIncrementChanged, onSeekForwardIncrementChanged, onShuffleModeEnabledChanged, onSkipSilenceEnabledChanged, onSurfaceSizeChanged, onTracksChanged, onTrackSelectionParametersChanged, onVideoSizeChanged, onVolumeChanged
-
-
-
-
Field Detail
-
VIDEO_FORMAT
public static final Format VIDEO_FORMAT
A generic videoFormat
which can be used to set up aFakeMediaSource
.
-
AUDIO_FORMAT
public static final Format AUDIO_FORMAT
A generic audioFormat
which can be used to set up aFakeMediaSource
.
-
-
Method Detail
-
start
@CanIgnoreReturnValue public ExoPlayerTestRunner start()
Starts the test runner on its own thread. This will trigger the creation of the player, the listener registration, the start of the action schedule, the initial set of media items and the preparation of the player.- Returns:
- This test runner.
-
start
@CanIgnoreReturnValue public ExoPlayerTestRunner start(boolean doPrepare)
Starts the test runner on its own thread. This will trigger the creation of the player, the listener registration, the start of the action schedule and the initial set of media items.- Parameters:
doPrepare
- Whether the player should be prepared.- Returns:
- This test runner.
-
blockUntilEnded
@CanIgnoreReturnValue public ExoPlayerTestRunner blockUntilEnded(long timeoutMs) throws Exception
Blocks the current thread until the test runner finishes. A test is deemed to be finished when the playback state transitioned toPlayer.STATE_ENDED
orPlayer.STATE_IDLE
for the specified number of times. The test also finishes when anExoPlaybackException
is thrown.- Parameters:
timeoutMs
- The maximum time to wait for the test runner to finish. If this time elapsed the method will throw aTimeoutException
.- Returns:
- This test runner.
- Throws:
Exception
- If any exception occurred during playback, release, or due to a timeout.
-
blockUntilActionScheduleFinished
@CanIgnoreReturnValue public ExoPlayerTestRunner blockUntilActionScheduleFinished(long timeoutMs) throws TimeoutException, InterruptedException
Blocks the current thread until the action schedule finished. This does not release the test runner and the test must still callblockUntilEnded(long)
.- Parameters:
timeoutMs
- The maximum time to wait for the action schedule to finish.- Returns:
- This test runner.
- Throws:
TimeoutException
- If the action schedule did not finish within the specified timeout.InterruptedException
- If the test thread gets interrupted while waiting.
-
assertTimelinesSame
public void assertTimelinesSame(Timeline... timelines)
Asserts that the timelines reported byPlayer.Listener.onTimelineChanged(Timeline, int)
are the same to the provided timelines. This assert differs from testing equality by not comparing period ids which may be different due to id mapping of child source period ids.- Parameters:
timelines
- A list of expectedTimeline
s.
-
assertTimelineChangeReasonsEqual
public void assertTimelineChangeReasonsEqual(Integer... reasons)
Asserts that the timeline change reasons reported byPlayer.Listener.onTimelineChanged(Timeline, int)
are equal to the provided timeline change reasons.
-
assertPlaybackStatesEqual
public void assertPlaybackStatesEqual(Integer... states)
Asserts that the playback states reported byPlayer.Listener.onPlaybackStateChanged(int)
are equal to the provided playback states.
-
assertNoPositionDiscontinuities
public void assertNoPositionDiscontinuities()
Asserts thatPlayer.Listener.onPositionDiscontinuity(Player.PositionInfo, Player.PositionInfo, int)
was not called.
-
assertPositionDiscontinuityReasonsEqual
public void assertPositionDiscontinuityReasonsEqual(Integer... discontinuityReasons)
Asserts that the discontinuity reasons reported byPlayer.Listener.onPositionDiscontinuity(Player.PositionInfo, Player.PositionInfo, int)
are equal to the provided values.- Parameters:
discontinuityReasons
- The expected discontinuity reasons.
-
assertPlayedPeriodIndices
public void assertPlayedPeriodIndices(Integer... periodIndices)
Asserts that the indices of played periods is equal to the provided list of periods. A period is considered to be played if it was the current period after a position discontinuity or a media source preparation. When the same period is repeated automatically due to enabled repeat modes, it is reported twice. Seeks within the current period are not reported.- Parameters:
periodIndices
- A list of expected period indices.
-
onTimelineChanged
public void onTimelineChanged(Timeline timeline, @TimelineChangeReason @com.google.android.exoplayer2.Player.TimelineChangeReason int reason)
Description copied from interface:Player.Listener
Called when the value ofPlayer.getCurrentTimeline()
changes.Note that the current
MediaItem
or playback position may change as a result of a timeline change. If playback can't continue smoothly because of this timeline change, a separatePlayer.Listener.onPositionDiscontinuity(PositionInfo, PositionInfo, int)
callback will be triggered.Player.Listener.onEvents(Player, Events)
will also be called to report this event along with other events that happen in the sameLooper
message queue iteration.- Specified by:
onTimelineChanged
in interfacePlayer.Listener
- Parameters:
timeline
- The latest timeline. Never null, but may be empty.reason
- ThePlayer.TimelineChangeReason
responsible for this timeline change.
-
onMediaItemTransition
public void onMediaItemTransition(@Nullable MediaItem mediaItem, @MediaItemTransitionReason @com.google.android.exoplayer2.Player.MediaItemTransitionReason int reason)
Description copied from interface:Player.Listener
Called when playback transitions to a media item or starts repeating a media item according to the currentrepeat mode
.Note that this callback is also called when the value of
Player.getCurrentTimeline()
becomes non-empty or empty.Player.Listener.onEvents(Player, Events)
will also be called to report this event along with other events that happen in the sameLooper
message queue iteration.- Specified by:
onMediaItemTransition
in interfacePlayer.Listener
- Parameters:
mediaItem
- TheMediaItem
. May be null if the playlist becomes empty.reason
- The reason for the transition.
-
onPlaybackStateChanged
public void onPlaybackStateChanged(@State @com.google.android.exoplayer2.Player.State int playbackState)
Description copied from interface:Player.Listener
Called when the value returned fromPlayer.getPlaybackState()
changes.Player.Listener.onEvents(Player, Events)
will also be called to report this event along with other events that happen in the sameLooper
message queue iteration.- Specified by:
onPlaybackStateChanged
in interfacePlayer.Listener
- Parameters:
playbackState
- The new playbackPlayer.State
.
-
onPlayerError
public void onPlayerError(PlaybackException error)
Description copied from interface:Player.Listener
Called when an error occurs. The playback state will transition toPlayer.STATE_IDLE
immediately after this method is called. The player instance can still be used, andPlayer.release()
must still be called on the player should it no longer be required.Player.Listener.onEvents(Player, Events)
will also be called to report this event along with other events that happen in the sameLooper
message queue iteration.Implementations of Player may pass an instance of a subclass of
PlaybackException
to this method in order to include more information about the error.- Specified by:
onPlayerError
in interfacePlayer.Listener
- Parameters:
error
- The error.
-
onPositionDiscontinuity
public void onPositionDiscontinuity(Player.PositionInfo oldPosition, Player.PositionInfo newPosition, @DiscontinuityReason @com.google.android.exoplayer2.Player.DiscontinuityReason int reason)
Description copied from interface:Player.Listener
Called when a position discontinuity occurs.A position discontinuity occurs when the playing period changes, the playback position jumps within the period currently being played, or when the playing period has been skipped or removed.
Player.Listener.onEvents(Player, Events)
will also be called to report this event along with other events that happen in the sameLooper
message queue iteration.- Specified by:
onPositionDiscontinuity
in interfacePlayer.Listener
- Parameters:
oldPosition
- The position before the discontinuity.newPosition
- The position after the discontinuity.reason
- ThePlayer.DiscontinuityReason
responsible for the discontinuity.
-
onActionScheduleFinished
public void onActionScheduleFinished()
Description copied from interface:ActionSchedule.Callback
Called when action schedule finished executing all its actions.- Specified by:
onActionScheduleFinished
in interfaceActionSchedule.Callback
-
-