From fb3585bfee2a5fb0cba11597770e1a65de946dc2 Mon Sep 17 00:00:00 2001 From: Arturo Mejia Date: Mon, 24 Aug 2020 09:49:33 -0400 Subject: [PATCH] Closes issue #8202: Increase downloadJobState.currentBytesCopied --- .../downloads/AbstractFetchDownloadService.kt | 6 ++++-- .../downloads/AbstractFetchDownloadServiceTest.kt | 15 +++++++++++++++ docs/changelog.md | 3 +++ 3 files changed, 22 insertions(+), 2 deletions(-) diff --git a/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/AbstractFetchDownloadService.kt b/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/AbstractFetchDownloadService.kt index f67dd4a789b..62d4bbc0f99 100644 --- a/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/AbstractFetchDownloadService.kt +++ b/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/AbstractFetchDownloadService.kt @@ -621,7 +621,8 @@ abstract class AbstractFetchDownloadService : Service() { } } - private fun copyInChunks(downloadJobState: DownloadJobState, inStream: InputStream, outStream: OutputStream) { + @VisibleForTesting + internal fun copyInChunks(downloadJobState: DownloadJobState, inStream: InputStream, outStream: OutputStream) { val data = ByteArray(CHUNK_SIZE) logger.debug("starting copyInChunks ${downloadJobState.state.url}" + " currentBytesCopied ${downloadJobState.currentBytesCopied}") @@ -632,9 +633,10 @@ abstract class AbstractFetchDownloadService : Service() { // If bytesRead is -1, there's no data left to read from the stream if (bytesRead == -1) { break } + downloadJobState.currentBytesCopied += bytesRead val newState = downloadJobState.state.copy( - currentBytesCopied = downloadJobState.currentBytesCopied + bytesRead + currentBytesCopied = downloadJobState.currentBytesCopied ) updateDownloadState(newState) diff --git a/components/feature/downloads/src/test/java/mozilla/components/feature/downloads/AbstractFetchDownloadServiceTest.kt b/components/feature/downloads/src/test/java/mozilla/components/feature/downloads/AbstractFetchDownloadServiceTest.kt index d487c664510..4ea719be6c4 100644 --- a/components/feature/downloads/src/test/java/mozilla/components/feature/downloads/AbstractFetchDownloadServiceTest.kt +++ b/components/feature/downloads/src/test/java/mozilla/components/feature/downloads/AbstractFetchDownloadServiceTest.kt @@ -1375,4 +1375,19 @@ class AbstractFetchDownloadServiceTest { assertTrue(downloadJobState.canUpdateNotification()) } + + @Test + fun `copyInChunks must alter download currentBytesCopied`() = runBlocking { + val downloadJobState = DownloadJobState(state = mock(), status = DOWNLOADING) + val inputStream = mock() + + assertEquals(0, downloadJobState.currentBytesCopied) + + doReturn(15, -1).`when`(inputStream).read(any()) + doNothing().`when`(service).updateDownloadState(any()) + + service.copyInChunks(downloadJobState, inputStream, mock()) + + assertEquals(15, downloadJobState.currentBytesCopied) + } } diff --git a/docs/changelog.md b/docs/changelog.md index 18b1fe7fa1c..99a65cb4428 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -21,6 +21,9 @@ permalink: /changelog/ * **feature-app-links** * Fixed [issue #8169](https://github.com/mozilla-mobile/android-components/issues/8169) App links dialog will now call `showNow` to immediately show the dialog. +* **feature-downloads** + * 🚒 Fix [issue #8202](https://github.com/mozilla-mobile/android-components/issues/8202) Download's ui were always showing failed status. + # 55.0.0 * [Commits](https://github.com/mozilla-mobile/android-components/compare/v54.0.0...v55.0.0)