Răsfoiți Sursa

Separate method pullImageUrlsFromNetwork to allow testing

inorichi 9 ani în urmă
părinte
comite
e7ecfd1e84

+ 13 - 15
app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java

@@ -62,21 +62,19 @@ public class ChapterManagerImpl extends BaseManager implements ChapterManager {
             subscriber.onCompleted();
         });
 
-        Observable<Integer> newChaptersObs =
-                chapterList
-                    .flatMap(dbChapters -> Observable.from(chapters)
-                            .filter(c -> !dbChapters.contains(c))
-                            .toList()
-                            .flatMap(this::insertChapters)
-                            .map(PutResults::numberOfInserts));
-
-        Observable<Integer> deletedChaptersObs =
-                chapterList
-                    .flatMap(dbChapters -> Observable.from(dbChapters)
-                            .filter(c -> !chapters.contains(c))
-                            .toList()
-                            .flatMap(this::deleteChapters)
-                            .map( d -> d.results().size() ));
+        Observable<Integer> newChaptersObs = chapterList
+                .flatMap(dbChapters -> Observable.from(chapters)
+                        .filter(c -> !dbChapters.contains(c))
+                        .toList()
+                        .flatMap(this::insertChapters)
+                        .map(PutResults::numberOfInserts));
+
+        Observable<Integer> deletedChaptersObs = chapterList
+                .flatMap(dbChapters -> Observable.from(dbChapters)
+                        .filter(c -> !chapters.contains(c))
+                        .toList()
+                        .flatMap(this::deleteChapters)
+                        .map( d -> d.results().size() ));
 
         return Observable.zip(newChaptersObs, deletedChaptersObs,
                 (insertions, deletions) -> new PostResult(0, insertions, deletions)

+ 23 - 28
app/src/main/java/eu/kanade/mangafeed/sources/Batoto.java

@@ -114,20 +114,12 @@ public class Batoto extends Source {
         String url = getUrlFromPageNumber(page);
         return mNetworkService
                 .getStringResponse(url, mNetworkService.NULL_CACHE_CONTROL, REQUEST_HEADERS)
-                .flatMap(response -> Observable.just(parseHtmlToLatestUpdates(response)));
+                .flatMap(response -> Observable.just(parsePopularMangasFromHtml(response)));
     }
 
-    private List<Manga> parseHtmlToLatestUpdates(String unparsedHtml) {
+    private List<Manga> parsePopularMangasFromHtml(String unparsedHtml) {
         Document parsedDocument = Jsoup.parse(unparsedHtml);
 
-        List<Manga> updatedMangaList = scrapeUpdateMangasFromParsedDocument(parsedDocument);
-        //updateLibraryInDatabase(updatedMangaList);
-
-        return updatedMangaList;
-    }
-
-
-    private List<Manga> scrapeUpdateMangasFromParsedDocument(Document parsedDocument) {
         List<Manga> updatedMangaList = new ArrayList<>();
 
         Elements updatedHtmlBlocks = parsedDocument.select("tr:not([id]):not([class])");
@@ -388,30 +380,33 @@ public class Batoto extends Source {
 
         return mCacheManager.getImageUrlsFromDiskCache(chapterUrl)
                 .onErrorResumeNext(throwable -> {
-                    return mNetworkService
-                            .getStringResponse(chapterUrl, mNetworkService.NULL_CACHE_CONTROL, REQUEST_HEADERS)
-                            .subscribeOn(Schedulers.io())
-                            .flatMap(unparsedHtml -> Observable.from(parseHtmlToPageUrls(unparsedHtml)))
-                            .buffer(3)
-                            .concatMap(batchedPageUrls -> {
-                                List<Observable<String>> imageUrlObservables = new ArrayList<>();
-                                for (String pageUrl : batchedPageUrls) {
-                                    Observable<String> temporaryObservable = mNetworkService
-                                            .getStringResponse(pageUrl, mNetworkService.NULL_CACHE_CONTROL, REQUEST_HEADERS)
-                                            .flatMap(unparsedHtml -> Observable.just(parseHtmlToImageUrl(unparsedHtml)))
-                                            .subscribeOn(Schedulers.io());
-
-                                    imageUrlObservables.add(temporaryObservable);
-                                }
-
-                                return Observable.merge(imageUrlObservables);
-                            })
+                    return getImageUrlsFromNetwork(chapterUrl)
                             .doOnNext(imageUrl -> temporaryCachedImageUrls.add(imageUrl))
                             .doOnCompleted(mCacheManager.putImageUrlsToDiskCache(chapterUrl, temporaryCachedImageUrls));
                 })
                 .onBackpressureBuffer();
     }
 
+    public Observable<String> getImageUrlsFromNetwork(final String chapterUrl) {
+        return mNetworkService
+                .getStringResponse(chapterUrl, mNetworkService.NULL_CACHE_CONTROL, REQUEST_HEADERS)
+                .flatMap(unparsedHtml -> Observable.from(parseHtmlToPageUrls(unparsedHtml)))
+                .buffer(3)
+                .concatMap(batchedPageUrls -> {
+                    List<Observable<String>> imageUrlObservables = new ArrayList<>();
+                    for (String pageUrl : batchedPageUrls) {
+                        Observable<String> temporaryObservable = mNetworkService
+                                .getStringResponse(pageUrl, mNetworkService.NULL_CACHE_CONTROL, REQUEST_HEADERS)
+                                .flatMap(unparsedHtml -> Observable.just(parseHtmlToImageUrl(unparsedHtml)))
+                                .subscribeOn(Schedulers.io());
+
+                        imageUrlObservables.add(temporaryObservable);
+                    }
+
+                    return Observable.merge(imageUrlObservables);
+                });
+    }
+
     private List<String> parseHtmlToPageUrls(String unparsedHtml) {
         Document parsedDocument = Jsoup.parse(unparsedHtml);
 

+ 18 - 3
app/src/test/java/eu/kanade/mangafeed/BatotoTest.java

@@ -2,6 +2,8 @@ package eu.kanade.mangafeed;
 
 import android.os.Build;
 
+import junit.framework.Assert;
+
 import org.junit.Before;
 import org.junit.Test;
 import org.junit.runner.RunWith;
@@ -9,10 +11,15 @@ import org.robolectric.RobolectricGradleTestRunner;
 import org.robolectric.RuntimeEnvironment;
 import org.robolectric.annotation.Config;
 
+import java.util.List;
+
 import eu.kanade.mangafeed.data.caches.CacheManager;
 import eu.kanade.mangafeed.data.helpers.NetworkHelper;
+import eu.kanade.mangafeed.data.models.Manga;
 import eu.kanade.mangafeed.sources.Batoto;
+import rx.android.schedulers.AndroidSchedulers;
 import rx.observers.TestSubscriber;
+import rx.schedulers.Schedulers;
 
 @Config(constants = BuildConfig.class, sdk = Build.VERSION_CODES.LOLLIPOP)
 @RunWith(RobolectricGradleTestRunner.class)
@@ -32,9 +39,17 @@ public class BatotoTest {
 
     @Test
     public void testImageList() {
-        TestSubscriber a = new TestSubscriber();
+        List<String> imageUrls = b.getImageUrlsFromNetwork(chapterUrl)
+                .toList().toBlocking().single();
+
+        Assert.assertTrue(imageUrls.size() > 5);
+    }
+
+    @Test
+    public void testMangaList() {
+        List<Manga> mangaList = b.pullPopularMangasFromNetwork(1)
+                .toBlocking().first();
 
-        b.pullImageUrlsFromNetwork(chapterUrl).subscribe(a);
-        a.assertNoErrors();
+        Assert.assertTrue(mangaList.size() > 25);
     }
 }