Search in sources :

Example 11 with TestPlayerRunHelper.playUntilPosition

use of com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.playUntilPosition in project ExoPlayer by google.

the class PlaybackStatsListenerTest method playerRelease_callsAllPendingCallbacks.

@Test
public void playerRelease_callsAllPendingCallbacks() throws Exception {
    PlaybackStatsListener.Callback callback = mock(PlaybackStatsListener.Callback.class);
    PlaybackStatsListener playbackStatsListener = new PlaybackStatsListener(/* keepHistory= */
    true, callback);
    player.addAnalyticsListener(playbackStatsListener);
    MediaSource mediaSource = new FakeMediaSource(new FakeTimeline(/* windowCount= */
    1));
    player.setMediaSources(ImmutableList.of(mediaSource, mediaSource));
    player.prepare();
    TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY);
    // Play close to the end of the first item to ensure the second session is already created, but
    // the first one isn't finished yet.
    TestPlayerRunHelper.playUntilPosition(player, /* windowIndex= */
    0, /* positionMs= */
    player.getDuration());
    runUntilPendingCommandsAreFullyHandled(player);
    player.release();
    ShadowLooper.idleMainLooper();
    ArgumentCaptor<AnalyticsListener.EventTime> eventTimeCaptor = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(callback, times(2)).onPlaybackStatsReady(eventTimeCaptor.capture(), any());
    assertThat(eventTimeCaptor.getAllValues().stream().map(eventTime -> eventTime.windowIndex).collect(Collectors.toList())).containsExactly(0, 1);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) TestPlayerRunHelper.runUntilPendingCommandsAreFullyHandled(com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.runUntilPendingCommandsAreFullyHandled) RunWith(org.junit.runner.RunWith) PlaybackParameters(com.google.android.exoplayer2.PlaybackParameters) Player(com.google.android.exoplayer2.Player) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) ApplicationProvider(androidx.test.core.app.ApplicationProvider) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) After(org.junit.After) MediaSource(com.google.android.exoplayer2.source.MediaSource) Before(org.junit.Before) MediaItem(com.google.android.exoplayer2.MediaItem) ShadowLooper(org.robolectric.shadows.ShadowLooper) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) TestPlayerRunHelper(com.google.android.exoplayer2.robolectric.TestPlayerRunHelper) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) Mockito.never(org.mockito.Mockito.never) Nullable(androidx.annotation.Nullable) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) Mockito.mock(org.mockito.Mockito.mock) MediaSource(com.google.android.exoplayer2.source.MediaSource) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) Test(org.junit.Test)

Example 12 with TestPlayerRunHelper.playUntilPosition

use of com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.playUntilPosition in project ExoPlayer by google.

the class PlaybackStatsListenerTest method playlistClear_callsAllPendingCallbacks.

@Test
public void playlistClear_callsAllPendingCallbacks() throws Exception {
    PlaybackStatsListener.Callback callback = mock(PlaybackStatsListener.Callback.class);
    PlaybackStatsListener playbackStatsListener = new PlaybackStatsListener(/* keepHistory= */
    true, callback);
    player.addAnalyticsListener(playbackStatsListener);
    MediaSource mediaSource = new FakeMediaSource(new FakeTimeline(/* windowCount= */
    1));
    player.setMediaSources(ImmutableList.of(mediaSource, mediaSource));
    player.prepare();
    TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY);
    // Play close to the end of the first item to ensure the second session is already created, but
    // the first one isn't finished yet.
    TestPlayerRunHelper.playUntilPosition(player, /* windowIndex= */
    0, /* positionMs= */
    player.getDuration());
    runUntilPendingCommandsAreFullyHandled(player);
    player.clearMediaItems();
    ShadowLooper.idleMainLooper();
    ArgumentCaptor<AnalyticsListener.EventTime> eventTimeCaptor = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(callback, times(2)).onPlaybackStatsReady(eventTimeCaptor.capture(), any());
    assertThat(eventTimeCaptor.getAllValues().stream().map(eventTime -> eventTime.windowIndex).collect(Collectors.toList())).containsExactly(0, 1);
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) TestPlayerRunHelper.runUntilPendingCommandsAreFullyHandled(com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.runUntilPendingCommandsAreFullyHandled) RunWith(org.junit.runner.RunWith) PlaybackParameters(com.google.android.exoplayer2.PlaybackParameters) Player(com.google.android.exoplayer2.Player) AndroidJUnit4(androidx.test.ext.junit.runners.AndroidJUnit4) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) ApplicationProvider(androidx.test.core.app.ApplicationProvider) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) ArgumentCaptor(org.mockito.ArgumentCaptor) ImmutableList(com.google.common.collect.ImmutableList) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) After(org.junit.After) MediaSource(com.google.android.exoplayer2.source.MediaSource) Before(org.junit.Before) MediaItem(com.google.android.exoplayer2.MediaItem) ShadowLooper(org.robolectric.shadows.ShadowLooper) Mockito.times(org.mockito.Mockito.times) Test(org.junit.Test) Truth.assertThat(com.google.common.truth.Truth.assertThat) Collectors(java.util.stream.Collectors) Mockito.verify(org.mockito.Mockito.verify) TestPlayerRunHelper(com.google.android.exoplayer2.robolectric.TestPlayerRunHelper) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) Mockito.never(org.mockito.Mockito.never) Nullable(androidx.annotation.Nullable) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) Mockito.mock(org.mockito.Mockito.mock) MediaSource(com.google.android.exoplayer2.source.MediaSource) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) Test(org.junit.Test)

Example 13 with TestPlayerRunHelper.playUntilPosition

use of com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.playUntilPosition in project ExoPlayer by google.

the class ExoPlayerTest method stop_doesNotCallOnPositionDiscontinuity.

@Test
public void stop_doesNotCallOnPositionDiscontinuity() throws Exception {
    ExoPlayer player = new TestExoPlayerBuilder(context).build();
    Player.Listener listener = mock(Player.Listener.class);
    player.addListener(listener);
    player.setMediaSource(new FakeMediaSource());
    player.prepare();
    TestPlayerRunHelper.playUntilPosition(player, /* mediaItemIndex= */
    0, /* positionMs= */
    5 * C.MILLIS_PER_SECOND);
    player.stop();
    verify(listener, never()).onPositionDiscontinuity(any(), any(), anyInt());
    player.release();
}
Also used : Listener(com.google.android.exoplayer2.Player.Listener) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) Test(org.junit.Test)

Example 14 with TestPlayerRunHelper.playUntilPosition

use of com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.playUntilPosition in project ExoPlayer by google.

the class ExoPlayerTest method seekBack_callsOnPositionDiscontinuity.

@Test
public void seekBack_callsOnPositionDiscontinuity() throws Exception {
    ExoPlayer player = new TestExoPlayerBuilder(context).build();
    Player.Listener listener = mock(Player.Listener.class);
    player.addListener(listener);
    Timeline fakeTimeline = new FakeTimeline(new TimelineWindowDefinition(/* isSeekable= */
    true, /* isDynamic= */
    true, /* durationUs= */
    Util.msToUs(3 * C.DEFAULT_SEEK_BACK_INCREMENT_MS)));
    player.setMediaSource(new FakeMediaSource(fakeTimeline));
    player.prepare();
    TestPlayerRunHelper.playUntilPosition(player, /* mediaItemIndex= */
    0, /* positionMs= */
    2 * C.DEFAULT_SEEK_BACK_INCREMENT_MS);
    player.seekBack();
    ArgumentCaptor<Player.PositionInfo> oldPosition = ArgumentCaptor.forClass(Player.PositionInfo.class);
    ArgumentCaptor<Player.PositionInfo> newPosition = ArgumentCaptor.forClass(Player.PositionInfo.class);
    verify(listener, never()).onPositionDiscontinuity(any(), any(), not(eq(Player.DISCONTINUITY_REASON_SEEK)));
    verify(listener).onPositionDiscontinuity(oldPosition.capture(), newPosition.capture(), eq(Player.DISCONTINUITY_REASON_SEEK));
    List<Player.PositionInfo> oldPositions = oldPosition.getAllValues();
    List<Player.PositionInfo> newPositions = newPosition.getAllValues();
    assertThat(oldPositions.get(0).mediaItemIndex).isEqualTo(0);
    assertThat(oldPositions.get(0).positionMs).isIn(Range.closed(2 * C.DEFAULT_SEEK_BACK_INCREMENT_MS - 20, 2 * C.DEFAULT_SEEK_BACK_INCREMENT_MS));
    assertThat(oldPositions.get(0).contentPositionMs).isIn(Range.closed(2 * C.DEFAULT_SEEK_BACK_INCREMENT_MS - 20, 2 * C.DEFAULT_SEEK_BACK_INCREMENT_MS));
    assertThat(newPositions.get(0).mediaItemIndex).isEqualTo(0);
    assertThat(newPositions.get(0).positionMs).isIn(Range.closed(C.DEFAULT_SEEK_BACK_INCREMENT_MS - 20, C.DEFAULT_SEEK_BACK_INCREMENT_MS));
    assertThat(newPositions.get(0).contentPositionMs).isIn(Range.closed(C.DEFAULT_SEEK_BACK_INCREMENT_MS - 20, C.DEFAULT_SEEK_BACK_INCREMENT_MS));
    player.release();
}
Also used : Listener(com.google.android.exoplayer2.Player.Listener) NoUidTimeline(com.google.android.exoplayer2.testutil.NoUidTimeline) SinglePeriodTimeline(com.google.android.exoplayer2.source.SinglePeriodTimeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) TimelineWindowDefinition(com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition) PositionInfo(com.google.android.exoplayer2.Player.PositionInfo) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) Test(org.junit.Test)

Example 15 with TestPlayerRunHelper.playUntilPosition

use of com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.playUntilPosition in project ExoPlayer by google.

the class ExoPlayerTest method targetLiveOffsetInMedia_afterSeekToDefaultPositionInOtherStream_adjustsLiveOffsetToMediaOffset.

@Test
public void targetLiveOffsetInMedia_afterSeekToDefaultPositionInOtherStream_adjustsLiveOffsetToMediaOffset() throws Exception {
    long windowStartUnixTimeMs = 987_654_321_000L;
    long nowUnixTimeMs = windowStartUnixTimeMs + 20_000;
    ExoPlayer player = new TestExoPlayerBuilder(context).setClock(new FakeClock(/* initialTimeMs= */
    nowUnixTimeMs, /* isAutoAdvancing= */
    true)).build();
    Timeline liveTimeline1 = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    0, /* isSeekable= */
    true, /* isDynamic= */
    true, /* isLive= */
    true, /* isPlaceholder= */
    false, /* durationUs= */
    1000 * C.MICROS_PER_SECOND, /* defaultPositionUs= */
    8 * C.MICROS_PER_SECOND, /* windowOffsetInFirstPeriodUs= */
    Util.msToUs(windowStartUnixTimeMs), ImmutableList.of(AdPlaybackState.NONE), new MediaItem.Builder().setUri(Uri.EMPTY).setLiveConfiguration(new MediaItem.LiveConfiguration.Builder().setTargetOffsetMs(9_000).build()).build()));
    Timeline liveTimeline2 = new FakeTimeline(new TimelineWindowDefinition(/* periodCount= */
    1, /* id= */
    0, /* isSeekable= */
    true, /* isDynamic= */
    true, /* isLive= */
    true, /* isPlaceholder= */
    false, /* durationUs= */
    1000 * C.MICROS_PER_SECOND, /* defaultPositionUs= */
    8 * C.MICROS_PER_SECOND, /* windowOffsetInFirstPeriodUs= */
    Util.msToUs(windowStartUnixTimeMs), ImmutableList.of(AdPlaybackState.NONE), new MediaItem.Builder().setUri(Uri.EMPTY).setLiveConfiguration(new MediaItem.LiveConfiguration.Builder().setTargetOffsetMs(4_000).build()).build()));
    player.pause();
    player.addMediaSource(new FakeMediaSource(liveTimeline1));
    player.addMediaSource(new FakeMediaSource(liveTimeline2));
    // Ensure we override the target live offset to a seek position in the first live stream.
    player.seekTo(10_000);
    player.prepare();
    TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY);
    // Seek to default position in second stream.
    player.seekToNextMediaItem();
    // Play until close to the end of the available live window.
    TestPlayerRunHelper.playUntilPosition(player, /* mediaItemIndex= */
    1, /* positionMs= */
    999_000);
    long liveOffsetAtEnd = player.getCurrentLiveOffset();
    player.release();
    // Assert that player adjusted live offset to the media value.
    assertThat(liveOffsetAtEnd).isIn(Range.closed(3_900L, 4_100L));
}
Also used : NoUidTimeline(com.google.android.exoplayer2.testutil.NoUidTimeline) SinglePeriodTimeline(com.google.android.exoplayer2.source.SinglePeriodTimeline) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) FakeClock(com.google.android.exoplayer2.testutil.FakeClock) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) TestPlayerRunHelper.playUntilStartOfMediaItem(com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.playUntilStartOfMediaItem) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) TimelineWindowDefinition(com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) Test(org.junit.Test)

Aggregations

TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)24 Test (org.junit.Test)24 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)21 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)20 TimelineWindowDefinition (com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition)17 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)13 NoUidTimeline (com.google.android.exoplayer2.testutil.NoUidTimeline)13 Listener (com.google.android.exoplayer2.Player.Listener)12 TestPlayerRunHelper.playUntilStartOfMediaItem (com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.playUntilStartOfMediaItem)10 FakeClock (com.google.android.exoplayer2.testutil.FakeClock)10 PositionInfo (com.google.android.exoplayer2.Player.PositionInfo)9 InOrder (org.mockito.InOrder)5 Nullable (androidx.annotation.Nullable)2 ApplicationProvider (androidx.test.core.app.ApplicationProvider)2 AndroidJUnit4 (androidx.test.ext.junit.runners.AndroidJUnit4)2 ExoPlayer (com.google.android.exoplayer2.ExoPlayer)2 MediaItem (com.google.android.exoplayer2.MediaItem)2 PlaybackParameters (com.google.android.exoplayer2.PlaybackParameters)2 Player (com.google.android.exoplayer2.Player)2 TestPlayerRunHelper (com.google.android.exoplayer2.robolectric.TestPlayerRunHelper)2