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.
For #12258 - Retry deleting Pocket profile if initially failed
It's important to ensure the profile is deleted when the sponsored stories feature is disabled but since this involves a network call which may fail we need to support retrying a previous failed request until profile deletion is successful or the sponsored stories functionality is started again.
- Loading branch information
Showing
7 changed files
with
255 additions
and
31 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
36 changes: 36 additions & 0 deletions
36
...pocket/src/main/java/mozilla/components/service/pocket/update/DeleteSpocsProfileWorker.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,36 @@ | ||
/* 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.pocket.update | ||
|
||
import android.content.Context | ||
import androidx.work.CoroutineWorker | ||
import androidx.work.WorkerParameters | ||
import kotlinx.coroutines.Dispatchers | ||
import kotlinx.coroutines.withContext | ||
import mozilla.components.service.pocket.GlobalDependencyProvider | ||
|
||
/** | ||
* WorkManager Worker used for deleting the profile used for downloading Pocket sponsored stories. | ||
*/ | ||
internal class DeleteSpocsProfileWorker( | ||
context: Context, | ||
params: WorkerParameters | ||
) : CoroutineWorker(context, params) { | ||
|
||
override suspend fun doWork(): Result { | ||
return withContext(Dispatchers.IO) { | ||
if (GlobalDependencyProvider.SponsoredStories.useCases?.deleteProfile?.invoke() == true) { | ||
Result.success() | ||
} else { | ||
Result.retry() | ||
} | ||
} | ||
} | ||
|
||
internal companion object { | ||
const val DELETE_SPOCS_PROFILE_WORK_TAG = | ||
"mozilla.components.feature.pocket.spocs.profile.delete.work.tag" | ||
} | ||
} |
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
65 changes: 65 additions & 0 deletions
65
...et/src/test/java/mozilla/components/service/pocket/update/DeleteSpocsProfileWorkerTest.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,65 @@ | ||
/* 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.pocket.update | ||
|
||
import androidx.test.ext.junit.runners.AndroidJUnit4 | ||
import androidx.work.ListenableWorker.Result | ||
import androidx.work.await | ||
import androidx.work.testing.TestListenableWorkerBuilder | ||
import kotlinx.coroutines.ExperimentalCoroutinesApi | ||
import mozilla.components.service.pocket.GlobalDependencyProvider | ||
import mozilla.components.service.pocket.helpers.assertClassVisibility | ||
import mozilla.components.service.pocket.spocs.SpocsUseCases | ||
import mozilla.components.service.pocket.spocs.SpocsUseCases.DeleteProfile | ||
import mozilla.components.support.test.mock | ||
import mozilla.components.support.test.robolectric.testContext | ||
import mozilla.components.support.test.rule.MainCoroutineRule | ||
import mozilla.components.support.test.rule.runTestOnMain | ||
import org.junit.Assert.assertEquals | ||
import org.junit.Rule | ||
import org.junit.Test | ||
import org.junit.runner.RunWith | ||
import org.mockito.Mockito.doReturn | ||
import kotlin.reflect.KVisibility.INTERNAL | ||
|
||
@ExperimentalCoroutinesApi // for runTestOnMain | ||
@RunWith(AndroidJUnit4::class) | ||
class DeleteSpocsProfileWorkerTest { | ||
@get:Rule | ||
val mainCoroutineRule = MainCoroutineRule() | ||
|
||
@Test | ||
fun `GIVEN a DeleteSpocsProfileWorker THEN its visibility is internal`() { | ||
assertClassVisibility(RefreshSpocsWorker::class, INTERNAL) | ||
} | ||
|
||
@Test | ||
fun `GIVEN a DeleteSpocsProfileWorker WHEN profile deletion is successful THEN return success`() = runTestOnMain { | ||
val useCases: SpocsUseCases = mock() | ||
val deleteProfileUseCase: DeleteProfile = mock() | ||
doReturn(true).`when`(deleteProfileUseCase).invoke() | ||
doReturn(deleteProfileUseCase).`when`(useCases).deleteProfile | ||
GlobalDependencyProvider.SponsoredStories.initialize(useCases) | ||
val worker = TestListenableWorkerBuilder<DeleteSpocsProfileWorker>(testContext).build() | ||
|
||
val result = worker.startWork().await() | ||
|
||
assertEquals(Result.success(), result) | ||
} | ||
|
||
@Test | ||
fun `GIVEN a DeleteSpocsProfileWorker WHEN profile deletion fails THEN work should be retried`() = runTestOnMain { | ||
val useCases: SpocsUseCases = mock() | ||
val deleteProfileUseCase: DeleteProfile = mock() | ||
doReturn(false).`when`(deleteProfileUseCase).invoke() | ||
doReturn(deleteProfileUseCase).`when`(useCases).deleteProfile | ||
GlobalDependencyProvider.SponsoredStories.initialize(useCases) | ||
val worker = TestListenableWorkerBuilder<DeleteSpocsProfileWorker>(testContext).build() | ||
|
||
val result = worker.startWork().await() | ||
|
||
assertEquals(Result.retry(), result) | ||
} | ||
} |
Oops, something went wrong.