Skip to content

Commit

Permalink
For mozilla-mobile#9806 Close the download response before processing…
Browse files Browse the repository at this point in the history
… other actions
  • Loading branch information
Amejia481 authored and mergify[bot] committed Mar 11, 2021
1 parent ce83eda commit c5e9046
Show file tree
Hide file tree
Showing 2 changed files with 9 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,7 @@ class DownloadMiddleware(
is DownloadAction.RemoveAllDownloadsAction -> removeDownloads()
is DownloadAction.UpdateDownloadAction -> updateDownload(action.download, context)
is DownloadAction.RestoreDownloadsStateAction -> restoreDownloads(context.store)
is ContentAction.CancelDownloadAction -> closeDownloadResponse(context.store, action.sessionId)
is DownloadAction.AddDownloadAction -> {
if (!action.download.private && !saveDownload(context.store, action.download)) {
// The download was already added before, so we are ignoring this request.
Expand All @@ -77,7 +78,6 @@ class DownloadMiddleware(
is TabListAction.RemoveTabAction -> removePrivateNotifications(context.store, listOf(action.tabId))
is DownloadAction.AddDownloadAction -> sendDownloadIntent(action.download)
is DownloadAction.RestoreDownloadStateAction -> sendDownloadIntent(action.download)
is ContentAction.CancelDownloadAction -> closeDownloadResponse(context.store, action.sessionId)
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -481,6 +481,8 @@ class DownloadMiddlewareTest {

@Test
fun `WHEN an action for canceling a download response is received THEN a download response must be canceled`() = runBlockingTest {
val response = mock<Response>()
val download = DownloadState(id = "downloadID", url = "example.com/5MB.zip", response = response)
val applicationContext: Context = mock()
val downloadMiddleware = spy(DownloadMiddleware(
applicationContext,
Expand All @@ -493,12 +495,17 @@ class DownloadMiddlewareTest {
middleware = listOf(downloadMiddleware)
)

store.dispatch(ContentAction.CancelDownloadAction("tabID", "downloadID")).joinBlocking()
val tab = createTab("https://www.mozilla.org")

store.dispatch(TabListAction.AddTabAction(tab, select = true)).joinBlocking()
store.dispatch(ContentAction.UpdateDownloadAction(tab.id, download = download)).joinBlocking()
store.dispatch(ContentAction.CancelDownloadAction(tab.id, download.id)).joinBlocking()

dispatcher.advanceUntilIdle()
store.waitUntilIdle()

verify(downloadMiddleware, times(1)).closeDownloadResponse(any(), any())
verify(response).close()
}

@Test
Expand Down

0 comments on commit c5e9046

Please sign in to comment.