For #26239 - Avoid displaying multiple download items for the same file

This commit is contained in:
Alexandru2909 2022-08-03 09:18:01 +03:00 committed by mergify[bot]
parent f239a24d48
commit ec4ad984c2
2 changed files with 34 additions and 0 deletions

View File

@ -91,6 +91,7 @@ class DownloadFragment : LibraryPageFragment<DownloadItem>(), UserInteractionHan
@VisibleForTesting
internal fun provideDownloads(state: BrowserState): List<DownloadItem> {
return state.downloads.values
.distinctBy { it.fileName }
.sortedByDescending { it.createdTime } // sort from newest to oldest
.map {
DownloadItem(

View File

@ -147,4 +147,37 @@ class DownloadFragmentTest {
val list = fragment.provideDownloads(state)
assertEquals(expectedList[0].size, list[0].size)
}
@Test
fun `WHEN two download states point to the same existing file THEN only one download item is displayed`() {
val fragment = DownloadFragment()
val downloadedFile0 = File(
Environment.getExternalStoragePublicDirectory(Environment.DIRECTORY_DOWNLOADS),
"1.pdf"
)
val state: BrowserState = mockk(relaxed = true)
every { state.downloads } returns mapOf(
"1" to DownloadState(
id = "1",
createdTime = 1,
url = "url",
fileName = "1.pdf",
contentLength = 100,
status = DownloadState.Status.COMPLETED
),
"2" to DownloadState(
id = "2",
createdTime = 2,
url = "url",
fileName = "1.pdf",
contentLength = 100,
status = DownloadState.Status.COMPLETED
)
)
downloadedFile0.createNewFile()
val providedList = fragment.provideDownloads(state)
assertEquals(1, providedList.size)
}
}