From 78b6bf1d58ff0ec0f6061372982fbb9e1907f0d6 Mon Sep 17 00:00:00 2001 From: Arturo Mejia Date: Wed, 2 Dec 2020 17:00:30 -0500 Subject: [PATCH] Closes #9109 update tracking protection status when removing an exception by url. --- components/feature/session/build.gradle | 1 + .../session/TrackingProtectionUseCases.kt | 11 ++++ .../session/TrackingProtectionUseCasesTest.kt | 50 ++++++++++++++++++- docs/changelog.md | 3 ++ 4 files changed, 64 insertions(+), 1 deletion(-) diff --git a/components/feature/session/build.gradle b/components/feature/session/build.gradle index ac77c2bb75e..16138917d16 100644 --- a/components/feature/session/build.gradle +++ b/components/feature/session/build.gradle @@ -36,6 +36,7 @@ dependencies { implementation project(':support-ktx') implementation Dependencies.kotlin_stdlib + implementation Dependencies.androidx_core_ktx implementation Dependencies.google_material implementation Dependencies.androidx_swiperefreshlayout diff --git a/components/feature/session/src/main/java/mozilla/components/feature/session/TrackingProtectionUseCases.kt b/components/feature/session/src/main/java/mozilla/components/feature/session/TrackingProtectionUseCases.kt index f1071fc67c0..419a743d4c6 100644 --- a/components/feature/session/src/main/java/mozilla/components/feature/session/TrackingProtectionUseCases.kt +++ b/components/feature/session/src/main/java/mozilla/components/feature/session/TrackingProtectionUseCases.kt @@ -4,6 +4,8 @@ package mozilla.components.feature.session +import mozilla.components.browser.state.action.TrackingProtectionAction +import androidx.core.net.toUri import mozilla.components.browser.state.selector.findTabOrCustomTabOrSelectedTab import mozilla.components.browser.state.store.BrowserStore import mozilla.components.concept.engine.Engine @@ -71,6 +73,15 @@ class TrackingProtectionUseCases( */ operator fun invoke(exception: TrackingProtectionException) { engine.trackingProtectionExceptionStore.remove(exception) + // Find all tabs that need to update their tracking protection status. + val tabs = (store.state.tabs + store.state.customTabs).filter { tab -> + val tabDomain = tab.content.url.toUri().host + val exceptionDomain = exception.url.toUri().host + tabDomain == exceptionDomain + } + tabs.forEach { + store.dispatch(TrackingProtectionAction.ToggleExclusionListAction(it.id, false)) + } } } diff --git a/components/feature/session/src/test/java/mozilla/components/feature/session/TrackingProtectionUseCasesTest.kt b/components/feature/session/src/test/java/mozilla/components/feature/session/TrackingProtectionUseCasesTest.kt index a827ec0c112..238b8009fd2 100644 --- a/components/feature/session/src/test/java/mozilla/components/feature/session/TrackingProtectionUseCasesTest.kt +++ b/components/feature/session/src/test/java/mozilla/components/feature/session/TrackingProtectionUseCasesTest.kt @@ -4,11 +4,19 @@ package mozilla.components.feature.session +import androidx.test.ext.junit.runners.AndroidJUnit4 +import mozilla.components.browser.state.action.CustomTabListAction import mozilla.components.browser.state.action.EngineAction +import mozilla.components.browser.state.action.TabListAction +import mozilla.components.browser.state.selector.findCustomTab +import mozilla.components.browser.state.selector.findTab import mozilla.components.browser.state.state.BrowserState import mozilla.components.browser.state.state.ContentState import mozilla.components.browser.state.state.EngineState import mozilla.components.browser.state.state.TabSessionState +import mozilla.components.browser.state.state.TrackingProtectionState +import mozilla.components.browser.state.state.createCustomTab +import mozilla.components.browser.state.state.createTab import mozilla.components.browser.state.store.BrowserStore import mozilla.components.concept.engine.Engine import mozilla.components.concept.engine.EngineSession @@ -17,6 +25,7 @@ import mozilla.components.concept.engine.content.blocking.TrackingProtectionExce import mozilla.components.concept.engine.content.blocking.TrackingProtectionExceptionStorage import mozilla.components.support.test.any import mozilla.components.support.test.ext.joinBlocking +import mozilla.components.support.test.libstate.ext.waitUntilIdle import mozilla.components.support.test.mock import mozilla.components.support.test.whenever import org.junit.Assert.assertFalse @@ -25,9 +34,11 @@ import org.junit.Assert.assertNull import org.junit.Assert.assertTrue import org.junit.Before import org.junit.Test +import org.junit.runner.RunWith import org.mockito.Mockito.never import org.mockito.Mockito.verify +@RunWith(AndroidJUnit4::class) class TrackingProtectionUseCasesTest { private lateinit var exceptionStore: TrackingProtectionExceptionStorage private lateinit var engine: Engine @@ -146,10 +157,47 @@ class TrackingProtectionUseCasesTest { @Test fun `remove a tracking protection exception`() { - val exception: TrackingProtectionException = mock() + val tab1 = createTab("https://www.mozilla.org") + .copy(trackingProtection = TrackingProtectionState(ignoredOnTrackingProtection = true)) + + val tab2 = createTab("https://wiki.mozilla.org/") + .copy(trackingProtection = TrackingProtectionState(ignoredOnTrackingProtection = true)) + + val tab3 = createTab("https://www.mozilla.org/en-CA/") + .copy(trackingProtection = TrackingProtectionState(ignoredOnTrackingProtection = true)) + + val customTab = createCustomTab("https://www.mozilla.org/en-CA/") + .copy(trackingProtection = TrackingProtectionState(ignoredOnTrackingProtection = true)) + + val exception = object : TrackingProtectionException { + override val url: String = tab1.content.url + } + + store.dispatch(TabListAction.AddTabAction(tab1)).joinBlocking() + store.dispatch(TabListAction.AddTabAction(tab2)).joinBlocking() + store.dispatch(TabListAction.AddTabAction(tab3)).joinBlocking() + store.dispatch(CustomTabListAction.AddCustomTabAction(customTab)).joinBlocking() + store.waitUntilIdle() + + assertTrue(store.state.findTab(tab1.id)!!.trackingProtection.ignoredOnTrackingProtection) + assertTrue(store.state.findTab(tab2.id)!!.trackingProtection.ignoredOnTrackingProtection) + assertTrue(store.state.findTab(tab3.id)!!.trackingProtection.ignoredOnTrackingProtection) + assertTrue(store.state.findCustomTab(customTab.id)!!.trackingProtection.ignoredOnTrackingProtection) useCases.removeException(exception) + verify(exceptionStore).remove(exception) + + store.waitUntilIdle() + + assertFalse(store.state.findTab(tab1.id)!!.trackingProtection.ignoredOnTrackingProtection) + + // Different domain from tab1 MUST not be affected + assertTrue(store.state.findTab(tab2.id)!!.trackingProtection.ignoredOnTrackingProtection) + + // Another tabs with the same domain as tab1 MUST be updated + assertFalse(store.state.findTab(tab3.id)!!.trackingProtection.ignoredOnTrackingProtection) + assertFalse(store.state.findCustomTab(customTab.id)!!.trackingProtection.ignoredOnTrackingProtection) } @Test diff --git a/docs/changelog.md b/docs/changelog.md index b2bf3974bd2..ab2034e915d 100644 --- a/docs/changelog.md +++ b/docs/changelog.md @@ -21,6 +21,9 @@ permalink: /changelog/ * 🚒 Bug fixed [issue #9033](https://github.com/mozilla-mobile/android-components/issues/9033) - Fix resuming downloads in slow networks more details see the [Fenix issue](https://github.com/mozilla-mobile/fenix/issues/9354#issuecomment-731267368). * 🚒 Bug fixed [issue #9073](https://github.com/mozilla-mobile/android-components/issues/9073) - Fix crash downloading a file with multiple dots on it, for more details see the [Fenix issue](https://github.com/mozilla-mobile/fenix/issues/16443). +* **feature-session** + * 🚒 Bug fixed [issue #9109](https://github.com/mozilla-mobile/android-components/issues/9109) - Tracking protection shield not getting updated after deleting exception by url [Fenix issue](https://github.com/mozilla-mobile/fenix/issues/16670). + * **feature-app-links** * Added handling of PackageItemInfo.packageName NullPointerException on some Xiaomi and TCL devices