Skip to content

Commit

Permalink
Closes issue mozilla-mobile#8363: Call startForegroundService instead…
Browse files Browse the repository at this point in the history
… of startService on DownloadMiddleware
  • Loading branch information
Amejia481 committed Sep 9, 2020
1 parent 9d8bcc2 commit 21332cf
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import android.app.DownloadManager
import android.content.Context
import android.content.Intent
import androidx.annotation.VisibleForTesting
import androidx.core.content.ContextCompat
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.InternalCoroutinesApi
Expand Down Expand Up @@ -113,8 +114,13 @@ class DownloadMiddleware(
if (download.status !in arrayOf(COMPLETED, CANCELLED)) {
val intent = Intent(applicationContext, downloadServiceClass)
intent.putExtra(DownloadManager.EXTRA_DOWNLOAD_ID, download.id)
applicationContext.startService(intent)
startForegroundService(intent)
logger.debug("Sending download intent ${download.fileName}")
}
}

@VisibleForTesting
internal fun startForegroundService(intent: Intent) {
ContextCompat.startForegroundService(applicationContext, intent)
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ import org.junit.runner.RunWith
import org.mockito.Mockito.verify
import org.mockito.Mockito.times
import org.mockito.Mockito.never
import org.mockito.Mockito.spy

@RunWith(AndroidJUnit4::class)
class DownloadMiddlewareTest {
Expand All @@ -63,12 +64,12 @@ class DownloadMiddlewareTest {
@Test
fun `service is started when download is queued`() = runBlockingTest {
val applicationContext: Context = mock()
val downloadMiddleware = DownloadMiddleware(
val downloadMiddleware = spy(DownloadMiddleware(
applicationContext,
AbstractFetchDownloadService::class.java,
coroutineContext = dispatcher,
downloadStorage = mock()
)
))
val store = BrowserStore(
initialState = BrowserState(),
middleware = listOf(downloadMiddleware)
Expand All @@ -78,7 +79,7 @@ class DownloadMiddlewareTest {
store.dispatch(DownloadAction.AddDownloadAction(download)).joinBlocking()

val intentCaptor = argumentCaptor<Intent>()
verify(applicationContext).startService(intentCaptor.capture())
verify(downloadMiddleware).startForegroundService(intentCaptor.capture())
assertEquals(download.id, intentCaptor.value.getStringExtra(EXTRA_DOWNLOAD_ID))
}

Expand Down
1 change: 1 addition & 0 deletions docs/changelog.md
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ permalink: /changelog/
* 🌟 Added support for persisting/restoring downloads see issue [#7762](https://github.com/mozilla-mobile/android-components/issues/7762).
* 🌟 Added `DownloadStorage` for querying stored download metadata.
* 🚒 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**
Expand Down

0 comments on commit 21332cf

Please sign in to comment.