Skip to content

Commit 65f0ec6

Browse files
authored
Merge pull request #265 from mauriciocolli/improve-tests
Test if services recognizes their own items urls
2 parents 3525223 + 90ae5fb commit 65f0ec6

13 files changed

Lines changed: 312 additions & 286 deletions

extractor/src/test/java/org/schabi/newpipe/extractor/services/DefaultTests.java

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -2,33 +2,47 @@
22

33
import org.schabi.newpipe.extractor.InfoItem;
44
import org.schabi.newpipe.extractor.ListExtractor;
5+
import org.schabi.newpipe.extractor.NewPipe;
6+
import org.schabi.newpipe.extractor.StreamingService;
7+
import org.schabi.newpipe.extractor.channel.ChannelInfoItem;
8+
import org.schabi.newpipe.extractor.exceptions.ParsingException;
9+
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
10+
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
511
import org.schabi.newpipe.extractor.localization.DateWrapper;
12+
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem;
613
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
714

815
import java.util.Calendar;
916
import java.util.List;
1017

18+
import static junit.framework.TestCase.assertFalse;
1119
import static org.junit.Assert.*;
1220
import static org.schabi.newpipe.extractor.ExtractorAsserts.*;
21+
import static org.schabi.newpipe.extractor.StreamingService.*;
1322

1423
public final class DefaultTests {
15-
public static void defaultTestListOfItems(int expectedServiceId, List<? extends InfoItem> itemsList, List<Throwable> errors) {
16-
assertTrue("List of items is empty", !itemsList.isEmpty());
24+
public static void defaultTestListOfItems(StreamingService expectedService, List<? extends InfoItem> itemsList, List<Throwable> errors) throws ParsingException {
25+
assertFalse("List of items is empty", itemsList.isEmpty());
1726
assertFalse("List of items contains a null element", itemsList.contains(null));
18-
assertEmptyErrors("Errors during stream list extraction", errors);
27+
assertEmptyErrors("Errors during extraction", errors);
1928

2029
for (InfoItem item : itemsList) {
2130
assertIsSecureUrl(item.getUrl());
2231
if (item.getThumbnailUrl() != null && !item.getThumbnailUrl().isEmpty()) {
2332
assertIsSecureUrl(item.getThumbnailUrl());
2433
}
2534
assertNotNull("InfoItem type not set: " + item, item.getInfoType());
26-
assertEquals("Service id doesn't match: " + item, expectedServiceId, item.getServiceId());
35+
assertEquals("Unexpected item service id", expectedService.getServiceId(), item.getServiceId());
36+
assertNotEmpty("Item name not set: " + item, item.getName());
2737

2838
if (item instanceof StreamInfoItem) {
2939
StreamInfoItem streamInfoItem = (StreamInfoItem) item;
3040
assertNotEmpty("Uploader name not set: " + item, streamInfoItem.getUploaderName());
3141
assertNotEmpty("Uploader url not set: " + item, streamInfoItem.getUploaderUrl());
42+
assertIsSecureUrl(streamInfoItem.getUploaderUrl());
43+
44+
assertExpectedLinkType(expectedService, streamInfoItem.getUrl(), LinkType.STREAM);
45+
assertExpectedLinkType(expectedService, streamInfoItem.getUploaderUrl(), LinkType.CHANNEL);
3246

3347
final String textualUploadDate = streamInfoItem.getTextualUploadDate();
3448
if (textualUploadDate != null && !textualUploadDate.isEmpty()) {
@@ -37,34 +51,56 @@ public static void defaultTestListOfItems(int expectedServiceId, List<? extends
3751
assertTrue("Upload date not in the past", uploadDate.date().before(Calendar.getInstance()));
3852
}
3953

54+
} else if (item instanceof ChannelInfoItem) {
55+
final ChannelInfoItem channelInfoItem = (ChannelInfoItem) item;
56+
assertExpectedLinkType(expectedService, channelInfoItem.getUrl(), LinkType.CHANNEL);
57+
58+
} else if (item instanceof PlaylistInfoItem) {
59+
final PlaylistInfoItem playlistInfoItem = (PlaylistInfoItem) item;
60+
assertExpectedLinkType(expectedService, playlistInfoItem.getUrl(), LinkType.PLAYLIST);
4061
}
4162
}
4263
}
4364

44-
public static <T extends InfoItem> ListExtractor.InfoItemsPage<T> defaultTestRelatedItems(ListExtractor<T> extractor, int expectedServiceId) throws Exception {
65+
private static void assertExpectedLinkType(StreamingService expectedService, String url, LinkType expectedLinkType) throws ParsingException {
66+
final LinkType linkTypeByUrl = expectedService.getLinkTypeByUrl(url);
67+
68+
assertNotEquals("Url is not recognized by its own service: \"" + url + "\"",
69+
LinkType.NONE, linkTypeByUrl);
70+
assertEquals("Service returned wrong link type for: \"" + url + "\"",
71+
expectedLinkType, linkTypeByUrl);
72+
}
73+
74+
public static <T extends InfoItem> void assertNoMoreItems(ListExtractor<T> extractor) throws Exception {
75+
assertFalse("More items available when it shouldn't", extractor.hasNextPage());
76+
final String nextPageUrl = extractor.getNextPageUrl();
77+
assertTrue("Next page is not empty or null", nextPageUrl == null || nextPageUrl.isEmpty());
78+
}
79+
80+
public static <T extends InfoItem> ListExtractor.InfoItemsPage<T> defaultTestRelatedItems(ListExtractor<T> extractor) throws Exception {
4581
final ListExtractor.InfoItemsPage<T> page = extractor.getInitialPage();
4682
final List<T> itemsList = page.getItems();
4783
List<Throwable> errors = page.getErrors();
4884

49-
defaultTestListOfItems(expectedServiceId, itemsList, errors);
85+
defaultTestListOfItems(extractor.getService(), itemsList, errors);
5086
return page;
5187
}
5288

53-
public static <T extends InfoItem> ListExtractor.InfoItemsPage<T> defaultTestMoreItems(ListExtractor<T> extractor, int expectedServiceId) throws Exception {
89+
public static <T extends InfoItem> ListExtractor.InfoItemsPage<T> defaultTestMoreItems(ListExtractor<T> extractor) throws Exception {
5490
assertTrue("Doesn't have more items", extractor.hasNextPage());
5591
ListExtractor.InfoItemsPage<T> nextPage = extractor.getPage(extractor.getNextPageUrl());
5692
final List<T> items = nextPage.getItems();
57-
assertTrue("Next page is empty", !items.isEmpty());
93+
assertFalse("Next page is empty", items.isEmpty());
5894
assertEmptyErrors("Next page have errors", nextPage.getErrors());
5995

60-
defaultTestListOfItems(expectedServiceId, nextPage.getItems(), nextPage.getErrors());
96+
defaultTestListOfItems(extractor.getService(), nextPage.getItems(), nextPage.getErrors());
6197
return nextPage;
6298
}
6399

64-
public static void defaultTestGetPageInNewExtractor(ListExtractor<? extends InfoItem> extractor, ListExtractor<? extends InfoItem> newExtractor, int expectedServiceId) throws Exception {
100+
public static void defaultTestGetPageInNewExtractor(ListExtractor<? extends InfoItem> extractor, ListExtractor<? extends InfoItem> newExtractor) throws Exception {
65101
final String nextPageUrl = extractor.getNextPageUrl();
66102

67103
final ListExtractor.InfoItemsPage<? extends InfoItem> page = newExtractor.getPage(nextPageUrl);
68-
defaultTestListOfItems(expectedServiceId, page.getItems(), page.getErrors());
104+
defaultTestListOfItems(extractor.getService(), page.getItems(), page.getErrors());
69105
}
70106
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeChannelExtractorTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,12 +68,12 @@ public void testOriginalUrl() throws ParsingException {
6868

6969
@Test
7070
public void testRelatedItems() throws Exception {
71-
defaultTestRelatedItems(extractor, PeerTube.getServiceId());
71+
defaultTestRelatedItems(extractor);
7272
}
7373

7474
@Test
7575
public void testMoreRelatedItems() throws Exception {
76-
defaultTestMoreItems(extractor, PeerTube.getServiceId());
76+
defaultTestMoreItems(extractor);
7777
}
7878

7979
/*//////////////////////////////////////////////////////////////////////////
@@ -127,7 +127,7 @@ public static void setUp() throws Exception {
127127
@Test
128128
public void testGetPageInNewExtractor() throws Exception {
129129
final ChannelExtractor newExtractor = PeerTube.getChannelExtractor(extractor.getUrl());
130-
defaultTestGetPageInNewExtractor(extractor, newExtractor, PeerTube.getServiceId());
130+
defaultTestGetPageInNewExtractor(extractor, newExtractor);
131131
}
132132

133133
/*//////////////////////////////////////////////////////////////////////////
@@ -165,12 +165,12 @@ public void testOriginalUrl() throws ParsingException {
165165

166166
@Test
167167
public void testRelatedItems() throws Exception {
168-
defaultTestRelatedItems(extractor, PeerTube.getServiceId());
168+
defaultTestRelatedItems(extractor);
169169
}
170170

171171
@Test
172172
public void testMoreRelatedItems() throws Exception {
173-
defaultTestMoreItems(extractor, PeerTube.getServiceId());
173+
defaultTestMoreItems(extractor);
174174
}
175175

176176
/*//////////////////////////////////////////////////////////////////////////

extractor/src/test/java/org/schabi/newpipe/extractor/services/peertube/PeertubeTrendingExtractorTest.java

Lines changed: 50 additions & 65 deletions
Original file line numberDiff line numberDiff line change
@@ -5,90 +5,75 @@
55
import org.schabi.newpipe.DownloaderTestImpl;
66
import org.schabi.newpipe.extractor.ListExtractor;
77
import org.schabi.newpipe.extractor.NewPipe;
8+
import org.schabi.newpipe.extractor.exceptions.ParsingException;
89
import org.schabi.newpipe.extractor.kiosk.KioskExtractor;
10+
import org.schabi.newpipe.extractor.services.BaseListExtractorTest;
911
import org.schabi.newpipe.extractor.services.peertube.extractors.PeertubeTrendingExtractor;
12+
import org.schabi.newpipe.extractor.services.soundcloud.SoundcloudChartsExtractor;
13+
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeTrendingExtractor;
1014
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
1115

1216
import java.util.List;
1317

1418
import static org.junit.Assert.*;
15-
import static org.schabi.newpipe.extractor.ServiceList.PeerTube;
19+
import static org.schabi.newpipe.extractor.ServiceList.*;
20+
import static org.schabi.newpipe.extractor.services.DefaultTests.*;
1621

17-
/**
18-
* Test for {@link PeertubeTrendingExtractor}
19-
*/
2022
public class PeertubeTrendingExtractorTest {
23+
public static class Trending implements BaseListExtractorTest {
24+
private static PeertubeTrendingExtractor extractor;
2125

22-
static KioskExtractor extractor;
26+
@BeforeClass
27+
public static void setUp() throws Exception {
28+
NewPipe.init(DownloaderTestImpl.getInstance());
29+
// setting instance might break test when running in parallel
30+
PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host"));
31+
extractor = (PeertubeTrendingExtractor) PeerTube.getKioskList()
32+
.getExtractorById("Trending", null);
33+
extractor.fetchPage();
34+
}
2335

24-
@BeforeClass
25-
public static void setUp() throws Exception {
26-
NewPipe.init(DownloaderTestImpl.getInstance());
27-
// setting instance might break test when running in parallel
28-
PeerTube.setInstance(new PeertubeInstance("https://peertube.mastodon.host", "PeerTube on Mastodon.host"));
29-
extractor = PeerTube
30-
.getKioskList()
31-
.getExtractorById("Trending", null);
32-
extractor.fetchPage();
33-
}
36+
/*//////////////////////////////////////////////////////////////////////////
37+
// Extractor
38+
//////////////////////////////////////////////////////////////////////////*/
3439

35-
@Test
36-
public void testGetDownloader() throws Exception {
37-
assertNotNull(NewPipe.getDownloader());
38-
}
39-
40-
@Test
41-
public void testGetName() throws Exception {
42-
assertEquals(extractor.getName(), "Trending");
43-
}
40+
@Test
41+
public void testServiceId() {
42+
assertEquals(PeerTube.getServiceId(), extractor.getServiceId());
43+
}
4444

45-
@Test
46-
public void testId() {
47-
assertEquals(extractor.getId(), "Trending");
48-
}
45+
@Test
46+
public void testName() throws Exception {
47+
assertEquals("Trending", extractor.getName());
48+
}
4949

50-
@Test
51-
public void testGetStreams() throws Exception {
52-
ListExtractor.InfoItemsPage<StreamInfoItem> page = extractor.getInitialPage();
53-
if (!page.getErrors().isEmpty()) {
54-
System.err.println("----------");
55-
List<Throwable> errors = page.getErrors();
56-
for (Throwable e : errors) {
57-
e.printStackTrace();
58-
System.err.println("----------");
59-
}
50+
@Test
51+
public void testId() throws Exception {
52+
assertEquals("Trending", extractor.getId());
6053
}
61-
assertTrue("no streams are received",
62-
!page.getItems().isEmpty()
63-
&& page.getErrors().isEmpty());
64-
}
6554

66-
@Test
67-
public void testGetStreamsErrors() throws Exception {
68-
assertTrue("errors during stream list extraction", extractor.getInitialPage().getErrors().isEmpty());
69-
}
55+
@Test
56+
public void testUrl() throws ParsingException {
57+
assertEquals("https://peertube.mastodon.host/api/v1/videos?sort=-trending", extractor.getUrl());
58+
}
7059

71-
@Test
72-
public void testHasMoreStreams() throws Exception {
73-
// Setup the streams
74-
extractor.getInitialPage();
75-
assertTrue("has more streams", extractor.hasNextPage());
76-
}
60+
@Test
61+
public void testOriginalUrl() throws ParsingException {
62+
assertEquals("https://peertube.mastodon.host/api/v1/videos?sort=-trending", extractor.getOriginalUrl());
63+
}
7764

78-
@Test
79-
public void testGetNextPageUrl() throws Exception {
80-
assertTrue(extractor.hasNextPage());
81-
}
65+
/*//////////////////////////////////////////////////////////////////////////
66+
// ListExtractor
67+
//////////////////////////////////////////////////////////////////////////*/
8268

83-
@Test
84-
public void testGetNextPage() throws Exception {
85-
extractor.getInitialPage().getItems();
86-
assertFalse("extractor has next streams", extractor.getPage(extractor.getNextPageUrl()) == null
87-
|| extractor.getPage(extractor.getNextPageUrl()).getItems().isEmpty());
88-
}
69+
@Test
70+
public void testRelatedItems() throws Exception {
71+
defaultTestRelatedItems(extractor);
72+
}
8973

90-
@Test
91-
public void testGetCleanUrl() throws Exception {
92-
assertEquals(extractor.getUrl(), "https://peertube.mastodon.host/api/v1/videos?sort=-trending");
74+
@Test
75+
public void testMoreRelatedItems() throws Exception {
76+
defaultTestMoreItems(extractor);
77+
}
9378
}
9479
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/soundcloud/SoundcloudChannelExtractorTest.java

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -64,12 +64,12 @@ public void testOriginalUrl() throws ParsingException {
6464

6565
@Test
6666
public void testRelatedItems() throws Exception {
67-
defaultTestRelatedItems(extractor, SoundCloud.getServiceId());
67+
defaultTestRelatedItems(extractor);
6868
}
6969

7070
@Test
7171
public void testMoreRelatedItems() throws Exception {
72-
defaultTestMoreItems(extractor, SoundCloud.getServiceId());
72+
defaultTestMoreItems(extractor);
7373
}
7474

7575
/*//////////////////////////////////////////////////////////////////////////
@@ -120,7 +120,7 @@ public static void setUp() throws Exception {
120120
@Test
121121
public void testGetPageInNewExtractor() throws Exception {
122122
final ChannelExtractor newExtractor = SoundCloud.getChannelExtractor(extractor.getUrl());
123-
defaultTestGetPageInNewExtractor(extractor, newExtractor, SoundCloud.getServiceId());
123+
defaultTestGetPageInNewExtractor(extractor, newExtractor);
124124
}
125125

126126
/*//////////////////////////////////////////////////////////////////////////
@@ -158,12 +158,12 @@ public void testOriginalUrl() throws ParsingException {
158158

159159
@Test
160160
public void testRelatedItems() throws Exception {
161-
defaultTestRelatedItems(extractor, SoundCloud.getServiceId());
161+
defaultTestRelatedItems(extractor);
162162
}
163163

164164
@Test
165165
public void testMoreRelatedItems() throws Exception {
166-
defaultTestMoreItems(extractor, SoundCloud.getServiceId());
166+
defaultTestMoreItems(extractor);
167167
}
168168

169169
/*//////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)