Skip to content

Commit

Permalink
Close mozilla-mobile#8354: Do not restart FAILED downloads
Browse files Browse the repository at this point in the history
  • Loading branch information
Amejia481 committed Sep 10, 2020
1 parent edd37a8 commit 9453db4
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 3 deletions.
Expand Up @@ -20,6 +20,7 @@ import mozilla.components.browser.state.state.BrowserState
import mozilla.components.browser.state.state.content.DownloadState
import mozilla.components.browser.state.state.content.DownloadState.Status.CANCELLED
import mozilla.components.browser.state.state.content.DownloadState.Status.COMPLETED
import mozilla.components.browser.state.state.content.DownloadState.Status.FAILED
import mozilla.components.lib.state.Middleware
import mozilla.components.lib.state.MiddlewareContext
import mozilla.components.lib.state.Store
Expand Down Expand Up @@ -110,8 +111,9 @@ class DownloadMiddleware(
}
}

private fun sendDownloadIntent(download: DownloadState) {
if (download.status !in arrayOf(COMPLETED, CANCELLED)) {
@VisibleForTesting
internal fun sendDownloadIntent(download: DownloadState) {
if (download.status !in arrayOf(COMPLETED, CANCELLED, FAILED)) {
val intent = Intent(applicationContext, downloadServiceClass)
intent.putExtra(DownloadManager.EXTRA_DOWNLOAD_ID, download.id)
startForegroundService(intent)
Expand Down
Expand Up @@ -21,6 +21,8 @@ import mozilla.components.browser.state.state.BrowserState
import mozilla.components.browser.state.state.content.DownloadState
import mozilla.components.browser.state.state.content.DownloadState.Status.COMPLETED
import mozilla.components.browser.state.state.content.DownloadState.Status.INITIATED
import mozilla.components.browser.state.state.content.DownloadState.Status.FAILED
import mozilla.components.browser.state.state.content.DownloadState.Status.CANCELLED
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.support.test.any
import mozilla.components.support.test.argumentCaptor
Expand All @@ -38,6 +40,7 @@ import org.mockito.Mockito.verify
import org.mockito.Mockito.times
import org.mockito.Mockito.never
import org.mockito.Mockito.spy
import org.mockito.Mockito.reset

@RunWith(AndroidJUnit4::class)
class DownloadMiddlewareTest {
Expand Down Expand Up @@ -222,4 +225,30 @@ class DownloadMiddlewareTest {

assertEquals(download, store.state.downloads.values.first())
}

@Test
fun `sendDownloadIntent MUST call startForegroundService WHEN downloads are NOT COMPLETED, CANCELLED and FAILED`() = runBlockingTest {
val applicationContext: Context = mock()
val downloadMiddleware = spy(DownloadMiddleware(
applicationContext,
AbstractFetchDownloadService::class.java
))

val ignoredStatus = listOf(COMPLETED, CANCELLED, FAILED)
ignoredStatus.forEach { status ->
val download = DownloadState("https://mozilla.org/download", status = status)
downloadMiddleware.sendDownloadIntent(download)
verify(downloadMiddleware, times(0)).startForegroundService(any())
}

reset(downloadMiddleware)

val allowedStatus = DownloadState.Status.values().filter { it !in ignoredStatus }

allowedStatus.forEachIndexed { index, status ->
val download = DownloadState("https://mozilla.org/download", status = status)
downloadMiddleware.sendDownloadIntent(download)
verify(downloadMiddleware, times(index + 1)).startForegroundService(any())
}
}
}
4 changes: 3 additions & 1 deletion docs/changelog.md
Expand Up @@ -15,6 +15,9 @@ permalink: /changelog/
* **feature-recentlyclosed**
* Added a new [RecentlyClosedTabsStorage] and a [RecentlyClosedMiddleware] to maintain a list of restorable recently closed tabs.

* **feature-downloads**
* 🚒 Bug [issue #8354](https://github.com/mozilla-mobile/android-components/issues/8354) Do not restart FAILED downloads.

# 57.0.0

* [Commits](https://github.com/mozilla-mobile/android-components/compare/v56.0.0...v57.0.0)
Expand Down Expand Up @@ -66,7 +69,6 @@ permalink: /changelog/
* 🚒 Bug [issue #8190](https://github.com/mozilla-mobile/android-components/issues/8190) ArithmeticException: divide by zero in Download notification.
* 🚒 Bug [issue #8363](https://github.com/mozilla-mobile/android-components/issues/8363) IllegalStateException: Not allowed to start service Intent.


* **ui-widgets**
* 🆕 New VerticalSwipeRefreshLayout that comes to resolve many of the issues of the platform SwipeRefreshLayout and filters out other gestures than swipe down/up.

Expand Down

0 comments on commit 9453db4

Please sign in to comment.