1
0
Fork 0

Move SearchProviderModel extension to class

master
Tiger Oakes 2020-07-02 14:57:22 -07:00 committed by Mihai Branescu
parent 836199ff16
commit 41452e945b
4 changed files with 54 additions and 39 deletions

View File

@ -1,29 +0,0 @@
/* 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.ext
import org.mozilla.fenix.search.telemetry.SearchProviderModel
fun SearchProviderModel.containsAds(urlList: List<String>): Boolean {
return urlList.containsAds(this.extraAdServersRegexps)
}
private fun String.isAd(adRegexps: List<String>): Boolean {
for (adsRegex in adRegexps) {
if (Regex(adsRegex).containsMatchIn(this)) {
return true
}
}
return false
}
private fun List<String>.containsAds(adRegexps: List<String>): Boolean {
for (url in this) {
if (url.isAd(adRegexps)) {
return true
}
}
return false
}

View File

@ -6,11 +6,56 @@ package org.mozilla.fenix.search.telemetry
data class SearchProviderModel(
val name: String,
val regexp: String,
val regexp: Regex,
val queryParam: String,
val codeParam: String = "",
val codePrefixes: List<String> = ArrayList(),
val followOnParams: List<String> = ArrayList(),
val extraAdServersRegexps: List<String> = ArrayList(),
val followOnCookies: List<SearchProviderCookie> = ArrayList()
)
val codeParam: String,
val codePrefixes: List<String>,
val followOnParams: List<String>,
val extraAdServersRegexps: List<Regex>,
val followOnCookies: List<SearchProviderCookie>
) {
constructor(
name: String,
regexp: String,
queryParam: String,
codeParam: String = "",
codePrefixes: List<String> = emptyList(),
followOnParams: List<String> = emptyList(),
extraAdServersRegexps: List<String> = emptyList(),
followOnCookies: List<SearchProviderCookie> = emptyList()
) : this(
name = name,
regexp = regexp.toRegex(),
queryParam = queryParam,
codeParam = codeParam,
codePrefixes = codePrefixes,
followOnParams = followOnParams,
extraAdServersRegexps = extraAdServersRegexps.map { it.toRegex() },
followOnCookies = followOnCookies
)
/**
* Checks if any of the given URLs represent an ad from the search engine.
* Used to check if a clicked link was for an ad.
*/
fun containsAds(urlList: List<String>) = urlList.containsAds(extraAdServersRegexps)
private fun String.isAd(adRegexps: List<Regex>): Boolean {
for (adsRegex in adRegexps) {
if (adsRegex.containsMatchIn(this)) {
return true
}
}
return false
}
private fun List<String>.containsAds(adRegexps: List<Regex>): Boolean {
for (url in this) {
if (url.isAd(adRegexps)) {
return true
}
}
return false
}
}

View File

@ -10,7 +10,6 @@ import mozilla.components.concept.engine.Engine
import org.json.JSONObject
import org.mozilla.fenix.components.metrics.Event
import org.mozilla.fenix.components.metrics.MetricController
import org.mozilla.fenix.ext.containsAds
import org.mozilla.fenix.search.telemetry.BaseSearchTelemetry
import org.mozilla.fenix.search.telemetry.ExtensionInfo

View File

@ -2,14 +2,14 @@
* 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.ext
package org.mozilla.fenix.search
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Test
import org.mozilla.fenix.search.telemetry.SearchProviderModel
class AdsTest {
class SearchProviderModelTest {
private val testSearchProvider =
SearchProviderModel(