22
33import org .schabi .newpipe .extractor .InfoItem ;
44import 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 ;
511import org .schabi .newpipe .extractor .localization .DateWrapper ;
12+ import org .schabi .newpipe .extractor .playlist .PlaylistInfoItem ;
613import org .schabi .newpipe .extractor .stream .StreamInfoItem ;
714
815import java .util .Calendar ;
916import java .util .List ;
1017
18+ import static junit .framework .TestCase .assertFalse ;
1119import static org .junit .Assert .*;
1220import static org .schabi .newpipe .extractor .ExtractorAsserts .*;
21+ import static org .schabi .newpipe .extractor .StreamingService .*;
1322
1423public 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}
0 commit comments