Skip to content

Commit 694e85c

Browse files
committed
Add test
1 parent 6b11abd commit 694e85c

File tree

2 files changed

+38
-2
lines changed

2 files changed

+38
-2
lines changed

libraries/exoplayer_ima/src/main/java/androidx/media3/exoplayer/ima/AdTagLoader.java

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -402,9 +402,10 @@ public void deactivate() {
402402
* callback can be handled first.
403403
*/
404404
private void deactivateInternal(Player player) {
405-
if (!AdPlaybackState.NONE.equals(adPlaybackState)
405+
if (!adPlaybackState.equals(AdPlaybackState.NONE)
406406
&& imaPausedContent
407407
&& player.getPlayerError() == null) {
408+
// Only need to pause and store resume position if not in error state.
408409
if (adsManager != null) {
409410
adsManager.pause();
410411
}
@@ -550,7 +551,7 @@ public void onPlayWhenReadyChanged(
550551

551552
@Override
552553
public void onPlayerError(PlaybackException error) {
553-
if (imaAdState != IMA_AD_STATE_NONE && player.isPlayingAd()) {
554+
if (imaAdState != IMA_AD_STATE_NONE && checkNotNull(player).isPlayingAd()) {
554555
AdMediaInfo adMediaInfo = checkNotNull(imaAdMediaInfo);
555556
for (int i = 0; i < adCallbacks.size(); i++) {
556557
adCallbacks.get(i).onError(adMediaInfo);

libraries/exoplayer_ima/src/test/java/androidx/media3/exoplayer/ima/ImaAdsLoaderTest.java

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1481,6 +1481,41 @@ public void buildWithEnableContinuousPlayback_setsAdsRequestProperty() {
14811481
verify(mockAdsRequest).setContinuousPlayback(true);
14821482
}
14831483

1484+
@Test
1485+
public void contentErrorDuringAdPlayback_doesNotMarkAdAsFailed() throws IOException {
1486+
// Load and play a preroll ad.
1487+
imaAdsLoader.start(
1488+
adsMediaSource, TEST_DATA_SPEC, TEST_ADS_ID, adViewProvider, adsLoaderListener);
1489+
adEventListener.onAdEvent(getAdEvent(AdEventType.LOADED, mockPrerollSingleAd));
1490+
videoAdPlayer.loadAd(TEST_AD_MEDIA_INFO, mockAdPodInfo);
1491+
adEventListener.onAdEvent(getAdEvent(AdEventType.CONTENT_PAUSE_REQUESTED, mockPrerollSingleAd));
1492+
videoAdPlayer.playAd(TEST_AD_MEDIA_INFO);
1493+
fakePlayer.setPlayingAdPosition(
1494+
/* periodIndex= */ 0,
1495+
/* adGroupIndex= */ 0,
1496+
/* adIndexInAdGroup= */ 0,
1497+
/* positionMs= */ 0,
1498+
/* contentPositionMs= */ 0);
1499+
fakePlayer.setState(Player.STATE_READY, /* playWhenReady= */ true);
1500+
adEventListener.onAdEvent(getAdEvent(AdEventType.STARTED, mockPrerollSingleAd));
1501+
1502+
// Simulate a content preparation error while the ad is playing.
1503+
// The player is not playing an ad from its perspective, so isPlayingAd() is false.
1504+
fakePlayer.setPlayingContentPosition(/* periodIndex= */ 0, /* positionMs= */ 0);
1505+
ExoPlaybackException error =
1506+
ExoPlaybackException.createForSource(
1507+
new IOException("Content preparation error"),
1508+
PlaybackException.ERROR_CODE_IO_UNSPECIFIED);
1509+
fakePlayer.setPlayerError(error);
1510+
shadowOf(Looper.getMainLooper()).runToEndOfTasks();
1511+
1512+
// Verify that the ad is not marked as failed.
1513+
AdPlaybackState adPlaybackState = getAdPlaybackState(0);
1514+
assertThat(adPlaybackState.getAdGroup(0).states[0])
1515+
.isNotEqualTo(AdPlaybackState.AD_STATE_ERROR);
1516+
verify(mockVideoAdPlayerCallback, never()).onError(any());
1517+
}
1518+
14841519
private void setupMocks() {
14851520
ArgumentCaptor<Object> userRequestContextCaptor = ArgumentCaptor.forClass(Object.class);
14861521
doNothing().when(mockAdsRequest).setUserRequestContext(userRequestContextCaptor.capture());

0 commit comments

Comments
 (0)