From 40f66977c77bb743c276f1baad4849944bb769b6 Mon Sep 17 00:00:00 2001 From: Zhiheng Xu Date: Sun, 23 May 2021 15:33:57 -0400 Subject: [PATCH] Rewrite addToHistory test without using reflection --- .../player/playqueue/PlayQueueTest.java | 36 ++++++------------- 1 file changed, 11 insertions(+), 25 deletions(-) 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 1c0500984..da1173e9b 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 @@ -8,17 +8,17 @@ import org.junit.runner.RunWith; import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.stream.StreamType; -import java.lang.reflect.Field; import java.util.ArrayList; import java.util.Collections; import java.util.List; import java.util.Objects; import static org.junit.Assert.assertEquals; +import static org.junit.Assert.assertFalse; import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNull; +import static org.junit.Assert.assertTrue; import static org.mockito.Mockito.doReturn; -import static org.mockito.Mockito.mock; import static org.mockito.Mockito.spy; @SuppressWarnings("checkstyle:HideUtilityClassConstructor") @@ -53,9 +53,11 @@ public class PlayQueueTest { @Before public void setup() { - nonEmptyQueue = spy(makePlayQueue( - 0, Collections.nCopies(SIZE, mock(PlayQueueItem.class)) - )); + final List streams = new ArrayList<>(5); + for (int i = 0; i < 5; ++i) { + streams.add(makeItemWithUrl("URL_" + i)); + } + nonEmptyQueue = spy(makePlayQueue(0, streams)); emptyQueue = spy(makePlayQueue(0, new ArrayList<>())); } @@ -112,30 +114,14 @@ public class PlayQueueTest { assertEquals(0, emptyQueue.getIndex()); } - @SuppressWarnings("unchecked") @Test - public void addToHistory() throws NoSuchFieldException, IllegalAccessException { - final Field field; - field = PlayQueue.class.getDeclaredField("history"); - field.setAccessible(true); - List history; - - /* - history's size is currently 1. 0 is the also the current index, so history should not - be affected. - */ + public void addToHistory() { nonEmptyQueue.setIndex(0); - history = (List) Objects.requireNonNull( - field.get(nonEmptyQueue) - ); - assertEquals(1, history.size()); + assertFalse(nonEmptyQueue.previous()); - // Index 3 != 0, so the second history element should be the item at streams[3] nonEmptyQueue.setIndex(3); - history = (List) Objects.requireNonNull( - field.get(nonEmptyQueue) - ); - assertEquals(nonEmptyQueue.getItem(3), history.get(1)); + assertTrue(nonEmptyQueue.previous()); + assertEquals("URL_0", Objects.requireNonNull(nonEmptyQueue.getItem()).getUrl()); } }