2018-03-04 22:22:27 +01:00
|
|
|
package org.schabi.newpipe.extractor.services;
|
|
|
|
|
|
|
|
import org.schabi.newpipe.extractor.InfoItem;
|
|
|
|
import org.schabi.newpipe.extractor.ListExtractor;
|
2020-02-27 04:08:23 +01:00
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
|
|
|
import org.schabi.newpipe.extractor.StreamingService;
|
|
|
|
import org.schabi.newpipe.extractor.channel.ChannelInfoItem;
|
|
|
|
import org.schabi.newpipe.extractor.exceptions.ParsingException;
|
|
|
|
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
|
|
|
|
import org.schabi.newpipe.extractor.linkhandler.ListLinkHandlerFactory;
|
2019-11-03 19:45:25 +01:00
|
|
|
import org.schabi.newpipe.extractor.localization.DateWrapper;
|
2020-02-27 04:08:23 +01:00
|
|
|
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem;
|
2018-03-04 22:22:27 +01:00
|
|
|
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
|
|
|
|
|
2019-10-02 07:02:01 +02:00
|
|
|
import java.util.Calendar;
|
2018-03-04 22:22:27 +01:00
|
|
|
import java.util.List;
|
|
|
|
|
2020-02-27 04:08:23 +01:00
|
|
|
import static junit.framework.TestCase.assertFalse;
|
2018-03-04 22:22:27 +01:00
|
|
|
import static org.junit.Assert.*;
|
|
|
|
import static org.schabi.newpipe.extractor.ExtractorAsserts.*;
|
2020-02-27 04:08:23 +01:00
|
|
|
import static org.schabi.newpipe.extractor.StreamingService.*;
|
2018-03-04 22:22:27 +01:00
|
|
|
|
|
|
|
public final class DefaultTests {
|
2020-02-27 04:08:23 +01:00
|
|
|
public static void defaultTestListOfItems(StreamingService expectedService, List<? extends InfoItem> itemsList, List<Throwable> errors) throws ParsingException {
|
|
|
|
assertFalse("List of items is empty", itemsList.isEmpty());
|
2018-03-04 22:22:27 +01:00
|
|
|
assertFalse("List of items contains a null element", itemsList.contains(null));
|
2020-02-27 04:08:23 +01:00
|
|
|
assertEmptyErrors("Errors during extraction", errors);
|
2018-03-04 22:22:27 +01:00
|
|
|
|
|
|
|
for (InfoItem item : itemsList) {
|
|
|
|
assertIsSecureUrl(item.getUrl());
|
|
|
|
if (item.getThumbnailUrl() != null && !item.getThumbnailUrl().isEmpty()) {
|
|
|
|
assertIsSecureUrl(item.getThumbnailUrl());
|
|
|
|
}
|
|
|
|
assertNotNull("InfoItem type not set: " + item, item.getInfoType());
|
2020-02-27 04:08:23 +01:00
|
|
|
assertEquals("Unexpected item service id", expectedService.getServiceId(), item.getServiceId());
|
|
|
|
assertNotEmpty("Item name not set: " + item, item.getName());
|
2018-03-04 22:22:27 +01:00
|
|
|
|
|
|
|
if (item instanceof StreamInfoItem) {
|
|
|
|
StreamInfoItem streamInfoItem = (StreamInfoItem) item;
|
|
|
|
assertNotEmpty("Uploader name not set: " + item, streamInfoItem.getUploaderName());
|
|
|
|
assertNotEmpty("Uploader url not set: " + item, streamInfoItem.getUploaderUrl());
|
2020-02-27 04:08:23 +01:00
|
|
|
assertIsSecureUrl(streamInfoItem.getUploaderUrl());
|
|
|
|
|
|
|
|
assertExpectedLinkType(expectedService, streamInfoItem.getUrl(), LinkType.STREAM);
|
|
|
|
assertExpectedLinkType(expectedService, streamInfoItem.getUploaderUrl(), LinkType.CHANNEL);
|
2019-10-02 07:02:01 +02:00
|
|
|
|
|
|
|
final String textualUploadDate = streamInfoItem.getTextualUploadDate();
|
|
|
|
if (textualUploadDate != null && !textualUploadDate.isEmpty()) {
|
2019-11-03 19:45:25 +01:00
|
|
|
final DateWrapper uploadDate = streamInfoItem.getUploadDate();
|
2019-10-02 07:02:01 +02:00
|
|
|
assertNotNull("No parsed upload date", uploadDate);
|
2019-11-03 19:45:25 +01:00
|
|
|
assertTrue("Upload date not in the past", uploadDate.date().before(Calendar.getInstance()));
|
2019-10-02 07:02:01 +02:00
|
|
|
}
|
|
|
|
|
2020-02-27 04:08:23 +01:00
|
|
|
} else if (item instanceof ChannelInfoItem) {
|
|
|
|
final ChannelInfoItem channelInfoItem = (ChannelInfoItem) item;
|
|
|
|
assertExpectedLinkType(expectedService, channelInfoItem.getUrl(), LinkType.CHANNEL);
|
|
|
|
|
|
|
|
} else if (item instanceof PlaylistInfoItem) {
|
|
|
|
final PlaylistInfoItem playlistInfoItem = (PlaylistInfoItem) item;
|
|
|
|
assertExpectedLinkType(expectedService, playlistInfoItem.getUrl(), LinkType.PLAYLIST);
|
2018-03-04 22:22:27 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-02-27 04:08:23 +01:00
|
|
|
private static void assertExpectedLinkType(StreamingService expectedService, String url, LinkType expectedLinkType) throws ParsingException {
|
|
|
|
final LinkType linkTypeByUrl = expectedService.getLinkTypeByUrl(url);
|
|
|
|
|
|
|
|
assertNotEquals("Url is not recognized by its own service: \"" + url + "\"",
|
|
|
|
LinkType.NONE, linkTypeByUrl);
|
|
|
|
assertEquals("Service returned wrong link type for: \"" + url + "\"",
|
|
|
|
expectedLinkType, linkTypeByUrl);
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T extends InfoItem> void assertNoMoreItems(ListExtractor<T> extractor) throws Exception {
|
|
|
|
assertFalse("More items available when it shouldn't", extractor.hasNextPage());
|
|
|
|
final String nextPageUrl = extractor.getNextPageUrl();
|
|
|
|
assertTrue("Next page is not empty or null", nextPageUrl == null || nextPageUrl.isEmpty());
|
|
|
|
}
|
|
|
|
|
|
|
|
public static <T extends InfoItem> ListExtractor.InfoItemsPage<T> defaultTestRelatedItems(ListExtractor<T> extractor) throws Exception {
|
2018-03-11 21:54:41 +01:00
|
|
|
final ListExtractor.InfoItemsPage<T> page = extractor.getInitialPage();
|
|
|
|
final List<T> itemsList = page.getItems();
|
|
|
|
List<Throwable> errors = page.getErrors();
|
2018-03-04 22:22:27 +01:00
|
|
|
|
2020-02-27 04:08:23 +01:00
|
|
|
defaultTestListOfItems(extractor.getService(), itemsList, errors);
|
2018-03-11 21:54:41 +01:00
|
|
|
return page;
|
2018-03-04 22:22:27 +01:00
|
|
|
}
|
|
|
|
|
2020-02-27 04:08:23 +01:00
|
|
|
public static <T extends InfoItem> ListExtractor.InfoItemsPage<T> defaultTestMoreItems(ListExtractor<T> extractor) throws Exception {
|
2018-03-04 22:22:27 +01:00
|
|
|
assertTrue("Doesn't have more items", extractor.hasNextPage());
|
2018-03-11 21:54:41 +01:00
|
|
|
ListExtractor.InfoItemsPage<T> nextPage = extractor.getPage(extractor.getNextPageUrl());
|
|
|
|
final List<T> items = nextPage.getItems();
|
2020-02-27 04:08:23 +01:00
|
|
|
assertFalse("Next page is empty", items.isEmpty());
|
2018-03-04 22:22:27 +01:00
|
|
|
assertEmptyErrors("Next page have errors", nextPage.getErrors());
|
|
|
|
|
2020-02-27 04:08:23 +01:00
|
|
|
defaultTestListOfItems(extractor.getService(), nextPage.getItems(), nextPage.getErrors());
|
2018-03-04 22:22:27 +01:00
|
|
|
return nextPage;
|
|
|
|
}
|
|
|
|
|
2020-02-27 04:08:23 +01:00
|
|
|
public static void defaultTestGetPageInNewExtractor(ListExtractor<? extends InfoItem> extractor, ListExtractor<? extends InfoItem> newExtractor) throws Exception {
|
2018-03-04 22:22:27 +01:00
|
|
|
final String nextPageUrl = extractor.getNextPageUrl();
|
|
|
|
|
2018-03-11 21:54:41 +01:00
|
|
|
final ListExtractor.InfoItemsPage<? extends InfoItem> page = newExtractor.getPage(nextPageUrl);
|
2020-02-27 04:08:23 +01:00
|
|
|
defaultTestListOfItems(extractor.getService(), page.getItems(), page.getErrors());
|
2018-03-04 22:22:27 +01:00
|
|
|
}
|
|
|
|
}
|