Skip to content
This repository has been archived by the owner on Nov 1, 2022. It is now read-only.

Closes issue #8363: Call startForegroundService instead of startService on DownloadMiddleware #8364

Merged
merged 1 commit into from
Sep 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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