Enqueue tweaks - replace custom stub DownloadStateProvider with mockito mocks in test

This commit is contained in:
orionlee 2019-10-04 14:22:23 -07:00
parent 2f82a5d464
commit fb6fa010f8
1 changed files with 12 additions and 29 deletions

View File

@ -1,7 +1,5 @@
package de.danoeh.antennapod.core.storage; package de.danoeh.antennapod.core.storage;
import androidx.annotation.NonNull;
import org.junit.Test; import org.junit.Test;
import org.junit.runner.RunWith; import org.junit.runner.RunWith;
import org.junit.runners.Parameterized; import org.junit.runners.Parameterized;
@ -12,18 +10,18 @@ import java.util.ArrayList;
import java.util.Arrays; import java.util.Arrays;
import java.util.Collections; import java.util.Collections;
import java.util.Date; import java.util.Date;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import de.danoeh.antennapod.core.feed.FeedFile;
import de.danoeh.antennapod.core.feed.FeedItem; import de.danoeh.antennapod.core.feed.FeedItem;
import de.danoeh.antennapod.core.feed.FeedMedia; import de.danoeh.antennapod.core.feed.FeedMedia;
import de.danoeh.antennapod.core.feed.FeedMother; import de.danoeh.antennapod.core.feed.FeedMother;
import de.danoeh.antennapod.core.storage.ItemEnqueuePositionCalculator.Options; import de.danoeh.antennapod.core.storage.ItemEnqueuePositionCalculator.Options;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.mockito.Mockito.any;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.stub;
public class ItemEnqueuePositionCalculatorTest { public class ItemEnqueuePositionCalculatorTest {
@ -185,8 +183,9 @@ public class ItemEnqueuePositionCalculatorTest {
// Setup class under test // Setup class under test
// //
ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options); ItemEnqueuePositionCalculator calculator = new ItemEnqueuePositionCalculator(options);
MockDownloadRequester mockDownloadRequester = new MockDownloadRequester(); DownloadStateProvider stubDownloadStateProvider = mock(DownloadStateProvider.class);
calculator.downloadStateProvider = mockDownloadRequester; stub(stubDownloadStateProvider.isDownloadingFile(any(FeedMedia.class))).toReturn(false);
calculator.downloadStateProvider = stubDownloadStateProvider;
// Setup initial data // Setup initial data
// A shallow copy, as the test code will manipulate the queue // A shallow copy, as the test code will manipulate the queue
@ -196,25 +195,25 @@ public class ItemEnqueuePositionCalculatorTest {
// Test body // Test body
// User clicks download on feed item 101 // User clicks download on feed item 101
FeedItem tFI101 = tFI_isDownloading(101, mockDownloadRequester); FeedItem tFI101 = tFI_isDownloading(101, stubDownloadStateProvider);
doAddToQueueAndAssertResult(message + " (1st download)", doAddToQueueAndAssertResult(message + " (1st download)",
calculator, 0, tFI101, queue, calculator, 0, tFI101, queue,
idsExpectedAfter101); idsExpectedAfter101);
// Then user clicks download on feed item 102 // Then user clicks download on feed item 102
FeedItem tFI102 = tFI_isDownloading(102, mockDownloadRequester); FeedItem tFI102 = tFI_isDownloading(102, stubDownloadStateProvider);
doAddToQueueAndAssertResult(message + " (2nd download, it should preserve order of download)", doAddToQueueAndAssertResult(message + " (2nd download, it should preserve order of download)",
calculator, 0, tFI102, queue, calculator, 0, tFI102, queue,
idsExpectedAfter102); idsExpectedAfter102);
// Items 201 and 202 are added as part of a single DBWriter.addQueueItem() calls // Items 201 and 202 are added as part of a single DBWriter.addQueueItem() calls
FeedItem tFI201 = tFI_isDownloading(201, mockDownloadRequester); FeedItem tFI201 = tFI_isDownloading(201, stubDownloadStateProvider);
doAddToQueueAndAssertResult(message + " (bulk insertion, 1st item)", doAddToQueueAndAssertResult(message + " (bulk insertion, 1st item)",
calculator, 0, tFI201, queue, calculator, 0, tFI201, queue,
idsExpectedAfter201); idsExpectedAfter201);
FeedItem tFI202 = tFI_isDownloading(202, mockDownloadRequester); FeedItem tFI202 = tFI_isDownloading(202, stubDownloadStateProvider);
doAddToQueueAndAssertResult(message + " (bulk insertion, 2nd item)", doAddToQueueAndAssertResult(message + " (bulk insertion, 2nd item)",
calculator, 1, tFI202, queue, calculator, 1, tFI202, queue,
idsExpectedAfter202); idsExpectedAfter202);
@ -223,7 +222,7 @@ public class ItemEnqueuePositionCalculatorTest {
} }
private static FeedItem tFI_isDownloading(int id, MockDownloadRequester requester) { private static FeedItem tFI_isDownloading(int id, DownloadStateProvider stubDownloadStateProvider) {
FeedItem item = tFI(id); FeedItem item = tFI(id);
FeedMedia media = FeedMedia media =
new FeedMedia(item, "http://download.url.net/" + id new FeedMedia(item, "http://download.url.net/" + id
@ -231,26 +230,10 @@ public class ItemEnqueuePositionCalculatorTest {
media.setId(item.getId()); media.setId(item.getId());
item.setMedia(media); item.setMedia(media);
requester.mockDownloadingFile(media, true); stub(stubDownloadStateProvider.isDownloadingFile(media)).toReturn(true);
return item; return item;
} }
private static class MockDownloadRequester implements DownloadStateProvider {
private Map<Long, Boolean> downloadingByIds = new HashMap<>();
@Override
public synchronized boolean isDownloadingFile(@NonNull FeedFile item) {
return downloadingByIds.getOrDefault(item.getId(), false);
}
// All other parent methods should not be called
public void mockDownloadingFile(FeedFile item, boolean isDownloading) {
downloadingByIds.put(item.getId(), isDownloading);
}
}
} }