[fenix] Bug 1809507 - Use base domain on the cookie banner handling panel.

pull/600/head
Arturo Mejia 1 year ago committed by mergify[bot]
parent cda0bed4f8
commit d7eba470d3

@ -8,6 +8,11 @@ import android.content.Context
import android.view.LayoutInflater
import android.view.ViewGroup
import androidx.annotation.VisibleForTesting
import androidx.core.net.toUri
import kotlinx.coroutines.CoroutineScope
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.launch
import kotlinx.coroutines.plus
import mozilla.components.lib.publicsuffixlist.PublicSuffixList
import mozilla.components.support.ktx.kotlin.toShortUrl
import org.mozilla.fenix.R
@ -24,6 +29,7 @@ import org.mozilla.fenix.trackingprotection.ProtectionsState
class CookieBannerHandlingDetailsView(
container: ViewGroup,
private val context: Context,
private val ioScope: CoroutineScope,
private val publicSuffixList: PublicSuffixList,
val interactor: CookieBannerDetailsInteractor,
) {
@ -45,14 +51,22 @@ class CookieBannerHandlingDetailsView(
@VisibleForTesting
internal fun bindTitle(url: String, isCookieBannerHandlingEnabled: Boolean) {
val stringID =
if (isCookieBannerHandlingEnabled) {
R.string.reduce_cookie_banner_details_panel_title_off_for_site
} else {
R.string.reduce_cookie_banner_details_panel_title_on_for_site
ioScope.launch {
val host = url.toUri().host.orEmpty()
val domain = publicSuffixList.getPublicSuffixPlusOne(host).await()
launch(Dispatchers.Main) {
val stringID =
if (isCookieBannerHandlingEnabled) {
R.string.reduce_cookie_banner_details_panel_title_off_for_site
} else {
R.string.reduce_cookie_banner_details_panel_title_on_for_site
}
val data = domain ?: url
val shortUrl = data.toShortUrl(publicSuffixList)
binding.title.text = context.getString(stringID, shortUrl)
}
val shortUrl = url.toShortUrl(publicSuffixList)
binding.title.text = context.getString(stringID, shortUrl)
}
}
@VisibleForTesting

@ -85,6 +85,7 @@ class CookieBannerPanelDialogFragment : FenixDialogFragment() {
cookieBannersView = CookieBannerHandlingDetailsView(
context = requireContext(),
ioScope = viewLifecycleOwner.lifecycleScope + Dispatchers.IO,
container = binding.cookieBannerDetailsInfoLayout,
publicSuffixList = requireComponents.publicSuffixList,
interactor = DefaultCookieBannerDetailsInteractor(controller),

@ -6,18 +6,23 @@ package org.mozilla.fenix.settings.quicksettings.protections.cookiebanners
import android.widget.FrameLayout
import io.mockk.MockKAnnotations
import io.mockk.coEvery
import io.mockk.impl.annotations.MockK
import io.mockk.mockk
import io.mockk.spyk
import io.mockk.verify
import kotlinx.coroutines.CompletableDeferred
import kotlinx.coroutines.test.advanceUntilIdle
import mozilla.components.browser.state.state.createTab
import mozilla.components.lib.publicsuffixlist.PublicSuffixList
import mozilla.components.support.ktx.kotlin.toShortUrl
import mozilla.components.support.test.robolectric.testContext
import mozilla.components.support.test.rule.MainCoroutineRule
import mozilla.components.support.test.rule.runTestOnMain
import org.junit.Assert.assertEquals
import org.junit.Assert.assertFalse
import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Rule
import org.junit.Test
import org.junit.runner.RunWith
import org.mozilla.fenix.R
@ -35,6 +40,10 @@ class CookieBannerHandlingDetailsViewTest {
@MockK(relaxed = true)
private lateinit var publicSuffixList: PublicSuffixList
@get:Rule
val coroutinesTestRule = MainCoroutineRule()
private val scope = coroutinesTestRule.scope
@Before
fun setup() {
MockKAnnotations.init(this)
@ -45,6 +54,7 @@ class CookieBannerHandlingDetailsViewTest {
context = testContext,
publicSuffixList = publicSuffixList,
interactor = interactor,
ioScope = scope,
),
)
binding = view.binding
@ -74,34 +84,42 @@ class CookieBannerHandlingDetailsViewTest {
}
@Test
fun `GIVEN cookie banner handling is enabled WHEN biding title THEN title view must have the expected string`() {
val websiteUrl = "https://mozilla.org"
fun `GIVEN cookie banner handling is enabled WHEN biding title THEN title view must have the expected string`() =
runTestOnMain {
coEvery { publicSuffixList.getPublicSuffixPlusOne(any()) } returns CompletableDeferred("mozilla.org")
view.bindTitle(url = websiteUrl, isCookieBannerHandlingEnabled = true)
val websiteUrl = "https://mozilla.org"
val expectedText =
testContext.getString(
R.string.reduce_cookie_banner_details_panel_title_off_for_site,
websiteUrl.toShortUrl(publicSuffixList),
)
view.bindTitle(url = websiteUrl, isCookieBannerHandlingEnabled = true)
assertEquals(expectedText, view.binding.title.text)
}
val expectedText =
testContext.getString(
R.string.reduce_cookie_banner_details_panel_title_off_for_site,
"mozilla.org",
)
assertEquals(expectedText, view.binding.title.text)
}
@Test
fun `GIVEN cookie banner handling is disabled WHEN biding title THEN title view must have the expected string`() {
val websiteUrl = "https://mozilla.org"
fun `GIVEN cookie banner handling is disabled WHEN biding title THEN title view must have the expected string`() =
runTestOnMain {
coEvery { publicSuffixList.getPublicSuffixPlusOne(any()) } returns CompletableDeferred("mozilla.org")
view.bindTitle(url = websiteUrl, isCookieBannerHandlingEnabled = false)
val websiteUrl = "https://mozilla.org"
val expectedText =
testContext.getString(
R.string.reduce_cookie_banner_details_panel_title_on_for_site,
websiteUrl.toShortUrl(publicSuffixList),
)
view.bindTitle(url = websiteUrl, isCookieBannerHandlingEnabled = false)
assertEquals(expectedText, view.binding.title.text)
}
advanceUntilIdle()
val expectedText =
testContext.getString(
R.string.reduce_cookie_banner_details_panel_title_on_for_site,
"mozilla.org",
)
assertEquals(expectedText, view.binding.title.text)
}
@Test
fun `WHEN clicking the back button THEN view must delegate to the interactor#onBackPressed()`() {

Loading…
Cancel
Save