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

107 lines
5.7 KiB
Java
Raw Normal View History

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;
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;
import org.schabi.newpipe.extractor.localization.DateWrapper;
import org.schabi.newpipe.extractor.playlist.PlaylistInfoItem;
2018-03-04 22:22:27 +01:00
import org.schabi.newpipe.extractor.stream.StreamInfoItem;
import java.util.Calendar;
2018-03-04 22:22:27 +01:00
import java.util.List;
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.*;
import static org.schabi.newpipe.extractor.StreamingService.*;
2018-03-04 22:22:27 +01:00
public final class DefaultTests {
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));
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());
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());
assertIsSecureUrl(streamInfoItem.getUploaderUrl());
assertExpectedLinkType(expectedService, streamInfoItem.getUrl(), LinkType.STREAM);
assertExpectedLinkType(expectedService, streamInfoItem.getUploaderUrl(), LinkType.CHANNEL);
final String textualUploadDate = streamInfoItem.getTextualUploadDate();
if (textualUploadDate != null && !textualUploadDate.isEmpty()) {
final DateWrapper uploadDate = streamInfoItem.getUploadDate();
assertNotNull("No parsed upload date", uploadDate);
assertTrue("Upload date not in the past", uploadDate.date().before(Calendar.getInstance()));
}
} 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
}
}
}
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 {
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
defaultTestListOfItems(extractor.getService(), itemsList, errors);
return page;
2018-03-04 22:22:27 +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());
ListExtractor.InfoItemsPage<T> nextPage = extractor.getPage(extractor.getNextPageUrl());
final List<T> items = nextPage.getItems();
assertFalse("Next page is empty", items.isEmpty());
2018-03-04 22:22:27 +01:00
assertEmptyErrors("Next page have errors", nextPage.getErrors());
defaultTestListOfItems(extractor.getService(), nextPage.getItems(), nextPage.getErrors());
2018-03-04 22:22:27 +01:00
return nextPage;
}
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();
final ListExtractor.InfoItemsPage<? extends InfoItem> page = newExtractor.getPage(nextPageUrl);
defaultTestListOfItems(extractor.getService(), page.getItems(), page.getErrors());
2018-03-04 22:22:27 +01:00
}
}