Add test for search paging

This commit is contained in:
Zsombor Gegesy 2019-12-04 23:57:44 +01:00
parent 2123b3abd3
commit 68b0fd9650
1 changed files with 78 additions and 0 deletions

View File

@ -0,0 +1,78 @@
package org.schabi.newpipe.extractor.services.youtube.search;
import static java.util.Collections.singletonList;
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertNotNull;
import static org.junit.Assert.assertTrue;
import static org.schabi.newpipe.extractor.ServiceList.YouTube;
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import org.junit.BeforeClass;
import org.junit.Test;
import org.schabi.newpipe.DownloaderTestImpl;
import org.schabi.newpipe.extractor.InfoItem;
import org.schabi.newpipe.extractor.ListExtractor;
import org.schabi.newpipe.extractor.NewPipe;
import org.schabi.newpipe.extractor.services.youtube.extractors.YoutubeSearchExtractor;
import org.schabi.newpipe.extractor.services.youtube.linkHandler.YoutubeSearchQueryHandlerFactory;
public class YoutubeSearchPagingTest {
private static ListExtractor.InfoItemsPage<InfoItem> page1;
private static ListExtractor.InfoItemsPage<InfoItem> page2;
private static Set<String> urlList1;
private static Set<String> urlList2;
@BeforeClass
public static void setUpClass() throws Exception {
NewPipe.init(DownloaderTestImpl.getInstance());
YoutubeSearchExtractor extractor = (YoutubeSearchExtractor) YouTube.getSearchExtractor("cirque du soleil",
singletonList(YoutubeSearchQueryHandlerFactory.VIDEOS), null);
extractor.fetchPage();
page1 = extractor.getInitialPage();
urlList1 = extractUrls(page1.getItems());
assertEquals("page with items loaded", 20, page1.getItems().size());
assertEquals("distinct videos", 20, urlList1.size());
assertTrue("has more page", page1.hasNextPage());
assertNotNull("has next page url", page1.getNextPageUrl());
page2 = extractor.getPage(page1.getNextPageUrl());
urlList2 = extractUrls(page2.getItems());
}
private static Set<String> extractUrls(List<InfoItem> list) {
Set<String> result = new HashSet<>();
for (InfoItem item : list) {
result.add(item.getUrl());
}
return result;
}
@Test
public void firstPageOk() {
assertEquals("page with items loaded", 20, page1.getItems().size());
assertEquals("distinct videos", 20, urlList1.size());
}
@Test
public void secondPageLength() {
assertEquals("one page", 20, page2.getItems().size());
}
@Test
public void secondPageUniqueVideos() {
assertEquals("distinct videos", 20, urlList2.size());
}
@Test
public void noRepeatingVideosInPages() {
Set<String> intersection = new HashSet<>(urlList2);
intersection.retainAll(urlList1);
assertEquals("empty intersection", 0, intersection.size());
}
}