Commit 77a73e1d authored by MozLando's avatar MozLando
Browse files

Merge #8303



8303: Closes issue #8292: Improve tests on DownloadMiddleware. r=csadilek a=Amejia481

I removed the `MiddlewareContext` from functions where we are running on coroutine and pass the `Store` to avoid future instance of #8292
Co-authored-by: default avatarArturo Mejia <arturomejiamarmol@gmail.com>
parents 4a49208f 3699a9cc
......@@ -21,6 +21,7 @@ import mozilla.components.browser.state.state.content.DownloadState.Status.CANCE
import mozilla.components.browser.state.state.content.DownloadState.Status.COMPLETED
import mozilla.components.lib.state.Middleware
import mozilla.components.lib.state.MiddlewareContext
import mozilla.components.lib.state.Store
import mozilla.components.support.base.log.logger.Logger
import kotlin.coroutines.CoroutineContext
......@@ -48,10 +49,10 @@ class DownloadMiddleware(
action: BrowserAction
) {
when (action) {
is DownloadAction.RemoveDownloadAction -> removeDownload(action.downloadId, context)
is DownloadAction.RemoveDownloadAction -> removeDownload(action.downloadId, context.store)
is DownloadAction.RemoveAllDownloadsAction -> removeDownloads()
is DownloadAction.UpdateDownloadAction -> updateDownload(action.download, context)
is DownloadAction.RestoreDownloadsStateAction -> restoreDownloads(context)
is DownloadAction.RestoreDownloadsStateAction -> restoreDownloads(context.store)
}
next(action)
......@@ -72,9 +73,9 @@ class DownloadMiddleware(
private fun removeDownload(
downloadId: String,
context: MiddlewareContext<BrowserState, BrowserAction>
store: Store<BrowserState, BrowserAction>
) = scope.launch {
context.state.downloads[downloadId]?.let {
store.state.downloads[downloadId]?.let {
downloadStorage.remove(it)
logger.debug("Removed download ${it.fileName} from the storage")
}
......@@ -97,11 +98,11 @@ class DownloadMiddleware(
}
}
private fun restoreDownloads(context: MiddlewareContext<BrowserState, BrowserAction>) = scope.launch {
private fun restoreDownloads(store: Store<BrowserState, BrowserAction>) = scope.launch {
downloadStorage.getDownloads().collect { downloads ->
downloads.forEach { download ->
if (!context.state.downloads.containsKey(download.id)) {
context.store.dispatch(DownloadAction.RestoreDownloadStateAction(download))
if (!store.state.downloads.containsKey(download.id)) {
store.dispatch(DownloadAction.RestoreDownloadStateAction(download))
logger.debug("Download restarted from the storage ${download.fileName}")
}
}
......
......@@ -27,6 +27,7 @@ import mozilla.components.support.test.argumentCaptor
import mozilla.components.support.test.mock
import mozilla.components.support.test.whenever
import mozilla.components.support.test.ext.joinBlocking
import mozilla.components.support.test.libstate.ext.waitUntilIdle
import org.junit.After
import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue
......@@ -214,7 +215,9 @@ class DownloadMiddlewareTest {
assertTrue(store.state.downloads.isEmpty())
store.dispatch(DownloadAction.RestoreDownloadsStateAction).joinBlocking()
dispatcher.advanceUntilIdle()
store.waitUntilIdle()
assertEquals(download, store.state.downloads.values.first())
}
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment