diff options
author | orionlee <orionlee@yahoo.com> | 2019-09-30 12:18:41 -0700 |
---|---|---|
committer | orionlee <orionlee@yahoo.com> | 2019-09-30 12:18:41 -0700 |
commit | 4a0a825c0895e3052075b285c33cc0fdecaa291a (patch) | |
tree | a17e639341db123491022aead5a3f8dc5f108722 /app | |
parent | cbf6103c00d4baad160ed5e6b6acba536d940c24 (diff) | |
download | AntennaPod-4a0a825c0895e3052075b285c33cc0fdecaa291a.zip |
test refactor - ensure playback queue updated after download - make test logic more readable by hiding EventBus setup in a helper.
Diffstat (limited to 'app')
-rw-r--r-- | app/src/androidTest/java/de/test/antennapod/service/playback/PlaybackServiceTaskManagerTest.java | 37 |
1 files changed, 25 insertions, 12 deletions
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<FeedItemEventListener> 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<FeedItemEvent> events = new ArrayList<>(); |