From 8d0f2d371dacb98bd17add0c26d20c6dd6c47b6f Mon Sep 17 00:00:00 2001 From: Eric Xu Date: Fri, 21 May 2021 22:54:15 -0400 Subject: [PATCH] Test PlayQueue.setIndex(...) --- .../player/playqueue/PlayQueueTest.java | 71 +++++++++++++++++++ 1 file changed, 71 insertions(+) diff --git a/app/src/test/java/org/schabi/newpipe/player/playqueue/PlayQueueTest.java b/app/src/test/java/org/schabi/newpipe/player/playqueue/PlayQueueTest.java index 7ae383510..e050d1a82 100644 --- a/app/src/test/java/org/schabi/newpipe/player/playqueue/PlayQueueTest.java +++ b/app/src/test/java/org/schabi/newpipe/player/playqueue/PlayQueueTest.java @@ -7,11 +7,15 @@ import org.junit.runner.RunWith; import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.stream.StreamType; +import java.util.ArrayList; import java.util.Collections; import java.util.List; import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertNotEquals; +import static org.mockito.Mockito.doReturn; +import static org.mockito.Mockito.mock; +import static org.mockito.Mockito.spy; @SuppressWarnings("checkstyle:HideUtilityClassConstructor") @RunWith(Enclosed.class) @@ -31,6 +35,73 @@ public class PlayQueueTest { }; } + public static class SetIndexTests { + private static final int SIZE = 5; + private PlayQueue nonEmptyQueue; + private PlayQueue emptyQueue; + + @Before + public void setup() { + nonEmptyQueue = spy(mockPlayQueue( + 0, Collections.nCopies(SIZE, mock(PlayQueueItem.class)) + )); + emptyQueue = spy(mockPlayQueue(0, new ArrayList<>())); + } + + @Test + public void negative() { + nonEmptyQueue.setIndex(-5); + assertEquals(0, nonEmptyQueue.getIndex()); + + emptyQueue.setIndex(-5); + assertEquals(0, nonEmptyQueue.getIndex()); + } + + @Test + public void inBounds() { + nonEmptyQueue.setIndex(2); + assertEquals(2, nonEmptyQueue.getIndex()); + + // emptyQueue not tested because 0 isn't technically inBounds + } + + @Test + public void outOfBoundIsComplete() { + doReturn(true).when(nonEmptyQueue).isComplete(); + nonEmptyQueue.setIndex(7); + assertEquals(2, nonEmptyQueue.getIndex()); + + doReturn(true).when(emptyQueue).isComplete(); + emptyQueue.setIndex(2); + assertEquals(0, emptyQueue.getIndex()); + } + + @Test + public void outOfBoundsNotComplete() { + doReturn(false).when(nonEmptyQueue).isComplete(); + nonEmptyQueue.setIndex(7); + assertEquals(SIZE - 1, nonEmptyQueue.getIndex()); + + doReturn(false).when(emptyQueue).isComplete(); + emptyQueue.setIndex(2); + assertEquals(0, emptyQueue.getIndex()); + } + + @Test + public void indexZero() { + nonEmptyQueue.setIndex(0); + assertEquals(0, nonEmptyQueue.getIndex()); + + doReturn(true).when(emptyQueue).isComplete(); + emptyQueue.setIndex(0); + assertEquals(0, emptyQueue.getIndex()); + + doReturn(false).when(emptyQueue).isComplete(); + emptyQueue.setIndex(0); + assertEquals(0, emptyQueue.getIndex()); + } + } + public static class EqualsTests { private PlayQueueItem item1; private PlayQueueItem item2;