From e7ecfd1e844abf9e6c11231c149e9c99134ce2dd Mon Sep 17 00:00:00 2001 From: inorichi Date: Fri, 9 Oct 2015 13:38:28 +0200 Subject: [PATCH] Separate method pullImageUrlsFromNetwork to allow testing --- .../data/managers/ChapterManagerImpl.java | 26 +++++----- .../eu/kanade/mangafeed/sources/Batoto.java | 51 +++++++++---------- .../java/eu/kanade/mangafeed/BatotoTest.java | 21 ++++++-- 3 files changed, 53 insertions(+), 45 deletions(-) diff --git a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java b/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java index b37374d245..1d8da1e9fe 100644 --- a/app/src/main/java/eu/kanade/mangafeed/data/managers/ChapterManagerImpl.java +++ b/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 newChaptersObs = - chapterList - .flatMap(dbChapters -> Observable.from(chapters) - .filter(c -> !dbChapters.contains(c)) - .toList() - .flatMap(this::insertChapters) - .map(PutResults::numberOfInserts)); + Observable newChaptersObs = chapterList + .flatMap(dbChapters -> Observable.from(chapters) + .filter(c -> !dbChapters.contains(c)) + .toList() + .flatMap(this::insertChapters) + .map(PutResults::numberOfInserts)); - Observable deletedChaptersObs = - chapterList - .flatMap(dbChapters -> Observable.from(dbChapters) - .filter(c -> !chapters.contains(c)) - .toList() - .flatMap(this::deleteChapters) - .map( d -> d.results().size() )); + Observable 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) diff --git a/app/src/main/java/eu/kanade/mangafeed/sources/Batoto.java b/app/src/main/java/eu/kanade/mangafeed/sources/Batoto.java index 8a3929e0e3..a580170ad9 100644 --- a/app/src/main/java/eu/kanade/mangafeed/sources/Batoto.java +++ b/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 parseHtmlToLatestUpdates(String unparsedHtml) { + private List parsePopularMangasFromHtml(String unparsedHtml) { Document parsedDocument = Jsoup.parse(unparsedHtml); - List updatedMangaList = scrapeUpdateMangasFromParsedDocument(parsedDocument); - //updateLibraryInDatabase(updatedMangaList); - - return updatedMangaList; - } - - - private List scrapeUpdateMangasFromParsedDocument(Document parsedDocument) { List 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> imageUrlObservables = new ArrayList<>(); - for (String pageUrl : batchedPageUrls) { - Observable 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 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> imageUrlObservables = new ArrayList<>(); + for (String pageUrl : batchedPageUrls) { + Observable 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 parseHtmlToPageUrls(String unparsedHtml) { Document parsedDocument = Jsoup.parse(unparsedHtml); diff --git a/app/src/test/java/eu/kanade/mangafeed/BatotoTest.java b/app/src/test/java/eu/kanade/mangafeed/BatotoTest.java index 1511717244..cd9198e711 100644 --- a/app/src/test/java/eu/kanade/mangafeed/BatotoTest.java +++ b/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 imageUrls = b.getImageUrlsFromNetwork(chapterUrl) + .toList().toBlocking().single(); - b.pullImageUrlsFromNetwork(chapterUrl).subscribe(a); - a.assertNoErrors(); + Assert.assertTrue(imageUrls.size() > 5); + } + + @Test + public void testMangaList() { + List mangaList = b.pullPopularMangasFromNetwork(1) + .toBlocking().first(); + + Assert.assertTrue(mangaList.size() > 25); } }