test refactor - ensure playback queue updated after download - make test logic more readable by hiding EventBus setup in a helper.

This commit is contained in:
orionlee 2019-09-30 12:18:41 -07:00
parent cbf6103c00
commit 4a0a825c08
1 changed files with 25 additions and 12 deletions

View File

@ -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,17 +150,14 @@ 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)
@ -168,11 +166,26 @@ public class PlaybackServiceTaskManagerTest {
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<>();