Skip to content

Commit 4771273

Browse files
ychaparovcopybara-github
authored andcommitted
Fix race condition in player states test
Add a playerEnded conditionvariable to the listener that's responsible for updating the playerStates array PiperOrigin-RevId: 786240414
1 parent ca47e72 commit 4771273

File tree

1 file changed

+7
-0
lines changed

1 file changed

+7
-0
lines changed

libraries/transformer/src/androidTest/java/androidx/media3/transformer/CompositionPlayerSetCompositionTest.java

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,7 @@
3333
import androidx.media3.common.Timeline;
3434
import androidx.media3.common.audio.AudioProcessor;
3535
import androidx.media3.common.audio.SpeedProvider;
36+
import androidx.media3.common.util.ConditionVariable;
3637
import androidx.media3.effect.GlEffect;
3738
import androidx.test.ext.junit.rules.ActivityScenarioRule;
3839
import androidx.test.ext.junit.runners.AndroidJUnit4;
@@ -129,6 +130,7 @@ public void setComposition_withChangedRemoveAudio_playbackCompletes() throws Exc
129130
.build();
130131
EditedMediaItem mediaItemRemoveAudio = mediaItem.buildUpon().setRemoveAudio(true).build();
131132
AtomicBoolean changedComposition = new AtomicBoolean();
133+
ConditionVariable playerEnded = new ConditionVariable();
132134
CopyOnWriteArrayList<Integer> playerStates = new CopyOnWriteArrayList<>();
133135

134136
instrumentation.runOnMainSync(
@@ -149,13 +151,18 @@ public void onPlaybackStateChanged(@State int playbackState) {
149151
compositionPlayer.play();
150152
changedComposition.set(true);
151153
}
154+
} else if (playbackState == Player.STATE_ENDED) {
155+
playerEnded.open();
152156
}
153157
}
154158
});
155159
compositionPlayer.setComposition(createSingleSequenceComposition(mediaItem, mediaItem));
156160
compositionPlayer.prepare();
157161
});
158162

163+
// Wait until the final state is added to playerStates.
164+
playerEnded.block(TEST_TIMEOUT_MS);
165+
// waitUntilPlayerEnded should return immediate and will throw any player error.
159166
playerTestListener.waitUntilPlayerEnded();
160167
// Asserts that changing removeAudio does not cause the player to get back to buffering state,
161168
// because the player should not be re-prepared.

0 commit comments

Comments
 (0)