[go: nahoru, domu]

Pass ellipsis to framework

Bug: 122425531
Test: ./gradlew ui-port:test
Test: ./gradlew ui-port:connectedAndroidTest
Change-Id: I0090dd6c5febaf651372e183fc2e762c21319a66
diff --git a/ui/port/src/androidTest/java/androidx/ui/port/engine/text/platform/ParagraphAndroidTest.kt b/ui/port/src/androidTest/java/androidx/ui/port/engine/text/platform/ParagraphAndroidTest.kt
index 2693e41..595f4ae 100644
--- a/ui/port/src/androidTest/java/androidx/ui/port/engine/text/platform/ParagraphAndroidTest.kt
+++ b/ui/port/src/androidTest/java/androidx/ui/port/engine/text/platform/ParagraphAndroidTest.kt
@@ -43,7 +43,9 @@
 import org.hamcrest.Matchers.equalTo
 import org.hamcrest.Matchers.not
 import org.hamcrest.Matchers.nullValue
+import org.junit.Assert.assertFalse
 import org.junit.Assert.assertThat
+import org.junit.Assert.assertTrue
 import org.junit.Before
 import org.junit.Test
 import org.junit.runner.RunWith
@@ -736,11 +738,67 @@
         assertThat(typeface.isItalic, equalTo(false))
     }
 
+    @Test
+    fun testEllipsis_withMaxLineEqualsNull_doesNotEllipsis() {
+        val text = "abc"
+        val fontSize = 20f
+        val paragraphWidth = (text.length - 1) * fontSize
+        val paragraph = simpleParagraph(
+            text = text,
+            fontFamily = fontFamily,
+            fontSize = fontSize,
+            ellipsis = true
+        )
+        paragraph.layout(paragraphWidth)
+        for (i in 0 until paragraph.lineCount) {
+            assertFalse(paragraph.isEllipsisApplied(i))
+        }
+    }
+
+    @Test
+    fun testEllipsis_withMaxLinesLessThanTextLines_doesEllipsis() {
+        val text = "abcde"
+        val fontSize = 100f
+        // Note that on API 21, if the next line only contains 1 character, ellipsis won't work
+        val paragraphWidth = (text.length - 1.5f) * fontSize
+        val paragraph = simpleParagraph(
+            text = text,
+            fontFamily = fontFamily,
+            fontSize = fontSize,
+            ellipsis = true,
+            maxLines = 1
+        )
+        paragraph.layout(paragraphWidth)
+
+        assertTrue(paragraph.isEllipsisApplied(0))
+    }
+
+    @Test
+    fun testEllipsis_withMaxLinesMoreThanTextLines_doesNotEllipsis() {
+        val text = "abc"
+        val fontSize = 100f
+        val paragraphWidth = (text.length - 1) * fontSize
+        val maxLines = ceil(text.length * fontSize / paragraphWidth).toInt()
+        val paragraph = simpleParagraph(
+            text = text,
+            fontFamily = fontFamily,
+            fontSize = fontSize,
+            ellipsis = true,
+            maxLines = maxLines
+        )
+        paragraph.layout(paragraphWidth)
+
+        for (i in 0 until paragraph.lineCount) {
+            assertFalse(paragraph.isEllipsisApplied(i))
+        }
+    }
+
     private fun simpleParagraph(
         text: CharSequence = "",
         textStyles: List<ParagraphBuilder.TextStyleIndex> = listOf(),
         textAlign: TextAlign? = null,
         fontSize: Float? = null,
+        ellipsis: Boolean? = null,
         maxLines: Int? = null,
         fontFamily: FontFamily? = null,
         fontWeight: FontWeight? = null,
@@ -753,6 +811,7 @@
             typefaceAdapter = typefaceAdapter,
             paragraphStyle = ParagraphStyle(
                 textAlign = textAlign,
+                ellipsis = ellipsis,
                 maxLines = maxLines,
                 fontFamily = fontFamily,
                 fontSize = fontSize,
diff --git a/ui/port/src/main/java/androidx/ui/engine/text/platform/ParagraphAndroid.kt b/ui/port/src/main/java/androidx/ui/engine/text/platform/ParagraphAndroid.kt
index 5b033bc..64f6c6f 100644
--- a/ui/port/src/main/java/androidx/ui/engine/text/platform/ParagraphAndroid.kt
+++ b/ui/port/src/main/java/androidx/ui/engine/text/platform/ParagraphAndroid.kt
@@ -19,6 +19,7 @@
 import android.text.SpannableString
 import android.text.Spanned
 import android.text.TextPaint
+import android.text.TextUtils
 import android.text.style.AbsoluteSizeSpan
 import android.text.style.BackgroundColorSpan
 import android.text.style.ForegroundColorSpan
@@ -179,10 +180,17 @@
         val lineSpacingMultiplier =
             paragraphStyle.lineHeight ?: DEFAULT_LINESPACING_MULTIPLIER
 
+        val ellipsize = if (paragraphStyle.ellipsis == true) {
+            TextUtils.TruncateAt.END
+        } else {
+            null
+        }
+
         layout = TextLayout(
             charSequence = charSequence,
             width = floorWidth,
             textPaint = textPaint,
+            ellipsize = ellipsize,
             alignment = alignment,
             textDirectionHeuristic = textDirectionHeuristic,
             lineSpacingMultiplier = lineSpacingMultiplier,
@@ -209,6 +217,11 @@
 
     fun getLineWidth(index: Int): Float = ensureLayout.getLineWidth(index)
 
+    /**
+     * @return true if the given line is ellipsized, else false.
+     */
+    fun isEllipsisApplied(lineIndex: Int): Boolean = ensureLayout.isEllipsisApplied(lineIndex)
+
     fun paint(canvas: Canvas, x: Float, y: Float) {
         val tmpLayout = layout ?: throw IllegalStateException("paint cannot be " +
                 "called before layout() is called")