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 4a9f546058e..dfb62b8593f 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -45,6 +45,9 @@ permalink: /changelog/ * **browser-icons** * Expose `BrowserIcons.clear()` as a public API to remove all saved data from disk and memory caches. +* **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)