diff --git a/extractor/src/test/java/org/schabi/newpipe/NewPipeTestRunner.java b/extractor/src/test/java/org/schabi/newpipe/NewPipeTestRunner.java new file mode 100644 index 0000000000..fd98fc1eca --- /dev/null +++ b/extractor/src/test/java/org/schabi/newpipe/NewPipeTestRunner.java @@ -0,0 +1,118 @@ +package org.schabi.newpipe; + +import org.junit.Ignore; +import org.junit.internal.AssumptionViolatedException; +import org.junit.runner.Description; +import org.junit.runner.notification.Failure; +import org.junit.runner.notification.RunNotifier; +import org.junit.runners.BlockJUnit4ClassRunner; +import org.junit.runners.model.FrameworkMethod; +import org.junit.runners.model.InitializationError; +import org.junit.runners.model.Statement; + +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.TimeUnit; + +public class NewPipeTestRunner extends BlockJUnit4ClassRunner { + private final NewPipeTestRunnerOptions options; + + private List methodsNotFailing; + private int currentRetry; + + public NewPipeTestRunner(Class testClass) throws InitializationError { + super(testClass); + + if (testClass.isAnnotationPresent(NewPipeTestRunnerOptions.class)) { + options = (NewPipeTestRunnerOptions) testClass.getAnnotation(NewPipeTestRunnerOptions.class); + validateOptions(testClass); + } else { + throw new InitializationError("Test class " + testClass.getCanonicalName() + + " running with " + NewPipeTestRunner.class.getSimpleName() + " should have the @" + + NewPipeTestRunnerOptions.class.getSimpleName() + " annotation"); + } + } + + private void validateOptions(Class testClass) throws InitializationError { + if (options.classDelayMs() < 0) { + throw new InitializationError("classDelayMs value should not be negative in annotation @" + + NewPipeTestRunnerOptions.class.getSimpleName() + " in class " + testClass.getCanonicalName()); + } + if (options.methodDelayMs() < 0) { + throw new InitializationError("methodDelayMs value should not be negative in annotation @" + + NewPipeTestRunnerOptions.class.getSimpleName() + " in class " + testClass.getCanonicalName()); + } + if (options.retry() < 1) { + throw new InitializationError("retry value should be bigger than 0 in annotation @" + + NewPipeTestRunnerOptions.class.getSimpleName() + " in class " + testClass.getCanonicalName()); + } + } + + + private void sleep(int milliseconds) { + if (milliseconds > 0) { + try { + TimeUnit.MILLISECONDS.sleep(milliseconds); + } catch (InterruptedException e) { + throw new RuntimeException(e); + } + } + } + + @Override + public void run(RunNotifier notifier) { + sleep(options.classDelayMs()); // @see NewPipeTestRunnerOptions.classDelayMs + + methodsNotFailing = new ArrayList<>(); + for (currentRetry = 1; currentRetry <= options.retry(); ++currentRetry) { + if (getChildren().size() == methodsNotFailing.size()) { + break; + } + super.run(notifier); + } + } + + @Override + protected void runChild(FrameworkMethod method, RunNotifier notifier) { + if (isMethodAlreadyNotFailing(method)) return; + Description description = describeChild(method); + + if (isIgnored(method)) { + notifier.fireTestIgnored(description); + markMethodAsNotFailing(method); + + String ignoreReason = method.getAnnotation(Ignore.class).value(); + System.out.println(method.getName() + "() ignored because of @Ignore" + + (ignoreReason.isEmpty() ? "" : ": " + ignoreReason)); + + } else { + sleep(options.methodDelayMs()); // @see NewPipeTestRunnerOptions.methodDelayMs + Statement statement = methodBlock(method); + notifier.fireTestStarted(description); + + try { + statement.evaluate(); + markMethodAsNotFailing(method); + + } catch (Throwable e) { + if (currentRetry < options.retry() || e instanceof AssumptionViolatedException) { + notifier.fireTestAssumptionFailed(new Failure(description, e)); + } else { + notifier.fireTestFailure(new Failure(description, e)); // test is not going to be retried anymore + } + + } finally { + notifier.fireTestFinished(description); + } + } + } + + + private void markMethodAsNotFailing(FrameworkMethod method) { + methodsNotFailing.add(method.getName()); + } + + private boolean isMethodAlreadyNotFailing(FrameworkMethod method) { + return methodsNotFailing.contains(method.getName()); + } +} diff --git a/extractor/src/test/java/org/schabi/newpipe/NewPipeTestRunnerOptions.java b/extractor/src/test/java/org/schabi/newpipe/NewPipeTestRunnerOptions.java new file mode 100644 index 0000000000..95dab0e783 --- /dev/null +++ b/extractor/src/test/java/org/schabi/newpipe/NewPipeTestRunnerOptions.java @@ -0,0 +1,32 @@ +package org.schabi.newpipe; + +import java.lang.annotation.Retention; +import java.lang.annotation.RetentionPolicy; + +@Retention(RetentionPolicy.RUNTIME) +public @interface NewPipeTestRunnerOptions { + /** + * This tells the Runner to wait the specified time before + * running the tests in the annotated class. + * @return milliseconds to sleep before testing the class + */ + int classDelayMs() default 0; + + /** + * This tells the Runner to wait the specified time before + * invoking each test method in the annotated class. + * @return milliseconds to sleep before invoking each test method + */ + int methodDelayMs() default 0; + + /** + * This tells the Runner to retry at most the specified number of + * times running the tests in the annotated class. As soon as the + * maximum number of retries is hit or all test methods succeed, + * the Runner completes, respectively with the last errors or + * with success. + * @return the number of times the class should be tested before + * declaring one of its test methods as failed. + */ + int retry() default 1; +} diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/ExtractorAsserts.java b/extractor/src/test/java/org/schabi/newpipe/extractor/ExtractorAsserts.java index 1006f7b34e..b17d1592cd 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/ExtractorAsserts.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/ExtractorAsserts.java @@ -2,6 +2,9 @@ import javax.annotation.Nonnull; import javax.annotation.Nullable; + +import java.io.IOException; +import java.net.HttpURLConnection; import java.net.MalformedURLException; import java.net.URL; import java.util.List; @@ -20,6 +23,7 @@ public static void assertEmptyErrors(String message, List errors) { } } + @Nonnull private static URL urlFromString(String url) { try { @@ -38,6 +42,14 @@ public static void assertIsSecureUrl(String urlToCheck) { assertEquals("Protocol of URL is not secure", "https", url.getProtocol()); } + public static void assertResponseCodeOk(String url) throws IOException { + HttpURLConnection connection = (HttpURLConnection) urlFromString(url).openConnection(); + int responseCode = connection.getResponseCode(); + assertTrue("Url returned error code " + responseCode + ": " + url, responseCode < 400); + } + + + public static void assertNotEmpty(String stringToCheck) { assertNotEmpty(null, stringToCheck); } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchCountTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchCountTest.java index 35b138e424..7eb353f032 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchCountTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/search/YoutubeSearchCountTest.java @@ -30,7 +30,7 @@ public static void setUpClass() throws Exception { @Test public void testViewCount() { ChannelInfoItem ci = (ChannelInfoItem) itemsPage.getItems().get(0); - assertTrue("Count does not fit: " + Long.toString(ci.getSubscriberCount()), + assertTrue("Count does not fit: " + ci.getSubscriberCount(), 69043316 < ci.getSubscriberCount() && ci.getSubscriberCount() < 103043316); } } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorAgeRestrictedTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorAgeRestrictedTest.java index 6a24ec5f2a..e540021b20 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorAgeRestrictedTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorAgeRestrictedTest.java @@ -10,24 +10,17 @@ import org.schabi.newpipe.extractor.exceptions.ParsingException; import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor; import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeStreamLinkHandlerFactory; -import org.schabi.newpipe.extractor.stream.StreamExtractor; -import org.schabi.newpipe.extractor.stream.VideoStream; import org.schabi.newpipe.extractor.utils.Localization; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; import static org.junit.Assert.*; -import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl; import static org.schabi.newpipe.extractor.ServiceList.YouTube; /** * Test for {@link YoutubeStreamLinkHandlerFactory} */ -public class YoutubeStreamExtractorAgeRestrictedTest { - public static final String HTTPS = "https://"; - private static YoutubeStreamExtractor extractor; +public class YoutubeStreamExtractorAgeRestrictedTest extends YoutubeStreamExtractorBaseTest { @BeforeClass public static void setUp() throws Exception { @@ -37,97 +30,39 @@ public static void setUp() throws Exception { extractor.fetchPage(); } - @Test - public void testGetInvalidTimeStamp() throws ParsingException { - assertTrue(extractor.getTimeStamp() + "", extractor.getTimeStamp() <= 0); - } - - @Test - public void testGetValidTimeStamp() throws IOException, ExtractionException { - StreamExtractor extractor = YouTube.getStreamExtractor("https://youtu.be/FmG385_uUys?t=174"); - assertEquals(extractor.getTimeStamp() + "", "174"); - } - @Test public void testGetAgeLimit() throws ParsingException { assertEquals(18, extractor.getAgeLimit()); } - @Test - public void testGetName() throws ParsingException { - assertNotNull("name is null", extractor.getName()); - assertFalse("name is empty", extractor.getName().isEmpty()); - } - - @Test - public void testGetDescription() throws ParsingException { - assertNotNull(extractor.getDescription()); - assertFalse(extractor.getDescription().isEmpty()); - } - - @Test - public void testGetUploaderName() throws ParsingException { - assertNotNull(extractor.getUploaderName()); - assertFalse(extractor.getUploaderName().isEmpty()); - } - @Test public void testGetLength() throws ParsingException { assertEquals(1789, extractor.getLength()); } - @Test - public void testGetViews() throws ParsingException { - assertTrue(extractor.getViewCount() > 0); - } - - @Test - public void testGetUploadDate() throws ParsingException { - assertTrue(extractor.getUploadDate().length() > 0); - } - - @Test - public void testGetThumbnailUrl() throws ParsingException { - assertIsSecureUrl(extractor.getThumbnailUrl()); - } - - @Test - public void testGetUploaderAvatarUrl() throws ParsingException { - assertIsSecureUrl(extractor.getUploaderAvatarUrl()); - } - @Test public void testGetAudioStreams() throws IOException, ExtractionException { + super.testGetAudioStreams(); // audio streams are not always necessary assertFalse(extractor.getAudioStreams().isEmpty()); } @Test - public void testGetVideoStreams() throws IOException, ExtractionException { - List streams = new ArrayList<>(); - streams.addAll(extractor.getVideoStreams()); - streams.addAll(extractor.getVideoOnlyStreams()); - - assertTrue(Integer.toString(streams.size()),streams.size() > 0); - for (VideoStream s : streams) { - assertTrue(s.getUrl(), - s.getUrl().contains(HTTPS)); - assertTrue(s.resolution.length() > 0); - assertTrue(Integer.toString(s.getFormatId()), - 0 <= s.getFormatId() && s.getFormatId() <= 0x100); - } + @Ignore("Apparently age restricted videos have no related videos") + @Override + public void testGetRelatedVideos() throws IOException, ExtractionException { + super.testGetRelatedVideos(); } - @Test public void testGetSubtitlesListDefault() throws IOException, ExtractionException { - // Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null + // Video (/view?v=MmBeUZqv1QA) set in the setUp() method has no captions => null assertTrue(extractor.getSubtitlesDefault().isEmpty()); } @Test public void testGetSubtitlesList() throws IOException, ExtractionException { - // Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null + // Video (/view?v=MmBeUZqv1QA) set in the setUp() method has no captions => null assertTrue(extractor.getSubtitles(MediaFormat.TTML).isEmpty()); } } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorBaseTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorBaseTest.java new file mode 100644 index 0000000000..8252438703 --- /dev/null +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorBaseTest.java @@ -0,0 +1,121 @@ +package org.schabi.newpipe.extractor.services.youtube.stream; + +import org.junit.Test; +import org.schabi.newpipe.extractor.exceptions.ExtractionException; +import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor; +import org.schabi.newpipe.extractor.stream.AudioStream; +import org.schabi.newpipe.extractor.stream.StreamInfoItemsCollector; +import org.schabi.newpipe.extractor.stream.StreamType; +import org.schabi.newpipe.extractor.stream.VideoStream; +import org.schabi.newpipe.extractor.utils.Utils; + +import java.io.IOException; +import java.util.ArrayList; +import java.util.List; + +import static org.junit.Assert.*; +import static org.schabi.newpipe.extractor.ExtractorAsserts.*; + +public abstract class YoutubeStreamExtractorBaseTest { + public static YoutubeStreamExtractor extractor; + + @Test + public void testGetAgeLimit() throws ParsingException { + assertEquals(YoutubeStreamExtractor.NO_AGE_LIMIT, extractor.getAgeLimit()); + } + + @Test + public void testGetTitle() throws ParsingException { + assertFalse(extractor.getName().isEmpty()); + } + + @Test + public void testGetDescription() throws ParsingException { + assertNotNull(extractor.getDescription()); + assertFalse(extractor.getDescription().isEmpty()); + } + + @Test + public void testGetUploaderName() throws ParsingException { + assertNotNull(extractor.getUploaderName()); + assertFalse(extractor.getUploaderName().isEmpty()); + } + + @Test + public void testGetLength() throws ParsingException { + long length = extractor.getLength(); + assertTrue(String.valueOf(length), length > 0); + } + + @Test + public void testGetViewCount() throws ParsingException { + long count = extractor.getViewCount(); + assertTrue(Long.toString(count), count > 0); + } + + @Test + public void testGetUploadDate() throws ParsingException { + assertFalse(extractor.getUploadDate().isEmpty()); + } + + @Test + public void testGetUploaderUrl() throws ParsingException { + String uploaderUrl = extractor.getUploaderUrl(); + assertTrue(uploaderUrl, uploaderUrl.startsWith("https://www.youtube.com/channel/")); + } + + @Test + public void testGetThumbnailUrl() throws ParsingException { + assertIsSecureUrl(extractor.getThumbnailUrl()); + } + + @Test + public void testGetUploaderAvatarUrl() throws ParsingException { + assertIsSecureUrl(extractor.getUploaderAvatarUrl()); + } + + @Test + public void testGetAudioStreams() throws IOException, ExtractionException { + for (AudioStream s : extractor.getAudioStreams()) { + assertIsSecureUrl(s.url); + assertResponseCodeOk(s.url); + } + } + + @Test + public void testGetVideoStreams() throws IOException, ExtractionException { + List streams = new ArrayList<>(); + streams.addAll(extractor.getVideoStreams()); + streams.addAll(extractor.getVideoOnlyStreams()); + + for (VideoStream s : streams) { + assertIsSecureUrl(s.url); + assertResponseCodeOk(s.url); + assertTrue(s.resolution.length() > 0); + assertTrue(Integer.toString(s.getFormatId()), + 0 <= s.getFormatId() && s.getFormatId() <= 0x100); + } + assertFalse(streams.isEmpty()); + } + + @Test + public void testStreamType() throws ParsingException { + assertSame(extractor.getStreamType(), StreamType.VIDEO_STREAM); + } + + @Test + public void testGetRelatedVideos() throws ExtractionException, IOException { + StreamInfoItemsCollector relatedVideos = extractor.getRelatedStreams(); + Utils.printErrors(relatedVideos.getErrors()); + assertFalse(relatedVideos.getItems().isEmpty()); + assertTrue(relatedVideos.getErrors().isEmpty()); + } + + @Test + public void testGetTimestamp() throws ParsingException { + // invalid timestamp + long timestamp = extractor.getTimeStamp(); + assertTrue( timestamp + "", timestamp <= 0); + } +} diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorControversialTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorControversialTest.java index 261d521c16..2a38435b4c 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorControversialTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorControversialTest.java @@ -11,22 +11,17 @@ import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor; import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeStreamLinkHandlerFactory; import org.schabi.newpipe.extractor.stream.StreamExtractor; -import org.schabi.newpipe.extractor.stream.VideoStream; import org.schabi.newpipe.extractor.utils.Localization; import java.io.IOException; -import java.util.ArrayList; -import java.util.List; import static org.junit.Assert.*; -import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl; import static org.schabi.newpipe.extractor.ServiceList.YouTube; /** * Test for {@link YoutubeStreamLinkHandlerFactory} */ -public class YoutubeStreamExtractorControversialTest { - private static YoutubeStreamExtractor extractor; +public class YoutubeStreamExtractorControversialTest extends YoutubeStreamExtractorBaseTest { @BeforeClass public static void setUp() throws Exception { @@ -36,89 +31,33 @@ public static void setUp() throws Exception { extractor.fetchPage(); } - @Test - public void testGetInvalidTimeStamp() throws ParsingException { - assertTrue(extractor.getTimeStamp() + "", extractor.getTimeStamp() <= 0); - } - - @Test - public void testGetValidTimeStamp() throws IOException, ExtractionException { - StreamExtractor extractor = YouTube.getStreamExtractor("https://youtu.be/FmG385_uUys?t=174"); - assertEquals(extractor.getTimeStamp() + "", "174"); - } - @Test @Ignore public void testGetAgeLimit() throws ParsingException { assertEquals(18, extractor.getAgeLimit()); } - @Test - public void testGetName() throws ParsingException { - assertNotNull("name is null", extractor.getName()); - assertFalse("name is empty", extractor.getName().isEmpty()); - } - - @Test - public void testGetDescription() throws ParsingException { - assertNotNull(extractor.getDescription()); -// assertFalse(extractor.getDescription().isEmpty()); - } - - @Test - public void testGetUploaderName() throws ParsingException { - assertNotNull(extractor.getUploaderName()); - assertFalse(extractor.getUploaderName().isEmpty()); - } - @Test public void testGetLength() throws ParsingException { assertEquals(219, extractor.getLength()); } - @Test - public void testGetViews() throws ParsingException { - assertTrue(extractor.getViewCount() > 0); - } - - @Test - public void testGetUploadDate() throws ParsingException { - assertTrue(extractor.getUploadDate().length() > 0); - } - - @Test - public void testGetThumbnailUrl() throws ParsingException { - assertIsSecureUrl(extractor.getThumbnailUrl()); - } - - @Test - public void testGetUploaderAvatarUrl() throws ParsingException { - assertIsSecureUrl(extractor.getUploaderAvatarUrl()); - } - @Test public void testGetAudioStreams() throws IOException, ExtractionException { + super.testGetAudioStreams(); // audio streams are not always necessary assertFalse(extractor.getAudioStreams().isEmpty()); } - @Test - public void testGetVideoStreams() throws IOException, ExtractionException { - List streams = new ArrayList<>(); - streams.addAll(extractor.getVideoStreams()); - streams.addAll(extractor.getVideoOnlyStreams()); - assertTrue(streams.size() > 0); - } - @Test public void testGetSubtitlesListDefault() throws IOException, ExtractionException { - // Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null + // Video (/view?v=T4XJQO3qol8) set in the setUp() method has no captions => null assertFalse(extractor.getSubtitlesDefault().isEmpty()); } @Test public void testGetSubtitlesList() throws IOException, ExtractionException { - // Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null + // Video (/view?v=T4XJQO3qol8) set in the setUp() method has no captions => null assertFalse(extractor.getSubtitles(MediaFormat.TTML).isEmpty()); } } diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorDefaultTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorDefaultTest.java index a0e5050ab7..b720bc7d1e 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorDefaultTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorDefaultTest.java @@ -11,13 +11,11 @@ import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor; import org.schabi.newpipe.extractor.stream.*; import org.schabi.newpipe.extractor.utils.Localization; -import org.schabi.newpipe.extractor.utils.Utils; import java.io.IOException; import java.util.List; import static org.junit.Assert.*; -import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl; import static org.schabi.newpipe.extractor.ServiceList.YouTube; /* @@ -48,9 +46,7 @@ public class YoutubeStreamExtractorDefaultTest { /** * Test for {@link StreamExtractor} */ - public static class AdeleHello { - private static YoutubeStreamExtractor extractor; - + public static class AdeleHello extends YoutubeStreamExtractorBaseTest { @BeforeClass public static void setUp() throws Exception { NewPipe.init(Downloader.getInstance(), new Localization("GB", "en")); @@ -59,29 +55,6 @@ public static void setUp() throws Exception { extractor.fetchPage(); } - @Test - public void testGetInvalidTimeStamp() throws ParsingException { - assertTrue(extractor.getTimeStamp() + "", - extractor.getTimeStamp() <= 0); - } - - @Test - public void testGetValidTimeStamp() throws ExtractionException { - StreamExtractor extractor = YouTube.getStreamExtractor("https://youtu.be/FmG385_uUys?t=174"); - assertEquals(extractor.getTimeStamp() + "", "174"); - } - - @Test - public void testGetTitle() throws ParsingException { - assertFalse(extractor.getName().isEmpty()); - } - - @Test - public void testGetDescription() throws ParsingException { - assertNotNull(extractor.getDescription()); - assertFalse(extractor.getDescription().isEmpty()); - } - @Test public void testGetFullLinksInDescription() throws ParsingException { assertTrue(extractor.getDescription().contains("http://adele.com")); @@ -89,63 +62,18 @@ public void testGetFullLinksInDescription() throws ParsingException { } @Test - public void testGetUploaderName() throws ParsingException { - assertNotNull(extractor.getUploaderName()); - assertFalse(extractor.getUploaderName().isEmpty()); - } - - - @Test + @Override public void testGetLength() throws ParsingException { assertEquals(366, extractor.getLength()); } @Test + @Override public void testGetViewCount() throws ParsingException { - Long count = extractor.getViewCount(); + long count = extractor.getViewCount(); assertTrue(Long.toString(count), count >= /* specific to that video */ 1220025784); } - @Test - public void testGetUploadDate() throws ParsingException { - assertTrue(extractor.getUploadDate().length() > 0); - } - - @Test - public void testGetUploaderUrl() throws ParsingException { - assertEquals("https://www.youtube.com/channel/UCsRM0YB_dabtEPGPTKo-gcw", extractor.getUploaderUrl()); - } - - @Test - public void testGetThumbnailUrl() throws ParsingException { - assertIsSecureUrl(extractor.getThumbnailUrl()); - } - - @Test - public void testGetUploaderAvatarUrl() throws ParsingException { - assertIsSecureUrl(extractor.getUploaderAvatarUrl()); - } - - @Test - public void testGetAudioStreams() throws IOException, ExtractionException { - assertFalse(extractor.getAudioStreams().isEmpty()); - } - - @Test - public void testGetVideoStreams() throws IOException, ExtractionException { - for (VideoStream s : extractor.getVideoStreams()) { - assertIsSecureUrl(s.url); - assertTrue(s.resolution.length() > 0); - assertTrue(Integer.toString(s.getFormatId()), - 0 <= s.getFormatId() && s.getFormatId() <= 0x100); - } - } - - @Test - public void testStreamType() throws ParsingException { - assertTrue(extractor.getStreamType() == StreamType.VIDEO_STREAM); - } - @Test public void testGetDashMpd() throws ParsingException { // we dont expect this particular video to have a DASH file. For this purpouse we use a different test class. @@ -153,14 +81,6 @@ public void testGetDashMpd() throws ParsingException { extractor.getDashMpdUrl() != null && extractor.getDashMpdUrl().isEmpty()); } - @Test - public void testGetRelatedVideos() throws ExtractionException, IOException { - StreamInfoItemsCollector relatedVideos = extractor.getRelatedStreams(); - Utils.printErrors(relatedVideos.getErrors()); - assertFalse(relatedVideos.getItems().isEmpty()); - assertTrue(relatedVideos.getErrors().isEmpty()); - } - @Test public void testGetSubtitlesListDefault() throws IOException, ExtractionException { // Video (/view?v=YQHsXMglC9A) set in the setUp() method has no captions => null @@ -174,9 +94,7 @@ public void testGetSubtitlesList() throws IOException, ExtractionException { } } - public static class DescriptionTestPewdiepie { - private static YoutubeStreamExtractor extractor; - + public static class DescriptionTestPewdiepie extends YoutubeStreamExtractorBaseTest { @BeforeClass public static void setUp() throws Exception { NewPipe.init(Downloader.getInstance(), new Localization("GB", "en")); @@ -185,12 +103,6 @@ public static void setUp() throws Exception { extractor.fetchPage(); } - @Test - public void testGetDescription() throws ParsingException { - assertNotNull(extractor.getDescription()); - assertFalse(extractor.getDescription().isEmpty()); - } - @Test public void testGetFullLinksInDescription() throws ParsingException { assertTrue(extractor.getDescription().contains("https://www.reddit.com/r/PewdiepieSubmissions/")); @@ -203,9 +115,7 @@ public void testGetFullLinksInDescription() throws ParsingException { } } - public static class DescriptionTestUnboxing { - private static YoutubeStreamExtractor extractor; - + public static class DescriptionTestUnboxing extends YoutubeStreamExtractorBaseTest { @BeforeClass public static void setUp() throws Exception { NewPipe.init(Downloader.getInstance(), new Localization("GB", "en")); @@ -214,12 +124,6 @@ public static void setUp() throws Exception { extractor.fetchPage(); } - @Test - public void testGetDescription() throws ParsingException { - assertNotNull(extractor.getDescription()); - assertFalse(extractor.getDescription().isEmpty()); - } - @Test public void testGetFullLinksInDescription() throws ParsingException { assertTrue(extractor.getDescription().contains("https://www.youtube.com/watch?v=X7FLCHVXpsA&list=PL7u4lWXQ3wfI_7PgX0C-VTiwLeu0S4v34")); @@ -234,9 +138,7 @@ public void testGetFullLinksInDescription() throws ParsingException { } } - public static class FramesTest { - private static YoutubeStreamExtractor extractor; - + public static class FramesTest extends YoutubeStreamExtractorBaseTest { @BeforeClass public static void setUp() throws Exception { NewPipe.init(Downloader.getInstance(), new Localization("GB", "en")); diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorLivestreamTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorLivestreamTest.java index 3e24b7e36e..1886f294a3 100644 --- a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorLivestreamTest.java +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorLivestreamTest.java @@ -20,8 +20,8 @@ import static org.schabi.newpipe.extractor.ExtractorAsserts.assertIsSecureUrl; import static org.schabi.newpipe.extractor.ServiceList.YouTube; -public class YoutubeStreamExtractorLivestreamTest { - private static YoutubeStreamExtractor extractor; +public class YoutubeStreamExtractorLivestreamTest + extends YoutubeStreamExtractorBaseTest { @BeforeClass public static void setUp() throws Exception { @@ -31,36 +31,12 @@ public static void setUp() throws Exception { extractor.fetchPage(); } - @Test - public void testGetInvalidTimeStamp() throws ParsingException { - assertTrue(extractor.getTimeStamp() + "", - extractor.getTimeStamp() <= 0); - } - - @Test - public void testGetTitle() throws ParsingException { - assertFalse(extractor.getName().isEmpty()); - } - - @Test - public void testGetDescription() throws ParsingException { - assertNotNull(extractor.getDescription()); - assertFalse(extractor.getDescription().isEmpty()); - } - @Test public void testGetFullLinksInDescription() throws ParsingException { assertTrue(extractor.getDescription().contains("https://www.instagram.com/nathalie.baraton/")); assertFalse(extractor.getDescription().contains("https://www.instagram.com/nathalie.ba...")); } - @Test - public void testGetUploaderName() throws ParsingException { - assertNotNull(extractor.getUploaderName()); - assertFalse(extractor.getUploaderName().isEmpty()); - } - - @Test public void testGetLength() throws ParsingException { assertEquals(0, extractor.getLength()); @@ -72,41 +48,11 @@ public void testGetViewCount() throws ParsingException { assertTrue(Long.toString(count), count >= 7148995); } - @Test - public void testGetUploadDate() throws ParsingException { - assertTrue(extractor.getUploadDate().length() > 0); - } - @Test public void testGetUploaderUrl() throws ParsingException { assertEquals("https://www.youtube.com/channel/UCSJ4gkVC6NrvII8umztf0Ow", extractor.getUploaderUrl()); } - @Test - public void testGetThumbnailUrl() throws ParsingException { - assertIsSecureUrl(extractor.getThumbnailUrl()); - } - - @Test - public void testGetUploaderAvatarUrl() throws ParsingException { - assertIsSecureUrl(extractor.getUploaderAvatarUrl()); - } - - @Test - public void testGetAudioStreams() throws ExtractionException { - assertFalse(extractor.getAudioStreams().isEmpty()); - } - - @Test - public void testGetVideoStreams() throws ExtractionException { - for (VideoStream s : extractor.getVideoStreams()) { - assertIsSecureUrl(s.url); - assertTrue(s.resolution.length() > 0); - assertTrue(Integer.toString(s.getFormatId()), - 0 <= s.getFormatId() && s.getFormatId() <= 0x100); - } - } - @Test public void testStreamType() throws ParsingException { assertSame(extractor.getStreamType(), StreamType.LIVE_STREAM); @@ -118,14 +64,6 @@ public void testGetDashMpd() throws ParsingException { assertTrue(extractor.getDashMpdUrl(), extractor.getDashMpdUrl().isEmpty()); } - @Test - public void testGetRelatedVideos() throws ExtractionException, IOException { - StreamInfoItemsCollector relatedVideos = extractor.getRelatedStreams(); - Utils.printErrors(relatedVideos.getErrors()); - assertFalse(relatedVideos.getItems().isEmpty()); - assertTrue(relatedVideos.getErrors().isEmpty()); - } - @Test public void testGetSubtitlesListDefault() throws IOException, ExtractionException { assertTrue(extractor.getSubtitlesDefault().isEmpty()); diff --git a/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorTimestampTest.java b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorTimestampTest.java new file mode 100644 index 0000000000..464cfc0691 --- /dev/null +++ b/extractor/src/test/java/org/schabi/newpipe/extractor/services/youtube/stream/YoutubeStreamExtractorTimestampTest.java @@ -0,0 +1,29 @@ +package org.schabi.newpipe.extractor.services.youtube.stream; + +import org.junit.BeforeClass; +import org.junit.Test; +import org.schabi.newpipe.Downloader; +import org.schabi.newpipe.extractor.NewPipe; +import org.schabi.newpipe.extractor.exceptions.ParsingException; +import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeStreamExtractor; +import org.schabi.newpipe.extractor.utils.Localization; + +import static org.junit.Assert.*; +import static org.schabi.newpipe.extractor.ServiceList.YouTube; + +public class YoutubeStreamExtractorTimestampTest extends YoutubeStreamExtractorBaseTest { + + @BeforeClass + public static void setUp() throws Exception { + NewPipe.init(Downloader.getInstance(), new Localization("GB", "en")); + extractor = (YoutubeStreamExtractor) YouTube + .getStreamExtractor("https://youtu.be/FmG385_uUys?t=174"); + extractor.fetchPage(); + } + + @Override + @Test + public void testGetTimestamp() throws ParsingException { + assertEquals(extractor.getTimeStamp(), 174); + } +}