Skip to content

Commit

Permalink
Bug 1861415 - Filter out sponsored URLs from other suggestion provide…
Browse files Browse the repository at this point in the history
…rs in Fenix.

This commit:

* Adds an `AwesomeBarView.SearchResultFilter` type, with variants to
  only include results for the current search engine (`CurrentEngine`)
  and exclude results with a matching query parameter
  (`ExcludeSponsored`).
* Adds a `String.urlContainsQueryParameters` extension function for
  using the `ExcludeSponsored` filter with the synced tabs provider.
* Refactors the `AwesomeBarView.get*Provider` methods to take an
  optional `SearchResultFilter`.
  • Loading branch information
linabutler authored and mergify[bot] committed Nov 3, 2023
1 parent 8dcfad5 commit 4fd5f8f
Show file tree
Hide file tree
Showing 4 changed files with 560 additions and 163 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -234,6 +234,15 @@ fun String.tryGetHostFromUrl(): String = try {
this
}

/**
* Returns `true` if this string is a valid URL that contains [searchParameters] in its query parameters.
*/
fun String.urlContainsQueryParameters(searchParameters: String): Boolean = try {
URL(this).query?.split("&")?.any { it == searchParameters } ?: false
} catch (e: MalformedURLException) {
false
}

/**
* Compares 2 URLs and returns true if they have the same origin,
* which means: same protocol, same host, same port.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -568,6 +568,25 @@ class StringTest {
assertNull(invalidBase64String2.extractBase6RawString())
}

@Test
fun `GIVEN a URL with matching parameters WHEN testing if a URL contains query parameters THEN the result is true`() {
assertTrue("http://example.com?a".urlContainsQueryParameters("a"))
assertTrue("http://example.com?a&b&c".urlContainsQueryParameters("b"))
assertTrue("http://example.com?a=b".urlContainsQueryParameters("a=b"))
assertTrue("http://example.com?a=b&c=d&e=f".urlContainsQueryParameters("c=d"))
assertTrue("http://example.com?a=b&c=d&e=f#g=h".urlContainsQueryParameters("e=f"))
}

@Test
fun `GIVEN a URL without matching parameters WHEN testing if a URL contains query parameters THEN the result is false`() {
assertFalse("".urlContainsQueryParameters("a"))
assertFalse("!@#$%^&*()-+".urlContainsQueryParameters("a"))
assertFalse("http://example.com".urlContainsQueryParameters("a"))
assertFalse("http://example.com?a&b".urlContainsQueryParameters("c"))
assertFalse("http://example.com?a=b".urlContainsQueryParameters("a"))
assertFalse("http://example.com?a=b&c=d&e=f#g=h".urlContainsQueryParameters("g=h"))
}

private infix fun String.shortenedShouldBecome(expect: String) {
assertEquals(expect, this.shortened())
}
Expand Down
Loading

0 comments on commit 4fd5f8f

Please sign in to comment.