Commit 269135d5 authored by mimi89999's avatar mimi89999 Committed by Pier Angelo Vendrame
Browse files

Bug 1961757 - Set text direction in Android toolbar instead of adding...

Bug 1961757 - Set text direction in Android toolbar instead of adding directional marks. r=tthibaud,android-reviewers,petru

Differential Revision: https://phabricator.services.mozilla.com/D246181
parent 6ed375a7
Loading
Loading
Loading
Loading
+3 −0
Original line number Diff line number Diff line
@@ -48,6 +48,9 @@ internal class OriginView @JvmOverloads constructor(
        isClickable = true
        isFocusable = true

        textDirection = View.TEXT_DIRECTION_LTR
        layoutDirection = View.LAYOUT_DIRECTION_LTR

        setOnClickListener {
            if (onUrlClicked()) {
                toolbar.editMode()
+23 −0
Original line number Diff line number Diff line
/* 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 mozilla.components.browser.toolbar.display

import android.view.View
import androidx.test.ext.junit.runners.AndroidJUnit4
import mozilla.components.support.test.robolectric.testContext
import org.junit.Assert.assertEquals
import org.junit.Test
import org.junit.runner.RunWith

@RunWith(AndroidJUnit4::class)
class OriginViewTest {
    @Test
    fun `URL text direction is always LTR`() {
        val originView = OriginView(testContext)
        originView.url = "ختار.ار/www.mozilla.org/1"
        assertEquals(View.TEXT_DIRECTION_LTR, originView.urlView.textDirection)
        assertEquals(View.LAYOUT_DIRECTION_LTR, originView.urlView.layoutDirection)
    }
}
+2 −20
Original line number Diff line number Diff line
@@ -7,8 +7,6 @@ package mozilla.components.support.ktx.util
import android.text.TextUtils
import androidx.annotation.VisibleForTesting
import androidx.core.net.toUri
import androidx.core.text.TextDirectionHeuristicCompat
import androidx.core.text.TextDirectionHeuristicsCompat
import java.util.regex.Pattern

object URLStringUtils {
@@ -102,25 +100,9 @@ object URLStringUtils {
    /**
     * Generates a shorter version of the provided URL for display purposes by stripping it of
     * https/http and/or WWW prefixes and/or trailing slash when applicable.
     *
     * The returned text will always be displayed from left to right.
     * If the directionality would otherwise be RTL "\u200E" will be prepended to the result to force LTR.
     */
    fun toDisplayUrl(
        originalUrl: CharSequence,
        textDirectionHeuristic: TextDirectionHeuristicCompat = TextDirectionHeuristicsCompat.FIRSTSTRONG_LTR,
    ): CharSequence {
        val strippedText = maybeStripTrailingSlash(maybeStripUrlProtocol(originalUrl))

        return if (
            strippedText.isNotBlank() &&
            textDirectionHeuristic.isRtl(strippedText, 0, 1)
        ) {
            "\u200E" + strippedText
        } else {
            strippedText
        }
    }
    fun toDisplayUrl(originalUrl: CharSequence): CharSequence =
        maybeStripTrailingSlash(maybeStripUrlProtocol(originalUrl))

    private fun maybeStripUrlProtocol(url: CharSequence): CharSequence {
        if (url.startsWith(HTTPS)) {
+2 −15
Original line number Diff line number Diff line
@@ -16,8 +16,6 @@ import org.junit.Assert.assertTrue
import org.junit.Before
import org.junit.Test
import org.junit.runner.RunWith
import org.mockito.Mockito.spy
import org.mockito.Mockito.verify
import kotlin.random.Random

@RunWith(AndroidJUnit4::class)
@@ -246,20 +244,9 @@ class URLStringUtilsTest {
    }

    @Test
    fun showDisplayUrlAsLTREvenIfTextStartsWithArabicCharacters() {
    fun toDisplayUrlDoesNotAddImplicitDirectionalMarks() {
        val testDisplayUrl = URLStringUtils.toDisplayUrl("http://ختار.ار/www.mozilla.org/1")
        assertEquals("\u200Eختار.ار/www.mozilla.org/1", testDisplayUrl)
    }

    @Test
    fun toDisplayUrlAlwaysUseATextDirectionHeuristicToDetermineDirectionality() {
        val textHeuristic = spy(TestTextDirectionHeuristicCompat())

        URLStringUtils.toDisplayUrl("http://ختار.ار/www.mozilla.org/1", textHeuristic)
        verify(textHeuristic).isRtl("ختار.ار/www.mozilla.org/1", 0, 1)

        URLStringUtils.toDisplayUrl("http://www.mozilla.org/1", textHeuristic)
        verify(textHeuristic).isRtl("mozilla.org/1", 0, 1)
        assertEquals("ختار.ار/www.mozilla.org/1", testDisplayUrl)
    }

    @Test