summaryrefslogtreecommitdiff
path: root/app/src/androidTest/java/de/test/antennapod/service
diff options
context:
space:
mode:
authordaniel oeh <daniel.oeh@gmail.com>2014-11-19 16:50:50 +0100
committerdaniel oeh <daniel.oeh@gmail.com>2014-11-19 16:50:50 +0100
commit563ceaf39332943b1733036f5c3b9087c481d313 (patch)
tree2a77340ec440b030524f28e0e1fcb98196ba94fb /app/src/androidTest/java/de/test/antennapod/service
parentbaa7d5f11283cb7668d45b561af5d38f0ccb9632 (diff)
parent9588747edda50cd98374a599b47d4be60c562d59 (diff)
downloadAntennaPod-563ceaf39332943b1733036f5c3b9087c481d313.zip
Merge branch 'develop'0.9.9.5
Diffstat (limited to 'app/src/androidTest/java/de/test/antennapod/service')
-rw-r--r--app/src/androidTest/java/de/test/antennapod/service/download/HttpDownloaderTest.java170
-rw-r--r--app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceMediaPlayerTest.java1177
-rw-r--r--app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java333
3 files changed, 1680 insertions, 0 deletions
diff --git a/app/src/androidTest/java/de/test/antennapod/service/download/HttpDownloaderTest.java b/app/src/androidTest/java/de/test/antennapod/service/download/HttpDownloaderTest.java
new file mode 100644
index 000000000..65b1145a2
--- /dev/null
+++ b/app/src/androidTest/java/de/test/antennapod/service/download/HttpDownloaderTest.java
@@ -0,0 +1,170 @@
+package de.test.antennapod.service.download;
+
+import android.test.InstrumentationTestCase;
+import android.util.Log;
+import de.danoeh.antennapod.core.feed.FeedFile;
+import de.danoeh.antennapod.core.service.download.DownloadRequest;
+import de.danoeh.antennapod.core.service.download.DownloadStatus;
+import de.danoeh.antennapod.core.service.download.Downloader;
+import de.danoeh.antennapod.core.service.download.HttpDownloader;
+import de.danoeh.antennapod.core.util.DownloadError;
+import de.test.antennapod.util.service.download.HTTPBin;
+
+import java.io.File;
+import java.io.IOException;
+
+public class HttpDownloaderTest extends InstrumentationTestCase {
+ private static final String TAG = "HttpDownloaderTest";
+ private static final String DOWNLOAD_DIR = "testdownloads";
+
+ private static boolean successful = true;
+
+ private File destDir;
+
+ private HTTPBin httpServer;
+
+ public HttpDownloaderTest() {
+ super();
+ }
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ File[] contents = destDir.listFiles();
+ for (File f : contents) {
+ assertTrue(f.delete());
+ }
+
+ httpServer.stop();
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ destDir = getInstrumentation().getTargetContext().getExternalFilesDir(DOWNLOAD_DIR);
+ assertNotNull(destDir);
+ assertTrue(destDir.exists());
+ httpServer = new HTTPBin();
+ httpServer.start();
+ }
+
+ private FeedFileImpl setupFeedFile(String downloadUrl, String title, boolean deleteExisting) {
+ FeedFileImpl feedfile = new FeedFileImpl(downloadUrl);
+ String fileUrl = new File(destDir, title).getAbsolutePath();
+ File file = new File(fileUrl);
+ if (deleteExisting) {
+ Log.d(TAG, "Deleting file: " + file.delete());
+ }
+ feedfile.setFile_url(fileUrl);
+ return feedfile;
+ }
+
+ private Downloader download(String url, String title, boolean expectedResult) {
+ return download(url, title, expectedResult, true, null, null, true);
+ }
+
+ private Downloader download(String url, String title, boolean expectedResult, boolean deleteExisting, String username, String password, boolean deleteOnFail) {
+ FeedFile feedFile = setupFeedFile(url, title, deleteExisting);
+ DownloadRequest request = new DownloadRequest(feedFile.getFile_url(), url, title, 0, feedFile.getTypeAsInt(), username, password, deleteOnFail, null);
+ Downloader downloader = new HttpDownloader(request);
+ downloader.call();
+ DownloadStatus status = downloader.getResult();
+ assertNotNull(status);
+ assertTrue(status.isSuccessful() == expectedResult);
+ assertTrue(status.isDone());
+ // the file should not exist if the download has failed and deleteExisting was true
+ assertTrue(!deleteExisting || new File(feedFile.getFile_url()).exists() == expectedResult);
+ return downloader;
+ }
+
+
+ private static final String URL_404 = HTTPBin.BASE_URL + "/status/404";
+ private static final String URL_AUTH = HTTPBin.BASE_URL + "/basic-auth/user/passwd";
+
+ public void testPassingHttp() {
+ download(HTTPBin.BASE_URL + "/status/200", "test200", true);
+ }
+
+ public void testRedirect() {
+ download(HTTPBin.BASE_URL + "/redirect/4", "testRedirect", true);
+ }
+
+ public void testGzip() {
+ download("http://httpbin.org/gzip", "testGzip", true);
+ }
+
+ public void test404() {
+ download(URL_404, "test404", false);
+ }
+
+ public void testCancel() {
+ final String url = HTTPBin.BASE_URL + "/delay/3";
+ FeedFileImpl feedFile = setupFeedFile(url, "delay", true);
+ final Downloader downloader = new HttpDownloader(new DownloadRequest(feedFile.getFile_url(), url, "delay", 0, feedFile.getTypeAsInt()));
+ Thread t = new Thread() {
+ @Override
+ public void run() {
+ downloader.call();
+ }
+ };
+ t.start();
+ downloader.cancel();
+ try {
+ t.join();
+ } catch (InterruptedException e) {
+ e.printStackTrace();
+ }
+ DownloadStatus result = downloader.getResult();
+ assertTrue(result.isDone());
+ assertFalse(result.isSuccessful());
+ assertTrue(result.isCancelled());
+ assertFalse(new File(feedFile.getFile_url()).exists());
+ }
+
+ public void testDeleteOnFailShouldDelete() {
+ Downloader downloader = download(URL_404, "testDeleteOnFailShouldDelete", false, true, null, null, true);
+ assertFalse(new File(downloader.getDownloadRequest().getDestination()).exists());
+ }
+
+ public void testDeleteOnFailShouldNotDelete() throws IOException {
+ String filename = "testDeleteOnFailShouldDelete";
+ File dest = new File(destDir, filename);
+ dest.delete();
+ assertTrue(dest.createNewFile());
+ Downloader downloader = download(URL_404, filename, false, false, null, null, false);
+ assertTrue(new File(downloader.getDownloadRequest().getDestination()).exists());
+ }
+
+ public void testAuthenticationShouldSucceed() throws InterruptedException {
+ download(URL_AUTH, "testAuthSuccess", true, true, "user", "passwd", true);
+ }
+
+ public void testAuthenticationShouldFail() {
+ Downloader downloader = download(URL_AUTH, "testAuthSuccess", false, true, "user", "Wrong passwd", true);
+ assertEquals(DownloadError.ERROR_UNAUTHORIZED, downloader.getResult().getReason());
+ }
+
+ /* TODO: replace with smaller test file
+ public void testUrlWithSpaces() {
+ download("http://acedl.noxsolutions.com/ace/Don't Call Salman Rushdie Sneezy in Finland.mp3", "testUrlWithSpaces", true);
+ }
+ */
+
+ private static class FeedFileImpl extends FeedFile {
+ public FeedFileImpl(String download_url) {
+ super(null, download_url, false);
+ }
+
+
+ @Override
+ public String getHumanReadableIdentifier() {
+ return download_url;
+ }
+
+ @Override
+ public int getTypeAsInt() {
+ return 0;
+ }
+ }
+
+}
diff --git a/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceMediaPlayerTest.java b/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceMediaPlayerTest.java
new file mode 100644
index 000000000..aac4c245a
--- /dev/null
+++ b/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceMediaPlayerTest.java
@@ -0,0 +1,1177 @@
+package de.test.antennapod.service.playback;
+
+import android.content.Context;
+import android.media.RemoteControlClient;
+import android.test.InstrumentationTestCase;
+import de.danoeh.antennapod.core.feed.Feed;
+import de.danoeh.antennapod.core.feed.FeedItem;
+import de.danoeh.antennapod.core.feed.FeedMedia;
+import de.danoeh.antennapod.core.service.playback.PlaybackServiceMediaPlayer;
+import de.danoeh.antennapod.core.service.playback.PlayerStatus;
+import de.danoeh.antennapod.core.storage.PodDBAdapter;
+import de.danoeh.antennapod.core.util.playback.Playable;
+import de.test.antennapod.util.service.download.HTTPBin;
+import junit.framework.AssertionFailedError;
+import org.apache.commons.io.IOUtils;
+
+import java.io.File;
+import java.io.FileOutputStream;
+import java.io.InputStream;
+import java.io.OutputStream;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Test class for PlaybackServiceMediaPlayer
+ */
+public class PlaybackServiceMediaPlayerTest extends InstrumentationTestCase {
+ private static final String TAG = "PlaybackServiceMediaPlayerTest";
+
+ private static final String PLAYABLE_FILE_URL = "http://127.0.0.1:" + HTTPBin.PORT + "/files/0";
+ private static final String PLAYABLE_DEST_URL = "psmptestfile.mp3";
+ private String PLAYABLE_LOCAL_URL = null;
+ private static final int LATCH_TIMEOUT_SECONDS = 10;
+
+ private HTTPBin httpServer;
+
+ private volatile AssertionFailedError assertionError;
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ PodDBAdapter.deleteDatabase(getInstrumentation().getTargetContext());
+ httpServer.stop();
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ assertionError = null;
+
+ final Context context = getInstrumentation().getTargetContext();
+ context.deleteDatabase(PodDBAdapter.DATABASE_NAME);
+ // make sure database is created
+ PodDBAdapter adapter = new PodDBAdapter(context);
+ adapter.open();
+ adapter.close();
+
+ httpServer = new HTTPBin();
+ httpServer.start();
+
+ File cacheDir = context.getExternalFilesDir("testFiles");
+ if (cacheDir == null)
+ cacheDir = context.getExternalFilesDir("testFiles");
+ File dest = new File(cacheDir, PLAYABLE_DEST_URL);
+
+ assertNotNull(cacheDir);
+ assertTrue(cacheDir.canWrite());
+ assertTrue(cacheDir.canRead());
+ if (!dest.exists()) {
+ InputStream i = getInstrumentation().getContext().getAssets().open("testfile.mp3");
+ OutputStream o = new FileOutputStream(new File(cacheDir, PLAYABLE_DEST_URL));
+ IOUtils.copy(i, o);
+ o.flush();
+ o.close();
+ i.close();
+ }
+ PLAYABLE_LOCAL_URL = "file://" + dest.getAbsolutePath();
+ assertEquals(0, httpServer.serveFile(dest));
+ }
+
+ private void checkPSMPInfo(PlaybackServiceMediaPlayer.PSMPInfo info) {
+ try {
+ switch (info.playerStatus) {
+ case PLAYING:
+ case PAUSED:
+ case PREPARED:
+ case PREPARING:
+ case INITIALIZED:
+ case INITIALIZING:
+ case SEEKING:
+ assertNotNull(info.playable);
+ break;
+ case STOPPED:
+ assertNull(info.playable);
+ break;
+ case ERROR:
+ assertNull(info.playable);
+ }
+ } catch (AssertionFailedError e) {
+ if (assertionError == null)
+ assertionError = e;
+ }
+ }
+
+ public void testInit() {
+ final Context c = getInstrumentation().getTargetContext();
+ PlaybackServiceMediaPlayer psmp = new PlaybackServiceMediaPlayer(c, defaultCallback);
+ psmp.shutdown();
+ }
+
+ private Playable writeTestPlayable(String downloadUrl, String fileUrl) {
+ final Context c = getInstrumentation().getTargetContext();
+ Feed f = new Feed(0, new Date(), "f", "l", "d", null, null, null, null, "i", null, null, "l", false);
+ f.setItems(new ArrayList<FeedItem>());
+ FeedItem i = new FeedItem(0, "t", "i", "l", new Date(), false, f);
+ f.getItems().add(i);
+ FeedMedia media = new FeedMedia(0, i, 0, 0, 0, "audio/wav", fileUrl, downloadUrl, fileUrl != null, null, 0);
+ i.setMedia(media);
+ PodDBAdapter adapter = new PodDBAdapter(c);
+ adapter.open();
+ adapter.setCompleteFeed(f);
+ assertTrue(media.getId() != 0);
+ adapter.close();
+ return media;
+ }
+
+
+ public void testPlayMediaObjectStreamNoStartNoPrepare() throws InterruptedException {
+ final Context c = getInstrumentation().getTargetContext();
+ final CountDownLatch countDownLatch = new CountDownLatch(2);
+ PlaybackServiceMediaPlayer.PSMPCallback callback = new PlaybackServiceMediaPlayer.PSMPCallback() {
+ @Override
+ public void statusChanged(PlaybackServiceMediaPlayer.PSMPInfo newInfo) {
+ try {
+ checkPSMPInfo(newInfo);
+ if (newInfo.playerStatus == PlayerStatus.ERROR)
+ throw new IllegalStateException("MediaPlayer error");
+ if (countDownLatch.getCount() == 0) {
+ fail();
+ } else if (countDownLatch.getCount() == 2) {
+ assertEquals(PlayerStatus.INITIALIZING, newInfo.playerStatus);
+ countDownLatch.countDown();
+ } else {
+ assertEquals(PlayerStatus.INITIALIZED, newInfo.playerStatus);
+ countDownLatch.countDown();
+ }
+ } catch (AssertionFailedError e) {
+ if (assertionError == null)
+ assertionError = e;
+ }
+ }
+
+ @Override
+ public void shouldStop() {
+
+ }
+
+ @Override
+ public void playbackSpeedChanged(float s) {
+
+ }
+
+ @Override
+ public void onBufferingUpdate(int percent) {
+
+ }
+
+ @Override
+ public boolean onMediaPlayerInfo(int code) {
+ return false;
+ }
+
+ @Override
+ public boolean onMediaPlayerError(Object inObj, int what, int extra) {
+ return false;
+ }
+
+ @Override
+ public boolean endPlayback(boolean playNextEpisode) {
+ return false;
+ }
+
+ @Override
+ public RemoteControlClient getRemoteControlClient() {
+ return null;
+ }
+ };
+ PlaybackServiceMediaPlayer psmp = new PlaybackServiceMediaPlayer(c, callback);
+ Playable p = writeTestPlayable(PLAYABLE_FILE_URL, null);
+ psmp.playMediaObject(p, true, false, false);
+ boolean res = countDownLatch.await(LATCH_TIMEOUT_SECONDS, TimeUnit.SECONDS);
+ if (assertionError != null)
+ throw assertionError;
+ assertTrue(res);
+
+ assertTrue(psmp.getPSMPInfo().playerStatus == PlayerStatus.INITIALIZED);
+ assertFalse(psmp.isStartWhenPrepared());
+ psmp.shutdown();
+ }
+
+ public void testPlayMediaObjectStreamStartNoPrepare() throws InterruptedException {
+ final Context c = getInstrumentation().getTargetContext();
+ final CountDownLatch countDownLatch = new CountDownLatch(2);
+ PlaybackServiceMediaPlayer.PSMPCallback callback = new PlaybackServiceMediaPlayer.PSMPCallback() {
+ @Override
+ public void statusChanged(PlaybackServiceMediaPlayer.PSMPInfo newInfo) {
+ try {
+ checkPSMPInfo(newInfo);
+ if (newInfo.playerStatus == PlayerStatus.ERROR)
+ throw new IllegalStateException("MediaPlayer error");
+ if (countDownLatch.getCount() == 0) {
+ fail();
+ } else if (countDownLatch.getCount() == 2) {
+ assertEquals(PlayerStatus.INITIALIZING, newInfo.playerStatus);
+ countDownLatch.countDown();
+ } else {
+ assertEquals(PlayerStatus.INITIALIZED, newInfo.playerStatus);
+ countDownLatch.countDown();
+ }
+ } catch (AssertionFailedError e) {
+ if (assertionError == null)
+ assertionError = e;
+ }
+ }
+
+ @Override
+ public void shouldStop() {
+
+ }
+
+ @Override
+ public void playbackSpeedChanged(float s) {
+
+ }
+
+ @Override
+ public void onBufferingUpdate(int percent) {
+
+ }
+
+ @Override
+ public boolean onMediaPlayerInfo(int code) {
+ return false;
+ }
+
+ @Override
+ public boolean onMediaPlayerError(Object inObj, int what, int extra) {
+ return false;
+ }
+
+ @Override
+ public boolean endPlayback(boolean playNextEpisode) {
+ return false;
+ }
+
+ @Override
+ public RemoteControlClient getRemoteControlClient() {
+ return null;
+ }
+ };
+ PlaybackServiceMediaPlayer psmp = new PlaybackServiceMediaPlayer(c, callback);
+ Playable p = writeTestPlayable(PLAYABLE_FILE_URL, null);
+ psmp.playMediaObject(p, true, true, false);
+
+ boolean res = countDownLatch.await(LATCH_TIMEOUT_SECONDS, TimeUnit.SECONDS);
+ if (assertionError != null)
+ throw assertionError;
+ assertTrue(res);
+
+ assertTrue(psmp.getPSMPInfo().playerStatus == PlayerStatus.INITIALIZED);
+ assertTrue(psmp.isStartWhenPrepared());
+ psmp.shutdown();
+ }
+
+ public void testPlayMediaObjectStreamNoStartPrepare() throws InterruptedException {
+ final Context c = getInstrumentation().getTargetContext();
+ final CountDownLatch countDownLatch = new CountDownLatch(4);
+ PlaybackServiceMediaPlayer.PSMPCallback callback = new PlaybackServiceMediaPlayer.PSMPCallback() {
+ @Override
+ public void statusChanged(PlaybackServiceMediaPlayer.PSMPInfo newInfo) {
+ try {
+ checkPSMPInfo(newInfo);
+ if (newInfo.playerStatus == PlayerStatus.ERROR)
+ throw new IllegalStateException("MediaPlayer error");
+ if (countDownLatch.getCount() == 0) {
+ fail();
+ } else if (countDownLatch.getCount() == 4) {
+ assertEquals(PlayerStatus.INITIALIZING, newInfo.playerStatus);
+ } else if (countDownLatch.getCount() == 3) {
+ assertEquals(PlayerStatus.INITIALIZED, newInfo.playerStatus);
+ } else if (countDownLatch.getCount() == 2) {
+ assertEquals(PlayerStatus.PREPARING, newInfo.playerStatus);
+ } else if (countDownLatch.getCount() == 1) {
+ assertEquals(PlayerStatus.PREPARED, newInfo.playerStatus);
+ }
+ countDownLatch.countDown();
+ } catch (AssertionFailedError e) {
+ if (assertionError == null)
+ assertionError = e;
+ }
+ }
+
+ @Override
+ public void shouldStop() {
+
+ }
+
+ @Override
+ public void playbackSpeedChanged(float s) {
+
+ }
+
+ @Override
+ public void onBufferingUpdate(int percent) {
+
+ }
+
+ @Override
+ public boolean onMediaPlayerInfo(int code) {
+ return false;
+ }
+
+ @Override
+ public boolean onMediaPlayerError(Object inObj, int what, int extra) {
+ return false;
+ }
+
+ @Override
+ public boolean endPlayback(boolean playNextEpisode) {
+ return false;
+ }
+
+ @Override
+ public RemoteControlClient getRemoteControlClient() {
+ return null;
+ }
+ };
+ PlaybackServiceMediaPlayer psmp = new PlaybackServiceMediaPlayer(c, callback);
+ Playable p = writeTestPlayable(PLAYABLE_FILE_URL, null);
+ psmp.playMediaObject(p, true, false, true);
+ boolean res = countDownLatch.await(LATCH_TIMEOUT_SECONDS, TimeUnit.SECONDS);
+ if (assertionError != null)
+ throw assertionError;
+ assertTrue(res);
+ assertTrue(psmp.getPSMPInfo().playerStatus == PlayerStatus.PREPARED);
+
+ psmp.shutdown();
+ }
+
+ public void testPlayMediaObjectStreamStartPrepare() throws InterruptedException {
+ final Context c = getInstrumentation().getTargetContext();
+ final CountDownLatch countDownLatch = new CountDownLatch(5);
+ PlaybackServiceMediaPlayer.PSMPCallback callback = new PlaybackServiceMediaPlayer.PSMPCallback() {
+ @Override
+ public void statusChanged(PlaybackServiceMediaPlayer.PSMPInfo newInfo) {
+ try {
+ checkPSMPInfo(newInfo);
+ if (newInfo.playerStatus == PlayerStatus.ERROR)
+ throw new IllegalStateException("MediaPlayer error");
+ if (countDownLatch.getCount() == 0) {
+ fail();
+
+ } else if (countDownLatch.getCount() == 5) {
+ assertEquals(PlayerStatus.INITIALIZING, newInfo.playerStatus);
+ } else if (countDownLatch.getCount() == 4) {
+ assertEquals(PlayerStatus.INITIALIZED, newInfo.playerStatus);
+ } else if (countDownLatch.getCount() == 3) {
+ assertEquals(PlayerStatus.PREPARING, newInfo.playerStatus);
+ } else if (countDownLatch.getCount() == 2) {
+ assertEquals(PlayerStatus.PREPARED, newInfo.playerStatus);
+ } else if (countDownLatch.getCount() == 1) {
+ assertEquals(PlayerStatus.PLAYING, newInfo.playerStatus);
+ }
+ countDownLatch.countDown();
+ } catch (AssertionFailedError e) {
+ if (assertionError == null)
+ assertionError = e;
+ }
+ }
+
+ @Override
+ public void shouldStop() {
+
+ }
+
+ @Override
+ public void playbackSpeedChanged(float s) {
+
+ }
+
+ @Override
+ public void onBufferingUpdate(int percent) {
+
+ }
+
+ @Override
+ public boolean onMediaPlayerInfo(int code) {
+ return false;
+ }
+
+ @Override
+ public boolean onMediaPlayerError(Object inObj, int what, int extra) {
+ return false;
+ }
+
+ @Override
+ public boolean endPlayback(boolean playNextEpisode) {
+ return false;
+ }
+
+ @Override
+ public RemoteControlClient getRemoteControlClient() {
+ return null;
+ }
+ };
+ PlaybackServiceMediaPlayer psmp = new PlaybackServiceMediaPlayer(c, callback);
+ Playable p = writeTestPlayable(PLAYABLE_FILE_URL, null);
+ psmp.playMediaObject(p, true, true, true);
+ boolean res = countDownLatch.await(LATCH_TIMEOUT_SECONDS, TimeUnit.SECONDS);
+ if (assertionError != null)
+ throw assertionError;
+ assertTrue(res);
+ assertTrue(psmp.getPSMPInfo().playerStatus == PlayerStatus.PLAYING);
+ psmp.shutdown();
+ }
+
+ public void testPlayMediaObjectLocalNoStartNoPrepare() throws InterruptedException {
+ final Context c = getInstrumentation().getTargetContext();
+ final CountDownLatch countDownLatch = new CountDownLatch(2);
+ PlaybackServiceMediaPlayer.PSMPCallback callback = new PlaybackServiceMediaPlayer.PSMPCallback() {
+ @Override
+ public void statusChanged(PlaybackServiceMediaPlayer.PSMPInfo newInfo) {
+ try {
+ checkPSMPInfo(newInfo);
+ if (newInfo.playerStatus == PlayerStatus.ERROR)
+ throw new IllegalStateException("MediaPlayer error");
+ if (countDownLatch.getCount() == 0) {
+ fail();
+ } else if (countDownLatch.getCount() == 2) {
+ assertEquals(PlayerStatus.INITIALIZING, newInfo.playerStatus);
+ countDownLatch.countDown();
+ } else {
+ assertEquals(PlayerStatus.INITIALIZED, newInfo.playerStatus);
+ countDownLatch.countDown();
+ }
+ } catch (AssertionFailedError e) {
+ if (assertionError == null)
+ assertionError = e;
+ }
+ }
+
+ @Override
+ public void shouldStop() {
+
+ }
+
+ @Override
+ public void playbackSpeedChanged(float s) {
+
+ }
+
+ @Override
+ public void onBufferingUpdate(int percent) {
+
+ }
+
+ @Override
+ public boolean onMediaPlayerInfo(int code) {
+ return false;
+ }
+
+ @Override
+ public boolean onMediaPlayerError(Object inObj, int what, int extra) {
+ return false;
+ }
+
+ @Override
+ public boolean endPlayback(boolean playNextEpisode) {
+ return false;
+ }
+
+ @Override
+ public RemoteControlClient getRemoteControlClient() {
+ return null;
+ }
+ };
+ PlaybackServiceMediaPlayer psmp = new PlaybackServiceMediaPlayer(c, callback);
+ Playable p = writeTestPlayable(PLAYABLE_FILE_URL, PLAYABLE_LOCAL_URL);
+ psmp.playMediaObject(p, false, false, false);
+ boolean res = countDownLatch.await(LATCH_TIMEOUT_SECONDS, TimeUnit.SECONDS);
+ if (assertionError != null)
+ throw assertionError;
+ assertTrue(res);
+ assertTrue(psmp.getPSMPInfo().playerStatus == PlayerStatus.INITIALIZED);
+ assertFalse(psmp.isStartWhenPrepared());
+ psmp.shutdown();
+ }
+
+ public void testPlayMediaObjectLocalStartNoPrepare() throws InterruptedException {
+ final Context c = getInstrumentation().getTargetContext();
+ final CountDownLatch countDownLatch = new CountDownLatch(2);
+ PlaybackServiceMediaPlayer.PSMPCallback callback = new PlaybackServiceMediaPlayer.PSMPCallback() {
+ @Override
+ public void statusChanged(PlaybackServiceMediaPlayer.PSMPInfo newInfo) {
+ try {
+ checkPSMPInfo(newInfo);
+ if (newInfo.playerStatus == PlayerStatus.ERROR)
+ throw new IllegalStateException("MediaPlayer error");
+ if (countDownLatch.getCount() == 0) {
+ fail();
+ } else if (countDownLatch.getCount() == 2) {
+ assertEquals(PlayerStatus.INITIALIZING, newInfo.playerStatus);
+ countDownLatch.countDown();
+ } else {
+ assertEquals(PlayerStatus.INITIALIZED, newInfo.playerStatus);
+ countDownLatch.countDown();
+ }
+ } catch (AssertionFailedError e) {
+ if (assertionError == null)
+ assertionError = e;
+ }
+ }
+
+ @Override
+ public void shouldStop() {
+
+ }
+
+ @Override
+ public void playbackSpeedChanged(float s) {
+
+ }
+
+ @Override
+ public void onBufferingUpdate(int percent) {
+
+ }
+
+ @Override
+ public boolean onMediaPlayerInfo(int code) {
+ return false;
+ }
+
+ @Override
+ public boolean onMediaPlayerError(Object inObj, int what, int extra) {
+ return false;
+ }
+
+ @Override
+ public boolean endPlayback(boolean playNextEpisode) {
+ return false;
+ }
+
+ @Override
+ public RemoteControlClient getRemoteControlClient() {
+ return null;
+ }
+ };
+ PlaybackServiceMediaPlayer psmp = new PlaybackServiceMediaPlayer(c, callback);
+ Playable p = writeTestPlayable(PLAYABLE_FILE_URL, PLAYABLE_LOCAL_URL);
+ psmp.playMediaObject(p, false, true, false);
+ boolean res = countDownLatch.await(LATCH_TIMEOUT_SECONDS, TimeUnit.SECONDS);
+ if (assertionError != null)
+ throw assertionError;
+ assertTrue(res);
+ assertTrue(psmp.getPSMPInfo().playerStatus == PlayerStatus.INITIALIZED);
+ assertTrue(psmp.isStartWhenPrepared());
+ psmp.shutdown();
+ }
+
+ public void testPlayMediaObjectLocalNoStartPrepare() throws InterruptedException {
+ final Context c = getInstrumentation().getTargetContext();
+ final CountDownLatch countDownLatch = new CountDownLatch(4);
+ PlaybackServiceMediaPlayer.PSMPCallback callback = new PlaybackServiceMediaPlayer.PSMPCallback() {
+ @Override
+ public void statusChanged(PlaybackServiceMediaPlayer.PSMPInfo newInfo) {
+ try {
+ checkPSMPInfo(newInfo);
+ if (newInfo.playerStatus == PlayerStatus.ERROR)
+ throw new IllegalStateException("MediaPlayer error");
+ if (countDownLatch.getCount() == 0) {
+ fail();
+ } else if (countDownLatch.getCount() == 4) {
+ assertEquals(PlayerStatus.INITIALIZING, newInfo.playerStatus);
+ } else if (countDownLatch.getCount() == 3) {
+ assertEquals(PlayerStatus.INITIALIZED, newInfo.playerStatus);
+ } else if (countDownLatch.getCount() == 2) {
+ assertEquals(PlayerStatus.PREPARING, newInfo.playerStatus);
+ } else if (countDownLatch.getCount() == 1) {
+ assertEquals(PlayerStatus.PREPARED, newInfo.playerStatus);
+ }
+ countDownLatch.countDown();
+ } catch (AssertionFailedError e) {
+ if (assertionError == null)
+ assertionError = e;
+ }
+ }
+
+ @Override
+ public void shouldStop() {
+
+ }
+
+ @Override
+ public void playbackSpeedChanged(float s) {
+
+ }
+
+ @Override
+ public void onBufferingUpdate(int percent) {
+
+ }
+
+ @Override
+ public boolean onMediaPlayerInfo(int code) {
+ return false;
+ }
+
+ @Override
+ public boolean onMediaPlayerError(Object inObj, int what, int extra) {
+ return false;
+ }
+
+ @Override
+ public boolean endPlayback(boolean playNextEpisode) {
+ return false;
+ }
+
+ @Override
+ public RemoteControlClient getRemoteControlClient() {
+ return null;
+ }
+ };
+ PlaybackServiceMediaPlayer psmp = new PlaybackServiceMediaPlayer(c, callback);
+ Playable p = writeTestPlayable(PLAYABLE_FILE_URL, PLAYABLE_LOCAL_URL);
+ psmp.playMediaObject(p, false, false, true);
+ boolean res = countDownLatch.await(LATCH_TIMEOUT_SECONDS, TimeUnit.SECONDS);
+ if (assertionError != null)
+ throw assertionError;
+ assertTrue(res);
+ assertTrue(psmp.getPSMPInfo().playerStatus == PlayerStatus.PREPARED);
+ psmp.shutdown();
+ }
+
+ public void testPlayMediaObjectLocalStartPrepare() throws InterruptedException {
+ final Context c = getInstrumentation().getTargetContext();
+ final CountDownLatch countDownLatch = new CountDownLatch(5);
+ PlaybackServiceMediaPlayer.PSMPCallback callback = new PlaybackServiceMediaPlayer.PSMPCallback() {
+ @Override
+ public void statusChanged(PlaybackServiceMediaPlayer.PSMPInfo newInfo) {
+ try {
+ checkPSMPInfo(newInfo);
+ if (newInfo.playerStatus == PlayerStatus.ERROR)
+ throw new IllegalStateException("MediaPlayer error");
+ if (countDownLatch.getCount() == 0) {
+ fail();
+ } else if (countDownLatch.getCount() == 5) {
+ assertEquals(PlayerStatus.INITIALIZING, newInfo.playerStatus);
+ } else if (countDownLatch.getCount() == 4) {
+ assertEquals(PlayerStatus.INITIALIZED, newInfo.playerStatus);
+ } else if (countDownLatch.getCount() == 3) {
+ assertEquals(PlayerStatus.PREPARING, newInfo.playerStatus);
+ } else if (countDownLatch.getCount() == 2) {
+ assertEquals(PlayerStatus.PREPARED, newInfo.playerStatus);
+ } else if (countDownLatch.getCount() == 1) {
+ assertEquals(PlayerStatus.PLAYING, newInfo.playerStatus);
+ }
+
+ } catch (AssertionFailedError e) {
+ if (assertionError == null)
+ assertionError = e;
+ } finally {
+ countDownLatch.countDown();
+ }
+ }
+
+ @Override
+ public void shouldStop() {
+
+ }
+
+ @Override
+ public void playbackSpeedChanged(float s) {
+
+ }
+
+ @Override
+ public void onBufferingUpdate(int percent) {
+
+ }
+
+ @Override
+ public boolean onMediaPlayerInfo(int code) {
+ return false;
+ }
+
+ @Override
+ public boolean onMediaPlayerError(Object inObj, int what, int extra) {
+ return false;
+ }
+
+ @Override
+ public boolean endPlayback(boolean playNextEpisode) {
+ return false;
+ }
+
+ @Override
+ public RemoteControlClient getRemoteControlClient() {
+ return null;
+ }
+ };
+ PlaybackServiceMediaPlayer psmp = new PlaybackServiceMediaPlayer(c, callback);
+ Playable p = writeTestPlayable(PLAYABLE_FILE_URL, PLAYABLE_LOCAL_URL);
+ psmp.playMediaObject(p, false, true, true);
+ boolean res = countDownLatch.await(LATCH_TIMEOUT_SECONDS, TimeUnit.SECONDS);
+ if (assertionError != null)
+ throw assertionError;
+ assertTrue(res);
+ assertTrue(psmp.getPSMPInfo().playerStatus == PlayerStatus.PLAYING);
+ psmp.shutdown();
+ }
+
+
+ private final PlaybackServiceMediaPlayer.PSMPCallback defaultCallback = new PlaybackServiceMediaPlayer.PSMPCallback() {
+ @Override
+ public void statusChanged(PlaybackServiceMediaPlayer.PSMPInfo newInfo) {
+ checkPSMPInfo(newInfo);
+ }
+
+ @Override
+ public void shouldStop() {
+
+ }
+
+ @Override
+ public void playbackSpeedChanged(float s) {
+
+ }
+
+ @Override
+ public void onBufferingUpdate(int percent) {
+
+ }
+
+ @Override
+ public boolean onMediaPlayerInfo(int code) {
+ return false;
+ }
+
+ @Override
+ public boolean onMediaPlayerError(Object inObj, int what, int extra) {
+ return false;
+ }
+
+ @Override
+ public boolean endPlayback(boolean playNextEpisode) {
+ return false;
+ }
+
+ @Override
+ public RemoteControlClient getRemoteControlClient() {
+ return null;
+ }
+ };
+
+ 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);
+
+ PlaybackServiceMediaPlayer.PSMPCallback callback = new PlaybackServiceMediaPlayer.PSMPCallback() {
+ @Override
+ public void statusChanged(PlaybackServiceMediaPlayer.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");
+ }
+
+ @Override
+ public void playbackSpeedChanged(float s) {
+
+ }
+
+ @Override
+ public void onBufferingUpdate(int percent) {
+
+ }
+
+ @Override
+ public boolean onMediaPlayerInfo(int code) {
+ return false;
+ }
+
+ @Override
+ public boolean onMediaPlayerError(Object inObj, int what, int extra) {
+ if (assertionError == null)
+ assertionError = new AssertionFailedError("Unexpected call to onMediaPlayerError");
+ return false;
+ }
+
+ @Override
+ public boolean endPlayback(boolean playNextEpisode) {
+ return false;
+ }
+
+ @Override
+ public RemoteControlClient getRemoteControlClient() {
+ return null;
+ }
+ };
+ PlaybackServiceMediaPlayer psmp = new PlaybackServiceMediaPlayer(c, callback);
+ Playable p = writeTestPlayable(PLAYABLE_FILE_URL, 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);
+ psmp.shutdown();
+ }
+
+ public void testPauseDefaultState() throws InterruptedException {
+ pauseTestSkeleton(PlayerStatus.STOPPED, false, false, false, 1);
+ }
+
+ public void testPausePlayingStateNoAbandonNoReinitNoStream() throws InterruptedException {
+ pauseTestSkeleton(PlayerStatus.PLAYING, false, false, false, LATCH_TIMEOUT_SECONDS);
+ }
+
+ public void testPausePlayingStateNoAbandonNoReinitStream() throws InterruptedException {
+ pauseTestSkeleton(PlayerStatus.PLAYING, true, false, false, LATCH_TIMEOUT_SECONDS);
+ }
+
+ public void testPausePlayingStateAbandonNoReinitNoStream() throws InterruptedException {
+ pauseTestSkeleton(PlayerStatus.PLAYING, false, true, false, LATCH_TIMEOUT_SECONDS);
+ }
+
+ public void testPausePlayingStateAbandonNoReinitStream() throws InterruptedException {
+ pauseTestSkeleton(PlayerStatus.PLAYING, true, true, false, LATCH_TIMEOUT_SECONDS);
+ }
+
+ public void testPausePlayingStateNoAbandonReinitNoStream() throws InterruptedException {
+ pauseTestSkeleton(PlayerStatus.PLAYING, false, false, true, LATCH_TIMEOUT_SECONDS);
+ }
+
+ public void testPausePlayingStateNoAbandonReinitStream() throws InterruptedException {
+ pauseTestSkeleton(PlayerStatus.PLAYING, true, false, true, LATCH_TIMEOUT_SECONDS);
+ }
+
+ public void testPausePlayingStateAbandonReinitNoStream() throws InterruptedException {
+ pauseTestSkeleton(PlayerStatus.PLAYING, false, true, true, LATCH_TIMEOUT_SECONDS);
+ }
+
+ public void testPausePlayingStateAbandonReinitStream() throws InterruptedException {
+ pauseTestSkeleton(PlayerStatus.PLAYING, true, true, true, LATCH_TIMEOUT_SECONDS);
+ }
+
+ private void resumeTestSkeleton(final PlayerStatus initialState, long timeoutSeconds) throws InterruptedException {
+ final Context c = getInstrumentation().getTargetContext();
+ final int latchCount = (initialState == PlayerStatus.PAUSED || initialState == PlayerStatus.PLAYING) ? 2 :
+ (initialState == PlayerStatus.PREPARED) ? 1 : 0;
+ final CountDownLatch countDownLatch = new CountDownLatch(latchCount);
+
+ PlaybackServiceMediaPlayer.PSMPCallback callback = new PlaybackServiceMediaPlayer.PSMPCallback() {
+ @Override
+ public void statusChanged(PlaybackServiceMediaPlayer.PSMPInfo newInfo) {
+ checkPSMPInfo(newInfo);
+ if (newInfo.playerStatus == PlayerStatus.ERROR) {
+ if (assertionError == null)
+ assertionError = new UnexpectedStateChange(newInfo.playerStatus);
+ } else if (newInfo.playerStatus == PlayerStatus.PLAYING) {
+ if (countDownLatch.getCount() == 0) {
+ if (assertionError == null)
+ assertionError = new UnexpectedStateChange(newInfo.playerStatus);
+ } else {
+ countDownLatch.countDown();
+ }
+ }
+
+ }
+
+ @Override
+ public void shouldStop() {
+
+ }
+
+ @Override
+ public void playbackSpeedChanged(float s) {
+
+ }
+
+ @Override
+ public void onBufferingUpdate(int percent) {
+
+ }
+
+ @Override
+ public boolean onMediaPlayerInfo(int code) {
+ return false;
+ }
+
+ @Override
+ public boolean onMediaPlayerError(Object inObj, int what, int extra) {
+ if (assertionError == null) {
+ assertionError = new AssertionFailedError("Unexpected call of onMediaPlayerError");
+ }
+ return false;
+ }
+
+ @Override
+ public boolean endPlayback(boolean playNextEpisode) {
+ return false;
+ }
+
+ @Override
+ public RemoteControlClient getRemoteControlClient() {
+ return null;
+ }
+ };
+ PlaybackServiceMediaPlayer psmp = new PlaybackServiceMediaPlayer(c, callback);
+ if (initialState == PlayerStatus.PREPARED || initialState == PlayerStatus.PLAYING || initialState == PlayerStatus.PAUSED) {
+ boolean startWhenPrepared = (initialState != PlayerStatus.PREPARED);
+ psmp.playMediaObject(writeTestPlayable(PLAYABLE_FILE_URL, PLAYABLE_LOCAL_URL), false, startWhenPrepared, true);
+ }
+ if (initialState == PlayerStatus.PAUSED) {
+ psmp.pause(false, false);
+ }
+ psmp.resume();
+ boolean res = countDownLatch.await(timeoutSeconds, TimeUnit.SECONDS);
+ if (assertionError != null)
+ throw assertionError;
+ assertTrue(res || (initialState != PlayerStatus.PAUSED && initialState != PlayerStatus.PREPARED));
+ psmp.shutdown();
+ }
+
+ public void testResumePausedState() throws InterruptedException {
+ resumeTestSkeleton(PlayerStatus.PAUSED, LATCH_TIMEOUT_SECONDS);
+ }
+
+ public void testResumePreparedState() throws InterruptedException {
+ resumeTestSkeleton(PlayerStatus.PREPARED, LATCH_TIMEOUT_SECONDS);
+ }
+
+ public void testResumePlayingState() throws InterruptedException {
+ resumeTestSkeleton(PlayerStatus.PLAYING, 1);
+ }
+
+ 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);
+ PlaybackServiceMediaPlayer.PSMPCallback callback = new PlaybackServiceMediaPlayer.PSMPCallback() {
+ @Override
+ public void statusChanged(PlaybackServiceMediaPlayer.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();
+ }
+ }
+
+ }
+
+ @Override
+ public void shouldStop() {
+
+ }
+
+ @Override
+ public void playbackSpeedChanged(float s) {
+
+ }
+
+ @Override
+ public void onBufferingUpdate(int percent) {
+
+ }
+
+ @Override
+ public boolean onMediaPlayerInfo(int code) {
+ return false;
+ }
+
+ @Override
+ public boolean onMediaPlayerError(Object inObj, int what, int extra) {
+ if (assertionError == null)
+ assertionError = new AssertionFailedError("Unexpected call to onMediaPlayerError");
+ return false;
+ }
+
+ @Override
+ public boolean endPlayback(boolean playNextEpisode) {
+ return false;
+ }
+
+ @Override
+ public RemoteControlClient getRemoteControlClient() {
+ return null;
+ }
+ };
+ PlaybackServiceMediaPlayer psmp = new PlaybackServiceMediaPlayer(c, callback);
+ Playable p = writeTestPlayable(PLAYABLE_FILE_URL, 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);
+ psmp.shutdown();
+ }
+
+ public void testPrepareInitializedState() throws InterruptedException {
+ prepareTestSkeleton(PlayerStatus.INITIALIZED, LATCH_TIMEOUT_SECONDS);
+ }
+
+ public void testPreparePlayingState() throws InterruptedException {
+ prepareTestSkeleton(PlayerStatus.PLAYING, 1);
+ }
+
+ public void testPreparePausedState() throws InterruptedException {
+ prepareTestSkeleton(PlayerStatus.PAUSED, 1);
+ }
+
+ public void testPreparePreparedState() throws InterruptedException {
+ prepareTestSkeleton(PlayerStatus.PREPARED, 1);
+ }
+
+ private void reinitTestSkeleton(final PlayerStatus initialState, final long timeoutSeconds) throws InterruptedException {
+ final Context c = getInstrumentation().getTargetContext();
+ final int latchCount = 2;
+ final CountDownLatch countDownLatch = new CountDownLatch(latchCount);
+ PlaybackServiceMediaPlayer.PSMPCallback callback = new PlaybackServiceMediaPlayer.PSMPCallback() {
+ @Override
+ public void statusChanged(PlaybackServiceMediaPlayer.PSMPInfo newInfo) {
+ checkPSMPInfo(newInfo);
+ if (newInfo.playerStatus == PlayerStatus.ERROR) {
+ if (assertionError == null)
+ assertionError = new UnexpectedStateChange(newInfo.playerStatus);
+ } else {
+ if (newInfo.playerStatus == initialState) {
+ countDownLatch.countDown();
+ } else if (countDownLatch.getCount() < latchCount && newInfo.playerStatus == PlayerStatus.INITIALIZED) {
+ countDownLatch.countDown();
+ }
+ }
+ }
+
+ @Override
+ public void shouldStop() {
+
+ }
+
+ @Override
+ public void playbackSpeedChanged(float s) {
+
+ }
+
+ @Override
+ public void onBufferingUpdate(int percent) {
+
+ }
+
+ @Override
+ public boolean onMediaPlayerInfo(int code) {
+ return false;
+ }
+
+ @Override
+ public boolean onMediaPlayerError(Object inObj, int what, int extra) {
+ if (assertionError == null)
+ assertionError = new AssertionFailedError("Unexpected call to onMediaPlayerError");
+ return false;
+ }
+
+ @Override
+ public boolean endPlayback(boolean playNextEpisode) {
+ return false;
+ }
+
+ @Override
+ public RemoteControlClient getRemoteControlClient() {
+ return null;
+ }
+ };
+ PlaybackServiceMediaPlayer psmp = new PlaybackServiceMediaPlayer(c, callback);
+ Playable p = writeTestPlayable(PLAYABLE_FILE_URL, PLAYABLE_LOCAL_URL);
+ boolean prepareImmediately = initialState != PlayerStatus.INITIALIZED;
+ boolean startImmediately = initialState != PlayerStatus.PREPARED;
+ psmp.playMediaObject(p, false, startImmediately, prepareImmediately);
+ if (initialState == PlayerStatus.PAUSED) {
+ psmp.pause(false, false);
+ }
+ psmp.reinit();
+ boolean res = countDownLatch.await(timeoutSeconds, TimeUnit.SECONDS);
+ if (assertionError != null)
+ throw assertionError;
+ assertTrue(res);
+ psmp.shutdown();
+ }
+
+ public void testReinitPlayingState() throws InterruptedException {
+ reinitTestSkeleton(PlayerStatus.PLAYING, LATCH_TIMEOUT_SECONDS);
+ }
+
+ public void testReinitPausedState() throws InterruptedException {
+ reinitTestSkeleton(PlayerStatus.PAUSED, LATCH_TIMEOUT_SECONDS);
+ }
+
+ public void testPreparedPlayingState() throws InterruptedException {
+ reinitTestSkeleton(PlayerStatus.PREPARED, LATCH_TIMEOUT_SECONDS);
+ }
+
+ public void testReinitInitializedState() throws InterruptedException {
+ reinitTestSkeleton(PlayerStatus.INITIALIZED, LATCH_TIMEOUT_SECONDS);
+ }
+
+ private static class UnexpectedStateChange extends AssertionFailedError {
+ public UnexpectedStateChange(PlayerStatus status) {
+ super("Unexpected state change: " + status);
+ }
+ }
+}
diff --git a/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java b/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java
new file mode 100644
index 000000000..81d684595
--- /dev/null
+++ b/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java
@@ -0,0 +1,333 @@
+package de.test.antennapod.service.playback;
+
+import android.content.Context;
+import android.test.InstrumentationTestCase;
+import de.danoeh.antennapod.core.feed.EventDistributor;
+import de.danoeh.antennapod.core.feed.Feed;
+import de.danoeh.antennapod.core.feed.FeedItem;
+import de.danoeh.antennapod.core.service.playback.PlaybackServiceTaskManager;
+import de.danoeh.antennapod.core.storage.PodDBAdapter;
+import de.danoeh.antennapod.core.util.playback.Playable;
+
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+import java.util.concurrent.CountDownLatch;
+import java.util.concurrent.TimeUnit;
+
+/**
+ * Test class for PlaybackServiceTaskManager
+ */
+public class PlaybackServiceTaskManagerTest extends InstrumentationTestCase {
+
+ @Override
+ protected void tearDown() throws Exception {
+ super.tearDown();
+ assertTrue(PodDBAdapter.deleteDatabase(getInstrumentation().getTargetContext()));
+ }
+
+ @Override
+ protected void setUp() throws Exception {
+ super.setUp();
+ final Context context = getInstrumentation().getTargetContext();
+ context.deleteDatabase(PodDBAdapter.DATABASE_NAME);
+ // make sure database is created
+ PodDBAdapter adapter = new PodDBAdapter(context);
+ adapter.open();
+ adapter.close();
+ }
+
+ public void testInit() {
+ PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(getInstrumentation().getTargetContext(), defaultPSTM);
+ pstm.shutdown();
+ }
+
+ private List<FeedItem> writeTestQueue(String pref) {
+ final Context c = getInstrumentation().getTargetContext();
+ final int NUM_ITEMS = 10;
+ Feed f = new Feed(0, new Date(), "title", "link", "d", null, null, null, null, "id", null, "null", "url", false);
+ f.setItems(new ArrayList<FeedItem>());
+ for (int i = 0; i < NUM_ITEMS; i++) {
+ f.getItems().add(new FeedItem(0, pref + i, pref + i, "link", new Date(), true, f));
+ }
+ PodDBAdapter adapter = new PodDBAdapter(c);
+ adapter.open();
+ adapter.setCompleteFeed(f);
+ adapter.setQueue(f.getItems());
+ adapter.close();
+
+ for (FeedItem item : f.getItems()) {
+ assertTrue(item.getId() != 0);
+ }
+ return f.getItems();
+ }
+
+ public void testGetQueueWriteBeforeCreation() throws InterruptedException {
+ final Context c = getInstrumentation().getTargetContext();
+ List<FeedItem> queue = writeTestQueue("a");
+ assertNotNull(queue);
+ PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
+ List<FeedItem> testQueue = pstm.getQueue();
+ assertNotNull(testQueue);
+ assertTrue(queue.size() == testQueue.size());
+ for (int i = 0; i < queue.size(); i++) {
+ assertTrue(queue.get(i).getId() == testQueue.get(i).getId());
+ }
+ pstm.shutdown();
+ }
+
+ public void testGetQueueWriteAfterCreation() throws InterruptedException {
+ final Context c = getInstrumentation().getTargetContext();
+
+ PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
+ List<FeedItem> testQueue = pstm.getQueue();
+ assertNotNull(testQueue);
+ assertTrue(testQueue.isEmpty());
+
+
+ final CountDownLatch countDownLatch = new CountDownLatch(1);
+ EventDistributor.EventListener queueListener = new EventDistributor.EventListener() {
+ @Override
+ public void update(EventDistributor eventDistributor, Integer arg) {
+ countDownLatch.countDown();
+ }
+ };
+ EventDistributor.getInstance().register(queueListener);
+ List<FeedItem> queue = writeTestQueue("a");
+ EventDistributor.getInstance().sendQueueUpdateBroadcast();
+ countDownLatch.await(5000, TimeUnit.MILLISECONDS);
+
+ assertNotNull(queue);
+ testQueue = pstm.getQueue();
+ assertNotNull(testQueue);
+ assertTrue(queue.size() == testQueue.size());
+ for (int i = 0; i < queue.size(); i++) {
+ assertTrue(queue.get(i).getId() == testQueue.get(i).getId());
+ }
+ pstm.shutdown();
+ }
+
+ public void testStartPositionSaver() throws InterruptedException {
+ final Context c = getInstrumentation().getTargetContext();
+ final int NUM_COUNTDOWNS = 2;
+ final int TIMEOUT = 3 * PlaybackServiceTaskManager.POSITION_SAVER_WAITING_INTERVAL;
+ final CountDownLatch countDownLatch = new CountDownLatch(NUM_COUNTDOWNS);
+ PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, new PlaybackServiceTaskManager.PSTMCallback() {
+ @Override
+ public void positionSaverTick() {
+ countDownLatch.countDown();
+ }
+
+ @Override
+ public void onSleepTimerExpired() {
+
+ }
+
+ @Override
+ public void onWidgetUpdaterTick() {
+
+ }
+
+ @Override
+ public void onChapterLoaded(Playable media) {
+
+ }
+ });
+ pstm.startPositionSaver();
+ countDownLatch.await(TIMEOUT, TimeUnit.MILLISECONDS);
+ pstm.shutdown();
+ }
+
+ public void testIsPositionSaverActive() {
+ final Context c = getInstrumentation().getTargetContext();
+ PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
+ pstm.startPositionSaver();
+ assertTrue(pstm.isPositionSaverActive());
+ pstm.shutdown();
+ }
+
+ public void testCancelPositionSaver() {
+ final Context c = getInstrumentation().getTargetContext();
+ PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
+ pstm.startPositionSaver();
+ pstm.cancelPositionSaver();
+ assertFalse(pstm.isPositionSaverActive());
+ pstm.shutdown();
+ }
+
+ public void testStartWidgetUpdater() throws InterruptedException {
+ final Context c = getInstrumentation().getTargetContext();
+ final int NUM_COUNTDOWNS = 2;
+ final int TIMEOUT = 3 * PlaybackServiceTaskManager.WIDGET_UPDATER_NOTIFICATION_INTERVAL;
+ final CountDownLatch countDownLatch = new CountDownLatch(NUM_COUNTDOWNS);
+ PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, new PlaybackServiceTaskManager.PSTMCallback() {
+ @Override
+ public void positionSaverTick() {
+
+ }
+
+ @Override
+ public void onSleepTimerExpired() {
+
+ }
+
+ @Override
+ public void onWidgetUpdaterTick() {
+ countDownLatch.countDown();
+ }
+
+ @Override
+ public void onChapterLoaded(Playable media) {
+
+ }
+ });
+ pstm.startWidgetUpdater();
+ countDownLatch.await(TIMEOUT, TimeUnit.MILLISECONDS);
+ pstm.shutdown();
+ }
+
+ public void testIsWidgetUpdaterActive() {
+ final Context c = getInstrumentation().getTargetContext();
+ PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
+ pstm.startWidgetUpdater();
+ assertTrue(pstm.isWidgetUpdaterActive());
+ pstm.shutdown();
+ }
+
+ public void testCancelWidgetUpdater() {
+ final Context c = getInstrumentation().getTargetContext();
+ PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
+ pstm.startWidgetUpdater();
+ pstm.cancelWidgetUpdater();
+ assertFalse(pstm.isWidgetUpdaterActive());
+ pstm.shutdown();
+ }
+
+ public void testCancelAllTasksNoTasksStarted() {
+ final Context c = getInstrumentation().getTargetContext();
+ PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
+ pstm.cancelAllTasks();
+ assertFalse(pstm.isPositionSaverActive());
+ assertFalse(pstm.isWidgetUpdaterActive());
+ assertFalse(pstm.isSleepTimerActive());
+ pstm.shutdown();
+ }
+
+ public void testCancelAllTasksAllTasksStarted() {
+ final Context c = getInstrumentation().getTargetContext();
+ PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
+ pstm.startWidgetUpdater();
+ pstm.startPositionSaver();
+ pstm.setSleepTimer(100000);
+ pstm.cancelAllTasks();
+ assertFalse(pstm.isPositionSaverActive());
+ assertFalse(pstm.isWidgetUpdaterActive());
+ assertFalse(pstm.isSleepTimerActive());
+ pstm.shutdown();
+ }
+
+ public void testSetSleepTimer() throws InterruptedException {
+ final Context c = getInstrumentation().getTargetContext();
+ final long TIME = 2000;
+ final long TIMEOUT = 2 * TIME;
+ final CountDownLatch countDownLatch = new CountDownLatch(1);
+ PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, new PlaybackServiceTaskManager.PSTMCallback() {
+ @Override
+ public void positionSaverTick() {
+
+ }
+
+ @Override
+ public void onSleepTimerExpired() {
+ if (countDownLatch.getCount() == 0) {
+ fail();
+ }
+ countDownLatch.countDown();
+ }
+
+ @Override
+ public void onWidgetUpdaterTick() {
+
+ }
+
+ @Override
+ public void onChapterLoaded(Playable media) {
+
+ }
+ });
+ pstm.setSleepTimer(TIME);
+ countDownLatch.await(TIMEOUT, TimeUnit.MILLISECONDS);
+ pstm.shutdown();
+ }
+
+ public void testDisableSleepTimer() throws InterruptedException {
+ final Context c = getInstrumentation().getTargetContext();
+ final long TIME = 1000;
+ final long TIMEOUT = 2 * TIME;
+ final CountDownLatch countDownLatch = new CountDownLatch(1);
+ PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, new PlaybackServiceTaskManager.PSTMCallback() {
+ @Override
+ public void positionSaverTick() {
+
+ }
+
+ @Override
+ public void onSleepTimerExpired() {
+ fail("Sleeptimer expired");
+ }
+
+ @Override
+ public void onWidgetUpdaterTick() {
+
+ }
+
+ @Override
+ public void onChapterLoaded(Playable media) {
+
+ }
+ });
+ pstm.setSleepTimer(TIME);
+ pstm.disableSleepTimer();
+ assertFalse(countDownLatch.await(TIMEOUT, TimeUnit.MILLISECONDS));
+ pstm.shutdown();
+ }
+
+ public void testIsSleepTimerActivePositive() {
+ final Context c = getInstrumentation().getTargetContext();
+ PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
+ pstm.setSleepTimer(10000);
+ assertTrue(pstm.isSleepTimerActive());
+ pstm.shutdown();
+ }
+
+ public void testIsSleepTimerActiveNegative() {
+ final Context c = getInstrumentation().getTargetContext();
+ PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM);
+ pstm.setSleepTimer(10000);
+ pstm.disableSleepTimer();
+ assertFalse(pstm.isSleepTimerActive());
+ pstm.shutdown();
+ }
+
+ private final PlaybackServiceTaskManager.PSTMCallback defaultPSTM = new PlaybackServiceTaskManager.PSTMCallback() {
+ @Override
+ public void positionSaverTick() {
+
+ }
+
+ @Override
+ public void onSleepTimerExpired() {
+
+ }
+
+ @Override
+ public void onWidgetUpdaterTick() {
+
+ }
+
+ @Override
+ public void onChapterLoaded(Playable media) {
+
+ }
+ };
+}