From c5ab05574fd12f206f64c7bdc8a983c522dd4027 Mon Sep 17 00:00:00 2001 From: Roger Yang Date: Wed, 22 Apr 2020 14:20:45 -0400 Subject: [PATCH] Close #6421: Download try again should not crash when download does not exist --- .../downloads/manager/FetchDownloadManager.kt | 2 +- .../manager/FetchDownloadManagerTest.kt | 23 +++++++++++++++++++ 2 files changed, 24 insertions(+), 1 deletion(-) diff --git a/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/manager/FetchDownloadManager.kt b/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/manager/FetchDownloadManager.kt index eb6184cbccb..3d5f8a2132b 100644 --- a/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/manager/FetchDownloadManager.kt +++ b/components/feature/downloads/src/main/java/mozilla/components/feature/downloads/manager/FetchDownloadManager.kt @@ -76,7 +76,7 @@ class FetchDownloadManager( } override fun tryAgain(downloadId: Long) { - val download = queuedDownloads[downloadId] + val download = queuedDownloads[downloadId] ?: return val intent = Intent(applicationContext, service.java) intent.putDownloadExtra(download) diff --git a/components/feature/downloads/src/test/java/mozilla/components/feature/downloads/manager/FetchDownloadManagerTest.kt b/components/feature/downloads/src/test/java/mozilla/components/feature/downloads/manager/FetchDownloadManagerTest.kt index fedb2b2115b..567b98bbb65 100644 --- a/components/feature/downloads/src/test/java/mozilla/components/feature/downloads/manager/FetchDownloadManagerTest.kt +++ b/components/feature/downloads/src/test/java/mozilla/components/feature/downloads/manager/FetchDownloadManagerTest.kt @@ -30,6 +30,7 @@ import org.junit.Test import org.junit.runner.RunWith import org.mockito.Mockito.verify import mozilla.components.feature.downloads.AbstractFetchDownloadService.DownloadJobStatus +import org.junit.Assert.assertFalse import org.mockito.Mockito.times @RunWith(AndroidJUnit4::class) @@ -101,6 +102,28 @@ class FetchDownloadManagerTest { assertTrue(downloadCompleted) } + @Test + fun `try again should not crash when download does not exist`() { + val context: Context = mock() + downloadManager = FetchDownloadManager(context, MockDownloadService::class, broadcastManager) + var downloadCompleted = false + + downloadManager.onDownloadStopped = { _, _, _ -> downloadCompleted = true } + + grantPermissions() + + val id = downloadManager.download(download)!! + + verify(context).startService(any()) + notifyDownloadCompleted(id) + assertTrue(downloadCompleted) + + downloadCompleted = false + downloadManager.tryAgain(id + 1) + assertFalse(downloadCompleted) + verify(context, times(1)).startService(any()) + } + @Test fun `trying to download a file with invalid protocol must NOT triggered a download`() {