From 82701fd0f080590806cad5c9785203306083800e Mon Sep 17 00:00:00 2001 From: Alexandru2909 Date: Tue, 1 Nov 2022 17:55:58 +0200 Subject: [PATCH] [fenix] For https://github.com/mozilla-mobile/fenix/issues/26911 - Allow users to enable re-supported Fennec extensions --- .../addons/InstalledAddonDetailsFragment.kt | 32 ++++++++- .../mozilla/fenix/components/Components.kt | 10 +-- .../InstalledAddonDetailsFragmentTest.kt | 65 +++++++++++++++++++ 3 files changed, 95 insertions(+), 12 deletions(-) create mode 100644 app/src/test/java/org/mozilla/fenix/addons/InstalledAddonDetailsFragmentTest.kt diff --git a/app/src/main/java/org/mozilla/fenix/addons/InstalledAddonDetailsFragment.kt b/app/src/main/java/org/mozilla/fenix/addons/InstalledAddonDetailsFragment.kt index 7eb06ba7a..7c009bc30 100644 --- a/app/src/main/java/org/mozilla/fenix/addons/InstalledAddonDetailsFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/addons/InstalledAddonDetailsFragment.kt @@ -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() diff --git a/app/src/main/java/org/mozilla/fenix/components/Components.kt b/app/src/main/java/org/mozilla/fenix/components/Components.kt index 97944688c..1d48a4272 100644 --- a/app/src/main/java/org/mozilla/fenix/components/Components.kt +++ b/app/src/main/java/org/mozilla/fenix/components/Components.kt @@ -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() - }, ) } diff --git a/app/src/test/java/org/mozilla/fenix/addons/InstalledAddonDetailsFragmentTest.kt b/app/src/test/java/org/mozilla/fenix/addons/InstalledAddonDetailsFragmentTest.kt new file mode 100644 index 000000000..779c8b010 --- /dev/null +++ b/app/src/test/java/org/mozilla/fenix/addons/InstalledAddonDetailsFragmentTest.kt @@ -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() + + @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() + 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() + every { addon.isDisabledAsUnsupported() } returns true + every { addon.isSupported() } returns true + every { fragment.addon } returns addon + val capturedAddon = slot() + 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()) } + } +}