Search in sources :

Example 31 with TestPlayerRunHelper.runUntilPlaybackState

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

the class DefaultAnalyticsCollectorTest method onEvents_isReportedWithCorrectEventTimes.

@Test
public void onEvents_isReportedWithCorrectEventTimes() throws Exception {
    ExoPlayer player = new TestExoPlayerBuilder(ApplicationProvider.getApplicationContext()).build();
    Surface surface = new Surface(new SurfaceTexture(/* texName= */
    0));
    player.setVideoSurface(surface);
    AnalyticsListener listener = mock(AnalyticsListener.class);
    Format[] formats = new Format[] { new Format.Builder().setSampleMimeType(MimeTypes.VIDEO_H264).build(), new Format.Builder().setSampleMimeType(MimeTypes.AUDIO_AAC).build() };
    player.addAnalyticsListener(listener);
    // Trigger some simultaneous events.
    player.setMediaSource(new FakeMediaSource(new FakeTimeline(), formats));
    player.seekTo(2_000);
    player.setPlaybackParameters(new PlaybackParameters(/* speed= */
    2.0f));
    ShadowLooper.runMainLooperToNextTask();
    // Move to another item and fail with a third one to trigger events with different EventTimes.
    player.prepare();
    TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_READY);
    player.addMediaSource(new FakeMediaSource(new FakeTimeline(), formats));
    player.play();
    TestPlayerRunHelper.runUntilPositionDiscontinuity(player, Player.DISCONTINUITY_REASON_AUTO_TRANSITION);
    player.setMediaItem(MediaItem.fromUri("http://this-will-throw-an-exception.mp4"));
    TestPlayerRunHelper.runUntilError(player);
    ShadowLooper.runMainLooperToNextTask();
    player.release();
    surface.release();
    // Verify that expected individual callbacks have been called and capture EventTimes.
    ArgumentCaptor<AnalyticsListener.EventTime> individualTimelineChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onTimelineChanged(individualTimelineChangedEventTimes.capture(), anyInt());
    ArgumentCaptor<AnalyticsListener.EventTime> individualMediaItemTransitionEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onMediaItemTransition(individualMediaItemTransitionEventTimes.capture(), any(), anyInt());
    ArgumentCaptor<AnalyticsListener.EventTime> individualPositionDiscontinuityEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onPositionDiscontinuity(individualPositionDiscontinuityEventTimes.capture(), anyInt());
    ArgumentCaptor<AnalyticsListener.EventTime> individualPlaybackStateChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onPlaybackStateChanged(individualPlaybackStateChangedEventTimes.capture(), anyInt());
    ArgumentCaptor<AnalyticsListener.EventTime> individualIsLoadingChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onIsLoadingChanged(individualIsLoadingChangedEventTimes.capture(), anyBoolean());
    ArgumentCaptor<AnalyticsListener.EventTime> individualTracksChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onTracksChanged(individualTracksChangedEventTimes.capture(), any(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualPlayWhenReadyChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onPlayWhenReadyChanged(individualPlayWhenReadyChangedEventTimes.capture(), anyBoolean(), anyInt());
    ArgumentCaptor<AnalyticsListener.EventTime> individualIsPlayingChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onIsPlayingChanged(individualIsPlayingChangedEventTimes.capture(), anyBoolean());
    ArgumentCaptor<AnalyticsListener.EventTime> individualPlayerErrorEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onPlayerError(individualPlayerErrorEventTimes.capture(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualPlaybackParametersChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onPlaybackParametersChanged(individualPlaybackParametersChangedEventTimes.capture(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualLoadStartedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onLoadStarted(individualLoadStartedEventTimes.capture(), any(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualLoadCompletedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onLoadCompleted(individualLoadCompletedEventTimes.capture(), any(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualLoadErrorEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onLoadError(individualLoadErrorEventTimes.capture(), any(), any(), any(), anyBoolean());
    ArgumentCaptor<AnalyticsListener.EventTime> individualVideoEnabledEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onVideoEnabled(individualVideoEnabledEventTimes.capture(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualAudioEnabledEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onAudioEnabled(individualAudioEnabledEventTimes.capture(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualDownstreamFormatChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onDownstreamFormatChanged(individualDownstreamFormatChangedEventTimes.capture(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualVideoInputFormatChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onVideoInputFormatChanged(individualVideoInputFormatChangedEventTimes.capture(), any(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualAudioInputFormatChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onAudioInputFormatChanged(individualAudioInputFormatChangedEventTimes.capture(), any(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualVideoDecoderInitializedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onVideoDecoderInitialized(individualVideoDecoderInitializedEventTimes.capture(), any(), anyLong(), anyLong());
    ArgumentCaptor<AnalyticsListener.EventTime> individualAudioDecoderInitializedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onAudioDecoderInitialized(individualAudioDecoderInitializedEventTimes.capture(), any(), anyLong(), anyLong());
    ArgumentCaptor<AnalyticsListener.EventTime> individualVideoDisabledEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onVideoDisabled(individualVideoDisabledEventTimes.capture(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualAudioDisabledEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onAudioDisabled(individualAudioDisabledEventTimes.capture(), any());
    ArgumentCaptor<AnalyticsListener.EventTime> individualRenderedFirstFrameEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onRenderedFirstFrame(individualRenderedFirstFrameEventTimes.capture(), any(), anyLong());
    ArgumentCaptor<AnalyticsListener.EventTime> individualVideoSizeChangedEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onVideoSizeChanged(individualVideoSizeChangedEventTimes.capture(), any());
    verify(listener, atLeastOnce()).onVideoSizeChanged(individualVideoSizeChangedEventTimes.capture(), anyInt(), anyInt(), anyInt(), anyFloat());
    ArgumentCaptor<AnalyticsListener.EventTime> individualAudioPositionAdvancingEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onAudioPositionAdvancing(individualAudioPositionAdvancingEventTimes.capture(), anyLong());
    ArgumentCaptor<AnalyticsListener.EventTime> individualVideoProcessingOffsetEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onVideoFrameProcessingOffset(individualVideoProcessingOffsetEventTimes.capture(), anyLong(), anyInt());
    ArgumentCaptor<AnalyticsListener.EventTime> individualDroppedFramesEventTimes = ArgumentCaptor.forClass(AnalyticsListener.EventTime.class);
    verify(listener, atLeastOnce()).onDroppedVideoFrames(individualDroppedFramesEventTimes.capture(), anyInt(), anyLong());
    // Verify the EventTimes reported with onEvents are a non-empty subset of the individual
    // callback EventTimes. We can only assert they are a non-empty subset because there may be
    // multiple events of the same type arriving in the same message queue iteration.
    ArgumentCaptor<AnalyticsListener.Events> eventsCaptor = ArgumentCaptor.forClass(AnalyticsListener.Events.class);
    verify(listener, atLeastOnce()).onEvents(eq(player), eventsCaptor.capture());
    SparseArray<List<AnalyticsListener.EventTime>> onEventsEventTimes = new SparseArray<>();
    for (AnalyticsListener.Events events : eventsCaptor.getAllValues()) {
        for (int i = 0; i < events.size(); i++) {
            @AnalyticsListener.EventFlags int event = events.get(i);
            if (onEventsEventTimes.get(event) == null) {
                onEventsEventTimes.put(event, new ArrayList<>());
            }
            onEventsEventTimes.get(event).add(events.getEventTime(event));
        }
    }
    // SparseArray.get returns null if the key doesn't exist, thus verifying the sets are non-empty.
    assertThat(individualTimelineChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_TIMELINE_CHANGED)).inOrder();
    assertThat(individualMediaItemTransitionEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_MEDIA_ITEM_TRANSITION)).inOrder();
    assertThat(individualPositionDiscontinuityEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_POSITION_DISCONTINUITY)).inOrder();
    assertThat(individualPlaybackStateChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_PLAYBACK_STATE_CHANGED)).inOrder();
    assertThat(individualIsLoadingChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_IS_LOADING_CHANGED)).inOrder();
    assertThat(individualTracksChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_TRACKS_CHANGED)).inOrder();
    assertThat(individualPlayWhenReadyChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_PLAY_WHEN_READY_CHANGED)).inOrder();
    assertThat(individualIsPlayingChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_IS_PLAYING_CHANGED)).inOrder();
    assertThat(individualPlayerErrorEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_PLAYER_ERROR)).inOrder();
    assertThat(individualPlaybackParametersChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_PLAYBACK_PARAMETERS_CHANGED)).inOrder();
    assertThat(individualLoadStartedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_LOAD_STARTED)).inOrder();
    assertThat(individualLoadCompletedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_LOAD_COMPLETED)).inOrder();
    assertThat(individualLoadErrorEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_LOAD_ERROR)).inOrder();
    assertThat(individualVideoEnabledEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_VIDEO_ENABLED)).inOrder();
    assertThat(individualAudioEnabledEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_AUDIO_ENABLED)).inOrder();
    assertThat(individualDownstreamFormatChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_DOWNSTREAM_FORMAT_CHANGED)).inOrder();
    assertThat(individualVideoInputFormatChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_VIDEO_INPUT_FORMAT_CHANGED)).inOrder();
    assertThat(individualAudioInputFormatChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_AUDIO_INPUT_FORMAT_CHANGED)).inOrder();
    assertThat(individualVideoDecoderInitializedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_VIDEO_DECODER_INITIALIZED)).inOrder();
    assertThat(individualAudioDecoderInitializedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_AUDIO_DECODER_INITIALIZED)).inOrder();
    assertThat(individualVideoDisabledEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_VIDEO_DISABLED)).inOrder();
    assertThat(individualAudioDisabledEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_AUDIO_DISABLED)).inOrder();
    assertThat(individualRenderedFirstFrameEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_RENDERED_FIRST_FRAME)).inOrder();
    assertThat(individualVideoSizeChangedEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_VIDEO_SIZE_CHANGED)).inOrder();
    assertThat(individualAudioPositionAdvancingEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_AUDIO_POSITION_ADVANCING)).inOrder();
    assertThat(individualVideoProcessingOffsetEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_VIDEO_FRAME_PROCESSING_OFFSET)).inOrder();
    assertThat(individualDroppedFramesEventTimes.getAllValues()).containsAtLeastElementsIn(onEventsEventTimes.get(EVENT_DROPPED_VIDEO_FRAMES)).inOrder();
}
Also used : FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) ExoPlayer(com.google.android.exoplayer2.ExoPlayer) Surface(android.view.Surface) SparseArray(android.util.SparseArray) SurfaceTexture(android.graphics.SurfaceTexture) Format(com.google.android.exoplayer2.Format) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) ArrayList(java.util.ArrayList) List(java.util.List) ImmutableList(com.google.common.collect.ImmutableList) TestExoPlayerBuilder(com.google.android.exoplayer2.testutil.TestExoPlayerBuilder) PlaybackParameters(com.google.android.exoplayer2.PlaybackParameters) Test(org.junit.Test)

Example 32 with TestPlayerRunHelper.runUntilPlaybackState

use of com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.runUntilPlaybackState 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 33 with TestPlayerRunHelper.runUntilPlaybackState

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

the class PlaybackStatsListenerTest method playback_withoutKeepHistory_updatesStats.

@Test
public void playback_withoutKeepHistory_updatesStats() throws Exception {
    PlaybackStatsListener playbackStatsListener = new PlaybackStatsListener(/* keepHistory= */
    false, /* callback= */
    null);
    player.addAnalyticsListener(playbackStatsListener);
    player.setMediaSource(new FakeMediaSource(new FakeTimeline(/* windowCount= */
    1)));
    player.prepare();
    player.play();
    TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_ENDED);
    runUntilPendingCommandsAreFullyHandled(player);
    @Nullable PlaybackStats playbackStats = playbackStatsListener.getPlaybackStats();
    assertThat(playbackStats).isNotNull();
    assertThat(playbackStats.endedCount).isEqualTo(1);
}
Also used : FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) Nullable(androidx.annotation.Nullable) Test(org.junit.Test)

Example 34 with TestPlayerRunHelper.runUntilPlaybackState

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

the class PlaybackStatsListenerTest method playback_withKeepHistory_updatesStats.

@Test
public void playback_withKeepHistory_updatesStats() throws Exception {
    PlaybackStatsListener playbackStatsListener = new PlaybackStatsListener(/* keepHistory= */
    true, /* callback= */
    null);
    player.addAnalyticsListener(playbackStatsListener);
    player.setMediaSource(new FakeMediaSource(new FakeTimeline(/* windowCount= */
    1)));
    player.prepare();
    player.play();
    TestPlayerRunHelper.runUntilPlaybackState(player, Player.STATE_ENDED);
    runUntilPendingCommandsAreFullyHandled(player);
    @Nullable PlaybackStats playbackStats = playbackStatsListener.getPlaybackStats();
    assertThat(playbackStats).isNotNull();
    assertThat(playbackStats.endedCount).isEqualTo(1);
}
Also used : FakeMediaSource(com.google.android.exoplayer2.testutil.FakeMediaSource) FakeTimeline(com.google.android.exoplayer2.testutil.FakeTimeline) Nullable(androidx.annotation.Nullable) Test(org.junit.Test)

Example 35 with TestPlayerRunHelper.runUntilPlaybackState

use of com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.runUntilPlaybackState 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)

Aggregations

Test (org.junit.Test)53 TestExoPlayerBuilder (com.google.android.exoplayer2.testutil.TestExoPlayerBuilder)32 FakeMediaSource (com.google.android.exoplayer2.testutil.FakeMediaSource)31 FakeTimeline (com.google.android.exoplayer2.testutil.FakeTimeline)31 FakeClock (com.google.android.exoplayer2.testutil.FakeClock)29 ExoPlayer (com.google.android.exoplayer2.ExoPlayer)23 TimelineWindowDefinition (com.google.android.exoplayer2.testutil.FakeTimeline.TimelineWindowDefinition)23 PlaybackOutput (com.google.android.exoplayer2.robolectric.PlaybackOutput)18 Context (android.content.Context)17 Listener (com.google.android.exoplayer2.Player.Listener)17 CapturingRenderersFactory (com.google.android.exoplayer2.testutil.CapturingRenderersFactory)17 SinglePeriodTimeline (com.google.android.exoplayer2.source.SinglePeriodTimeline)16 NoUidTimeline (com.google.android.exoplayer2.testutil.NoUidTimeline)16 PositionInfo (com.google.android.exoplayer2.Player.PositionInfo)13 TestPlayerRunHelper.playUntilStartOfMediaItem (com.google.android.exoplayer2.robolectric.TestPlayerRunHelper.playUntilStartOfMediaItem)11 SurfaceTexture (android.graphics.SurfaceTexture)10 Surface (android.view.Surface)10 InOrder (org.mockito.InOrder)10 Nullable (androidx.annotation.Nullable)7 MediaSource (com.google.android.exoplayer2.source.MediaSource)5