From 6ebf1defe7153910dd3a7503ea215079814da7cb Mon Sep 17 00:00:00 2001 From: Borjan Tchakaloff Date: Sun, 31 Mar 2019 16:20:00 +0200 Subject: [PATCH] Validate that the item state is only changed when needed Follow-up to commit 8172d87477dd593745d4776417ef3dd7884d17fb (#3067) that adds test coverage for the resolved issue. Also, fix that commit by making the update condition more explicit: the FeedItem state is only changed when a state switch is necessary. In other words, an item marked as *new* that gets downloaded should lose the *new* mark and gain the *unplayed* mark instead. --- .../antennapod/core/feed/FeedMedia.java | 2 +- .../antennapod/core/feed/FeedMediaMother.java | 13 ++++ .../antennapod/core/feed/FeedMediaTest.java | 72 +++++++++++++++++++ 3 files changed, 86 insertions(+), 1 deletion(-) create mode 100644 core/src/test/java/de/danoeh/antennapod/core/feed/FeedMediaMother.java create mode 100644 core/src/test/java/de/danoeh/antennapod/core/feed/FeedMediaTest.java diff --git a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedMedia.java b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedMedia.java index 84346e2d4..f3a43e2d0 100644 --- a/core/src/main/java/de/danoeh/antennapod/core/feed/FeedMedia.java +++ b/core/src/main/java/de/danoeh/antennapod/core/feed/FeedMedia.java @@ -594,7 +594,7 @@ public class FeedMedia extends FeedFile implements Playable { @Override public void setDownloaded(boolean downloaded) { super.setDownloaded(downloaded); - if(item != null && downloaded && !item.isPlayed()) { + if(item != null && downloaded && item.isNew()) { item.setPlayed(false); } } diff --git a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMediaMother.java b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMediaMother.java new file mode 100644 index 000000000..d95b8787c --- /dev/null +++ b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMediaMother.java @@ -0,0 +1,13 @@ +package de.danoeh.antennapod.core.feed; + +class FeedMediaMother { + + private static final String EPISODE_URL = "http://example.com/episode"; + private static final long SIZE = 42; + private static final String MIME_TYPE = "audio/mp3"; + + static FeedMedia anyFeedMedia() { + return new FeedMedia(null, EPISODE_URL, SIZE, MIME_TYPE); + } + +} diff --git a/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMediaTest.java b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMediaTest.java new file mode 100644 index 000000000..f27a54f84 --- /dev/null +++ b/core/src/test/java/de/danoeh/antennapod/core/feed/FeedMediaTest.java @@ -0,0 +1,72 @@ +package de.danoeh.antennapod.core.feed; + +import org.junit.Before; +import org.junit.Test; + +import static de.danoeh.antennapod.core.feed.FeedMediaMother.anyFeedMedia; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.never; +import static org.mockito.Mockito.verify; +import static org.mockito.Mockito.when; + +public class FeedMediaTest { + + private FeedMedia media; + + @Before + public void setUp() { + media = anyFeedMedia(); + } + + /** + * Downloading a media from a not new and not played item should not change the item state. + */ + @Test + public void testDownloadMediaOfNotNewAndNotPlayedItem_unchangedItemState() { + FeedItem item = mock(FeedItem.class); + when(item.isNew()).thenReturn(false); + when(item.isPlayed()).thenReturn(false); + + media.setItem(item); + media.setDownloaded(true); + + verify(item, never()).setNew(); + verify(item, never()).setPlayed(true); + verify(item, never()).setPlayed(false); + } + + /** + * Downloading a media from a played item (thus not new) should not change the item state. + */ + @Test + public void testDownloadMediaOfPlayedItem_unchangedItemState() { + FeedItem item = mock(FeedItem.class); + when(item.isNew()).thenReturn(false); + when(item.isPlayed()).thenReturn(true); + + media.setItem(item); + media.setDownloaded(true); + + verify(item, never()).setNew(); + verify(item, never()).setPlayed(true); + verify(item, never()).setPlayed(false); + } + + /** + * Downloading a media from a new item (thus not played) should change the item to not played. + */ + @Test + public void testDownloadMediaOfNewItem_changedToNotPlayedItem() { + FeedItem item = mock(FeedItem.class); + when(item.isNew()).thenReturn(true); + when(item.isPlayed()).thenReturn(false); + + media.setItem(item); + media.setDownloaded(true); + + verify(item).setPlayed(false); + verify(item, never()).setNew(); + verify(item, never()).setPlayed(true); + } + +}