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 abd007e29b..a643d5866f 100644 --- a/app/src/main/java/org/mozilla/fenix/addons/InstalledAddonDetailsFragment.kt +++ b/app/src/main/java/org/mozilla/fenix/addons/InstalledAddonDetailsFragment.kt @@ -118,11 +118,24 @@ class InstalledAddonDetailsFragment : Fragment() { bindRemoveButton() } + @VisibleForTesting + internal fun provideEnableSwitch() = binding.enableSwitch + + @VisibleForTesting + internal fun providePrivateBrowsingSwitch() = binding.allowInPrivateBrowsingSwitch + + @VisibleForTesting @SuppressWarnings("LongMethod") - private fun bindEnableSwitch() { - val switch = binding.enableSwitch - val privateBrowsingSwitch = binding.allowInPrivateBrowsingSwitch + internal fun bindEnableSwitch() { + val switch = provideEnableSwitch() + val privateBrowsingSwitch = providePrivateBrowsingSwitch() switch.setState(addon.isEnabled()) + // When the ad-on is blocklisted, we do not want to enable the toggle switch + // because users shouldn't be able to re-enable a blocklisted add-on. + if (addon.isDisabledAsBlocklisted()) { + switch.isEnabled = false + return + } switch.setOnCheckedChangeListener { v, isChecked -> val addonManager = v.context.components.addonManager switch.isClickable = false diff --git a/app/src/test/java/org/mozilla/fenix/addons/InstalledAddonDetailsFragmentTest.kt b/app/src/test/java/org/mozilla/fenix/addons/InstalledAddonDetailsFragmentTest.kt index 779c8b010c..4eda7bc830 100644 --- a/app/src/test/java/org/mozilla/fenix/addons/InstalledAddonDetailsFragmentTest.kt +++ b/app/src/test/java/org/mozilla/fenix/addons/InstalledAddonDetailsFragmentTest.kt @@ -4,6 +4,7 @@ package org.mozilla.fenix.addons +import com.google.android.material.switchmaterial.SwitchMaterial import io.mockk.Runs import io.mockk.every import io.mockk.just @@ -62,4 +63,38 @@ class InstalledAddonDetailsFragmentTest { verify { addonManager.enableAddon(addon, EnableSource.APP_SUPPORT, any(), any()) } verify { addonManager.enableAddon(capturedAddon.captured, EnableSource.USER, any(), any()) } } + + @Test + fun `GIVEN blocklisted addon WHEN biding the enable switch THEN disable the switch`() { + val addon = mockk() + val enableSwitch = mockk(relaxed = true) + val privateBrowsingSwitch = mockk(relaxed = true) + + every { fragment.provideEnableSwitch() } returns enableSwitch + every { fragment.providePrivateBrowsingSwitch() } returns privateBrowsingSwitch + every { addon.isEnabled() } returns true + every { addon.isDisabledAsBlocklisted() } returns true + every { fragment.addon } returns addon + + fragment.bindEnableSwitch() + + verify { enableSwitch.isEnabled = false } + } + + @Test + fun `GIVEN enabled addon WHEN biding the enable switch THEN do not disable the switch`() { + val addon = mockk() + val enableSwitch = mockk(relaxed = true) + val privateBrowsingSwitch = mockk(relaxed = true) + + every { fragment.provideEnableSwitch() } returns enableSwitch + every { fragment.providePrivateBrowsingSwitch() } returns privateBrowsingSwitch + every { addon.isDisabledAsBlocklisted() } returns false + every { addon.isEnabled() } returns true + every { fragment.addon } returns addon + + fragment.bindEnableSwitch() + + verify(exactly = 0) { enableSwitch.isEnabled = false } + } }