Skip to content

Commit 40f6697

Browse files
committed
Rewrite addToHistory test without using reflection
1 parent e518c0d commit 40f6697

1 file changed

Lines changed: 11 additions & 25 deletions

File tree

app/src/test/java/org/schabi/newpipe/player/playqueue/PlayQueueTest.java

Lines changed: 11 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -8,17 +8,17 @@
88
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
99
import org.schabi.newpipe.extractor.stream.StreamType;
1010

11-
import java.lang.reflect.Field;
1211
import java.util.ArrayList;
1312
import java.util.Collections;
1413
import java.util.List;
1514
import java.util.Objects;
1615

1716
import static org.junit.Assert.assertEquals;
17+
import static org.junit.Assert.assertFalse;
1818
import static org.junit.Assert.assertNotEquals;
1919
import static org.junit.Assert.assertNull;
20+
import static org.junit.Assert.assertTrue;
2021
import static org.mockito.Mockito.doReturn;
21-
import static org.mockito.Mockito.mock;
2222
import static org.mockito.Mockito.spy;
2323

2424
@SuppressWarnings("checkstyle:HideUtilityClassConstructor")
@@ -53,9 +53,11 @@ public static class SetIndexTests {
5353

5454
@Before
5555
public void setup() {
56-
nonEmptyQueue = spy(makePlayQueue(
57-
0, Collections.nCopies(SIZE, mock(PlayQueueItem.class))
58-
));
56+
final List<PlayQueueItem> streams = new ArrayList<>(5);
57+
for (int i = 0; i < 5; ++i) {
58+
streams.add(makeItemWithUrl("URL_" + i));
59+
}
60+
nonEmptyQueue = spy(makePlayQueue(0, streams));
5961
emptyQueue = spy(makePlayQueue(0, new ArrayList<>()));
6062
}
6163

@@ -112,30 +114,14 @@ public void indexZero() {
112114
assertEquals(0, emptyQueue.getIndex());
113115
}
114116

115-
@SuppressWarnings("unchecked")
116117
@Test
117-
public void addToHistory() throws NoSuchFieldException, IllegalAccessException {
118-
final Field field;
119-
field = PlayQueue.class.getDeclaredField("history");
120-
field.setAccessible(true);
121-
List<PlayQueueItem> history;
122-
123-
/*
124-
history's size is currently 1. 0 is the also the current index, so history should not
125-
be affected.
126-
*/
118+
public void addToHistory() {
127119
nonEmptyQueue.setIndex(0);
128-
history = (List<PlayQueueItem>) Objects.requireNonNull(
129-
field.get(nonEmptyQueue)
130-
);
131-
assertEquals(1, history.size());
120+
assertFalse(nonEmptyQueue.previous());
132121

133-
// Index 3 != 0, so the second history element should be the item at streams[3]
134122
nonEmptyQueue.setIndex(3);
135-
history = (List<PlayQueueItem>) Objects.requireNonNull(
136-
field.get(nonEmptyQueue)
137-
);
138-
assertEquals(nonEmptyQueue.getItem(3), history.get(1));
123+
assertTrue(nonEmptyQueue.previous());
124+
assertEquals("URL_0", Objects.requireNonNull(nonEmptyQueue.getItem()).getUrl());
139125
}
140126
}
141127

0 commit comments

Comments
 (0)