1
0
Fork 0

For #5281 - Use TrackerLog for reporting ETP categories (#5556)

master
Emily Kager 2019-09-25 10:48:06 -07:00 committed by Arturo Mejia
parent b6e6e36ccd
commit b0b60aa27d
7 changed files with 158 additions and 133 deletions

View File

@ -4,33 +4,39 @@
package org.mozilla.fenix.trackingprotection package org.mozilla.fenix.trackingprotection
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory.AD import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory.ANALYTICS
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory.CRYPTOMINING import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory.CRYPTOMINING
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory.FINGERPRINTING import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory.FINGERPRINTING
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory.SOCIAL import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory.MOZILLA_SOCIAL
import mozilla.components.concept.engine.content.blocking.Tracker import mozilla.components.concept.engine.content.blocking.Tracker
import mozilla.components.concept.engine.content.blocking.TrackerLog
import org.mozilla.fenix.ext.tryGetHostFromUrl import org.mozilla.fenix.ext.tryGetHostFromUrl
import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.CROSS_SITE_TRACKING_COOKIES
import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.CRYPTOMINERS import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.CRYPTOMINERS
import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.FINGERPRINTERS import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.FINGERPRINTERS
import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.SOCIAL_MEDIA_TRACKERS import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.SOCIAL_MEDIA_TRACKERS
import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.TRACKING_CONTENT import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.TRACKING_CONTENT
import java.util.EnumMap import java.util.EnumMap
typealias BucketMap = Map<TrackingProtectionCategory, List<String>>
/** /**
* Sorts [Tracker]s into different buckets and exposes them as a map. * Sorts [Tracker]s into different buckets and exposes them as a map.
*/ */
class TrackerBuckets { class TrackerBuckets {
private var trackers = emptyList<Tracker>() private var trackers = emptyList<TrackerLog>()
var buckets = emptyMap<TrackingProtectionCategory, List<String>>()
data class BucketedTrackerLog(var blockedBucketMap: BucketMap, var loadedBucketMap: BucketMap)
var buckets: BucketedTrackerLog = BucketedTrackerLog(emptyMap(), emptyMap())
private set private set
/** /**
* If [newTrackers] has changed since the last call, * If [newTrackers] has changed since the last call,
* update [buckets] based on the new trackers list. * update [buckets] based on the new tracker log list.
*/ */
fun updateIfNeeded(newTrackers: List<Tracker>) { fun updateIfNeeded(newTrackers: List<TrackerLog>) {
if (newTrackers != trackers) { if (newTrackers != trackers) {
trackers = newTrackers trackers = newTrackers
buckets = putTrackersInBuckets(newTrackers) buckets = putTrackersInBuckets(newTrackers)
@ -38,44 +44,75 @@ class TrackerBuckets {
} }
/** /**
* Returns true if there are no trackers. * Returns true if there are no trackers being blocked.
*/ */
fun isEmpty() = buckets.isEmpty() fun blockedIsEmpty() = buckets.blockedBucketMap.isEmpty()
/**
* Returns true if there are no trackers loaded.
*/
fun loadedIsEmpty() = buckets.loadedBucketMap.isEmpty()
/** /**
* Gets the tracker URLs for a given category. * Gets the tracker URLs for a given category.
*/ */
operator fun get(key: TrackingProtectionCategory) = buckets[key].orEmpty() operator fun get(key: TrackingProtectionCategory, blocked: Boolean) =
if (blocked) buckets.blockedBucketMap[key].orEmpty() else buckets.loadedBucketMap[key].orEmpty()
companion object { companion object {
@Suppress("ComplexMethod")
private fun putTrackersInBuckets( private fun putTrackersInBuckets(
list: List<Tracker> list: List<TrackerLog>
): Map<TrackingProtectionCategory, List<String>> { ): BucketedTrackerLog {
val map = EnumMap<TrackingProtectionCategory, List<String>>(TrackingProtectionCategory::class.java) val blockedMap =
EnumMap<TrackingProtectionCategory, List<String>>(TrackingProtectionCategory::class.java)
val loadedMap =
EnumMap<TrackingProtectionCategory, List<String>>(TrackingProtectionCategory::class.java)
for (item in list) { for (item in list) {
when { when {
CRYPTOMINING in item.trackingCategories -> { // Blocked categories
map[CRYPTOMINERS] = map[CRYPTOMINERS].orEmpty() + item.cookiesHasBeenBlocked -> {
blockedMap[CROSS_SITE_TRACKING_COOKIES] =
blockedMap[CROSS_SITE_TRACKING_COOKIES].orEmpty() + item.url.tryGetHostFromUrl()
}
CRYPTOMINING in item.blockedCategories -> {
blockedMap[CRYPTOMINERS] = blockedMap[CRYPTOMINERS].orEmpty() +
item.url.tryGetHostFromUrl() item.url.tryGetHostFromUrl()
} }
FINGERPRINTING in item.trackingCategories -> { FINGERPRINTING in item.blockedCategories -> {
map[FINGERPRINTERS] = map[FINGERPRINTERS].orEmpty() + blockedMap[FINGERPRINTERS] = blockedMap[FINGERPRINTERS].orEmpty() +
item.url.tryGetHostFromUrl() item.url.tryGetHostFromUrl()
} }
SOCIAL in item.trackingCategories -> { MOZILLA_SOCIAL in item.blockedCategories -> {
map[SOCIAL_MEDIA_TRACKERS] = map[SOCIAL_MEDIA_TRACKERS].orEmpty() + blockedMap[SOCIAL_MEDIA_TRACKERS] =
blockedMap[SOCIAL_MEDIA_TRACKERS].orEmpty() +
item.url.tryGetHostFromUrl()
}
TrackingCategory.SCRIPTS_AND_SUB_RESOURCES in item.blockedCategories -> {
blockedMap[TRACKING_CONTENT] = blockedMap[TRACKING_CONTENT].orEmpty() +
item.url.tryGetHostFromUrl() item.url.tryGetHostFromUrl()
} }
AD in item.trackingCategories || // Loaded categories
SOCIAL in item.trackingCategories || CRYPTOMINING in item.loadedCategories -> {
ANALYTICS in item.trackingCategories -> { loadedMap[CRYPTOMINERS] = loadedMap[CRYPTOMINERS].orEmpty() +
map[TRACKING_CONTENT] = map[TRACKING_CONTENT].orEmpty() + item.url.tryGetHostFromUrl()
}
FINGERPRINTING in item.loadedCategories -> {
loadedMap[FINGERPRINTERS] = loadedMap[FINGERPRINTERS].orEmpty() +
item.url.tryGetHostFromUrl()
}
MOZILLA_SOCIAL in item.loadedCategories -> {
loadedMap[SOCIAL_MEDIA_TRACKERS] =
loadedMap[SOCIAL_MEDIA_TRACKERS].orEmpty() +
item.url.tryGetHostFromUrl()
}
TrackingCategory.SCRIPTS_AND_SUB_RESOURCES in item.loadedCategories -> {
loadedMap[TRACKING_CONTENT] = loadedMap[TRACKING_CONTENT].orEmpty() +
item.url.tryGetHostFromUrl() item.url.tryGetHostFromUrl()
} }
} }
} }
return map return BucketedTrackerLog(blockedMap, loadedMap)
} }
} }
} }

View File

@ -24,8 +24,10 @@ import kotlinx.android.synthetic.main.fragment_tracking_protection.view.*
import kotlinx.coroutines.launch import kotlinx.coroutines.launch
import mozilla.components.browser.session.Session import mozilla.components.browser.session.Session
import mozilla.components.concept.engine.content.blocking.Tracker import mozilla.components.concept.engine.content.blocking.Tracker
import mozilla.components.feature.session.TrackingProtectionUseCases
import mozilla.components.lib.state.ext.observe import mozilla.components.lib.state.ext.observe
import mozilla.components.support.base.feature.BackHandler import mozilla.components.support.base.feature.BackHandler
import mozilla.components.support.base.log.logger.Logger
import org.mozilla.fenix.HomeActivity import org.mozilla.fenix.HomeActivity
import org.mozilla.fenix.R import org.mozilla.fenix.R
import org.mozilla.fenix.components.StoreProvider import org.mozilla.fenix.components.StoreProvider
@ -92,8 +94,7 @@ class TrackingProtectionPanelDialogFragment : AppCompatDialogFragment(), BackHan
session, session,
url, url,
trackingProtectionEnabled, trackingProtectionEnabled,
session?.trackersBlocked ?: listOf(), listOf(),
session?.trackersLoaded ?: listOf(),
TrackingProtectionState.Mode.Normal TrackingProtectionState.Mode.Normal
) )
) )
@ -105,6 +106,7 @@ class TrackingProtectionPanelDialogFragment : AppCompatDialogFragment(), BackHan
) )
trackingProtectionView = trackingProtectionView =
TrackingProtectionPanelView(view.fragment_tp, trackingProtectionInteractor) TrackingProtectionPanelView(view.fragment_tp, trackingProtectionInteractor)
updateTrackers()
return view return view
} }
@ -116,20 +118,33 @@ class TrackingProtectionPanelDialogFragment : AppCompatDialogFragment(), BackHan
} }
override fun onTrackerBlocked(session: Session, tracker: Tracker, all: List<Tracker>) { override fun onTrackerBlocked(session: Session, tracker: Tracker, all: List<Tracker>) {
trackingProtectionStore.dispatch( updateTrackers()
TrackingProtectionAction.TrackerListChange(all)
)
trackingProtectionStore.dispatch(
TrackingProtectionAction.TrackerLoadedListChange(session.trackersLoaded)
)
} }
override fun onTrackerLoaded(session: Session, tracker: Tracker, all: List<Tracker>) { override fun onTrackerLoaded(session: Session, tracker: Tracker, all: List<Tracker>) {
trackingProtectionStore.dispatch( updateTrackers()
TrackingProtectionAction.TrackerListChange(session.trackersBlocked) }
}
private fun updateTrackers() {
context?.let { context ->
val session =
context.components.core.sessionManager.findSessionById(sessionId) ?: return
val useCase = TrackingProtectionUseCases(
sessionManager = context.components.core.sessionManager,
engine = context.components.core.engine
) )
trackingProtectionStore.dispatch(
TrackingProtectionAction.TrackerLoadedListChange(all) useCase.fetchTrackingLogs(
session,
onSuccess = {
trackingProtectionStore.dispatch(
TrackingProtectionAction.TrackerLogChange(it)
)
},
onError = {
Logger.error("TrackingProtectionUseCases - fetchTrackingLogs onError", it)
}
) )
} }
} }

View File

@ -68,7 +68,6 @@ class TrackingProtectionPanelView(
private var mode: TrackingProtectionState.Mode = TrackingProtectionState.Mode.Normal private var mode: TrackingProtectionState.Mode = TrackingProtectionState.Mode.Normal
private var bucketedTrackers = TrackerBuckets() private var bucketedTrackers = TrackerBuckets()
private var bucketedLoadedTrackers = TrackerBuckets()
fun update(state: TrackingProtectionState) { fun update(state: TrackingProtectionState) {
if (state.mode != mode) { if (state.mode != mode) {
@ -76,7 +75,6 @@ class TrackingProtectionPanelView(
} }
bucketedTrackers.updateIfNeeded(state.listTrackers) bucketedTrackers.updateIfNeeded(state.listTrackers)
bucketedLoadedTrackers.updateIfNeeded(state.listTrackersLoaded)
when (val mode = state.mode) { when (val mode = state.mode) {
is TrackingProtectionState.Mode.Normal -> setUIForNormalMode(state) is TrackingProtectionState.Mode.Normal -> setUIForNormalMode(state)
@ -92,32 +90,31 @@ class TrackingProtectionPanelView(
normal_mode.visibility = View.VISIBLE normal_mode.visibility = View.VISIBLE
protection_settings.isGone = state.session?.customTabConfig != null protection_settings.isGone = state.session?.customTabConfig != null
not_blocking_header.isGone = bucketedLoadedTrackers.isEmpty() not_blocking_header.isGone = bucketedTrackers.loadedIsEmpty()
bindUrl(state.url) bindUrl(state.url)
bindTrackingProtectionInfo(state.isTrackingProtectionEnabled) bindTrackingProtectionInfo(state.isTrackingProtectionEnabled)
protection_settings.setOnClickListener { protection_settings.setOnClickListener {
interactor.selectTrackingProtectionSettings() interactor.selectTrackingProtectionSettings()
} }
blocking_header.isGone = bucketedTrackers.isEmpty() blocking_header.isGone = bucketedTrackers.blockedIsEmpty()
updateCategoryVisibility() updateCategoryVisibility()
setCategoryClickListeners() setCategoryClickListeners()
} }
private fun updateCategoryVisibility() { private fun updateCategoryVisibility() {
cross_site_tracking.isGone = bucketedTrackers[CROSS_SITE_TRACKING_COOKIES].isEmpty() cross_site_tracking.isGone =
social_media_trackers.isGone = bucketedTrackers[SOCIAL_MEDIA_TRACKERS].isEmpty() bucketedTrackers.get(CROSS_SITE_TRACKING_COOKIES, true).isEmpty()
fingerprinters.isGone = bucketedTrackers[FINGERPRINTERS].isEmpty() social_media_trackers.isGone = bucketedTrackers.get(SOCIAL_MEDIA_TRACKERS, true).isEmpty()
tracking_content.isGone = bucketedTrackers[TRACKING_CONTENT].isEmpty() fingerprinters.isGone = bucketedTrackers.get(FINGERPRINTERS, true).isEmpty()
cryptominers.isGone = bucketedTrackers[CRYPTOMINERS].isEmpty() tracking_content.isGone = bucketedTrackers.get(TRACKING_CONTENT, true).isEmpty()
cryptominers.isGone = bucketedTrackers.get(CRYPTOMINERS, true).isEmpty()
cross_site_tracking_loaded.isGone =
bucketedLoadedTrackers[CROSS_SITE_TRACKING_COOKIES].isEmpty()
social_media_trackers_loaded.isGone = social_media_trackers_loaded.isGone =
bucketedLoadedTrackers[SOCIAL_MEDIA_TRACKERS].isEmpty() bucketedTrackers.get(SOCIAL_MEDIA_TRACKERS, false).isEmpty()
fingerprinters_loaded.isGone = bucketedLoadedTrackers[FINGERPRINTERS].isEmpty() fingerprinters_loaded.isGone = bucketedTrackers.get(FINGERPRINTERS, false).isEmpty()
tracking_content_loaded.isGone = bucketedLoadedTrackers[TRACKING_CONTENT].isEmpty() tracking_content_loaded.isGone = bucketedTrackers.get(TRACKING_CONTENT, false).isEmpty()
cryptominers_loaded.isGone = bucketedLoadedTrackers[CRYPTOMINERS].isEmpty() cryptominers_loaded.isGone = bucketedTrackers.get(CRYPTOMINERS, false).isEmpty()
} }
private fun setCategoryClickListeners() { private fun setCategoryClickListeners() {
@ -128,7 +125,6 @@ class TrackingProtectionPanelView(
cryptominers.setOnClickListener(this) cryptominers.setOnClickListener(this)
social_media_trackers_loaded.setOnClickListener(this) social_media_trackers_loaded.setOnClickListener(this)
fingerprinters_loaded.setOnClickListener(this) fingerprinters_loaded.setOnClickListener(this)
cross_site_tracking_loaded.setOnClickListener(this)
tracking_content_loaded.setOnClickListener(this) tracking_content_loaded.setOnClickListener(this)
cryptominers_loaded.setOnClickListener(this) cryptominers_loaded.setOnClickListener(this)
} }
@ -148,7 +144,7 @@ class TrackingProtectionPanelView(
normal_mode.visibility = View.GONE normal_mode.visibility = View.GONE
details_mode.visibility = View.VISIBLE details_mode.visibility = View.VISIBLE
category_title.text = context.getString(category.title) category_title.text = context.getString(category.title)
blocking_text_list.text = bucketedTrackers[category].joinToString("\n") blocking_text_list.text = bucketedTrackers.get(category, categoryBlocked).joinToString("\n")
category_description.text = context.getString(category.description) category_description.text = context.getString(category.description)
details_blocking_header.text = context.getString( details_blocking_header.text = context.getString(
if (categoryBlocked) { if (categoryBlocked) {
@ -195,7 +191,7 @@ class TrackingProtectionPanelView(
private fun getCategory(v: View) = when (v.id) { private fun getCategory(v: View) = when (v.id) {
R.id.social_media_trackers, R.id.social_media_trackers_loaded -> SOCIAL_MEDIA_TRACKERS R.id.social_media_trackers, R.id.social_media_trackers_loaded -> SOCIAL_MEDIA_TRACKERS
R.id.fingerprinters, R.id.fingerprinters_loaded -> FINGERPRINTERS R.id.fingerprinters, R.id.fingerprinters_loaded -> FINGERPRINTERS
R.id.cross_site_tracking, R.id.cross_site_tracking_loaded -> CROSS_SITE_TRACKING_COOKIES R.id.cross_site_tracking -> CROSS_SITE_TRACKING_COOKIES
R.id.tracking_content, R.id.tracking_content_loaded -> TRACKING_CONTENT R.id.tracking_content, R.id.tracking_content_loaded -> TRACKING_CONTENT
R.id.cryptominers, R.id.cryptominers_loaded -> CRYPTOMINERS R.id.cryptominers, R.id.cryptominers_loaded -> CRYPTOMINERS
else -> null else -> null
@ -207,7 +203,6 @@ class TrackingProtectionPanelView(
private fun isLoaded(v: View) = when (v.id) { private fun isLoaded(v: View) = when (v.id) {
R.id.social_media_trackers_loaded, R.id.social_media_trackers_loaded,
R.id.fingerprinters_loaded, R.id.fingerprinters_loaded,
R.id.cross_site_tracking_loaded,
R.id.tracking_content_loaded, R.id.tracking_content_loaded,
R.id.cryptominers_loaded -> true R.id.cryptominers_loaded -> true

View File

@ -5,7 +5,7 @@
package org.mozilla.fenix.trackingprotection package org.mozilla.fenix.trackingprotection
import mozilla.components.browser.session.Session import mozilla.components.browser.session.Session
import mozilla.components.concept.engine.content.blocking.Tracker import mozilla.components.concept.engine.content.blocking.TrackerLog
import mozilla.components.lib.state.Action import mozilla.components.lib.state.Action
import mozilla.components.lib.state.State import mozilla.components.lib.state.State
import mozilla.components.lib.state.Store import mozilla.components.lib.state.Store
@ -27,16 +27,12 @@ sealed class TrackingProtectionAction : Action {
data class Change( data class Change(
val url: String, val url: String,
val isTrackingProtectionEnabled: Boolean, val isTrackingProtectionEnabled: Boolean,
val listTrackers: List<Tracker>, val listTrackers: List<TrackerLog>,
val listTrackersLoaded: List<Tracker>,
val mode: TrackingProtectionState.Mode val mode: TrackingProtectionState.Mode
) : TrackingProtectionAction() ) : TrackingProtectionAction()
data class UrlChange(val url: String) : TrackingProtectionAction() data class UrlChange(val url: String) : TrackingProtectionAction()
data class TrackerListChange(val listTrackers: List<Tracker>) : TrackingProtectionAction() data class TrackerLogChange(val listTrackers: List<TrackerLog>) : TrackingProtectionAction()
data class TrackerLoadedListChange(val listTrackersLoaded: List<Tracker>) :
TrackingProtectionAction()
data class TrackerBlockingChanged(val isTrackingProtectionEnabled: Boolean) : data class TrackerBlockingChanged(val isTrackingProtectionEnabled: Boolean) :
TrackingProtectionAction() TrackingProtectionAction()
@ -52,16 +48,14 @@ sealed class TrackingProtectionAction : Action {
* The state for the Tracking Protection Panel * The state for the Tracking Protection Panel
* @property url Current URL to display * @property url Current URL to display
* @property isTrackingProtectionEnabled Current status of tracking protection for this session (ie is an exception) * @property isTrackingProtectionEnabled Current status of tracking protection for this session (ie is an exception)
* @property listTrackers List of currently blocked Trackers * @property listTrackers Current Tracker Log list of blocked and loaded tracker categories
* @property listTrackersLoaded List of currently not blocked Trackers
* @property mode Current Mode of TrackingProtection * @property mode Current Mode of TrackingProtection
*/ */
data class TrackingProtectionState( data class TrackingProtectionState(
val session: Session?, val session: Session?,
val url: String, val url: String,
val isTrackingProtectionEnabled: Boolean, val isTrackingProtectionEnabled: Boolean,
val listTrackers: List<Tracker>, val listTrackers: List<TrackerLog>,
val listTrackersLoaded: List<Tracker>,
val mode: Mode val mode: Mode
) : State { ) : State {
sealed class Mode { sealed class Mode {
@ -110,12 +104,7 @@ fun trackingProtectionStateReducer(
is TrackingProtectionAction.UrlChange -> state.copy( is TrackingProtectionAction.UrlChange -> state.copy(
url = action.url url = action.url
) )
is TrackingProtectionAction.TrackerListChange -> state.copy( is TrackingProtectionAction.TrackerLogChange -> state.copy(listTrackers = action.listTrackers)
listTrackers = action.listTrackers
)
is TrackingProtectionAction.TrackerLoadedListChange -> state.copy(
listTrackersLoaded = action.listTrackersLoaded
)
TrackingProtectionAction.ExitDetailsMode -> state.copy( TrackingProtectionAction.ExitDetailsMode -> state.copy(
mode = TrackingProtectionState.Mode.Normal mode = TrackingProtectionState.Mode.Normal
) )

View File

@ -111,16 +111,6 @@
app:layout_constraintStart_toStartOf="parent" app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@id/tracking_content" /> app:layout_constraintTop_toBottomOf="@id/tracking_content" />
<TextView
android:id="@+id/cross_site_tracking_loaded"
style="@style/QuickSettingsText.Icon"
android:layout_width="match_parent"
android:layout_height="@dimen/tracking_protection_item_height"
android:drawableStart="@drawable/ic_cookies"
android:text="@string/etp_cookies_title"
android:visibility="gone"
app:layout_constraintTop_toBottomOf="@id/not_blocking_header" />
<TextView <TextView
android:id="@+id/fingerprinters_loaded" android:id="@+id/fingerprinters_loaded"
style="@style/QuickSettingsText.Icon" style="@style/QuickSettingsText.Icon"
@ -129,7 +119,7 @@
android:drawableStart="@drawable/ic_fingerprinters" android:drawableStart="@drawable/ic_fingerprinters"
android:text="@string/etp_fingerprinters_title" android:text="@string/etp_fingerprinters_title"
android:visibility="gone" android:visibility="gone"
app:layout_constraintTop_toBottomOf="@id/cross_site_tracking_loaded" /> app:layout_constraintTop_toBottomOf="@id/not_blocking_header" />
<TextView <TextView
android:id="@+id/cryptominers_loaded" android:id="@+id/cryptominers_loaded"

View File

@ -4,50 +4,70 @@
package org.mozilla.fenix.trackingprotection package org.mozilla.fenix.trackingprotection
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory.AD
import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory.FINGERPRINTING import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory.FINGERPRINTING
import mozilla.components.concept.engine.content.blocking.Tracker import mozilla.components.concept.engine.EngineSession.TrackingProtectionPolicy.TrackingCategory.MOZILLA_SOCIAL
import mozilla.components.concept.engine.content.blocking.TrackerLog
import org.junit.Assert.assertEquals import org.junit.Assert.assertEquals
import org.junit.Assert.assertTrue import org.junit.Assert.assertTrue
import org.junit.Test import org.junit.Test
import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.CRYPTOMINERS import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.CRYPTOMINERS
import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.FINGERPRINTERS import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.FINGERPRINTERS
import org.mozilla.fenix.trackingprotection.TrackingProtectionCategory.TRACKING_CONTENT
class TrackerBucketsTest { class TrackerBucketsTest {
@Test @Test
fun `initializes with empty map`() { fun `initializes with empty map`() {
assertTrue(TrackerBuckets().isEmpty()) assertTrue(TrackerBuckets().buckets.blockedBucketMap.isEmpty())
assertTrue(TrackerBuckets().buckets.isEmpty()) assertTrue(TrackerBuckets().buckets.loadedBucketMap.isEmpty())
} }
@Test @Test
fun `getter accesses corresponding bucket`() { fun `getter accesses corresponding bucket`() {
val buckets = TrackerBuckets() val buckets = TrackerBuckets()
buckets.updateIfNeeded(listOf( buckets.updateIfNeeded(
Tracker("http://facebook.com", listOf(FINGERPRINTING, AD)), listOf(
Tracker("https://google.com", listOf(AD)), TrackerLog(
Tracker("https://mozilla.com") "http://facebook.com",
)) listOf(MOZILLA_SOCIAL)
),
TrackerLog("https://google.com", listOf(), listOf(FINGERPRINTING)),
TrackerLog("https://mozilla.com")
)
)
assertEquals(listOf("google.com"), buckets[TRACKING_CONTENT]) assertEquals(listOf("google.com"), buckets.buckets.blockedBucketMap[FINGERPRINTERS])
assertEquals(listOf("facebook.com"), buckets[FINGERPRINTERS]) assertEquals(
assertEquals(emptyList<String>(), buckets[CRYPTOMINERS]) listOf("facebook.com"),
buckets.buckets.loadedBucketMap[TrackingProtectionCategory.SOCIAL_MEDIA_TRACKERS]
)
assertTrue(buckets.buckets.blockedBucketMap[CRYPTOMINERS].isNullOrEmpty())
assertTrue(buckets.buckets.loadedBucketMap[CRYPTOMINERS].isNullOrEmpty())
} }
@Test @Test
fun `sorts trackers into bucket`() { fun `sorts trackers into bucket`() {
val buckets = TrackerBuckets() val buckets = TrackerBuckets()
buckets.updateIfNeeded(listOf( buckets.updateIfNeeded(
Tracker("http://facebook.com", listOf(FINGERPRINTING, AD)), listOf(
Tracker("https://google.com", listOf(AD)), TrackerLog(
Tracker("https://mozilla.com") "http://facebook.com",
)) listOf(MOZILLA_SOCIAL)
),
TrackerLog("https://google.com", listOf(), listOf(FINGERPRINTING)),
TrackerLog("https://mozilla.com")
)
)
assertEquals(mapOf( assertEquals(
TRACKING_CONTENT to listOf("google.com"), mapOf(
FINGERPRINTERS to listOf("facebook.com") TrackingProtectionCategory.SOCIAL_MEDIA_TRACKERS to listOf("facebook.com")
), buckets.buckets) ), buckets.buckets.loadedBucketMap
)
assertEquals(
mapOf(
FINGERPRINTERS to listOf("google.com")
), buckets.buckets.blockedBucketMap
)
} }
} }

View File

@ -5,11 +5,11 @@
package org.mozilla.fenix.trackingprotection package org.mozilla.fenix.trackingprotection
import io.mockk.mockk import io.mockk.mockk
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotSame
import kotlinx.coroutines.runBlocking import kotlinx.coroutines.runBlocking
import mozilla.components.browser.session.Session import mozilla.components.browser.session.Session
import mozilla.components.concept.engine.content.blocking.Tracker import mozilla.components.concept.engine.content.blocking.TrackerLog
import org.junit.Assert.assertEquals
import org.junit.Assert.assertNotSame
import org.junit.Test import org.junit.Test
class TrackingProtectionStoreTest { class TrackingProtectionStoreTest {
@ -69,9 +69,9 @@ class TrackingProtectionStoreTest {
fun trackerListChanged() = runBlocking { fun trackerListChanged() = runBlocking {
val initialState = defaultState() val initialState = defaultState()
val store = TrackingProtectionStore(initialState) val store = TrackingProtectionStore(initialState)
val tracker = Tracker("url", listOf()) val tracker = TrackerLog("url", listOf())
store.dispatch(TrackingProtectionAction.TrackerListChange(listOf(tracker))).join() store.dispatch(TrackingProtectionAction.TrackerLogChange(listOf(tracker))).join()
assertNotSame(initialState, store.state) assertNotSame(initialState, store.state)
assertEquals( assertEquals(
listOf(tracker), listOf(tracker),
@ -79,20 +79,6 @@ class TrackingProtectionStoreTest {
) )
} }
@Test
fun trackerLoadedListChanged() = runBlocking {
val initialState = defaultState()
val store = TrackingProtectionStore(initialState)
val tracker = Tracker("url", listOf())
store.dispatch(TrackingProtectionAction.TrackerLoadedListChange(listOf(tracker))).join()
assertNotSame(initialState, store.state)
assertEquals(
listOf(tracker),
store.state.listTrackersLoaded
)
}
@Test @Test
fun urlChanged() = runBlocking { fun urlChanged() = runBlocking {
val initialState = defaultState() val initialState = defaultState()
@ -110,14 +96,13 @@ class TrackingProtectionStoreTest {
fun onChange() = runBlocking { fun onChange() = runBlocking {
val initialState = defaultState() val initialState = defaultState()
val store = TrackingProtectionStore(initialState) val store = TrackingProtectionStore(initialState)
val tracker = Tracker("url", listOf()) val tracker = TrackerLog("url", listOf(), listOf(), cookiesHasBeenBlocked = false)
store.dispatch( store.dispatch(
TrackingProtectionAction.Change( TrackingProtectionAction.Change(
"newURL", "newURL",
false, false,
listOf(tracker), listOf(tracker),
listOf(),
TrackingProtectionState.Mode.Details( TrackingProtectionState.Mode.Details(
TrackingProtectionCategory.FINGERPRINTERS, TrackingProtectionCategory.FINGERPRINTERS,
true true
@ -133,10 +118,6 @@ class TrackingProtectionStoreTest {
false, false,
store.state.isTrackingProtectionEnabled store.state.isTrackingProtectionEnabled
) )
assertEquals(
listOf<Tracker>(),
store.state.listTrackersLoaded
)
assertEquals( assertEquals(
listOf(tracker), listOf(tracker),
store.state.listTrackers store.state.listTrackers
@ -152,7 +133,6 @@ class TrackingProtectionStoreTest {
url = "www.mozilla.org", url = "www.mozilla.org",
isTrackingProtectionEnabled = true, isTrackingProtectionEnabled = true,
listTrackers = listOf(), listTrackers = listOf(),
listTrackersLoaded = listOf(),
mode = TrackingProtectionState.Mode.Normal mode = TrackingProtectionState.Mode.Normal
) )
@ -161,7 +141,6 @@ class TrackingProtectionStoreTest {
url = "www.mozilla.org", url = "www.mozilla.org",
isTrackingProtectionEnabled = true, isTrackingProtectionEnabled = true,
listTrackers = listOf(), listTrackers = listOf(),
listTrackersLoaded = listOf(),
mode = TrackingProtectionState.Mode.Details(TrackingProtectionCategory.CRYPTOMINERS, true) mode = TrackingProtectionState.Mode.Details(TrackingProtectionCategory.CRYPTOMINERS, true)
) )
} }