BookInfoPresenter extends BasePresenter<BookInfoContract.View> implements BookInfoContract.Presenter { @Override public void downloadBook(final FireBookDetails bookInfo) { if (bookInfo == null || bookInfo.getBookUrl() == null) { analytics.trackDownloadBookFailed(bookInfo, "book_not_available"); getView().showSnackBarMessage(R.string.book_not_available); return; } if (bookInfo.isDownloading()) { getView().showSnackBarMessage(R.string.book_is_downloading); return; } bookInfo.setIsDownloading(true); analytics.trackDownloadBookStarted(bookInfo); addSubscription(downloadService.downloadFile(bookInfo).subscribeOn(ioScheduler).observeOn(mainScheduler) .subscribe(new Subscriber<DownloadProgressItem>() { @Override public void onCompleted() { } @Override public void onError(Throwable e) { bookInfo.setIsDownloading(false); String error = null; if (e != null) { getView().showSnackBarMessage(R.string.failed_to_download_book, e.getMessage()); error = e.getMessage(); } analytics.trackDownloadBookFailed(bookInfo, error); } @Override public void onNext(DownloadProgressItem downloadProgressItem) { getView().showDownloadProgress(downloadProgressItem.getDownloadProgress()); if (downloadProgressItem.isComplete()) { analytics.trackViewBook(bookInfo); BookPages bookPages = downloadProgressItem.getBookPages(); if (bookPages == null) { getView().showSnackBarMessage(R.string.failed_to_open_book); analytics.trackDownloadBookFailed(bookInfo, "failed_to_open_book"); return; } getView().showDownloadFinished(); bookInfo.setIsDownloading(false); getView().openBook(bookInfo, bookPages, bookInfo.getFolderLocation()); } } })); } BookInfoPresenter(@NonNull BookService bookService, @NonNull DownloadService downloadService,
                      @NonNull Analytics analytics, Scheduler ioScheduler, Scheduler mainScheduler); @Override void loadContributorInformation(FireBookDetails bookDetailId); @Override void downloadBook(final FireBookDetails bookInfo); @Override void shareBookClicked(FireBookDetails bookInfo);  }
@Test public void downloadBook_SendsProgressUpdates_UpdatesView() { BOOK_DETAIL.setBookUrl("http: BOOK_DETAIL.setIsDownloading(false); when(downloadService.downloadFile(BOOK_DETAIL)).thenReturn(observableWithProgress(10)); bookInfoPresenter.downloadBook(BOOK_DETAIL); verify(bookInfoView).showDownloadProgress(10); }
@Test public void downloadBook_Completed_UpdatesView() { BOOK_DETAIL.setBookUrl("http: BOOK_DETAIL.setIsDownloading(false); when(downloadService.downloadFile(BOOK_DETAIL)).thenReturn(completeDownload()); bookInfoPresenter.downloadBook(BOOK_DETAIL); verify(bookInfoView).showDownloadProgress(100); verify(bookInfoView).showDownloadFinished(); verify(bookInfoView).openBook(any(FireBookDetails.class), any(BookPages.class), anyString()); verify(analytics).trackViewBook(BOOK_DETAIL); }
@Test public void downloadBook_CompletedBookPagesNull_ShowsErrorMsg() { BOOK_DETAIL.setBookUrl("http: BOOK_DETAIL.setIsDownloading(false); when(downloadService.downloadFile(BOOK_DETAIL)).thenReturn(Observable.just(new DownloadProgressItem(100, 100))); bookInfoPresenter.downloadBook(BOOK_DETAIL); verify(bookInfoView).showDownloadProgress(100); verify(bookInfoView).showSnackBarMessage(R.string.failed_to_open_book); verify(bookInfoView, never()).showDownloadFinished(); verify(bookInfoView, never()).openBook(any(FireBookDetails.class), any(BookPages.class), anyString()); verify(analytics).trackDownloadBookFailed(any(FireBookDetails.class), anyString()); }
@Test public void downloadBook_OnError_ShowsErrorMessage() { String errorMsg = "Failed to download"; BOOK_DETAIL.setBookUrl("http: BOOK_DETAIL.setIsDownloading(false); when(downloadService.downloadFile(BOOK_DETAIL)) .thenReturn(Observable.<DownloadProgressItem>error(new Exception(errorMsg))); bookInfoPresenter.downloadBook(BOOK_DETAIL); verify(bookInfoView, never()).showDownloadProgress(100); verify(bookInfoView, never()).showDownloadFinished(); verify(bookInfoView, never()).openBook(any(FireBookDetails.class), any(BookPages.class), anyString()); verify(bookInfoView).showSnackBarMessage(R.string.failed_to_download_book, errorMsg); verify(analytics).trackDownloadBookFailed(BOOK_DETAIL, errorMsg); }
@Test public void downloadBook_BookNotAvailable_ShowSnackBarMessage() { bookInfoPresenter.downloadBook(null); verify(bookInfoView).showSnackBarMessage(R.string.book_not_available); }
@Test public void downloadBook_BookUrlNull_ShowSnackBarMessage() { bookInfoPresenter.downloadBook(BOOK_DETAIL); verify(bookInfoView).showSnackBarMessage(R.string.book_not_available); verify(analytics).trackDownloadBookFailed(any(FireBookDetails.class), anyString()); }
@Test public void downloadbook_BookAlreadyDownloading_ShowSnackbarErrorMessage() { BOOK_DETAIL.setBookUrl("http: BOOK_DETAIL.setIsDownloading(true); bookInfoPresenter.downloadBook(BOOK_DETAIL); verify(bookInfoView).showSnackBarMessage(R.string.book_is_downloading); }