Commit 464408c5 authored by Arturo Mejia's avatar Arturo Mejia Committed by Sebastian Kaspari
Browse files

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

parent 40966887
...@@ -8,6 +8,7 @@ import android.app.DownloadManager ...@@ -8,6 +8,7 @@ import android.app.DownloadManager
import android.content.Context import android.content.Context
import android.content.Intent import android.content.Intent
import androidx.annotation.VisibleForTesting import androidx.annotation.VisibleForTesting
import androidx.core.content.ContextCompat
import kotlinx.coroutines.CoroutineScope import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.InternalCoroutinesApi import kotlinx.coroutines.InternalCoroutinesApi
...@@ -113,8 +114,13 @@ class DownloadMiddleware( ...@@ -113,8 +114,13 @@ class DownloadMiddleware(
if (download.status !in arrayOf(COMPLETED, CANCELLED)) { if (download.status !in arrayOf(COMPLETED, CANCELLED)) {
val intent = Intent(applicationContext, downloadServiceClass) val intent = Intent(applicationContext, downloadServiceClass)
intent.putExtra(DownloadManager.EXTRA_DOWNLOAD_ID, download.id) intent.putExtra(DownloadManager.EXTRA_DOWNLOAD_ID, download.id)
applicationContext.startService(intent) startForegroundService(intent)
logger.debug("Sending download intent ${download.fileName}") logger.debug("Sending download intent ${download.fileName}")
} }
} }
@VisibleForTesting
internal fun startForegroundService(intent: Intent) {
ContextCompat.startForegroundService(applicationContext, intent)
}
} }
...@@ -37,6 +37,7 @@ import org.junit.runner.RunWith ...@@ -37,6 +37,7 @@ import org.junit.runner.RunWith
import org.mockito.Mockito.verify import org.mockito.Mockito.verify
import org.mockito.Mockito.times import org.mockito.Mockito.times
import org.mockito.Mockito.never import org.mockito.Mockito.never
import org.mockito.Mockito.spy
@RunWith(AndroidJUnit4::class) @RunWith(AndroidJUnit4::class)
class DownloadMiddlewareTest { class DownloadMiddlewareTest {
...@@ -63,12 +64,12 @@ class DownloadMiddlewareTest { ...@@ -63,12 +64,12 @@ class DownloadMiddlewareTest {
@Test @Test
fun `service is started when download is queued`() = runBlockingTest { fun `service is started when download is queued`() = runBlockingTest {
val applicationContext: Context = mock() val applicationContext: Context = mock()
val downloadMiddleware = DownloadMiddleware( val downloadMiddleware = spy(DownloadMiddleware(
applicationContext, applicationContext,
AbstractFetchDownloadService::class.java, AbstractFetchDownloadService::class.java,
coroutineContext = dispatcher, coroutineContext = dispatcher,
downloadStorage = mock() downloadStorage = mock()
) ))
val store = BrowserStore( val store = BrowserStore(
initialState = BrowserState(), initialState = BrowserState(),
middleware = listOf(downloadMiddleware) middleware = listOf(downloadMiddleware)
...@@ -78,7 +79,7 @@ class DownloadMiddlewareTest { ...@@ -78,7 +79,7 @@ class DownloadMiddlewareTest {
store.dispatch(DownloadAction.AddDownloadAction(download)).joinBlocking() store.dispatch(DownloadAction.AddDownloadAction(download)).joinBlocking()
val intentCaptor = argumentCaptor<Intent>() val intentCaptor = argumentCaptor<Intent>()
verify(applicationContext).startService(intentCaptor.capture()) verify(downloadMiddleware).startForegroundService(intentCaptor.capture())
assertEquals(download.id, intentCaptor.value.getStringExtra(EXTRA_DOWNLOAD_ID)) assertEquals(download.id, intentCaptor.value.getStringExtra(EXTRA_DOWNLOAD_ID))
} }
......
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