2018-02-22 15:52:38 +01:00
|
|
|
package org.schabi.newpipe.extractor.services.youtube;
|
|
|
|
|
|
|
|
import org.junit.BeforeClass;
|
|
|
|
import org.junit.Test;
|
|
|
|
import org.schabi.newpipe.Downloader;
|
|
|
|
import org.schabi.newpipe.extractor.NewPipe;
|
|
|
|
import org.schabi.newpipe.extractor.ServiceList;
|
2018-07-13 18:02:40 +02:00
|
|
|
import org.schabi.newpipe.extractor.linkhandler.LinkHandlerFactory;
|
2018-05-08 21:19:03 +02:00
|
|
|
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeSubscriptionExtractor;
|
2018-02-22 15:52:38 +01:00
|
|
|
import org.schabi.newpipe.extractor.subscription.SubscriptionExtractor;
|
|
|
|
import org.schabi.newpipe.extractor.subscription.SubscriptionItem;
|
2018-09-15 21:47:53 +02:00
|
|
|
import org.schabi.newpipe.extractor.utils.Localization;
|
2018-02-22 15:52:38 +01:00
|
|
|
|
|
|
|
import java.io.ByteArrayInputStream;
|
|
|
|
import java.io.File;
|
|
|
|
import java.io.FileInputStream;
|
|
|
|
import java.util.Arrays;
|
|
|
|
import java.util.List;
|
|
|
|
|
|
|
|
import static org.junit.Assert.*;
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Test for {@link YoutubeSubscriptionExtractor}
|
|
|
|
*/
|
|
|
|
public class YoutubeSubscriptionExtractorTest {
|
|
|
|
private static YoutubeSubscriptionExtractor subscriptionExtractor;
|
2018-07-13 18:02:40 +02:00
|
|
|
private static LinkHandlerFactory urlHandler;
|
2018-02-22 15:52:38 +01:00
|
|
|
|
|
|
|
@BeforeClass
|
|
|
|
public static void setupClass() {
|
2018-09-15 21:47:53 +02:00
|
|
|
NewPipe.init(Downloader.getInstance(), new Localization("GB", "en"));
|
2018-02-22 15:52:38 +01:00
|
|
|
subscriptionExtractor = new YoutubeSubscriptionExtractor(ServiceList.YouTube);
|
2018-08-05 14:14:36 +02:00
|
|
|
urlHandler = ServiceList.YouTube.getChannelLHFactory();
|
2018-02-22 15:52:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testFromInputStream() throws Exception {
|
2018-04-06 07:45:52 +02:00
|
|
|
File testFile = new File("extractor/src/test/resources/youtube_export_test.xml");
|
|
|
|
if (!testFile.exists()) testFile = new File("src/test/resources/youtube_export_test.xml");
|
|
|
|
|
2018-02-22 15:52:38 +01:00
|
|
|
List<SubscriptionItem> subscriptionItems = subscriptionExtractor.fromInputStream(new FileInputStream(testFile));
|
|
|
|
assertTrue("List doesn't have exactly 8 items (had " + subscriptionItems.size() + ")", subscriptionItems.size() == 8);
|
|
|
|
|
|
|
|
for (SubscriptionItem item : subscriptionItems) {
|
|
|
|
assertNotNull(item.getName());
|
|
|
|
assertNotNull(item.getUrl());
|
|
|
|
assertTrue(urlHandler.acceptUrl(item.getUrl()));
|
|
|
|
assertFalse(item.getServiceId() == -1);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Test
|
|
|
|
public void testEmptySourceException() throws Exception {
|
|
|
|
String emptySource = "<opml version=\"1.1\"><body>" +
|
|
|
|
"<outline text=\"Testing\" title=\"123\" />" +
|
|
|
|
"</body></opml>";
|
|
|
|
|
|
|
|
List<SubscriptionItem> items = subscriptionExtractor.fromInputStream(new ByteArrayInputStream(emptySource.getBytes("UTF-8")));
|
|
|
|
assertTrue(items.isEmpty());
|
|
|
|
}
|
|
|
|
|
2019-04-26 19:59:23 +02:00
|
|
|
@Test
|
|
|
|
public void testSubscriptionWithEmptyTitleInSource() throws Exception {
|
|
|
|
String channelName = "NAME OF CHANNEL";
|
|
|
|
String emptySource = "<opml version=\"1.1\"><body><outline text=\"YouTube Subscriptions\" title=\"YouTube Subscriptions\">" +
|
|
|
|
|
|
|
|
"<outline text=\"\" title=\"\" type=\"rss\" xmlUrl=\"https://www.youtube.com/feeds/videos.xml?channel_id=AA0AaAa0AaaaAAAAAA0aa0AA\" />" +
|
|
|
|
|
|
|
|
"<outline text=\"" + channelName + "\" title=\"" + channelName +
|
|
|
|
"\" type=\"rss\" xmlUrl=\"https://www.youtube.com/feeds/videos.xml?channel_id=AA0AaAa0AaaaAAAAAA0aa0AA\" />" +
|
|
|
|
|
|
|
|
"</outline></body></opml>";
|
|
|
|
|
|
|
|
List<SubscriptionItem> items = subscriptionExtractor.fromInputStream(new ByteArrayInputStream(emptySource.getBytes("UTF-8")));
|
|
|
|
assertTrue("List doesn't have exactly 1 item (had " + items.size() + ")", items.size() == 1);
|
|
|
|
assertTrue("Item does not have the right title \"" + channelName + "\" (had \"" + items.get(0).getName() + "\")", items.get(0).getName().equals(channelName));
|
|
|
|
}
|
|
|
|
|
2018-02-22 15:52:38 +01:00
|
|
|
@Test
|
|
|
|
public void testInvalidSourceException() {
|
|
|
|
List<String> invalidList = Arrays.asList(
|
|
|
|
"<xml><notvalid></notvalid></xml>",
|
|
|
|
"<opml><notvalid></notvalid></opml>",
|
|
|
|
"<opml><body></body></opml>",
|
|
|
|
"<opml><body><outline text=\"fail\" title=\"fail\" type=\"rss\" xmlUgrl=\"invalidTag\"/></outline></body></opml>",
|
|
|
|
"<opml><body><outline><outline text=\"invalid\" title=\"url\" type=\"rss\"" +
|
|
|
|
" xmlUrl=\"https://www.youtube.com/feeds/videos.xml?channel_not_id=|||||||\"/></outline></body></opml>",
|
|
|
|
"",
|
|
|
|
null,
|
|
|
|
"\uD83D\uDC28\uD83D\uDC28\uD83D\uDC28",
|
|
|
|
"gibberish");
|
|
|
|
|
|
|
|
for (String invalidContent : invalidList) {
|
|
|
|
try {
|
|
|
|
if (invalidContent != null) {
|
|
|
|
byte[] bytes = invalidContent.getBytes("UTF-8");
|
|
|
|
subscriptionExtractor.fromInputStream(new ByteArrayInputStream(bytes));
|
|
|
|
} else {
|
|
|
|
subscriptionExtractor.fromInputStream(null);
|
|
|
|
}
|
|
|
|
|
|
|
|
fail("didn't throw exception");
|
|
|
|
} catch (Exception e) {
|
|
|
|
// System.out.println(" -> " + e);
|
|
|
|
boolean isExpectedException = e instanceof SubscriptionExtractor.InvalidSourceException;
|
|
|
|
assertTrue("\"" + e.getClass().getSimpleName() + "\" is not the expected exception", isExpectedException);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|