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

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

@ -8,6 +8,11 @@ import android.content.Context
import android.view.LayoutInflater import android.view.LayoutInflater
import android.view.ViewGroup import android.view.ViewGroup
import androidx.annotation.VisibleForTesting 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.lib.publicsuffixlist.PublicSuffixList
import mozilla.components.support.ktx.kotlin.toShortUrl import mozilla.components.support.ktx.kotlin.toShortUrl
import org.mozilla.fenix.R import org.mozilla.fenix.R
@ -24,6 +29,7 @@ import org.mozilla.fenix.trackingprotection.ProtectionsState
class CookieBannerHandlingDetailsView( class CookieBannerHandlingDetailsView(
container: ViewGroup, container: ViewGroup,
private val context: Context, private val context: Context,
private val ioScope: CoroutineScope,
private val publicSuffixList: PublicSuffixList, private val publicSuffixList: PublicSuffixList,
val interactor: CookieBannerDetailsInteractor, val interactor: CookieBannerDetailsInteractor,
) { ) {
@ -45,14 +51,22 @@ class CookieBannerHandlingDetailsView(
@VisibleForTesting @VisibleForTesting
internal fun bindTitle(url: String, isCookieBannerHandlingEnabled: Boolean) { internal fun bindTitle(url: String, isCookieBannerHandlingEnabled: Boolean) {
val stringID = ioScope.launch {
if (isCookieBannerHandlingEnabled) { val host = url.toUri().host.orEmpty()
R.string.reduce_cookie_banner_details_panel_title_off_for_site val domain = publicSuffixList.getPublicSuffixPlusOne(host).await()
} else {
R.string.reduce_cookie_banner_details_panel_title_on_for_site 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 @VisibleForTesting

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

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

Loading…
Cancel
Save