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

Commit

Permalink
Closes issue #8363: Call startForegroundService instead of startServi…
Browse files Browse the repository at this point in the history
…ce on DownloadMiddleware
  • Loading branch information
Amejia481 authored and pocmo committed Sep 10, 2020
1 parent 4096688 commit 464408c
Show file tree
Hide file tree
Showing 2 changed files with 11 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

0 comments on commit 464408c

Please sign in to comment.