This repository has been archived by the owner on Nov 1, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 473
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Issue #11698: Add an updater to periodically fetch the Contile Top Sites
- Loading branch information
1 parent
134f6a0
commit 79a980e
Showing
8 changed files
with
400 additions
and
4 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
91 changes: 91 additions & 0 deletions
91
...ervice/contile/src/main/java/mozilla/components/service/contile/ContileTopSitesUpdater.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,91 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.service.contile | ||
|
||
import android.content.Context | ||
import androidx.annotation.VisibleForTesting | ||
import androidx.work.Constraints | ||
import androidx.work.ExistingPeriodicWorkPolicy | ||
import androidx.work.NetworkType | ||
import androidx.work.PeriodicWorkRequestBuilder | ||
import androidx.work.WorkManager | ||
import mozilla.components.support.base.log.logger.Logger | ||
import java.util.concurrent.TimeUnit | ||
|
||
/** | ||
* Provides functionality to schedule updates of Contile top sites. | ||
* | ||
* @property context A reference to the application context. | ||
* @property provider An instance of [ContileTopSitesProvider] which provides access to the Contile | ||
* services API for fetching top sites. | ||
* @property frequency Optional [Frequency] that specifies how often the Contile top site updates | ||
* should happen. | ||
*/ | ||
class ContileTopSitesUpdater( | ||
private val context: Context, | ||
private val provider: ContileTopSitesProvider, | ||
private val frequency: Frequency = Frequency(1, TimeUnit.DAYS) | ||
) { | ||
|
||
private val logger = Logger("ContileTopSitesUpdater") | ||
|
||
/** | ||
* Starts a work request in the background to periodically update the list of | ||
* Contile top sites. | ||
*/ | ||
fun startPeriodicWork() { | ||
ContileTopSitesUseCases.initialize(provider) | ||
|
||
WorkManager.getInstance(context).enqueueUniquePeriodicWork( | ||
PERIODIC_WORK_TAG, | ||
ExistingPeriodicWorkPolicy.KEEP, | ||
createPeriodicWorkRequest() | ||
) | ||
|
||
logger.info("Started periodic work to update Contile top sites") | ||
} | ||
|
||
/** | ||
* Stops the work request to periodically update the list of Contile top sites. | ||
*/ | ||
fun stopPeriodicWork() { | ||
ContileTopSitesUseCases.destroy() | ||
|
||
WorkManager.getInstance(context).cancelUniqueWork(PERIODIC_WORK_TAG) | ||
|
||
logger.info("Stopped periodic work to update Contile top sites") | ||
} | ||
|
||
@VisibleForTesting | ||
internal fun createPeriodicWorkRequest() = | ||
PeriodicWorkRequestBuilder<ContileTopSitesUpdaterWorker>( | ||
repeatInterval = frequency.repeatInterval, | ||
repeatIntervalTimeUnit = frequency.repeatIntervalTimeUnit | ||
).apply { | ||
setConstraints(getWorkerConstraints()) | ||
addTag(PERIODIC_WORK_TAG) | ||
}.build() | ||
|
||
@VisibleForTesting | ||
internal fun getWorkerConstraints() = Constraints.Builder() | ||
.setRequiresStorageNotLow(true) | ||
.setRequiredNetworkType(NetworkType.CONNECTED) | ||
.build() | ||
|
||
companion object { | ||
internal const val PERIODIC_WORK_TAG = "mozilla.components.service.contile.periodicWork" | ||
} | ||
} | ||
|
||
/** | ||
* Indicates how often Contile top sites should be updated. | ||
* | ||
* @property repeatInterval Long indicating how often the update should happen. | ||
* @property repeatIntervalTimeUnit The time unit of the [repeatInterval]. | ||
*/ | ||
data class Frequency( | ||
val repeatInterval: Long, | ||
val repeatIntervalTimeUnit: TimeUnit | ||
) |
34 changes: 34 additions & 0 deletions
34
.../contile/src/main/java/mozilla/components/service/contile/ContileTopSitesUpdaterWorker.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,34 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.service.contile | ||
|
||
import android.content.Context | ||
import androidx.work.CoroutineWorker | ||
import androidx.work.WorkerParameters | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import mozilla.components.support.base.log.logger.Logger | ||
|
||
/** | ||
* An implementation of [CoroutineWorker] to perform Contile top site updates. | ||
*/ | ||
internal class ContileTopSitesUpdaterWorker( | ||
context: Context, | ||
params: WorkerParameters | ||
) : CoroutineWorker(context, params) { | ||
|
||
private val logger = Logger("ContileTopSitesUpdaterWorker") | ||
|
||
@Suppress("TooGenericExceptionCaught") | ||
override suspend fun doWork(): Result = withContext(Dispatchers.IO) { | ||
try { | ||
ContileTopSitesUseCases().refreshContileTopSites.invoke() | ||
Result.success() | ||
} catch (e: Exception) { | ||
logger.error("Failed to refresh Contile top sites", e) | ||
Result.failure() | ||
} | ||
} | ||
} |
58 changes: 58 additions & 0 deletions
58
...rvice/contile/src/main/java/mozilla/components/service/contile/ContileTopSitesUseCases.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,58 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.service.contile | ||
|
||
import androidx.annotation.VisibleForTesting | ||
|
||
/** | ||
* Contains use cases related to the Contlie top sites feature. | ||
*/ | ||
internal class ContileTopSitesUseCases() { | ||
|
||
/** | ||
* Refresh Contile top sites use case. | ||
*/ | ||
class RefreshContileTopSitesUseCase internal constructor() { | ||
/** | ||
* Refreshes the Contile top sites. | ||
*/ | ||
suspend operator fun invoke() { | ||
requireContileTopSitesProvider().getTopSites(allowCache = false) | ||
} | ||
} | ||
|
||
internal companion object { | ||
@VisibleForTesting internal var provider: ContileTopSitesProvider? = null | ||
|
||
/** | ||
* Initializes the [ContileTopSitesProvider] which will providde access to the Contile | ||
* services API. | ||
*/ | ||
internal fun initialize(provider: ContileTopSitesProvider) { | ||
this.provider = provider | ||
} | ||
|
||
/** | ||
* Unbinds the [ContileTopSitesProvider]. | ||
*/ | ||
internal fun destroy() { | ||
this.provider = null | ||
} | ||
|
||
/** | ||
* Returns the [ContileTopSitesProvider], otherwise throw an exception if the [provider] | ||
* has not been initialized. | ||
*/ | ||
internal fun requireContileTopSitesProvider(): ContileTopSitesProvider { | ||
return requireNotNull(provider) { | ||
"initialize must be called before trying to access the ContileTopSitesProvider" | ||
} | ||
} | ||
} | ||
|
||
val refreshContileTopSites: RefreshContileTopSitesUseCase by lazy { | ||
RefreshContileTopSitesUseCase() | ||
} | ||
} |
95 changes: 95 additions & 0 deletions
95
...ce/contile/src/test/java/mozilla/components/service/contile/ContileTopSitesUpdaterTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,95 @@ | ||
/* This Source Code Form is subject to the terms of the Mozilla Public | ||
* License, v. 2.0. If a copy of the MPL was not distributed with this | ||
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */ | ||
|
||
package mozilla.components.service.contile | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.work.Configuration | ||
import androidx.work.WorkInfo | ||
import androidx.work.WorkManager | ||
import androidx.work.await | ||
import androidx.work.testing.WorkManagerTestInitHelper | ||
import kotlinx.coroutines.runBlocking | ||
import mozilla.components.service.contile.ContileTopSitesUpdater.Companion.PERIODIC_WORK_TAG | ||
import mozilla.components.support.test.mock | ||
import mozilla.components.support.test.robolectric.testContext | ||
import org.junit.After | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Assert.assertNotNull | ||
import org.junit.Assert.assertNull | ||
import org.junit.Assert.assertTrue | ||
import org.junit.Before | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
|
||
@RunWith(AndroidJUnit4::class) | ||
class ContileTopSitesUpdaterTest { | ||
|
||
@Before | ||
fun setUp() { | ||
WorkManagerTestInitHelper.initializeTestWorkManager( | ||
testContext, | ||
Configuration.Builder().build() | ||
) | ||
} | ||
|
||
@After | ||
fun tearDown() { | ||
WorkManager.getInstance(testContext).cancelUniqueWork(PERIODIC_WORK_TAG) | ||
} | ||
|
||
@Test | ||
fun `WHEN periodic work is started THEN work is queued`() = runBlocking { | ||
val updater = ContileTopSitesUpdater(testContext, provider = mock()) | ||
val workManager = WorkManager.getInstance(testContext) | ||
var workInfo = workManager.getWorkInfosForUniqueWork(PERIODIC_WORK_TAG).await() | ||
|
||
assertTrue(workInfo.isEmpty()) | ||
assertNull(ContileTopSitesUseCases.provider) | ||
|
||
updater.startPeriodicWork() | ||
|
||
assertNotNull(ContileTopSitesUseCases.provider) | ||
assertNotNull(ContileTopSitesUseCases.requireContileTopSitesProvider()) | ||
|
||
workInfo = workManager.getWorkInfosForUniqueWork(PERIODIC_WORK_TAG).await() | ||
val work = workInfo.first() | ||
|
||
assertEquals(1, workInfo.size) | ||
assertEquals(WorkInfo.State.ENQUEUED, work.state) | ||
assertTrue(work.tags.contains(PERIODIC_WORK_TAG)) | ||
} | ||
|
||
@Test | ||
fun `GIVEN periodic work is started WHEN period work is stopped THEN no work is queued`() = runBlocking { | ||
val updater = ContileTopSitesUpdater(testContext, provider = mock()) | ||
val workManager = WorkManager.getInstance(testContext) | ||
var workInfo = workManager.getWorkInfosForUniqueWork(PERIODIC_WORK_TAG).await() | ||
|
||
assertTrue(workInfo.isEmpty()) | ||
|
||
updater.startPeriodicWork() | ||
|
||
workInfo = workManager.getWorkInfosForUniqueWork(PERIODIC_WORK_TAG).await() | ||
|
||
assertEquals(1, workInfo.size) | ||
|
||
updater.stopPeriodicWork() | ||
|
||
workInfo = workManager.getWorkInfosForUniqueWork(PERIODIC_WORK_TAG).await() | ||
val work = workInfo.first() | ||
|
||
assertNull(ContileTopSitesUseCases.provider) | ||
assertEquals(WorkInfo.State.CANCELLED, work.state) | ||
} | ||
|
||
@Test | ||
fun `WHEN period work request is created THEN it contains the correct constraints`() { | ||
val updater = ContileTopSitesUpdater(testContext, provider = mock()) | ||
val workRequest = updater.createPeriodicWorkRequest() | ||
|
||
assertTrue(workRequest.tags.contains(PERIODIC_WORK_TAG)) | ||
assertEquals(updater.getWorkerConstraints(), workRequest.workSpec.constraints) | ||
} | ||
} |
Oops, something went wrong.