Skip to content

Commit

Permalink
Closes issue mozilla-mobile#8456: Don't process add requests
Browse files Browse the repository at this point in the history
for downloads already added.
  • Loading branch information
Amejia481 committed Sep 21, 2020
1 parent 6d759bf commit 5eaf3f2
Show file tree
Hide file tree
Showing 3 changed files with 47 additions and 10 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -55,21 +55,21 @@ class DownloadMiddleware(
is DownloadAction.RemoveAllDownloadsAction -> removeDownloads()
is DownloadAction.UpdateDownloadAction -> updateDownload(action.download, context)
is DownloadAction.RestoreDownloadsStateAction -> restoreDownloads(context.store)
is DownloadAction.AddDownloadAction -> {
if (!saveDownload(context.store, action.download)) {
// The download was already added before, so we are ignoring this request.
logger.debug("Ignored add action for ${action.download.id} " +
"download already in store.downloads")
return
}
}
}

next(action)

when (action) {
is DownloadAction.AddDownloadAction -> {
scope.launch {
downloadStorage.add(action.download)
logger.debug("Added download ${action.download.fileName} to the storage")
}
sendDownloadIntent(action.download)
}
is DownloadAction.RestoreDownloadStateAction -> {
sendDownloadIntent(action.download)
}
is DownloadAction.AddDownloadAction -> sendDownloadIntent(action.download)
is DownloadAction.RestoreDownloadStateAction -> sendDownloadIntent(action.download)
}
}

Expand Down Expand Up @@ -111,6 +111,18 @@ class DownloadMiddleware(
}
}

private fun saveDownload(store: Store<BrowserState, BrowserAction>, download: DownloadState): Boolean {
return if (!store.state.downloads.containsKey(download.id)) {
scope.launch {
downloadStorage.add(download)
logger.debug("Added download ${download.fileName} to the storage")
}
true
} else {
false
}
}

@VisibleForTesting
internal fun sendDownloadIntent(download: DownloadState) {
if (download.status !in arrayOf(COMPLETED, CANCELLED, FAILED)) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,29 @@ class DownloadMiddlewareTest {
verify(downloadStorage).add(download)
}

@Test
fun `previously added downloads MUST be ignored`() = runBlockingTest {
val applicationContext: Context = mock()
val downloadStorage: DownloadStorage = mock()
val download = DownloadState("https://mozilla.org/download")
val downloadMiddleware = DownloadMiddleware(
applicationContext,
AbstractFetchDownloadService::class.java,
downloadStorage = downloadStorage,
coroutineContext = dispatcher
)
val store = BrowserStore(
initialState = BrowserState(
downloads = mapOf(download.id to download)
),
middleware = listOf(downloadMiddleware)
)

store.dispatch(DownloadAction.AddDownloadAction(download)).joinBlocking()

verify(downloadStorage, never()).add(download)
}

@Test
fun `RemoveDownloadAction MUST remove from the storage`() = runBlockingTest {
val applicationContext: Context = mock()
Expand Down
2 changes: 2 additions & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@ permalink: /changelog/
* ⚠️ **This is a breaking change**: Removed `TabsUseCases.removeAllTabsOfType()`.
* **browser-session**
* Added `SessionManager.removeNormalSessions()` and `SessionManager.removePrivateSessions()`.
* **feature-downloads**
* 🚒 Bug fixed [issue #8456](https://github.com/mozilla-mobile/android-components/issues/8456) Crash SQLiteConstraintException UNIQUE constraint failed: downloads.id (code 1555).

# 59.0.0

Expand Down

0 comments on commit 5eaf3f2

Please sign in to comment.