From 9469ebc6c3a8b0ed2cb268f4e7b153199d3d6dfb Mon Sep 17 00:00:00 2001 From: orionlee Date: Sun, 29 Sep 2019 12:46:21 -0700 Subject: bugfix: Ensure playback service use the updated feedItem after media download completes (rather than trying to stream) --- .../playback/PlaybackServiceTaskManagerTest.java | 50 ++++++++++++++++++++-- 1 file changed, 46 insertions(+), 4 deletions(-) (limited to 'app') 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 index 9c0e90929..91a796638 100644 --- a/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java +++ b/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java @@ -5,6 +5,11 @@ import android.support.test.InstrumentationRegistry; import android.support.test.annotation.UiThreadTest; import android.support.test.filters.LargeTest; +import org.greenrobot.eventbus.EventBus; +import org.junit.After; +import org.junit.Before; +import org.junit.Test; + import java.util.ArrayList; import java.util.Date; import java.util.List; @@ -15,14 +20,15 @@ import de.danoeh.antennapod.core.event.QueueEvent; 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.feed.FeedMedia; import de.danoeh.antennapod.core.service.playback.PlaybackServiceTaskManager; +import de.danoeh.antennapod.core.storage.DBReader; +import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.core.storage.PodDBAdapter; import de.danoeh.antennapod.core.util.playback.Playable; -import org.greenrobot.eventbus.EventBus; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; +import static org.hamcrest.CoreMatchers.is; +import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -123,6 +129,42 @@ public class PlaybackServiceTaskManagerTest { pstm.shutdown(); } + @Test + public void testQueueUpdatedUponDownloadComplete() throws Exception { + final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext(); + { // Setup test data + List queue = writeTestQueue("a"); + FeedItem item = DBReader.getFeedItem(queue.get(0).getId()); + FeedMedia media = new FeedMedia(item, "http://abc.test/acme.mp3", 12345, "audio/mp3"); + item.setMedia(media); + DBWriter.setFeedMedia(media).get(); + DBWriter.setFeedItem(item).get(); + } + + PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); + final FeedItem testItem = pstm.getQueue().get(0); + assertThat("The item is not yet downloaded", + testItem.getMedia().isDownloaded(), is(false)); + + { // simulate download complete (in DownloadService.MediaHandlerThread) + FeedItem item = DBReader.getFeedItem(testItem.getId()); + item.getMedia().setDownloaded(true); + item.getMedia().setFile_url("file://123"); + item.setAutoDownload(false); + DBWriter.setFeedItem(item).get(); + DBWriter.setFeedMedia(item.getMedia()).get(); + } + + // an approximation to ensure the item update event has been posted and processed. + Thread.sleep(10); + + final FeedItem itemUpdated = pstm.getQueue().get(0); + assertThat("The queue in PlaybackService has been updated item after download is completed", + itemUpdated.getMedia().isDownloaded(), is(true)); + + pstm.shutdown(); + } + @Test public void testStartPositionSaver() throws InterruptedException { final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext(); -- cgit v1.2.3 From 71de4607a35c0164f91687cabc81b62abd1df51c Mon Sep 17 00:00:00 2001 From: orionlee Date: Sun, 29 Sep 2019 12:55:02 -0700 Subject: bugfix: Ensure when media download completes and item update event is posted, the feed media has been updated, --- .../antennapod/service/playback/PlaybackServiceTaskManagerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app') 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 index 91a796638..53a402e7b 100644 --- a/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java +++ b/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java @@ -151,8 +151,8 @@ public class PlaybackServiceTaskManagerTest { item.getMedia().setDownloaded(true); item.getMedia().setFile_url("file://123"); item.setAutoDownload(false); - DBWriter.setFeedItem(item).get(); DBWriter.setFeedMedia(item.getMedia()).get(); + DBWriter.setFeedItem(item).get(); } // an approximation to ensure the item update event has been posted and processed. -- cgit v1.2.3 From 38e9cafee3afb7010fb870e547dceb0f8ae9feec Mon Sep 17 00:00:00 2001 From: orionlee Date: Mon, 30 Sep 2019 11:59:55 -0700 Subject: test tweak - ensure playback queue updated after download - follow de-facto convention for test feeds --- .../antennapod/service/playback/PlaybackServiceTaskManagerTest.java | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'app') 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 index 53a402e7b..38b097d51 100644 --- a/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java +++ b/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java @@ -135,7 +135,7 @@ public class PlaybackServiceTaskManagerTest { { // Setup test data List queue = writeTestQueue("a"); FeedItem item = DBReader.getFeedItem(queue.get(0).getId()); - FeedMedia media = new FeedMedia(item, "http://abc.test/acme.mp3", 12345, "audio/mp3"); + FeedMedia media = new FeedMedia(item, "http://example.com/episode.mp3", 12345, "audio/mp3"); item.setMedia(media); DBWriter.setFeedMedia(media).get(); DBWriter.setFeedItem(item).get(); -- cgit v1.2.3 From cbf6103c00d4baad160ed5e6b6acba536d940c24 Mon Sep 17 00:00:00 2001 From: orionlee Date: Mon, 30 Sep 2019 12:07:53 -0700 Subject: test tweak - ensure playback queue updated after download - await for the event explicitly. --- .../playback/PlaybackServiceTaskManagerTest.java | 26 ++++++++++++++++++++-- 1 file changed, 24 insertions(+), 2 deletions(-) (limited to 'app') 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 index 38b097d51..1d2a72c92 100644 --- a/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java +++ b/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java @@ -5,7 +5,9 @@ import android.support.test.InstrumentationRegistry; import android.support.test.annotation.UiThreadTest; import android.support.test.filters.LargeTest; +import org.awaitility.Awaitility; import org.greenrobot.eventbus.EventBus; +import org.greenrobot.eventbus.Subscribe; import org.junit.After; import org.junit.Before; import org.junit.Test; @@ -16,6 +18,7 @@ import java.util.List; import java.util.concurrent.CountDownLatch; import java.util.concurrent.TimeUnit; +import de.danoeh.antennapod.core.event.FeedItemEvent; import de.danoeh.antennapod.core.event.QueueEvent; import de.danoeh.antennapod.core.feed.EventDistributor; import de.danoeh.antennapod.core.feed.Feed; @@ -146,6 +149,9 @@ public class PlaybackServiceTaskManagerTest { assertThat("The item is not yet downloaded", testItem.getMedia().isDownloaded(), is(false)); + FeedItemEventListener feedItemEventListener = new FeedItemEventListener(); + EventBus.getDefault().register(feedItemEventListener); + { // simulate download complete (in DownloadService.MediaHandlerThread) FeedItem item = DBReader.getFeedItem(testItem.getId()); item.getMedia().setDownloaded(true); @@ -155,16 +161,32 @@ public class PlaybackServiceTaskManagerTest { DBWriter.setFeedItem(item).get(); } - // an approximation to ensure the item update event has been posted and processed. - Thread.sleep(10); + Awaitility.await() + .atMost(1000, TimeUnit.MILLISECONDS) + .until(() -> feedItemEventListener.getEvents().size() > 0); final FeedItem itemUpdated = pstm.getQueue().get(0); assertThat("The queue in PlaybackService has been updated item after download is completed", itemUpdated.getMedia().isDownloaded(), is(true)); + EventBus.getDefault().unregister(feedItemEventListener); pstm.shutdown(); } + private static class FeedItemEventListener { + + private final List events = new ArrayList<>(); + + @Subscribe + public void onEvent(FeedItemEvent event) { + events.add(event); + } + + List getEvents() { + return events; + } + } + @Test public void testStartPositionSaver() throws InterruptedException { final Context c = InstrumentationRegistry.getInstrumentation().getTargetContext(); -- cgit v1.2.3 From 4a0a825c0895e3052075b285c33cc0fdecaa291a Mon Sep 17 00:00:00 2001 From: orionlee Date: Mon, 30 Sep 2019 12:18:41 -0700 Subject: test refactor - ensure playback queue updated after download - make test logic more readable by hiding EventBus setup in a helper. --- .../playback/PlaybackServiceTaskManagerTest.java | 37 +++++++++++++++------- 1 file changed, 25 insertions(+), 12 deletions(-) (limited to 'app') 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 index 1d2a72c92..2880e86eb 100644 --- a/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java +++ b/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java @@ -29,6 +29,7 @@ import de.danoeh.antennapod.core.storage.DBReader; import de.danoeh.antennapod.core.storage.DBWriter; import de.danoeh.antennapod.core.storage.PodDBAdapter; import de.danoeh.antennapod.core.util.playback.Playable; +import io.reactivex.functions.Consumer; import static org.hamcrest.CoreMatchers.is; import static org.hamcrest.MatcherAssert.assertThat; @@ -149,30 +150,42 @@ public class PlaybackServiceTaskManagerTest { assertThat("The item is not yet downloaded", testItem.getMedia().isDownloaded(), is(false)); - FeedItemEventListener feedItemEventListener = new FeedItemEventListener(); - EventBus.getDefault().register(feedItemEventListener); - - { // simulate download complete (in DownloadService.MediaHandlerThread) + withFeedItemEventListener( feedItemEventListener -> { + // simulate download complete (in DownloadService.MediaHandlerThread) FeedItem item = DBReader.getFeedItem(testItem.getId()); item.getMedia().setDownloaded(true); item.getMedia().setFile_url("file://123"); item.setAutoDownload(false); DBWriter.setFeedMedia(item.getMedia()).get(); DBWriter.setFeedItem(item).get(); - } - Awaitility.await() - .atMost(1000, TimeUnit.MILLISECONDS) - .until(() -> feedItemEventListener.getEvents().size() > 0); + Awaitility.await() + .atMost(1000, TimeUnit.MILLISECONDS) + .until(() -> feedItemEventListener.getEvents().size() > 0); - final FeedItem itemUpdated = pstm.getQueue().get(0); - assertThat("The queue in PlaybackService has been updated item after download is completed", - itemUpdated.getMedia().isDownloaded(), is(true)); + final FeedItem itemUpdated = pstm.getQueue().get(0); + assertThat("The queue in PlaybackService has been updated item after download is completed", + itemUpdated.getMedia().isDownloaded(), is(true)); + }); - EventBus.getDefault().unregister(feedItemEventListener); pstm.shutdown(); } + /** + * Provides an listener subscribing to {@link FeedItemEvent} that the callers can use + * + * Note: it uses RxJava's version of {@link Consumer} because it allows exceptions to be thrown. + */ + private static void withFeedItemEventListener(Consumer consumer) throws Exception { + FeedItemEventListener feedItemEventListener = new FeedItemEventListener(); + try { + EventBus.getDefault().register(feedItemEventListener); + consumer.accept(feedItemEventListener); + } finally { + EventBus.getDefault().unregister(feedItemEventListener); + } + } + private static class FeedItemEventListener { private final List events = new ArrayList<>(); -- cgit v1.2.3 From c0befc98542bc147a8aa341c48e6445d4cfbbfb7 Mon Sep 17 00:00:00 2001 From: orionlee Date: Mon, 30 Sep 2019 12:28:27 -0700 Subject: test tweak - ensure playback queue updated after download - use more concise assertTrue/False --- .../service/playback/PlaybackServiceTaskManagerTest.java | 9 +++------ 1 file changed, 3 insertions(+), 6 deletions(-) (limited to 'app') 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 index 2880e86eb..3a3a64d80 100644 --- a/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java +++ b/app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java @@ -31,8 +31,6 @@ import de.danoeh.antennapod.core.storage.PodDBAdapter; import de.danoeh.antennapod.core.util.playback.Playable; import io.reactivex.functions.Consumer; -import static org.hamcrest.CoreMatchers.is; -import static org.hamcrest.MatcherAssert.assertThat; import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotNull; import static org.junit.Assert.assertTrue; @@ -147,8 +145,7 @@ public class PlaybackServiceTaskManagerTest { PlaybackServiceTaskManager pstm = new PlaybackServiceTaskManager(c, defaultPSTM); final FeedItem testItem = pstm.getQueue().get(0); - assertThat("The item is not yet downloaded", - testItem.getMedia().isDownloaded(), is(false)); + assertFalse("The item should not yet be downloaded", testItem.getMedia().isDownloaded()); withFeedItemEventListener( feedItemEventListener -> { // simulate download complete (in DownloadService.MediaHandlerThread) @@ -164,8 +161,8 @@ public class PlaybackServiceTaskManagerTest { .until(() -> feedItemEventListener.getEvents().size() > 0); final FeedItem itemUpdated = pstm.getQueue().get(0); - assertThat("The queue in PlaybackService has been updated item after download is completed", - itemUpdated.getMedia().isDownloaded(), is(true)); + assertTrue("media.isDownloaded() should be true - The queue in PlaybackService should be updated after download is completed", + itemUpdated.getMedia().isDownloaded()); }); pstm.shutdown(); -- cgit v1.2.3