Commit 14bbc298 authored by mimi89999's avatar mimi89999 Committed by Pier Angelo Vendrame
Browse files

Bug 1964251 - Replace logic of RegistrableDomain renderStyle in Android...

Bug 1964251 - Replace logic of RegistrableDomain renderStyle in Android toolbar component. r=android-reviewers,petru

Differential Revision: https://phabricator.services.mozilla.com/D251501
parent b849804d
Loading
Loading
Loading
Loading
+3 −5
Original line number Diff line number Diff line
@@ -76,8 +76,9 @@ internal class URLRenderer(
        toolbar.url = when (configuration.renderStyle) {
            // Display only the eTLD+1 (direct subdomain of the public suffix), uncolored
            ToolbarFeature.RenderStyle.RegistrableDomain -> {
                val host = url.toUri().host?.ifEmpty { null }
                host?.let { getRegistrableDomain(host, configuration) } ?: url
                getRegistrableDomainOrHostSpan(url, configuration.publicSuffixList)?.let { (start, end) ->
                    url.substring(start, end)
                } ?: url
            }
            // Display the registrableDomain with color and URL with another color
            ToolbarFeature.RenderStyle.ColoredUrl -> SpannableStringBuilder(url).apply {
@@ -97,9 +98,6 @@ internal class URLRenderer(
    }
}

private suspend fun getRegistrableDomain(host: String, configuration: ToolbarFeature.UrlRenderConfiguration) =
    configuration.publicSuffixList.getPublicSuffixPlusOne(host).await()

/**
 * Determines the position span of the registrable domain within a host string.
 *
+107 −0
Original line number Diff line number Diff line
@@ -161,6 +161,33 @@ class URLRendererTest {
        assertEquals(0, spans.size)
    }

    private suspend fun testRenderWithRegistrableDomain(
        testUrl: String,
        expectedUrl: String,
    ) {
        val configuration = ToolbarFeature.UrlRenderConfiguration(
            publicSuffixList = PublicSuffixList(testContext, Dispatchers.Unconfined),
            registrableDomainColor = Color.RED,
            urlColor = Color.GREEN,
            renderStyle = ToolbarFeature.RenderStyle.RegistrableDomain,
        )

        val toolbar: Toolbar = mock()

        val renderer = URLRenderer(toolbar, configuration)

        renderer.updateUrl(testUrl)

        val captor = argumentCaptor<CharSequence>()
        verify(toolbar).url = captor.capture()

        assertNotNull(captor.value)
        assertTrue(captor.value is String)
        val url = captor.value as String

        assertEquals(expectedUrl, url)
    }

    @Test
    fun `GIVEN a simple domain WHEN getting registrable domain span in host THEN span is returned`() {
        runTestOnMain {
@@ -402,6 +429,86 @@ class URLRendererTest {
            testRenderWithUncoloredUrl("content://media/external/file/1000000000")
        }
    }

    @Test
    fun `GIVEN a simple URL WHEN rendering it THEN registrable domain is set`() {
        runTestOnMain {
            testRenderWithRegistrableDomain(
                testUrl = "https://www.mozilla.org/",
                expectedUrl = "mozilla.org",
            )
        }
    }

    @Test
    fun `GIVEN a URL with a trailing period in the domain WHEN rendering it THEN registrable domain is set`() {
        runTestOnMain {
            testRenderWithRegistrableDomain(
                testUrl = "https://www.mozilla.org./",
                expectedUrl = "mozilla.org",
            )
        }
    }

    @Test
    fun `GIVEN a URL with a repeated domain WHEN rendering it THEN the last occurrence of domain is set`() {
        runTestOnMain {
            testRenderWithRegistrableDomain(
                testUrl = "https://mozilla.org.mozilla.org/",
                expectedUrl = "mozilla.org",
            )
        }
    }

    @Test
    fun `GIVEN a URL with an IPv4 address WHEN rendering it THEN the IP part is set`() {
        runTestOnMain {
            testRenderWithRegistrableDomain(
                testUrl = "http://127.0.0.1/",
                expectedUrl = "127.0.0.1",
            )
        }
    }

    @Test
    fun `GIVEN a URL with an IPv6 address WHEN rendering it THEN the IP part is set`() {
        runTestOnMain {
            testRenderWithRegistrableDomain(
                testUrl = "http://[::1]/",
                expectedUrl = "[::1]",
            )
        }
    }

    @Test
    fun `GIVEN a URL with a non PSL domain WHEN rendering it THEN host set`() {
        runTestOnMain {
            testRenderWithRegistrableDomain(
                testUrl = "http://localhost/",
                expectedUrl = "localhost",
            )
        }
    }

    @Test
    fun `GIVEN an internal page name WHEN rendering it THEN it is set`() {
        runTestOnMain {
            testRenderWithRegistrableDomain(
                testUrl = "about:mozilla",
                expectedUrl = "about:mozilla",
            )
        }
    }

    @Test
    fun `GIVEN a content URI WHEN rendering it THEN it is set`() {
        runTestOnMain {
            testRenderWithRegistrableDomain(
                testUrl = "content://media/external/file/1000000000",
                expectedUrl = "content://media/external/file/1000000000",
            )
        }
    }
}

/**