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

Commit

Permalink
Get feature-addons tests to pass
Browse files Browse the repository at this point in the history
  • Loading branch information
grigoryk committed Feb 15, 2022
1 parent 37fabbc commit 338892c
Show file tree
Hide file tree
Showing 7 changed files with 54 additions and 35 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,6 @@ class AddonManager(
}

val supportedAddonIds = supportedAddons.map { it.id }

// Get all installed addons that are not yet supported.
val unsupportedAddons = installedExtensions
.filterKeys { !supportedAddonIds.contains(it) }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ internal class SupportedAddonsWorker(
private val logger = Logger("SupportedAddonsWorker")

@Suppress("TooGenericExceptionCaught", "MaxLineLength")
override suspend fun doWork(): Result = withContext(Dispatchers.IO) {
override suspend fun doWork(): Result {
try {
logger.info("Trying to check for new supported add-ons")

Expand Down Expand Up @@ -180,7 +180,7 @@ internal class SupportedAddonsWorker(
GlobalAddonDependencyProvider.onCrash?.invoke(exception)
}
}
Result.success()
return Result.success()
}

@VisibleForTesting
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import androidx.work.WorkManager
import androidx.work.WorkerParameters
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.Job
import kotlinx.coroutines.launch
import mozilla.components.concept.engine.webextension.WebExtension
import mozilla.components.concept.engine.webextension.WebExtensionException
Expand Down Expand Up @@ -65,7 +66,7 @@ interface AddonUpdater {
* Unregisters the given [addonId] for periodically checking for new updates.
* @param addonId The unique id of the addon.
*/
fun unregisterForFutureUpdates(addonId: String)
fun unregisterForFutureUpdates(addonId: String): Job

/**
* Try to perform an update on the given [addonId].
Expand Down Expand Up @@ -181,11 +182,11 @@ class DefaultAddonUpdater(
/**
* See [AddonUpdater.unregisterForFutureUpdates]
*/
override fun unregisterForFutureUpdates(addonId: String) {
override fun unregisterForFutureUpdates(addonId: String): Job {
WorkManager.getInstance(applicationContext)
.cancelUniqueWork(getUniquePeriodicWorkName(addonId))
logger.info("unregisterForFutureUpdates $addonId")
scope.launch {
return scope.launch {
updateAttempStorage.remove(addonId)
}
}
Expand Down
8 changes: 6 additions & 2 deletions components/feature/addons/src/test/java/AddonManagerTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.launch
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.runTest
import mozilla.components.browser.state.action.WebExtensionAction
import mozilla.components.browser.state.state.BrowserState
import mozilla.components.browser.state.state.WebExtensionState
Expand Down Expand Up @@ -51,6 +53,8 @@ import org.mockito.Mockito.verify
@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class AddonManagerTest {
private val testDispatcher = StandardTestDispatcher()

@Before
fun setup() {
WebExtensionSupport.installedExtensions.clear()
Expand Down Expand Up @@ -150,7 +154,7 @@ class AddonManagerTest {
}

@Test
fun `getAddons - returns temporary add-ons as supported`() = runBlocking {
fun `getAddons - returns temporary add-ons as supported`() = runTest(testDispatcher) {
val addonsProvider: AddonsProvider = mock()
whenever(addonsProvider.getAvailableAddons(anyBoolean(), eq(null), language = anyString())).thenReturn(listOf())

Expand All @@ -177,7 +181,7 @@ class AddonManagerTest {
WebExtensionSupport.installedExtensions["temp_ext"] = temporaryExtension

val addonManager = spy(AddonManager(store, mock(), addonsProvider, mock()))
whenever(addonManager.getIconDispatcher()).thenReturn(Dispatchers.Main)
whenever(addonManager.getIconDispatcher()).thenReturn(testDispatcher)

val addons = addonManager.getAddons()
assertEquals(1, addons.size)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,19 @@
package mozilla.components.feature.addons.migration

import android.app.NotificationManager
import android.util.Log
import androidx.core.content.getSystemService
import androidx.test.ext.junit.runners.AndroidJUnit4
import androidx.work.ListenableWorker
import androidx.work.await
import androidx.work.testing.TestListenableWorkerBuilder
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.StandardTestDispatcher
import kotlinx.coroutines.test.resetMain
import kotlinx.coroutines.test.runTest
import kotlinx.coroutines.test.setMain
import mozilla.components.concept.engine.webextension.EnableSource
import mozilla.components.feature.addons.Addon
import mozilla.components.feature.addons.AddonManager
Expand Down Expand Up @@ -39,16 +46,22 @@ import org.mockito.Mockito.verify
import java.io.IOException
import java.lang.Exception

@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class SupportedAddonsWorkerTest {
private val testDispatcher = StandardTestDispatcher()


@Before
fun setUp() {
GlobalAddonDependencyProvider.addonManager = null
Dispatchers.setMain(testDispatcher)
}

@After
fun after() {
GlobalAddonDependencyProvider.addonManager = null
Dispatchers.resetMain()
}

@Test
Expand All @@ -69,9 +82,9 @@ class SupportedAddonsWorkerTest {
GlobalAddonDependencyProvider.initialize(addonManager, mock(), onCrash)
val onErrorCaptor = argumentCaptor<((Throwable) -> Unit)>()

runBlocking {
runTest(testDispatcher) {
whenever(addonManager.getAddons()).thenReturn(listOf(unsupportedAddon))
val result = worker.startWork().await()
val result = worker.doWork()

assertEquals(ListenableWorker.Result.success(), result)

Expand All @@ -96,8 +109,8 @@ class SupportedAddonsWorkerTest {
GlobalAddonDependencyProvider.initialize(addonManager, mock(), crashReporter)
GlobalAddonDependencyProvider.addonManager = null

runBlocking {
val result = worker.startWork().await()
runTest {
val result = worker.doWork()

assertEquals(ListenableWorker.Result.success(), result)
assertTrue(crashWasReported)
Expand All @@ -115,9 +128,9 @@ class SupportedAddonsWorkerTest {

GlobalAddonDependencyProvider.initialize(addonManager, mock(), crashReporter)

runBlocking {
runTest {
whenever(addonManager.getAddons()).thenThrow(AddonManagerException(IOException()))
val result = worker.startWork().await()
val result = worker.doWork()

assertEquals(ListenableWorker.Result.success(), result)
assertFalse(crashWasReported)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,10 @@ import androidx.work.await
import androidx.work.testing.TestListenableWorkerBuilder
import junit.framework.TestCase.assertEquals
import junit.framework.TestCase.assertTrue
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.UnconfinedTestDispatcher
import kotlinx.coroutines.test.runTest
import mozilla.components.browser.state.store.BrowserStore
import mozilla.components.concept.engine.Engine
import mozilla.components.concept.engine.webextension.WebExtension
Expand All @@ -31,6 +34,7 @@ import org.mockito.Mockito.doReturn
import org.mockito.Mockito.spy
import org.mockito.Mockito.verify

@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class AddonUpdaterWorkerTest {

Expand Down Expand Up @@ -77,10 +81,10 @@ class AddonUpdaterWorkerTest {
onFinishCaptor.value.invoke(AddonUpdater.Status.SuccessfullyUpdated)
}

runBlocking {
runTest {
doReturn(this).`when`(worker).attemptScope

val result = worker.startWork().await()
val result = worker.doWork()

assertEquals(ListenableWorker.Result.success(), result)
verify(worker).saveUpdateAttempt(addonId, AddonUpdater.Status.SuccessfullyUpdated)
Expand All @@ -102,8 +106,8 @@ class AddonUpdaterWorkerTest {
onFinishCaptor.value.invoke(AddonUpdater.Status.NoUpdateAvailable)
}

runBlocking {
val result = worker.startWork().await()
runTest {
val result = worker.doWork()

assertEquals(ListenableWorker.Result.success(), result)
}
Expand All @@ -124,8 +128,8 @@ class AddonUpdaterWorkerTest {
onFinishCaptor.value.invoke(AddonUpdater.Status.NotInstalled)
}

runBlocking {
val result = worker.startWork().await()
runTest {
val result = worker.doWork()

assertEquals(ListenableWorker.Result.failure(), result)
}
Expand All @@ -149,8 +153,8 @@ class AddonUpdaterWorkerTest {
onFinishCaptor.value.invoke(AddonUpdater.Status.Error("error", recoverableException))
}

runBlocking {
val result = worker.startWork().await()
runTest {
val result = worker.doWork()

assertEquals(ListenableWorker.Result.retry(), result)
updateAttemptStorage.saveOrUpdate(any())
Expand All @@ -175,8 +179,8 @@ class AddonUpdaterWorkerTest {
onFinishCaptor.value.invoke(AddonUpdater.Status.Error("error", unrecoverableException))
}

runBlocking {
val result = worker.startWork().await()
runTest {
val result = worker.doWork()

assertEquals(ListenableWorker.Result.success(), result)
updateAttemptStorage.saveOrUpdate(any())
Expand All @@ -203,8 +207,8 @@ class AddonUpdaterWorkerTest {
onFinishCaptor.value.invoke(AddonUpdater.Status.Error("error", Exception()))
}

runBlocking {
val result = worker.startWork().await()
runTest {
val result = worker.doWork()

assertEquals(ListenableWorker.Result.success(), result)
assertTrue(crashWasReported)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.ExperimentalCoroutinesApi
import kotlinx.coroutines.runBlocking
import kotlinx.coroutines.test.*
import mozilla.components.concept.engine.webextension.DisabledFlags
import mozilla.components.concept.engine.webextension.Metadata
import mozilla.components.concept.engine.webextension.WebExtension
Expand All @@ -31,10 +32,9 @@ import mozilla.components.feature.addons.update.DefaultAddonUpdater.Companion.WO
import mozilla.components.feature.addons.update.DefaultAddonUpdater.NotificationHandlerService
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.whenever
import org.junit.After
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.ArgumentMatchers
Expand All @@ -43,13 +43,10 @@ import org.mockito.Mockito.spy
import org.mockito.Mockito.verify
import java.util.concurrent.TimeUnit

@ExperimentalCoroutinesApi
@RunWith(AndroidJUnit4::class)
class DefaultAddonUpdaterTest {

@ExperimentalCoroutinesApi
@get:Rule
val coroutinesTestRule = MainCoroutineRule()

@Before
fun setUp() {
val configuration = Configuration.Builder().build()
Expand Down Expand Up @@ -267,15 +264,16 @@ class DefaultAddonUpdaterTest {
fun `unregisterForFutureUpdates - will remove scheduled work for future update`() {
val frequency = Frequency(1, TimeUnit.DAYS)
val updater = DefaultAddonUpdater(testContext, frequency)
updater.scope = CoroutineScope(Dispatchers.Main)
val testDispatcher = StandardTestDispatcher()
updater.scope = TestScope(testDispatcher)

val addonId = "addonId"

updater.updateAttempStorage = mock()

val workId = updater.getUniquePeriodicWorkName(addonId)

runBlocking {
runTest(testDispatcher) {
val workManger = WorkManager.getInstance(testContext)
var workData = workManger.getWorkInfosForUniqueWork(workId).await()

Expand All @@ -288,7 +286,7 @@ class DefaultAddonUpdaterTest {

assertExtensionIsRegisteredFoUpdates(updater, addonId)

updater.unregisterForFutureUpdates(addonId)
updater.unregisterForFutureUpdates(addonId).join()

workData = workManger.getWorkInfosForUniqueWork(workId).await()
assertEquals(WorkInfo.State.CANCELLED, workData.first().state)
Expand Down

0 comments on commit 338892c

Please sign in to comment.