1
0
mirror of https://github.com/TeamNewPipe/NewPipe.git synced 2024-11-22 19:12:45 +01:00

Rewrite addToHistory test without using reflection

This commit is contained in:
Zhiheng Xu 2021-05-23 15:33:57 -04:00
parent e518c0dc14
commit 40f66977c7

View File

@ -8,17 +8,17 @@ import org.junit.runner.RunWith;
import org.schabi.newpipe.extractor.stream.StreamInfoItem; import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import org.schabi.newpipe.extractor.stream.StreamType; import org.schabi.newpipe.extractor.stream.StreamType;
import java.lang.reflect.Field;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.List; import java.util.List;
import java.util.Objects; import java.util.Objects;
import static org.junit.Assert.assertEquals; import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNotEquals; import static org.junit.Assert.assertNotEquals;
import static org.junit.Assert.assertNull; import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertTrue;
import static org.mockito.Mockito.doReturn; import static org.mockito.Mockito.doReturn;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.spy; import static org.mockito.Mockito.spy;
@SuppressWarnings("checkstyle:HideUtilityClassConstructor") @SuppressWarnings("checkstyle:HideUtilityClassConstructor")
@ -53,9 +53,11 @@ public class PlayQueueTest {
@Before @Before
public void setup() { public void setup() {
nonEmptyQueue = spy(makePlayQueue( final List<PlayQueueItem> streams = new ArrayList<>(5);
0, Collections.nCopies(SIZE, mock(PlayQueueItem.class)) for (int i = 0; i < 5; ++i) {
)); streams.add(makeItemWithUrl("URL_" + i));
}
nonEmptyQueue = spy(makePlayQueue(0, streams));
emptyQueue = spy(makePlayQueue(0, new ArrayList<>())); emptyQueue = spy(makePlayQueue(0, new ArrayList<>()));
} }
@ -112,30 +114,14 @@ public class PlayQueueTest {
assertEquals(0, emptyQueue.getIndex()); assertEquals(0, emptyQueue.getIndex());
} }
@SuppressWarnings("unchecked")
@Test @Test
public void addToHistory() throws NoSuchFieldException, IllegalAccessException { public void addToHistory() {
final Field field;
field = PlayQueue.class.getDeclaredField("history");
field.setAccessible(true);
List<PlayQueueItem> history;
/*
history's size is currently 1. 0 is the also the current index, so history should not
be affected.
*/
nonEmptyQueue.setIndex(0); nonEmptyQueue.setIndex(0);
history = (List<PlayQueueItem>) Objects.requireNonNull( assertFalse(nonEmptyQueue.previous());
field.get(nonEmptyQueue)
);
assertEquals(1, history.size());
// Index 3 != 0, so the second history element should be the item at streams[3]
nonEmptyQueue.setIndex(3); nonEmptyQueue.setIndex(3);
history = (List<PlayQueueItem>) Objects.requireNonNull( assertTrue(nonEmptyQueue.previous());
field.get(nonEmptyQueue) assertEquals("URL_0", Objects.requireNonNull(nonEmptyQueue.getItem()).getUrl());
);
assertEquals(nonEmptyQueue.getItem(3), history.get(1));
} }
} }