2
0
mirror of https://github.com/fork-maintainers/iceraven-browser synced 2024-11-15 18:12:54 +00:00

For #26911 - Allow users to enable re-supported Fennec extensions

This commit is contained in:
Alexandru2909 2022-11-01 17:55:58 +02:00 committed by mergify[bot]
parent 90b8d339e1
commit 62a661ab97
3 changed files with 95 additions and 12 deletions

View File

@ -8,6 +8,7 @@ import android.os.Bundle
import android.view.LayoutInflater
import android.view.View
import android.view.ViewGroup
import androidx.annotation.VisibleForTesting
import androidx.core.view.isVisible
import androidx.fragment.app.Fragment
import androidx.lifecycle.lifecycleScope
@ -17,7 +18,9 @@ import androidx.navigation.fragment.findNavController
import com.google.android.material.switchmaterial.SwitchMaterial
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import mozilla.components.concept.engine.webextension.EnableSource
import mozilla.components.feature.addons.Addon
import mozilla.components.feature.addons.AddonManager
import mozilla.components.feature.addons.AddonManagerException
import mozilla.components.feature.addons.ui.translateName
import org.mozilla.fenix.HomeActivity
@ -32,7 +35,8 @@ import org.mozilla.fenix.ext.showToolbar
*/
@Suppress("LargeClass", "TooManyFunctions")
class InstalledAddonDetailsFragment : Fragment() {
private lateinit var addon: Addon
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal lateinit var addon: Addon
private var _binding: FragmentInstalledAddOnDetailsBinding? = null
private val binding get() = _binding!!
@ -124,8 +128,8 @@ class InstalledAddonDetailsFragment : Fragment() {
switch.isClickable = false
binding.removeAddOn.isEnabled = false
if (isChecked) {
addonManager.enableAddon(
addon,
enableAddon(
addonManager,
onSuccess = {
runIfFragmentIsAttached {
this.addon = it
@ -207,6 +211,28 @@ class InstalledAddonDetailsFragment : Fragment() {
}
}
@VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
internal fun enableAddon(
addonManager: AddonManager,
onSuccess: (Addon) -> Unit,
onError: (Throwable) -> Unit,
) {
// If the addon is migrated from Fennec and supported in Fenix, for the addon to be enabled,
// we need to also request the addon to be enabled as supported by the app
if (addon.isSupported() && addon.isDisabledAsUnsupported()) {
addonManager.enableAddon(
addon,
EnableSource.APP_SUPPORT,
{ enabledAddon ->
addonManager.enableAddon(enabledAddon, EnableSource.USER, onSuccess, onError)
},
onError,
)
} else {
addonManager.enableAddon(addon, EnableSource.USER, onSuccess, onError)
}
}
private fun bindSettings() {
binding.settings.apply {
isVisible = shouldSettingsBeVisible()

View File

@ -7,10 +7,8 @@ package org.mozilla.fenix.components
import android.annotation.SuppressLint
import android.app.Application
import android.content.Context
import android.content.Intent
import androidx.compose.runtime.Composable
import androidx.compose.ui.platform.LocalContext
import androidx.core.net.toUri
import com.google.android.play.core.review.ReviewManagerFactory
import mozilla.components.feature.addons.AddonManager
import mozilla.components.feature.addons.amo.AddonCollectionProvider
@ -22,7 +20,6 @@ import mozilla.components.support.base.worker.Frequency
import org.mozilla.fenix.BuildConfig
import org.mozilla.fenix.Config
import org.mozilla.fenix.FeatureFlags
import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R
import org.mozilla.fenix.autofill.AutofillConfirmActivity
import org.mozilla.fenix.autofill.AutofillSearchActivity
@ -33,8 +30,8 @@ import org.mozilla.fenix.ext.asRecentTabs
import org.mozilla.fenix.ext.components
import org.mozilla.fenix.ext.filterState
import org.mozilla.fenix.ext.settings
import org.mozilla.fenix.gleanplumb.state.MessagingMiddleware
import org.mozilla.fenix.ext.sort
import org.mozilla.fenix.gleanplumb.state.MessagingMiddleware
import org.mozilla.fenix.home.PocketUpdatesMiddleware
import org.mozilla.fenix.home.blocklist.BlocklistHandler
import org.mozilla.fenix.home.blocklist.BlocklistMiddleware
@ -142,11 +139,6 @@ class Components(private val context: Context) {
DefaultSupportedAddonsChecker(
context,
Frequency(12, TimeUnit.HOURS),
onNotificationClickIntent = Intent(context, HomeActivity::class.java).apply {
action = Intent.ACTION_VIEW
flags = Intent.FLAG_ACTIVITY_NEW_TASK or Intent.FLAG_ACTIVITY_CLEAR_TASK
data = "${BuildConfig.DEEP_LINK_SCHEME}://settings_addon_manager".toUri()
},
)
}

View File

@ -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 org.mozilla.fenix.addons
import io.mockk.Runs
import io.mockk.every
import io.mockk.just
import io.mockk.mockk
import io.mockk.slot
import io.mockk.spyk
import io.mockk.verify
import mozilla.components.concept.engine.webextension.EnableSource
import mozilla.components.feature.addons.Addon
import mozilla.components.feature.addons.AddonManager
import org.junit.Before
import org.junit.Test
class InstalledAddonDetailsFragmentTest {
private lateinit var fragment: InstalledAddonDetailsFragment
private val addonManager = mockk<AddonManager>()
@Before
fun setup() {
fragment = spyk(InstalledAddonDetailsFragment())
}
@Test
fun `GIVEN add-on is supported and not disabled WHEN enabling it THEN the add-on is requested by the user`() {
val addon = mockk<Addon>()
every { addon.isDisabledAsUnsupported() } returns false
every { addon.isSupported() } returns true
every { fragment.addon } returns addon
every { addonManager.enableAddon(any(), any(), any(), any()) } just Runs
fragment.enableAddon(addonManager, {}, {})
verify { addonManager.enableAddon(addon, EnableSource.USER, any(), any()) }
}
@Test
fun `GIVEN add-on is supported and disabled as previously unsupported WHEN enabling it THEN the add-on is requested by both the app and the user`() {
val addon = mockk<Addon>()
every { addon.isDisabledAsUnsupported() } returns true
every { addon.isSupported() } returns true
every { fragment.addon } returns addon
val capturedAddon = slot<Addon>()
val capturedOnSuccess = slot<(Addon) -> Unit>()
every {
addonManager.enableAddon(
capture(capturedAddon),
any(),
capture(capturedOnSuccess),
any(),
)
} answers { capturedOnSuccess.captured.invoke(capturedAddon.captured) }
fragment.enableAddon(addonManager, {}, {})
verify { addonManager.enableAddon(addon, EnableSource.APP_SUPPORT, any(), any()) }
verify { addonManager.enableAddon(capturedAddon.captured, EnableSource.USER, any(), any()) }
}
}