diff --git a/app-test/src/test/java/org/bspeice/minimalbible/test/activity/downloader/manager/BookManagerTest.java b/app-test/src/test/java/org/bspeice/minimalbible/test/activity/downloader/manager/BookManagerTest.java index 7e73081..16b27a6 100644 --- a/app-test/src/test/java/org/bspeice/minimalbible/test/activity/downloader/manager/BookManagerTest.java +++ b/app-test/src/test/java/org/bspeice/minimalbible/test/activity/downloader/manager/BookManagerTest.java @@ -10,6 +10,7 @@ import org.bspeice.minimalbible.activity.downloader.manager.BookManager; import org.bspeice.minimalbible.activity.downloader.manager.DLProgressEvent; import org.bspeice.minimalbible.activity.downloader.manager.RefreshManager; import org.crosswire.common.progress.JobManager; +import org.crosswire.common.progress.Progress; import org.crosswire.common.progress.WorkEvent; import org.crosswire.common.progress.WorkListener; import org.crosswire.jsword.book.Book; @@ -158,6 +159,45 @@ public class BookManagerTest implements Injector { verify(driver, times(1)).delete(secondMockBook); } + /** + * Make sure that when workProgressed is fired, the correct progress event gets triggered + * Previously, integer roundoff errors led to some strange results + */ + @Test + public void testWorkProgressedCorrectProgress() { + Book mockBook = mock(Book.class); + when(mockBook.getInitials()).thenReturn("mockBook"); + String bookJobName = bookManager.getJobId(mockBook); + bookManager.getBookMappings().put(bookJobName, mockBook); + + // Percent to degrees + int workProgress = 1; + int totalWork = 2; + final int circularProgress = 180; + WorkEvent ev = mock(WorkEvent.class); + Progress p = mock(Progress.class); + + when(p.getJobID()).thenReturn(bookJobName); + when(p.getWorkDone()).thenReturn(workProgress); + when(p.getTotalWork()).thenReturn(totalWork); + + when(ev.getJob()).thenReturn(p); + + final AtomicBoolean progressCorrect = new AtomicBoolean(false); + bookManager.getDownloadEvents() + .subscribe(new Action1() { + @Override + public void call(DLProgressEvent dlProgressEvent) { + progressCorrect.set(circularProgress == dlProgressEvent.toCircular()); + } + }); + + bookManager.workProgressed(ev); + + await().atMost(10, TimeUnit.SECONDS) + .untilTrue(progressCorrect); + } + /** * Modules needed for this test case */