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
1 changed files with 11 additions and 25 deletions

View File

@ -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<PlayQueueItem> 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<PlayQueueItem> 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<PlayQueueItem>) 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<PlayQueueItem>) Objects.requireNonNull(
field.get(nonEmptyQueue)
);
assertEquals(nonEmptyQueue.getItem(3), history.get(1));
assertTrue(nonEmptyQueue.previous());
assertEquals("URL_0", Objects.requireNonNull(nonEmptyQueue.getItem()).getUrl());
}
}