From ce5aa26878481a5c07bd611d186ddd735b504d5e Mon Sep 17 00:00:00 2001 From: orionlee Date: Wed, 23 May 2018 22:13:01 -0700 Subject: [PATCH] refactoring test - factor out common operations of calc position, add to queue and verify result into common helper. --- .../antennapod/core/storage/DBWriterTest.java | 47 ++++++++++--------- 1 file changed, 26 insertions(+), 21 deletions(-) diff --git a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java index afdfc323d..5603f8778 100644 --- a/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java +++ b/core/src/test/java/de/danoeh/antennapod/core/storage/DBWriterTest.java @@ -86,11 +86,10 @@ public class DBWriterTest { // shallow copy to which the test will add items List queue = new ArrayList<>(curQueue); - FeedItem tFI = tFI(TFI_ID); - int posActual = calculator.calcPosition(posAmongAdded, tFI, queue); - queue.add(posActual, tFI); - assertEquals(message, idsExpected, toIDs(queue)); + doAddToQueueAndAssertResult(message, + calculator, posAmongAdded, tFI, queue, + idsExpected); } } @@ -201,32 +200,27 @@ public class DBWriterTest { // User clicks download on feed item 101 FeedItem tFI101 = tFI_isDownloading(101, mockDownloadRequester); - - int pos101Actual = calculator.calcPosition(0, tFI101, queue); - queue.add(pos101Actual, tFI101); - assertEquals(message + " (1st download)", - idsExpectedAfter101, toIDs(queue)); + doAddToQueueAndAssertResult(message + " (1st download)", + calculator, 0, tFI101, queue, + idsExpectedAfter101); // Then user clicks download on feed item 102 FeedItem tFI102 = tFI_isDownloading(102, mockDownloadRequester); - int pos102Actual = calculator.calcPosition(0, tFI102, queue); - queue.add(pos102Actual, tFI102); - assertEquals(message + " (2nd download, it should preserve order of download)", - idsExpectedAfter102, toIDs(queue)); + doAddToQueueAndAssertResult(message + " (2nd download, it should preserve order of download)", + calculator, 0, tFI102, queue, + idsExpectedAfter102); // Items 201 and 202 are added as part of a single DBWriter.addQueueItem() calls FeedItem tFI201 = tFI_isDownloading(201, mockDownloadRequester); - int pos201Actual = calculator.calcPosition(0, tFI201, queue); - queue.add(pos201Actual, tFI201); - assertEquals(message + " (bulk insertion, 1st item)", - idsExpectedAfter201, toIDs(queue)); + doAddToQueueAndAssertResult(message + " (bulk insertion, 1st item)", + calculator, 0, tFI201, queue, + idsExpectedAfter201); FeedItem tFI202 = tFI_isDownloading(202, mockDownloadRequester); - int pos202Actual = calculator.calcPosition(1, tFI202, queue); - queue.add(pos202Actual, tFI202); - assertEquals(message + " (bulk insertion, 2nd item)", - idsExpectedAfter202, toIDs(queue)); + doAddToQueueAndAssertResult(message + " (bulk insertion, 2nd item)", + calculator, 0, tFI202, queue, + idsExpectedAfter202); // TODO: simulate download failure cases. } @@ -267,6 +261,17 @@ public class DBWriterTest { // - common queue (of items) for tests // - construct FeedItems for tests + static void doAddToQueueAndAssertResult(String message, + ItemEnqueuePositionCalculator calculator, + int positionAmongAdd, + FeedItem itemToAdd, + List queue, + List idsExpected) { + int posActual = calculator.calcPosition(positionAmongAdd, itemToAdd, queue); + queue.add(posActual, itemToAdd); + assertEquals(message, idsExpected, toIDs(queue)); + } + static final List QUEUE_EMPTY = Collections.unmodifiableList(Arrays.asList()); static final List QUEUE_DEFAULT = Collections.unmodifiableList(Arrays.asList(tFI(11), tFI(12), tFI(13), tFI(14)));