Search in sources :

Example 11 with PlaybackServiceMediaPlayer

use of de.danoeh.antennapod.playback.base.PlaybackServiceMediaPlayer in project AntennaPod by AntennaPod.

the class PlaybackServiceMediaPlayerTest method prepareTestSkeleton.

private void prepareTestSkeleton(final PlayerStatus initialState, long timeoutSeconds) throws InterruptedException {
    final Context c = getInstrumentation().getTargetContext();
    final int latchCount = 1;
    final CountDownLatch countDownLatch = new CountDownLatch(latchCount);
    CancelablePSMPCallback callback = new CancelablePSMPCallback(new DefaultPSMPCallback() {

        @Override
        public void statusChanged(LocalPSMP.PSMPInfo newInfo) {
            checkPSMPInfo(newInfo);
            if (newInfo.playerStatus == PlayerStatus.ERROR) {
                if (assertionError == null)
                    assertionError = new UnexpectedStateChange(newInfo.playerStatus);
            } else {
                if (initialState == PlayerStatus.INITIALIZED && newInfo.playerStatus == PlayerStatus.PREPARED) {
                    countDownLatch.countDown();
                } else if (initialState != PlayerStatus.INITIALIZED && initialState == newInfo.playerStatus) {
                    countDownLatch.countDown();
                }
            }
        }
    });
    PlaybackServiceMediaPlayer psmp = new LocalPSMP(c, callback);
    Playable p = writeTestPlayable(playableFileUrl, PLAYABLE_LOCAL_URL);
    if (initialState == PlayerStatus.INITIALIZED || initialState == PlayerStatus.PLAYING || initialState == PlayerStatus.PREPARED || initialState == PlayerStatus.PAUSED) {
        boolean prepareImmediately = (initialState != PlayerStatus.INITIALIZED);
        boolean startWhenPrepared = (initialState != PlayerStatus.PREPARED);
        psmp.playMediaObject(p, false, startWhenPrepared, prepareImmediately);
        if (initialState == PlayerStatus.PAUSED) {
            psmp.pause(false, false);
        }
        psmp.prepare();
    }
    boolean res = countDownLatch.await(timeoutSeconds, TimeUnit.SECONDS);
    if (initialState != PlayerStatus.INITIALIZED) {
        assertEquals(initialState, psmp.getPSMPInfo().playerStatus);
    }
    if (assertionError != null)
        throw assertionError;
    assertTrue(res);
    callback.cancel();
    psmp.shutdown();
}
Also used : Context(android.content.Context) Playable(de.danoeh.antennapod.model.playback.Playable) LocalPSMP(de.danoeh.antennapod.core.service.playback.LocalPSMP) PlaybackServiceMediaPlayer(de.danoeh.antennapod.playback.base.PlaybackServiceMediaPlayer) CountDownLatch(java.util.concurrent.CountDownLatch)

Example 12 with PlaybackServiceMediaPlayer

use of de.danoeh.antennapod.playback.base.PlaybackServiceMediaPlayer in project AntennaPod by AntennaPod.

the class PlaybackServiceMediaPlayerTest method testInit.

@Test
@UiThreadTest
public void testInit() {
    final Context c = getInstrumentation().getTargetContext();
    PlaybackServiceMediaPlayer psmp = new LocalPSMP(c, new DefaultPSMPCallback());
    psmp.shutdown();
}
Also used : Context(android.content.Context) LocalPSMP(de.danoeh.antennapod.core.service.playback.LocalPSMP) PlaybackServiceMediaPlayer(de.danoeh.antennapod.playback.base.PlaybackServiceMediaPlayer) MediumTest(androidx.test.filters.MediumTest) Test(org.junit.Test) UiThreadTest(androidx.test.annotation.UiThreadTest) UiThreadTest(androidx.test.annotation.UiThreadTest)

Example 13 with PlaybackServiceMediaPlayer

use of de.danoeh.antennapod.playback.base.PlaybackServiceMediaPlayer in project AntennaPod by AntennaPod.

the class PlaybackServiceMediaPlayerTest method pauseTestSkeleton.

private void pauseTestSkeleton(final PlayerStatus initialState, final boolean stream, final boolean abandonAudioFocus, final boolean reinit, long timeoutSeconds) throws InterruptedException {
    final Context c = getInstrumentation().getTargetContext();
    final int latchCount = (stream && reinit) ? 2 : 1;
    final CountDownLatch countDownLatch = new CountDownLatch(latchCount);
    CancelablePSMPCallback callback = new CancelablePSMPCallback(new DefaultPSMPCallback() {

        @Override
        public void statusChanged(LocalPSMP.PSMPInfo newInfo) {
            checkPSMPInfo(newInfo);
            if (newInfo.playerStatus == PlayerStatus.ERROR) {
                if (assertionError == null)
                    assertionError = new UnexpectedStateChange(newInfo.playerStatus);
            } else if (initialState != PlayerStatus.PLAYING) {
                if (assertionError == null)
                    assertionError = new UnexpectedStateChange(newInfo.playerStatus);
            } else {
                switch(newInfo.playerStatus) {
                    case PAUSED:
                        if (latchCount == countDownLatch.getCount())
                            countDownLatch.countDown();
                        else {
                            if (assertionError == null)
                                assertionError = new UnexpectedStateChange(newInfo.playerStatus);
                        }
                        break;
                    case INITIALIZED:
                        if (stream && reinit && countDownLatch.getCount() < latchCount) {
                            countDownLatch.countDown();
                        } else if (countDownLatch.getCount() < latchCount) {
                            if (assertionError == null)
                                assertionError = new UnexpectedStateChange(newInfo.playerStatus);
                        }
                        break;
                }
            }
        }

        @Override
        public void shouldStop() {
            if (assertionError == null)
                assertionError = new AssertionFailedError("Unexpected call to shouldStop");
        }
    });
    PlaybackServiceMediaPlayer psmp = new LocalPSMP(c, callback);
    Playable p = writeTestPlayable(playableFileUrl, PLAYABLE_LOCAL_URL);
    if (initialState == PlayerStatus.PLAYING) {
        psmp.playMediaObject(p, stream, true, true);
    }
    psmp.pause(abandonAudioFocus, reinit);
    boolean res = countDownLatch.await(timeoutSeconds, TimeUnit.SECONDS);
    if (assertionError != null)
        throw assertionError;
    assertTrue(res || initialState != PlayerStatus.PLAYING);
    callback.cancel();
    psmp.shutdown();
}
Also used : Context(android.content.Context) LocalPSMP(de.danoeh.antennapod.core.service.playback.LocalPSMP) PlaybackServiceMediaPlayer(de.danoeh.antennapod.playback.base.PlaybackServiceMediaPlayer) CountDownLatch(java.util.concurrent.CountDownLatch) Playable(de.danoeh.antennapod.model.playback.Playable) AssertionFailedError(junit.framework.AssertionFailedError)

Aggregations

Context (android.content.Context)13 LocalPSMP (de.danoeh.antennapod.core.service.playback.LocalPSMP)13 PlaybackServiceMediaPlayer (de.danoeh.antennapod.playback.base.PlaybackServiceMediaPlayer)13 CountDownLatch (java.util.concurrent.CountDownLatch)12 Playable (de.danoeh.antennapod.model.playback.Playable)11 UiThreadTest (androidx.test.annotation.UiThreadTest)9 MediumTest (androidx.test.filters.MediumTest)9 AssertionFailedError (junit.framework.AssertionFailedError)9 Test (org.junit.Test)9