diff --git a/app/src/main/java/org/mozilla/fenix/ext/List.kt b/app/src/main/java/org/mozilla/fenix/ext/List.kt new file mode 100644 index 000000000..95510f66f --- /dev/null +++ b/app/src/main/java/org/mozilla/fenix/ext/List.kt @@ -0,0 +1,20 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.fenix.ext + +import org.mozilla.fenix.library.downloads.DownloadItem +import java.io.File + +/** + * Checks a List of DownloadItems to verify whether items + * on that list are present on the disk or not. If a user has + * deleted the downloaded item it should not show on the downloaded + * list. + */ +fun List.filterNotExistsOnDisk(): List { + return this.filter { + File(it.filePath).exists() + } +} diff --git a/app/src/main/java/org/mozilla/fenix/library/downloads/DownloadFragment.kt b/app/src/main/java/org/mozilla/fenix/library/downloads/DownloadFragment.kt index 82ed98e0a..52af61d85 100644 --- a/app/src/main/java/org/mozilla/fenix/library/downloads/DownloadFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/library/downloads/DownloadFragment.kt @@ -18,6 +18,7 @@ import org.mozilla.fenix.R import org.mozilla.fenix.browser.browsingmode.BrowsingMode import org.mozilla.fenix.components.StoreProvider import org.mozilla.fenix.components.metrics.Event +import org.mozilla.fenix.ext.filterNotExistsOnDisk import org.mozilla.fenix.ext.requireComponents import org.mozilla.fenix.ext.showToolbar import org.mozilla.fenix.library.LibraryPageFragment @@ -43,7 +44,7 @@ class DownloadFragment : LibraryPageFragment(), UserInteractionHan it.value.contentLength.toString(), it.value.contentType ) - } + }.filterNotExistsOnDisk() downloadStore = StoreProvider.get(this) { DownloadFragmentStore( diff --git a/app/src/test/java/org/mozilla/fenix/ext/ListTest.kt b/app/src/test/java/org/mozilla/fenix/ext/ListTest.kt new file mode 100644 index 000000000..f565b66fd --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/ext/ListTest.kt @@ -0,0 +1,76 @@ +/* This Source Code Form is subject to the terms of the Mozilla Public + * License, v. 2.0. If a copy of the MPL was not distributed with this + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ + +package org.mozilla.fenix.ext + +import org.junit.Assert.assertEquals +import org.junit.Test +import org.junit.runner.RunWith +import org.mozilla.fenix.helpers.FenixRobolectricTestRunner +import org.mozilla.fenix.library.downloads.DownloadItem +import java.io.File + +@RunWith(FenixRobolectricTestRunner::class) +class ListTest { + + @Test + fun `Test download in list but not on disk removed from list`() { + val filePath1 = "filepath.txt" + val filePath3 = "filepath3.txt" + + var file1 = File(filePath1) + var file3 = File(filePath3) + + // Create files + file1.createNewFile() + file3.createNewFile() + + val item1 = DownloadItem(71, "filepath.txt", filePath1, "71 Mb", "Image/png") + val item2 = DownloadItem(71, "filepath2.txt", "filepath2.txt", "71 Mb", "Image/png") + val item3 = DownloadItem(71, "filepath3.txt", filePath3, "71 Mb", "Image/png") + + val testList = mutableListOf(item1, item2, item3) + val comparisonList: MutableList = mutableListOf(item1, item3) + + val resultList = testList.filterNotExistsOnDisk() + + assertEquals(comparisonList, resultList) + + // Cleanup files + file1.delete() + file3.delete() + } + + @Test + fun `Test download in list and on disk remain in list`() { + val filePath1 = "filepath.txt" + val filePath2 = "filepath.txt" + val filePath3 = "filepath3.txt" + + var file1 = File(filePath1) + var file2 = File(filePath2) + var file3 = File(filePath3) + + // Create files + file1.createNewFile() + file2.createNewFile() + file3.createNewFile() + + val item1 = DownloadItem(71, "filepath.txt", filePath1, "71 Mb", "text/plain") + val item2 = DownloadItem(71, "filepath2.txt", filePath2, "71 Mb", "text/plain") + val item3 = DownloadItem(71, "filepath3.txt", filePath3, "71 Mb", "text/plain") + + val testList = mutableListOf(item1, item2, item3) + val comparisonList: MutableList = mutableListOf(item1, item2, item3) + + val resultList = testList.filterNotExistsOnDisk() + + assertEquals(comparisonList, resultList) + + // Cleanup files + file1.delete() + file2.delete() + file3.delete() + } +}