Skip to content

Commit e590417

Browse files
mauriciocolliTobiGr
authored andcommitted
Test if services recognizes their own items urls
1 parent 3525223 commit e590417

9 files changed

Lines changed: 91 additions & 59 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/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
/*//////////////////////////////////////////////////////////////////////////

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

Lines changed: 8 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
import org.schabi.newpipe.DownloaderTestImpl;
88
import org.schabi.newpipe.extractor.ListExtractor;
99
import org.schabi.newpipe.extractor.NewPipe;
10-
import org.schabi.newpipe.extractor.ServiceList;
1110
import org.schabi.newpipe.extractor.playlist.PlaylistExtractor;
1211
import org.schabi.newpipe.extractor.services.BasePlaylistExtractorTest;
1312
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
@@ -67,13 +66,13 @@ public void testOriginalUrl() throws Exception {
6766

6867
@Test
6968
public void testRelatedItems() throws Exception {
70-
defaultTestRelatedItems(extractor, SoundCloud.getServiceId());
69+
defaultTestRelatedItems(extractor);
7170
}
7271

7372
@Test
7473
public void testMoreRelatedItems() {
7574
try {
76-
defaultTestMoreItems(extractor, SoundCloud.getServiceId());
75+
defaultTestMoreItems(extractor);
7776
} catch (Throwable ignored) {
7877
return;
7978
}
@@ -165,12 +164,12 @@ public void testOriginalUrl() throws Exception {
165164

166165
@Test
167166
public void testRelatedItems() throws Exception {
168-
defaultTestRelatedItems(extractor, SoundCloud.getServiceId());
167+
defaultTestRelatedItems(extractor);
169168
}
170169

171170
@Test
172171
public void testMoreRelatedItems() throws Exception {
173-
defaultTestMoreItems(extractor, SoundCloud.getServiceId());
172+
defaultTestMoreItems(extractor);
174173
}
175174

176175
/*//////////////////////////////////////////////////////////////////////////
@@ -226,11 +225,10 @@ public static void setUp() throws Exception {
226225
// Additional Testing
227226
//////////////////////////////////////////////////////////////////////////*/
228227

229-
@Ignore
230228
@Test
231229
public void testGetPageInNewExtractor() throws Exception {
232230
final PlaylistExtractor newExtractor = SoundCloud.getPlaylistExtractor(extractor.getUrl());
233-
defaultTestGetPageInNewExtractor(extractor, newExtractor, SoundCloud.getServiceId());
231+
defaultTestGetPageInNewExtractor(extractor, newExtractor);
234232
}
235233

236234
/*//////////////////////////////////////////////////////////////////////////
@@ -269,18 +267,18 @@ public void testOriginalUrl() throws Exception {
269267
@Ignore
270268
@Test
271269
public void testRelatedItems() throws Exception {
272-
defaultTestRelatedItems(extractor, SoundCloud.getServiceId());
270+
defaultTestRelatedItems(extractor);
273271
}
274272

275273
//TODO: FUCK THIS: This triggers a 500 at sever
276274
@Ignore
277275
@Test
278276
public void testMoreRelatedItems() throws Exception {
279-
ListExtractor.InfoItemsPage<StreamInfoItem> currentPage = defaultTestMoreItems(extractor, ServiceList.SoundCloud.getServiceId());
277+
ListExtractor.InfoItemsPage<StreamInfoItem> currentPage = defaultTestMoreItems(extractor);
280278
// Test for 2 more levels
281279
for (int i = 0; i < 2; i++) {
282280
currentPage = extractor.getPage(currentPage.getNextPageUrl());
283-
defaultTestListOfItems(SoundCloud.getServiceId(), currentPage.getItems(), currentPage.getErrors());
281+
defaultTestListOfItems(SoundCloud, currentPage.getItems(), currentPage.getErrors());
284282
}
285283
}
286284

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelExtractorTest.java

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@
44
import org.junit.Test;
55
import org.schabi.newpipe.DownloaderTestImpl;
66
import org.schabi.newpipe.extractor.NewPipe;
7-
import org.schabi.newpipe.extractor.ServiceList;
87
import org.schabi.newpipe.extractor.channel.ChannelExtractor;
98
import org.schabi.newpipe.extractor.exceptions.ContentNotAvailableException;
109
import org.schabi.newpipe.extractor.exceptions.ParsingException;
@@ -88,12 +87,12 @@ public void testOriginalUrl() throws ParsingException {
8887

8988
@Test
9089
public void testRelatedItems() throws Exception {
91-
defaultTestRelatedItems(extractor, YouTube.getServiceId());
90+
defaultTestRelatedItems(extractor);
9291
}
9392

9493
@Test
9594
public void testMoreRelatedItems() throws Exception {
96-
defaultTestMoreItems(extractor, ServiceList.YouTube.getServiceId());
95+
defaultTestMoreItems(extractor);
9796
}
9897

9998
/*//////////////////////////////////////////////////////////////////////////
@@ -178,12 +177,12 @@ public void testOriginalUrl() throws ParsingException {
178177

179178
@Test
180179
public void testRelatedItems() throws Exception {
181-
defaultTestRelatedItems(extractor, YouTube.getServiceId());
180+
defaultTestRelatedItems(extractor);
182181
}
183182

184183
@Test
185184
public void testMoreRelatedItems() throws Exception {
186-
defaultTestMoreItems(extractor, ServiceList.YouTube.getServiceId());
185+
defaultTestMoreItems(extractor);
187186
}
188187

189188
/*//////////////////////////////////////////////////////////////////////////
@@ -241,7 +240,7 @@ public static void setUp() throws Exception {
241240
@Test
242241
public void testGetPageInNewExtractor() throws Exception {
243242
final ChannelExtractor newExtractor = YouTube.getChannelExtractor(extractor.getUrl());
244-
defaultTestGetPageInNewExtractor(extractor, newExtractor, YouTube.getServiceId());
243+
defaultTestGetPageInNewExtractor(extractor, newExtractor);
245244
}
246245

247246
/*//////////////////////////////////////////////////////////////////////////
@@ -280,12 +279,12 @@ public void testOriginalUrl() throws ParsingException {
280279

281280
@Test
282281
public void testRelatedItems() throws Exception {
283-
defaultTestRelatedItems(extractor, YouTube.getServiceId());
282+
defaultTestRelatedItems(extractor);
284283
}
285284

286285
@Test
287286
public void testMoreRelatedItems() throws Exception {
288-
defaultTestMoreItems(extractor, ServiceList.YouTube.getServiceId());
287+
defaultTestMoreItems(extractor);
289288
}
290289

291290
/*//////////////////////////////////////////////////////////////////////////
@@ -371,12 +370,12 @@ public void testOriginalUrl() throws ParsingException {
371370

372371
@Test
373372
public void testRelatedItems() throws Exception {
374-
defaultTestRelatedItems(extractor, YouTube.getServiceId());
373+
defaultTestRelatedItems(extractor);
375374
}
376375

377376
@Test
378377
public void testMoreRelatedItems() throws Exception {
379-
defaultTestMoreItems(extractor, ServiceList.YouTube.getServiceId());
378+
defaultTestMoreItems(extractor);
380379
}
381380

382381
/*//////////////////////////////////////////////////////////////////////////
@@ -461,12 +460,12 @@ public void testOriginalUrl() throws ParsingException {
461460

462461
@Test
463462
public void testRelatedItems() throws Exception {
464-
defaultTestRelatedItems(extractor, YouTube.getServiceId());
463+
defaultTestRelatedItems(extractor);
465464
}
466465

467466
@Test
468467
public void testMoreRelatedItems() throws Exception {
469-
defaultTestMoreItems(extractor, YouTube.getServiceId());
468+
defaultTestMoreItems(extractor);
470469
}
471470

472471
/*//////////////////////////////////////////////////////////////////////////
@@ -553,13 +552,13 @@ public void testOriginalUrl() throws ParsingException {
553552

554553
@Test
555554
public void testRelatedItems() throws Exception {
556-
defaultTestRelatedItems(extractor, YouTube.getServiceId());
555+
defaultTestRelatedItems(extractor);
557556
}
558557

559558
@Test
560559
public void testMoreRelatedItems() {
561560
try {
562-
defaultTestMoreItems(extractor, YouTube.getServiceId());
561+
defaultTestMoreItems(extractor);
563562
} catch (Throwable ignored) {
564563
return;
565564
}

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeChannelLocalizationTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ private void testLocalizationsFor(String channelUrl) throws Exception {
4949
final ChannelExtractor extractor = YouTube.getChannelExtractor(channelUrl);
5050
extractor.forceLocalization(currentLocalization);
5151
extractor.fetchPage();
52-
itemsPage = defaultTestRelatedItems(extractor, YouTube.getServiceId());
52+
itemsPage = defaultTestRelatedItems(extractor);
5353
} catch (Throwable e) {
5454
System.out.println("[!] " + currentLocalization + " → failed");
5555
throw e;

extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/YoutubeCommentsExtractorTest.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -84,7 +84,7 @@ private boolean getCommentsFromCommentsInfoHelper(String url) throws IOException
8484
public void testGetCommentsAllData() throws IOException, ExtractionException {
8585
InfoItemsPage<CommentsInfoItem> comments = extractorYT.getInitialPage();
8686

87-
DefaultTests.defaultTestListOfItems(YouTube.getServiceId(), comments.getItems(), comments.getErrors());
87+
DefaultTests.defaultTestListOfItems(YouTube, comments.getItems(), comments.getErrors());
8888
for (CommentsInfoItem c : comments.getItems()) {
8989
assertFalse(StringUtil.isBlank(c.getAuthorEndpoint()));
9090
assertFalse(StringUtil.isBlank(c.getAuthorName()));

0 commit comments

Comments
 (0)