From 3021df8078f6dba9ace0d07d3276315cce14b34d Mon Sep 17 00:00:00 2001 From: Arturo Mejia Date: Mon, 5 Feb 2024 15:03:58 -0500 Subject: [PATCH] Bug 1876806 - Do not remove downloaded files in private tabs after deleting browsing data --- .../downloads/AbstractFetchDownloadService.kt | 5 +++- .../AbstractFetchDownloadServiceTest.kt | 24 +++++++++++++++++++ 2 files changed, 28 insertions(+), 1 deletion(-) diff --git a/android-components/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/AbstractFetchDownloadService.kt b/android-components/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/AbstractFetchDownloadService.kt index 03476445ece2..fb8411352aec 100644 --- a/android-components/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/AbstractFetchDownloadService.kt +++ b/android-components/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/AbstractFetchDownloadService.kt @@ -271,7 +271,10 @@ abstract class AbstractFetchDownloadService : Service() { internal fun handleRemovePrivateDownloadIntent(download: DownloadState) { if (download.private) { downloadJobs[download.id]?.let { - cancelDownloadJob(it) + // Do not cancel already completed downloads. + if (it.status != COMPLETED) { + cancelDownloadJob(it) + } removeDownloadJob(it) } store.dispatch(DownloadAction.RemoveDownloadAction(download.id)) diff --git a/android-components/components/feature/downloads/src/test/java/mozilla/components/feature/downloads/AbstractFetchDownloadServiceTest.kt b/android-components/components/feature/downloads/src/test/java/mozilla/components/feature/downloads/AbstractFetchDownloadServiceTest.kt index a601e863cf7a..504354e185fc 100644 --- a/android-components/components/feature/downloads/src/test/java/mozilla/components/feature/downloads/AbstractFetchDownloadServiceTest.kt +++ b/android-components/components/feature/downloads/src/test/java/mozilla/components/feature/downloads/AbstractFetchDownloadServiceTest.kt @@ -271,6 +271,30 @@ class AbstractFetchDownloadServiceTest { service.handleRemovePrivateDownloadIntent(downloadState) + verify(service, times(0)).cancelDownloadJob(downloadJobState) + verify(service).removeDownloadJob(downloadJobState) + verify(browserStore).dispatch(DownloadAction.RemoveDownloadAction(downloadState.id)) + } + + @Test + fun `WHEN handleRemovePrivateDownloadIntent is called with a private download AND not COMPLETED status THEN removeDownloadJob and cancelDownloadJob must be called`() { + val downloadState = DownloadState(url = "mozilla.org/mozilla.txt", private = true) + val downloadJobState = DownloadJobState(state = downloadState, status = DOWNLOADING) + val browserStore = mock() + val service = spy( + object : AbstractFetchDownloadService() { + override val httpClient = client + override val store = browserStore + override val notificationsDelegate = this@AbstractFetchDownloadServiceTest.notificationsDelegate + }, + ) + + doAnswer { Unit }.`when`(service).removeDownloadJob(any()) + + service.downloadJobs[downloadState.id] = downloadJobState + + service.handleRemovePrivateDownloadIntent(downloadState) + verify(service).cancelDownloadJob(downloadJobState) verify(service).removeDownloadJob(downloadJobState) verify(browserStore).dispatch(DownloadAction.RemoveDownloadAction(downloadState.id))