Louis Pullen-Freilich | 2694068 | 2019-05-01 16:44:01 +0100 | [diff] [blame] | 1 | package androidx.ui.engine.text.platform |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 2 | |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 3 | import android.graphics.Paint |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 4 | import android.text.TextPaint |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 5 | import android.text.style.AbsoluteSizeSpan |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 6 | import android.text.style.BackgroundColorSpan |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 7 | import android.text.style.ForegroundColorSpan |
Haoyu Zhang | 7fc5917 | 2019-03-01 17:03:31 -0800 | [diff] [blame] | 8 | import android.text.style.LeadingMarginSpan |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 9 | import android.text.style.LocaleSpan |
Haoyu Zhang | 24eb32f | 2019-05-21 16:16:37 -0700 | [diff] [blame] | 10 | import android.text.style.RelativeSizeSpan |
Haoyu Zhang | 1575092 | 2019-02-19 13:44:17 -0800 | [diff] [blame] | 11 | import android.text.style.ScaleXSpan |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 12 | import android.text.style.StrikethroughSpan |
| 13 | import android.text.style.UnderlineSpan |
Haoyu Zhang | 97d5e72 | 2019-01-04 17:32:34 -0800 | [diff] [blame] | 14 | import androidx.test.filters.SdkSuppress |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 15 | import androidx.test.filters.SmallTest |
| 16 | import androidx.test.platform.app.InstrumentationRegistry |
Haoyu Zhang | 2eb38cd | 2018-11-21 15:05:28 -0800 | [diff] [blame] | 17 | import androidx.text.StaticLayoutCompat |
Haoyu Zhang | 40ce6bf | 2019-01-28 15:58:20 -0800 | [diff] [blame] | 18 | import androidx.text.style.BaselineShiftSpan |
Haoyu Zhang | 588aaef | 2019-02-20 14:47:39 -0800 | [diff] [blame] | 19 | import androidx.text.style.FontFeatureSpan |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 20 | import androidx.text.style.LetterSpacingSpan |
Haoyu Zhang | 2a036c83 | 2019-03-08 11:57:13 -0800 | [diff] [blame] | 21 | import androidx.text.style.ShadowSpan |
Haoyu Zhang | 1575092 | 2019-02-19 13:44:17 -0800 | [diff] [blame] | 22 | import androidx.text.style.SkewXSpan |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 23 | import androidx.text.style.TypefaceSpan |
Haoyu Zhang | 97d5e72 | 2019-01-04 17:32:34 -0800 | [diff] [blame] | 24 | import androidx.text.style.WordSpacingSpan |
Haoyu Zhang | 7fc5917 | 2019-03-01 17:03:31 -0800 | [diff] [blame] | 25 | import androidx.ui.core.px |
Haoyu Zhang | 2a036c83 | 2019-03-08 11:57:13 -0800 | [diff] [blame] | 26 | import androidx.ui.engine.geometry.Offset |
Haoyu Zhang | 40ce6bf | 2019-01-28 15:58:20 -0800 | [diff] [blame] | 27 | import androidx.ui.engine.text.BaselineShift |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 28 | import androidx.ui.engine.text.FontStyle |
Siyamed Sinir | 2179601 | 2018-12-26 13:51:17 -0800 | [diff] [blame] | 29 | import androidx.ui.engine.text.FontSynthesis |
Louis Pullen-Freilich | 2694068 | 2019-05-01 16:44:01 +0100 | [diff] [blame] | 30 | import androidx.ui.engine.text.FontTestData.Companion.BASIC_MEASURE_FONT |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 31 | import androidx.ui.engine.text.FontWeight |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 32 | import androidx.ui.engine.text.ParagraphStyle |
| 33 | import androidx.ui.engine.text.TextAlign |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 34 | import androidx.ui.engine.text.TextDecoration |
Louis Pullen-Freilich | 2694068 | 2019-05-01 16:44:01 +0100 | [diff] [blame] | 35 | import androidx.ui.engine.text.TextGeometricTransform |
| 36 | import androidx.ui.engine.text.TextIndent |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 37 | import androidx.ui.engine.text.font.FontFamily |
| 38 | import androidx.ui.engine.text.font.asFontFamily |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 39 | import androidx.ui.engine.window.Locale |
Siyamed Sinir | 00aa717 | 2019-02-01 13:15:56 -0800 | [diff] [blame] | 40 | import androidx.ui.matchers.equalToBitmap |
| 41 | import androidx.ui.matchers.hasSpan |
| 42 | import androidx.ui.matchers.hasSpanOnTop |
George Mount | a314578 | 2019-05-02 10:08:03 -0700 | [diff] [blame] | 43 | import androidx.ui.graphics.Color |
Haoyu Zhang | 2a036c83 | 2019-03-08 11:57:13 -0800 | [diff] [blame] | 44 | import androidx.ui.painting.Shadow |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 45 | import androidx.ui.painting.AnnotatedString |
| 46 | import androidx.ui.painting.TextStyle |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 47 | import com.nhaarman.mockitokotlin2.any |
| 48 | import com.nhaarman.mockitokotlin2.eq |
| 49 | import com.nhaarman.mockitokotlin2.mock |
| 50 | import com.nhaarman.mockitokotlin2.never |
| 51 | import com.nhaarman.mockitokotlin2.spy |
| 52 | import com.nhaarman.mockitokotlin2.times |
| 53 | import com.nhaarman.mockitokotlin2.verify |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 54 | import org.hamcrest.Matchers.equalTo |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 55 | import org.hamcrest.Matchers.not |
| 56 | import org.hamcrest.Matchers.nullValue |
Haoyu Zhang | c11ef5a | 2019-01-14 16:35:05 -0800 | [diff] [blame] | 57 | import org.junit.Assert.assertFalse |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 58 | import org.junit.Assert.assertThat |
Haoyu Zhang | c11ef5a | 2019-01-14 16:35:05 -0800 | [diff] [blame] | 59 | import org.junit.Assert.assertTrue |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 60 | import org.junit.Before |
| 61 | import org.junit.Test |
| 62 | import org.junit.runner.RunWith |
| 63 | import org.junit.runners.JUnit4 |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 64 | import kotlin.math.ceil |
| 65 | |
| 66 | @RunWith(JUnit4::class) |
| 67 | @SmallTest |
| 68 | class ParagraphAndroidTest { |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 69 | private lateinit var fontFamily: FontFamily |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 70 | |
| 71 | @Before |
| 72 | fun setup() { |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 73 | // This sample font provides the following features: |
| 74 | // 1. The width of most of visible characters equals to font size. |
| 75 | // 2. The LTR/RTL characters are rendered as â–¶/â—€. |
| 76 | // 3. The fontMetrics passed to TextPaint has descend - ascend equal to 1.2 * fontSize. |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 77 | fontFamily = BASIC_MEASURE_FONT.asFontFamily() |
| 78 | fontFamily.context = InstrumentationRegistry.getInstrumentation().context |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 79 | } |
| 80 | |
| 81 | @Test |
| 82 | fun draw_with_newline_and_line_break_default_values() { |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 83 | val fontSize = 50.0f |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 84 | for (text in arrayOf("abc\ndef", "\u05D0\u05D1\u05D2\n\u05D3\u05D4\u05D5")) { |
| 85 | val paragraphAndroid = simpleParagraph( |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 86 | text = text, |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 87 | fontSize = fontSize, |
| 88 | fontFamily = fontFamily |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 89 | ) |
| 90 | |
| 91 | // 2 chars width |
| 92 | paragraphAndroid.layout(width = 2 * fontSize) |
| 93 | |
| 94 | val textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 95 | textPaint.textSize = fontSize |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 96 | textPaint.typeface = TypefaceAdapter().create(fontFamily) |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 97 | |
Haoyu Zhang | 2eb38cd | 2018-11-21 15:05:28 -0800 | [diff] [blame] | 98 | val staticLayout = StaticLayoutCompat.Builder( |
| 99 | text, |
| 100 | textPaint, |
| 101 | ceil(paragraphAndroid.width).toInt() |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 102 | ) |
Haoyu Zhang | 2eb38cd | 2018-11-21 15:05:28 -0800 | [diff] [blame] | 103 | .setEllipsizedWidth(ceil(paragraphAndroid.width).toInt()) |
| 104 | .build() |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 105 | assertThat(paragraphAndroid.bitmap(), equalToBitmap(staticLayout.bitmap())) |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 109 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 110 | fun testAnnotatedString_setColorOnWholeText() { |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 111 | val text = "abcde" |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 112 | val textStyle = TextStyle(color = Color(0xFF0000FF.toInt())) |
| 113 | |
| 114 | val paragraph = simpleParagraph( |
| 115 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 116 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)) |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 117 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 118 | paragraph.layout(100.0f) |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 119 | |
| 120 | assertThat(paragraph.underlyingText, hasSpan(ForegroundColorSpan::class, 0, text.length)) |
| 121 | } |
| 122 | |
| 123 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 124 | fun testAnnotatedString_setColorOnPartOfText() { |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 125 | val text = "abcde" |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 126 | val textStyle = TextStyle(color = Color(0xFF0000FF.toInt())) |
| 127 | |
| 128 | val paragraph = simpleParagraph( |
| 129 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 130 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length)) |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 131 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 132 | paragraph.layout(100.0f) |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 133 | |
| 134 | assertThat(paragraph.underlyingText, hasSpan(ForegroundColorSpan::class, 0, "abc".length)) |
| 135 | } |
| 136 | |
| 137 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 138 | fun testAnnotatedString_setColorTwice_lastOneOverwrite() { |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 139 | val text = "abcde" |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 140 | val textStyle = TextStyle(color = Color(0xFF0000FF.toInt())) |
| 141 | val textStyleOverwrite = TextStyle(color = Color(0xFF00FF00.toInt())) |
| 142 | |
| 143 | val paragraph = simpleParagraph( |
| 144 | text = text, |
| 145 | textStyles = listOf( |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 146 | AnnotatedString.Item(textStyle, 0, text.length), |
| 147 | AnnotatedString.Item(textStyleOverwrite, 0, "abc".length) |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 148 | ) |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 149 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 150 | paragraph.layout(100.0f) |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 151 | |
| 152 | assertThat(paragraph.underlyingText, hasSpan(ForegroundColorSpan::class, 0, text.length)) |
| 153 | assertThat(paragraph.underlyingText, hasSpan(ForegroundColorSpan::class, 0, "abc".length)) |
| 154 | assertThat( |
| 155 | paragraph.underlyingText, |
| 156 | hasSpanOnTop(ForegroundColorSpan::class, 0, "abc".length) |
| 157 | ) |
| 158 | } |
| 159 | |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 160 | @Test |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 161 | fun testStyle_setTextDecorationOnWholeText_withLineThrough() { |
| 162 | val text = "abcde" |
Siyamed Sinir | 9fd89f4 | 2019-05-20 18:04:29 -0700 | [diff] [blame] | 163 | val textStyle = TextStyle(decoration = TextDecoration.LineThrough) |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 164 | |
| 165 | val paragraph = simpleParagraph( |
| 166 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 167 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)) |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 168 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 169 | paragraph.layout(100.0f) |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 170 | |
| 171 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 172 | assertThat(paragraph.underlyingText, hasSpan(StrikethroughSpan::class, 0, text.length)) |
| 173 | } |
| 174 | |
| 175 | @Test |
| 176 | fun testStyle_setTextDecorationOnWholeText_withUnderline() { |
| 177 | val text = "abcde" |
Siyamed Sinir | 9fd89f4 | 2019-05-20 18:04:29 -0700 | [diff] [blame] | 178 | val textStyle = TextStyle(decoration = TextDecoration.Underline) |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 179 | |
| 180 | val paragraph = simpleParagraph( |
| 181 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 182 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)) |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 183 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 184 | paragraph.layout(100.0f) |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 185 | |
| 186 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 187 | assertThat(paragraph.underlyingText, hasSpan(UnderlineSpan::class, 0, text.length)) |
| 188 | } |
| 189 | |
| 190 | @Test |
| 191 | fun testStyle_setTextDecorationOnPartText_withLineThrough() { |
| 192 | val text = "abcde" |
Siyamed Sinir | 9fd89f4 | 2019-05-20 18:04:29 -0700 | [diff] [blame] | 193 | val textStyle = TextStyle(decoration = TextDecoration.LineThrough) |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 194 | |
| 195 | val paragraph = simpleParagraph( |
| 196 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 197 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length)) |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 198 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 199 | paragraph.layout(100.0f) |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 200 | |
| 201 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 202 | assertThat(paragraph.underlyingText, hasSpan(StrikethroughSpan::class, 0, "abc".length)) |
| 203 | } |
| 204 | |
| 205 | @Test |
| 206 | fun testStyle_setTextDecorationOnPartText_withUnderline() { |
| 207 | val text = "abcde" |
Siyamed Sinir | 9fd89f4 | 2019-05-20 18:04:29 -0700 | [diff] [blame] | 208 | val textStyle = TextStyle(decoration = TextDecoration.Underline) |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 209 | |
| 210 | val paragraph = simpleParagraph( |
| 211 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 212 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length)) |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 213 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 214 | paragraph.layout(100.0f) |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 215 | |
| 216 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 217 | assertThat(paragraph.underlyingText, hasSpan(UnderlineSpan::class, 0, "abc".length)) |
| 218 | } |
| 219 | |
| 220 | @Test |
| 221 | fun testStyle_setTextDecoration_withLineThroughAndUnderline() { |
| 222 | val text = "abcde" |
| 223 | val textStyle = TextStyle( |
| 224 | decoration = TextDecoration.combine( |
Siyamed Sinir | 9fd89f4 | 2019-05-20 18:04:29 -0700 | [diff] [blame] | 225 | listOf(TextDecoration.LineThrough, TextDecoration.Underline) |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 226 | ) |
| 227 | ) |
| 228 | |
| 229 | val paragraph = simpleParagraph( |
| 230 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 231 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length)) |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 232 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 233 | paragraph.layout(100.0f) |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 234 | |
| 235 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 236 | assertThat(paragraph.underlyingText, hasSpan(UnderlineSpan::class, 0, "abc".length)) |
| 237 | assertThat(paragraph.underlyingText, hasSpan(StrikethroughSpan::class, 0, "abc".length)) |
| 238 | } |
| 239 | |
| 240 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 241 | fun testAnnotatedString_setFontSizeOnWholeText() { |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 242 | val text = "abcde" |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 243 | val fontSize = 20.0f |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 244 | val paragraphWidth = text.length * fontSize |
| 245 | val textStyle = TextStyle(fontSize = fontSize) |
| 246 | |
| 247 | val paragraph = simpleParagraph( |
| 248 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 249 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)) |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 250 | ) |
| 251 | paragraph.layout(paragraphWidth) |
| 252 | |
| 253 | assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, text.length)) |
| 254 | } |
| 255 | |
| 256 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 257 | fun testAnnotatedString_setFontSizeOnPartText() { |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 258 | val text = "abcde" |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 259 | val fontSize = 20.0f |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 260 | val paragraphWidth = text.length * fontSize |
| 261 | val textStyle = TextStyle(fontSize = fontSize) |
| 262 | |
| 263 | val paragraph = simpleParagraph( |
| 264 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 265 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length)) |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 266 | ) |
| 267 | paragraph.layout(paragraphWidth) |
| 268 | |
| 269 | assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, "abc".length)) |
| 270 | } |
| 271 | |
| 272 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 273 | fun testAnnotatedString_setFontSizeTwice_lastOneOverwrite() { |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 274 | val text = "abcde" |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 275 | val fontSize = 20.0f |
| 276 | val fontSizeOverwrite = 30.0f |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 277 | val paragraphWidth = text.length * fontSizeOverwrite |
| 278 | val textStyle = TextStyle(fontSize = fontSize) |
| 279 | val textStyleOverwrite = TextStyle(fontSize = fontSizeOverwrite) |
| 280 | |
| 281 | val paragraph = simpleParagraph( |
| 282 | text = text, |
| 283 | textStyles = listOf( |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 284 | AnnotatedString.Item(textStyle, 0, text.length), |
| 285 | AnnotatedString.Item(textStyleOverwrite, 0, "abc".length) |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 286 | ) |
| 287 | ) |
| 288 | paragraph.layout(paragraphWidth) |
| 289 | |
| 290 | assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, text.length)) |
| 291 | assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, "abc".length)) |
| 292 | assertThat( |
| 293 | paragraph.underlyingText, |
| 294 | hasSpanOnTop(AbsoluteSizeSpan::class, 0, "abc".length) |
| 295 | ) |
| 296 | } |
| 297 | |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 298 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 299 | fun testAnnotatedString_setFontSizeScaleOnWholeText() { |
Haoyu Zhang | 24eb32f | 2019-05-21 16:16:37 -0700 | [diff] [blame] | 300 | val text = "abcde" |
| 301 | val fontSizeScale = 2.0f |
| 302 | val textStyle = TextStyle(fontSizeScale = fontSizeScale) |
| 303 | |
| 304 | val paragraph = simpleParagraph( |
| 305 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 306 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)) |
Haoyu Zhang | 24eb32f | 2019-05-21 16:16:37 -0700 | [diff] [blame] | 307 | ) |
| 308 | paragraph.layout(100f) |
| 309 | |
| 310 | assertThat( |
| 311 | paragraph.underlyingText, |
| 312 | hasSpan(RelativeSizeSpan::class, 0, text.length) { |
| 313 | it.sizeChange == fontSizeScale |
| 314 | } |
| 315 | ) |
| 316 | } |
| 317 | |
| 318 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 319 | fun testAnnotatedString_setFontSizeScaleOnPartText() { |
Haoyu Zhang | 24eb32f | 2019-05-21 16:16:37 -0700 | [diff] [blame] | 320 | val text = "abcde" |
| 321 | val fontSizeScale = 2.0f |
| 322 | val textStyle = TextStyle(fontSizeScale = fontSizeScale) |
| 323 | |
| 324 | val paragraph = simpleParagraph( |
| 325 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 326 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length)) |
Haoyu Zhang | 24eb32f | 2019-05-21 16:16:37 -0700 | [diff] [blame] | 327 | ) |
| 328 | paragraph.layout(100f) |
| 329 | |
| 330 | assertThat( |
| 331 | paragraph.underlyingText, |
| 332 | hasSpan(RelativeSizeSpan::class, 0, "abc".length) { |
| 333 | it.sizeChange == fontSizeScale |
| 334 | } |
| 335 | ) |
| 336 | } |
| 337 | |
| 338 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 339 | fun testAnnotatedString_setLetterSpacingOnWholeText() { |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 340 | val text = "abcde" |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 341 | val letterSpacing = 2.0f |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 342 | val textStyle = TextStyle(letterSpacing = letterSpacing) |
| 343 | |
| 344 | val paragraph = simpleParagraph( |
| 345 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 346 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)) |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 347 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 348 | paragraph.layout(100.0f) |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 349 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 350 | assertThat(paragraph.underlyingText, hasSpan(LetterSpacingSpan::class, 0, text.length)) |
| 351 | } |
| 352 | |
| 353 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 354 | fun testAnnotatedString_setLetterSpacingOnPartText() { |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 355 | val text = "abcde" |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 356 | val textStyle = TextStyle(letterSpacing = 2.0f) |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 357 | |
| 358 | val paragraph = simpleParagraph( |
| 359 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 360 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length)) |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 361 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 362 | paragraph.layout(100.0f) |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 363 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 364 | assertThat(paragraph.underlyingText, hasSpan(LetterSpacingSpan::class, 0, "abc".length)) |
| 365 | } |
| 366 | |
| 367 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 368 | fun testAnnotatedString_setLetterSpacingTwice_lastOneOverwrite() { |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 369 | val text = "abcde" |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 370 | val textStyle = TextStyle(letterSpacing = 2.0f) |
| 371 | val textStyleOverwrite = TextStyle(letterSpacing = 3.0f) |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 372 | |
| 373 | val paragraph = simpleParagraph( |
| 374 | text = text, |
| 375 | textStyles = listOf( |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 376 | AnnotatedString.Item(textStyle, 0, text.length), |
| 377 | AnnotatedString.Item(textStyleOverwrite, 0, "abc".length) |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 378 | ) |
| 379 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 380 | paragraph.layout(100.0f) |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 381 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 382 | assertThat(paragraph.underlyingText, hasSpan(LetterSpacingSpan::class, 0, text.length)) |
| 383 | assertThat(paragraph.underlyingText, hasSpan(LetterSpacingSpan::class, 0, "abc".length)) |
| 384 | assertThat( |
| 385 | paragraph.underlyingText, |
| 386 | hasSpanOnTop(LetterSpacingSpan::class, 0, "abc".length) |
| 387 | ) |
| 388 | } |
| 389 | |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 390 | @Test |
haoyu | f6e7c0c | 2019-06-18 16:04:02 -0700 | [diff] [blame] | 391 | @SdkSuppress(minSdkVersion = 29) |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 392 | fun testAnnotatedString_setWordSpacingOnWholeText() { |
Haoyu Zhang | 97d5e72 | 2019-01-04 17:32:34 -0800 | [diff] [blame] | 393 | val text = "ab cd" |
| 394 | val wordSpacing = 2.0f |
| 395 | val textStyle = TextStyle(wordSpacing = wordSpacing) |
| 396 | |
| 397 | val paragraph = simpleParagraph( |
| 398 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 399 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)) |
Haoyu Zhang | 97d5e72 | 2019-01-04 17:32:34 -0800 | [diff] [blame] | 400 | ) |
| 401 | // Notice that the width doesn't matter for this test. |
| 402 | paragraph.layout(100.0f) |
| 403 | |
| 404 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 405 | assertThat( |
| 406 | paragraph.underlyingText, |
| 407 | hasSpan(WordSpacingSpan::class, 0, text.length) { span -> |
| 408 | span.wordSpacing == wordSpacing.toFloat() |
| 409 | } |
| 410 | ) |
| 411 | } |
| 412 | |
| 413 | @Test |
haoyu | f6e7c0c | 2019-06-18 16:04:02 -0700 | [diff] [blame] | 414 | @SdkSuppress(minSdkVersion = 29) |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 415 | fun testAnnotatedString_setWordSpacingOnPartText() { |
Haoyu Zhang | 97d5e72 | 2019-01-04 17:32:34 -0800 | [diff] [blame] | 416 | val text = "abc d" |
| 417 | val wordSpacing = 2.0f |
| 418 | val textStyle = TextStyle(wordSpacing = wordSpacing) |
| 419 | |
| 420 | val paragraph = simpleParagraph( |
| 421 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 422 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length)) |
Haoyu Zhang | 97d5e72 | 2019-01-04 17:32:34 -0800 | [diff] [blame] | 423 | ) |
| 424 | // Notice that the width doesn't matter for this test. |
| 425 | paragraph.layout(100.0f) |
| 426 | |
| 427 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 428 | assertThat( |
| 429 | paragraph.underlyingText, |
| 430 | hasSpan(WordSpacingSpan::class, 0, "abc".length) { span -> |
| 431 | span.wordSpacing == wordSpacing.toFloat() |
| 432 | } |
| 433 | ) |
| 434 | } |
| 435 | |
| 436 | @Test |
haoyu | f6e7c0c | 2019-06-18 16:04:02 -0700 | [diff] [blame] | 437 | @SdkSuppress(minSdkVersion = 29) |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 438 | fun testAnnotatedString_setWordSpacingTwice_lastOneOverwrite() { |
Haoyu Zhang | 97d5e72 | 2019-01-04 17:32:34 -0800 | [diff] [blame] | 439 | val text = "abc d" |
| 440 | val wordSpacing = 2.0f |
| 441 | val textStyle = TextStyle(wordSpacing = wordSpacing) |
| 442 | val wordSpacingOverwrite = 3.0f |
| 443 | val textStyleOverwrite = TextStyle(wordSpacing = wordSpacingOverwrite) |
| 444 | |
| 445 | val paragraph = simpleParagraph( |
| 446 | text = text, |
| 447 | textStyles = listOf( |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 448 | AnnotatedString.Item(textStyle, 0, text.length), |
| 449 | AnnotatedString.Item(textStyleOverwrite, 0, "abc".length) |
Haoyu Zhang | 97d5e72 | 2019-01-04 17:32:34 -0800 | [diff] [blame] | 450 | ) |
| 451 | ) |
| 452 | // Notice that the width doesn't matter for this test. |
| 453 | paragraph.layout(100.0f) |
| 454 | |
| 455 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 456 | assertThat( |
| 457 | paragraph.underlyingText, |
| 458 | hasSpan(WordSpacingSpan::class, 0, text.length) { span -> |
| 459 | span.wordSpacing == wordSpacing.toFloat() |
| 460 | } |
| 461 | ) |
| 462 | assertThat( |
| 463 | paragraph.underlyingText, |
| 464 | hasSpan(WordSpacingSpan::class, 0, "abc".length) { span -> |
| 465 | span.wordSpacing == wordSpacingOverwrite.toFloat() |
| 466 | } |
| 467 | ) |
| 468 | assertThat( |
| 469 | paragraph.underlyingText, |
| 470 | hasSpanOnTop(WordSpacingSpan::class, 0, "abc".length) { span -> |
| 471 | span.wordSpacing == wordSpacingOverwrite.toFloat() |
| 472 | } |
| 473 | ) |
| 474 | } |
| 475 | |
| 476 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 477 | fun testAnnotatedString_setBackgroundOnWholeText() { |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 478 | val text = "abcde" |
| 479 | val color = Color(0xFF0000FF.toInt()) |
Haoyu Zhang | 437d945 | 2019-01-04 17:34:04 -0800 | [diff] [blame] | 480 | val textStyle = TextStyle(background = color) |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 481 | |
| 482 | val paragraph = simpleParagraph( |
| 483 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 484 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)) |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 485 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 486 | paragraph.layout(100.0f) |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 487 | |
| 488 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 489 | assertThat(paragraph.underlyingText, |
| 490 | hasSpan(BackgroundColorSpan::class, 0, text.length) { span -> |
George Mount | a314578 | 2019-05-02 10:08:03 -0700 | [diff] [blame] | 491 | span.backgroundColor == color.toArgb() |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 492 | } |
| 493 | ) |
| 494 | } |
| 495 | |
| 496 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 497 | fun testAnnotatedString_setBackgroundOnPartText() { |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 498 | val text = "abcde" |
| 499 | val color = Color(0xFF0000FF.toInt()) |
Haoyu Zhang | 437d945 | 2019-01-04 17:34:04 -0800 | [diff] [blame] | 500 | val textStyle = TextStyle(background = color) |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 501 | |
| 502 | val paragraph = simpleParagraph( |
| 503 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 504 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length)) |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 505 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 506 | paragraph.layout(100.0f) |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 507 | |
| 508 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 509 | assertThat(paragraph.underlyingText, |
| 510 | hasSpan(BackgroundColorSpan::class, 0, "abc".length) { span -> |
George Mount | a314578 | 2019-05-02 10:08:03 -0700 | [diff] [blame] | 511 | span.backgroundColor == color.toArgb() |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 512 | } |
| 513 | ) |
| 514 | } |
| 515 | |
| 516 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 517 | fun testAnnotatedString_setBackgroundTwice_lastOneOverwrite() { |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 518 | val text = "abcde" |
| 519 | val color = Color(0xFF0000FF.toInt()) |
Haoyu Zhang | 437d945 | 2019-01-04 17:34:04 -0800 | [diff] [blame] | 520 | val textStyle = TextStyle(background = color) |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 521 | val colorOverwrite = Color(0xFF00FF00.toInt()) |
Haoyu Zhang | 437d945 | 2019-01-04 17:34:04 -0800 | [diff] [blame] | 522 | val textStyleOverwrite = TextStyle(background = colorOverwrite) |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 523 | |
| 524 | val paragraph = simpleParagraph( |
| 525 | text = text, |
| 526 | textStyles = listOf( |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 527 | AnnotatedString.Item(textStyle, 0, text.length), |
| 528 | AnnotatedString.Item(textStyleOverwrite, 0, "abc".length) |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 529 | ) |
| 530 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 531 | paragraph.layout(100.0f) |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 532 | |
| 533 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 534 | assertThat(paragraph.underlyingText, |
| 535 | hasSpan(BackgroundColorSpan::class, 0, text.length) { span -> |
George Mount | a314578 | 2019-05-02 10:08:03 -0700 | [diff] [blame] | 536 | span.backgroundColor == color.toArgb() |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 537 | } |
| 538 | ) |
| 539 | assertThat(paragraph.underlyingText, |
| 540 | hasSpan(BackgroundColorSpan::class, 0, "abc".length) { span -> |
George Mount | a314578 | 2019-05-02 10:08:03 -0700 | [diff] [blame] | 541 | span.backgroundColor == colorOverwrite.toArgb() |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 542 | } |
| 543 | ) |
| 544 | assertThat( |
| 545 | paragraph.underlyingText, |
| 546 | hasSpanOnTop(BackgroundColorSpan::class, 0, "abc".length) { span -> |
George Mount | a314578 | 2019-05-02 10:08:03 -0700 | [diff] [blame] | 547 | span.backgroundColor == colorOverwrite.toArgb() |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 548 | } |
| 549 | ) |
| 550 | } |
| 551 | |
| 552 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 553 | fun testAnnotatedString_setLocaleOnWholeText() { |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 554 | val text = "abcde" |
| 555 | val locale = Locale("en", "US") |
| 556 | val textStyle = TextStyle(locale = locale) |
| 557 | |
| 558 | val paragraph = simpleParagraph( |
| 559 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 560 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)) |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 561 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 562 | paragraph.layout(100.0f) |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 563 | |
| 564 | assertThat(paragraph.underlyingText, hasSpan(LocaleSpan::class, 0, text.length)) |
| 565 | } |
| 566 | |
| 567 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 568 | fun testAnnotatedString_setLocaleOnPartText() { |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 569 | val text = "abcde" |
| 570 | val locale = Locale("en", "US") |
| 571 | val textStyle = TextStyle(locale = locale) |
| 572 | |
| 573 | val paragraph = simpleParagraph( |
| 574 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 575 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length)) |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 576 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 577 | paragraph.layout(100.0f) |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 578 | |
| 579 | assertThat(paragraph.underlyingText, hasSpan(LocaleSpan::class, 0, "abc".length)) |
| 580 | } |
| 581 | |
| 582 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 583 | fun testAnnotatedString_setLocaleTwice_lastOneOverwrite() { |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 584 | val text = "abcde" |
| 585 | val textStyle = TextStyle(locale = Locale("en", "US")) |
| 586 | val textStyleOverwrite = TextStyle(locale = Locale("ja", "JP")) |
| 587 | |
| 588 | val paragraph = simpleParagraph( |
| 589 | text = text, |
| 590 | textStyles = listOf( |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 591 | AnnotatedString.Item(textStyle, 0, text.length), |
| 592 | AnnotatedString.Item(textStyleOverwrite, 0, "abc".length) |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 593 | ) |
| 594 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 595 | paragraph.layout(100.0f) |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 596 | |
| 597 | assertThat(paragraph.underlyingText, hasSpan(LocaleSpan::class, 0, text.length)) |
| 598 | assertThat(paragraph.underlyingText, hasSpan(LocaleSpan::class, 0, "abc".length)) |
| 599 | assertThat( |
| 600 | paragraph.underlyingText, |
| 601 | hasSpanOnTop(LocaleSpan::class, 0, "abc".length) |
| 602 | ) |
| 603 | } |
| 604 | |
| 605 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 606 | fun testAnnotatedString_setBaselineShiftOnWholeText() { |
Haoyu Zhang | 40ce6bf | 2019-01-28 15:58:20 -0800 | [diff] [blame] | 607 | val text = "abcde" |
Siyamed Sinir | f544c15 | 2019-05-21 12:12:21 -0700 | [diff] [blame] | 608 | val textStyle = TextStyle(baselineShift = BaselineShift.Subscript) |
Haoyu Zhang | 40ce6bf | 2019-01-28 15:58:20 -0800 | [diff] [blame] | 609 | |
| 610 | val paragraph = simpleParagraph( |
| 611 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 612 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)) |
Haoyu Zhang | 40ce6bf | 2019-01-28 15:58:20 -0800 | [diff] [blame] | 613 | ) |
| 614 | // width is not important |
| 615 | paragraph.layout(100.0f) |
| 616 | |
| 617 | assertThat(paragraph.underlyingText, hasSpan(BaselineShiftSpan::class, 0, text.length)) |
| 618 | } |
| 619 | |
| 620 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 621 | fun testAnnotatedString_setBaselineShiftOnPartText() { |
Haoyu Zhang | 40ce6bf | 2019-01-28 15:58:20 -0800 | [diff] [blame] | 622 | val text = "abcde" |
Siyamed Sinir | f544c15 | 2019-05-21 12:12:21 -0700 | [diff] [blame] | 623 | val textStyle = TextStyle(baselineShift = BaselineShift.Superscript) |
Haoyu Zhang | 40ce6bf | 2019-01-28 15:58:20 -0800 | [diff] [blame] | 624 | |
| 625 | val paragraph = simpleParagraph( |
| 626 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 627 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length)) |
Haoyu Zhang | 40ce6bf | 2019-01-28 15:58:20 -0800 | [diff] [blame] | 628 | ) |
| 629 | // width is not important |
| 630 | paragraph.layout(100.0f) |
| 631 | |
| 632 | assertThat(paragraph.underlyingText, hasSpan(BaselineShiftSpan::class, 0, "abc".length)) |
| 633 | } |
| 634 | |
| 635 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 636 | fun testAnnotatedString_setBaselineShiftTwice_LastOneOnTop() { |
Haoyu Zhang | 40ce6bf | 2019-01-28 15:58:20 -0800 | [diff] [blame] | 637 | val text = "abcde" |
Siyamed Sinir | f544c15 | 2019-05-21 12:12:21 -0700 | [diff] [blame] | 638 | val textStyle = TextStyle(baselineShift = BaselineShift.Subscript) |
| 639 | val textStyleOverwrite = TextStyle(baselineShift = BaselineShift.Superscript) |
Haoyu Zhang | 40ce6bf | 2019-01-28 15:58:20 -0800 | [diff] [blame] | 640 | |
| 641 | val paragraph = simpleParagraph( |
| 642 | text = text, |
| 643 | textStyles = listOf( |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 644 | AnnotatedString.Item(textStyle, 0, text.length), |
| 645 | AnnotatedString.Item(textStyleOverwrite, 0, "abc".length) |
Haoyu Zhang | 40ce6bf | 2019-01-28 15:58:20 -0800 | [diff] [blame] | 646 | ) |
| 647 | ) |
| 648 | // width is not important |
| 649 | paragraph.layout(100.0f) |
| 650 | |
| 651 | assertThat(paragraph.underlyingText, hasSpan(BaselineShiftSpan::class, 0, text.length)) |
| 652 | assertThat(paragraph.underlyingText, hasSpan(BaselineShiftSpan::class, 0, "abc".length)) |
| 653 | assertThat( |
| 654 | paragraph.underlyingText, |
| 655 | hasSpanOnTop(BaselineShiftSpan::class, 0, "abc".length) |
| 656 | ) |
| 657 | } |
| 658 | |
| 659 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 660 | fun testAnnotatedString_setTextGeometricTransformWithNull_noSpanSet() { |
Haoyu Zhang | 1575092 | 2019-02-19 13:44:17 -0800 | [diff] [blame] | 661 | val text = "abcde" |
| 662 | val textStyle = TextStyle(textGeometricTransform = TextGeometricTransform(null, null)) |
| 663 | |
| 664 | val paragraph = simpleParagraph( |
| 665 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 666 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)) |
Haoyu Zhang | 1575092 | 2019-02-19 13:44:17 -0800 | [diff] [blame] | 667 | ) |
| 668 | // width is not important |
| 669 | paragraph.layout(100.0f) |
| 670 | |
| 671 | assertThat(paragraph.underlyingText, not(hasSpan(ScaleXSpan::class, 0, text.length))) |
| 672 | assertThat(paragraph.underlyingText, not(hasSpan(SkewXSpan::class, 0, text.length))) |
| 673 | } |
| 674 | |
| 675 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 676 | fun testAnnotatedString_setTextGeometricTransformWithScaleX() { |
Haoyu Zhang | 1575092 | 2019-02-19 13:44:17 -0800 | [diff] [blame] | 677 | val text = "abcde" |
| 678 | val scaleX = 0.5f |
| 679 | val textStyle = TextStyle(textGeometricTransform = TextGeometricTransform(scaleX, null)) |
| 680 | |
| 681 | val paragraph = simpleParagraph( |
| 682 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 683 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)) |
Haoyu Zhang | 1575092 | 2019-02-19 13:44:17 -0800 | [diff] [blame] | 684 | ) |
| 685 | // width is not important |
| 686 | paragraph.layout(100.0f) |
| 687 | |
| 688 | assertThat( |
| 689 | paragraph.underlyingText, |
| 690 | hasSpan(ScaleXSpan::class, 0, text.length) { it.scaleX == scaleX } |
| 691 | ) |
| 692 | assertThat(paragraph.underlyingText, not(hasSpan(SkewXSpan::class, 0, text.length))) |
| 693 | } |
| 694 | |
| 695 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 696 | fun testAnnotatedString_setTextGeometricTransformWithSkewX() { |
Haoyu Zhang | 1575092 | 2019-02-19 13:44:17 -0800 | [diff] [blame] | 697 | val text = "aa" |
| 698 | val skewX = 1f |
| 699 | val textStyle = TextStyle(textGeometricTransform = TextGeometricTransform(null, skewX)) |
| 700 | |
| 701 | val paragraph = simpleParagraph( |
| 702 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 703 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, text.length)) |
Haoyu Zhang | 1575092 | 2019-02-19 13:44:17 -0800 | [diff] [blame] | 704 | ) |
| 705 | // width is not important |
| 706 | paragraph.layout(100.0f) |
| 707 | |
| 708 | assertThat( |
| 709 | paragraph.underlyingText, |
| 710 | hasSpan(SkewXSpan::class, 0, text.length) { it.skewX == skewX } |
| 711 | ) |
| 712 | assertThat(paragraph.underlyingText, not(hasSpan(ScaleXSpan::class, 0, text.length))) |
| 713 | } |
| 714 | |
| 715 | @Test |
Siyamed Sinir | 2690d53 | 2019-06-21 13:16:00 -0700 | [diff] [blame] | 716 | fun textIndent_onWholeParagraph() { |
Haoyu Zhang | 7fc5917 | 2019-03-01 17:03:31 -0800 | [diff] [blame] | 717 | val text = "abc\ndef" |
| 718 | val firstLine = 40 |
| 719 | val restLine = 20 |
Haoyu Zhang | 7fc5917 | 2019-03-01 17:03:31 -0800 | [diff] [blame] | 720 | |
| 721 | val paragraph = simpleParagraph( |
| 722 | text = text, |
Siyamed Sinir | 2690d53 | 2019-06-21 13:16:00 -0700 | [diff] [blame] | 723 | textIndent = TextIndent(firstLine.px, restLine.px) |
Haoyu Zhang | 7fc5917 | 2019-03-01 17:03:31 -0800 | [diff] [blame] | 724 | ) |
| 725 | // width is not important |
| 726 | paragraph.layout(100.0f) |
| 727 | |
| 728 | assertThat( |
| 729 | paragraph.underlyingText, |
Siyamed Sinir | 2690d53 | 2019-06-21 13:16:00 -0700 | [diff] [blame] | 730 | hasSpan(LeadingMarginSpan.Standard::class, 0, text.length) { |
Haoyu Zhang | 7fc5917 | 2019-03-01 17:03:31 -0800 | [diff] [blame] | 731 | it.getLeadingMargin(true) == firstLine && it.getLeadingMargin(false) == restLine |
| 732 | } |
| 733 | ) |
| 734 | } |
| 735 | |
| 736 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 737 | fun testAnnotatedString_setShadow() { |
Haoyu Zhang | 2a036c83 | 2019-03-08 11:57:13 -0800 | [diff] [blame] | 738 | val text = "abcde" |
| 739 | val color = Color(0xFF00FF00.toInt()) |
| 740 | val offset = Offset(1f, 2f) |
| 741 | val radius = 3.px |
| 742 | val textStyle = TextStyle(shadow = Shadow(color, offset, radius)) |
| 743 | |
| 744 | val paragraph = simpleParagraph( |
| 745 | text = text, |
| 746 | textStyles = listOf( |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 747 | AnnotatedString.Item(textStyle, start = 0, end = text.length) |
Haoyu Zhang | 2a036c83 | 2019-03-08 11:57:13 -0800 | [diff] [blame] | 748 | ) |
| 749 | ) |
| 750 | // width is not important |
| 751 | paragraph.layout(100.0f) |
| 752 | |
| 753 | assertThat( |
| 754 | paragraph.underlyingText, |
| 755 | hasSpan(ShadowSpan::class, start = 0, end = text.length) { |
George Mount | a314578 | 2019-05-02 10:08:03 -0700 | [diff] [blame] | 756 | return@hasSpan it.color == color.toArgb() && |
Haoyu Zhang | 2a036c83 | 2019-03-08 11:57:13 -0800 | [diff] [blame] | 757 | it.offsetX == offset.dx && |
| 758 | it.offsetY == offset.dy && |
| 759 | it.radius == radius.value |
| 760 | } |
| 761 | ) |
| 762 | } |
| 763 | |
| 764 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 765 | fun testAnnotatedString_setShadowTwice_lastOnTop() { |
Haoyu Zhang | 2a036c83 | 2019-03-08 11:57:13 -0800 | [diff] [blame] | 766 | val text = "abcde" |
| 767 | val color = Color(0xFF00FF00.toInt()) |
| 768 | val offset = Offset(1f, 2f) |
| 769 | val radius = 3.px |
| 770 | val textStyle = TextStyle(shadow = Shadow(color, offset, radius)) |
| 771 | |
| 772 | val colorOverwrite = Color(0xFF0000FF.toInt()) |
| 773 | val offsetOverwrite = Offset(3f, 2f) |
| 774 | val radiusOverwrite = 1.px |
| 775 | val textStyleOverwrite = TextStyle( |
| 776 | shadow = Shadow(colorOverwrite, offsetOverwrite, radiusOverwrite) |
| 777 | ) |
| 778 | |
| 779 | val paragraph = simpleParagraph( |
| 780 | text = text, |
| 781 | textStyles = listOf( |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 782 | AnnotatedString.Item(textStyle, start = 0, end = text.length), |
| 783 | AnnotatedString.Item(textStyleOverwrite, start = 0, end = "abc".length) |
Haoyu Zhang | 2a036c83 | 2019-03-08 11:57:13 -0800 | [diff] [blame] | 784 | ) |
| 785 | ) |
| 786 | // width is not important |
| 787 | paragraph.layout(100.0f) |
| 788 | |
| 789 | assertThat( |
| 790 | paragraph.underlyingText, |
| 791 | hasSpan(ShadowSpan::class, start = 0, end = text.length) { |
George Mount | a314578 | 2019-05-02 10:08:03 -0700 | [diff] [blame] | 792 | return@hasSpan it.color == color.toArgb() && |
Haoyu Zhang | 2a036c83 | 2019-03-08 11:57:13 -0800 | [diff] [blame] | 793 | it.offsetX == offset.dx && |
| 794 | it.offsetY == offset.dy && |
| 795 | it.radius == radius.value |
| 796 | } |
| 797 | ) |
| 798 | assertThat( |
| 799 | paragraph.underlyingText, |
| 800 | hasSpanOnTop(ShadowSpan::class, start = 0, end = "abc".length) { |
George Mount | a314578 | 2019-05-02 10:08:03 -0700 | [diff] [blame] | 801 | return@hasSpanOnTop it.color == colorOverwrite.toArgb() && |
Haoyu Zhang | 2a036c83 | 2019-03-08 11:57:13 -0800 | [diff] [blame] | 802 | it.offsetX == offsetOverwrite.dx && |
| 803 | it.offsetY == offsetOverwrite.dy && |
| 804 | it.radius == radiusOverwrite.value |
| 805 | } |
| 806 | ) |
| 807 | } |
| 808 | |
| 809 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 810 | fun testAnnotatedString_fontFamily_addsTypefaceSpanWithCorrectTypeface() { |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 811 | val text = "abcde" |
| 812 | val textStyle = TextStyle( |
| 813 | fontFamily = fontFamily, |
Siyamed Sinir | adf982d | 2019-05-20 17:35:13 -0700 | [diff] [blame] | 814 | fontStyle = FontStyle.Italic, |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 815 | fontWeight = FontWeight.bold |
| 816 | ) |
| 817 | val expectedTypeface = TypefaceAdapter().create( |
| 818 | fontFamily = fontFamily, |
Siyamed Sinir | adf982d | 2019-05-20 17:35:13 -0700 | [diff] [blame] | 819 | fontStyle = FontStyle.Italic, |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 820 | fontWeight = FontWeight.bold |
| 821 | ) |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame] | 822 | val expectedStart = 0 |
| 823 | val expectedEnd = "abc".length |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 824 | |
| 825 | val paragraph = simpleParagraph( |
| 826 | text = text, |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame] | 827 | textStyles = listOf( |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 828 | AnnotatedString.Item( |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame] | 829 | textStyle, |
| 830 | expectedStart, |
| 831 | expectedEnd |
| 832 | ) |
| 833 | ) |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 834 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 835 | paragraph.layout(100.0f) |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 836 | |
| 837 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 838 | assertThat( |
| 839 | paragraph.underlyingText, |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame] | 840 | hasSpan(TypefaceSpan::class, expectedStart, expectedEnd) { span -> |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 841 | span.typeface == expectedTypeface |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame] | 842 | }) |
| 843 | } |
| 844 | |
| 845 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 846 | fun testAnnotatedString_fontFamily_whenFontSynthesizeTurnedOff() { |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame] | 847 | val text = "abcde" |
| 848 | val textStyle = TextStyle( |
| 849 | fontFamily = fontFamily, |
Siyamed Sinir | adf982d | 2019-05-20 17:35:13 -0700 | [diff] [blame] | 850 | fontStyle = FontStyle.Italic, |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame] | 851 | fontWeight = FontWeight.bold, |
Siyamed Sinir | 934a344 | 2019-06-18 12:57:13 -0700 | [diff] [blame] | 852 | fontSynthesis = FontSynthesis.None |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 853 | ) |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame] | 854 | val expectedTypeface = TypefaceAdapter().create( |
| 855 | fontFamily = fontFamily, |
Siyamed Sinir | adf982d | 2019-05-20 17:35:13 -0700 | [diff] [blame] | 856 | fontStyle = FontStyle.Italic, |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame] | 857 | fontWeight = FontWeight.bold, |
Siyamed Sinir | 934a344 | 2019-06-18 12:57:13 -0700 | [diff] [blame] | 858 | fontSynthesis = FontSynthesis.None |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame] | 859 | ) |
| 860 | val expectedStart = 0 |
| 861 | val expectedEnd = "abc".length |
| 862 | |
| 863 | val paragraph = simpleParagraph( |
| 864 | text = text, |
| 865 | textStyles = listOf( |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 866 | AnnotatedString.Item( |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame] | 867 | textStyle, |
| 868 | expectedStart, |
| 869 | expectedEnd |
| 870 | ) |
| 871 | ) |
| 872 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 873 | paragraph.layout(100.0f) |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame] | 874 | |
| 875 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 876 | assertThat( |
| 877 | paragraph.underlyingText, |
| 878 | hasSpan(TypefaceSpan::class, expectedStart, expectedEnd) { span -> |
| 879 | span.typeface == expectedTypeface |
| 880 | }) |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 881 | } |
| 882 | |
| 883 | @Test |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 884 | fun testAnnotatedString_fontFeatureSetting_setSpanOnText() { |
Haoyu Zhang | 588aaef | 2019-02-20 14:47:39 -0800 | [diff] [blame] | 885 | val text = "abc" |
| 886 | val fontFeatureSettings = "\"kern\" 0" |
| 887 | val textStyle = TextStyle(fontFeatureSettings = fontFeatureSettings) |
| 888 | |
| 889 | val paragraph = simpleParagraph( |
| 890 | text = text, |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 891 | textStyles = listOf(AnnotatedString.Item(textStyle, 0, "abc".length)) |
Haoyu Zhang | 588aaef | 2019-02-20 14:47:39 -0800 | [diff] [blame] | 892 | ) |
| 893 | // width is not important |
| 894 | paragraph.layout(100.0f) |
| 895 | |
| 896 | assertThat( |
| 897 | paragraph.underlyingText, |
| 898 | hasSpan(FontFeatureSpan::class, 0, "abc".length) { |
| 899 | it.fontFeatureSettings == fontFeatureSettings |
| 900 | }) |
| 901 | } |
| 902 | |
| 903 | @Test |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 904 | fun testEmptyFontFamily() { |
| 905 | val typefaceAdapter = mock<TypefaceAdapter>() |
| 906 | val paragraph = simpleParagraph( |
| 907 | text = "abc", |
| 908 | typefaceAdapter = typefaceAdapter |
| 909 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 910 | paragraph.layout(Float.MAX_VALUE) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 911 | |
| 912 | verify(typefaceAdapter, never()).create( |
| 913 | fontFamily = any(), |
| 914 | fontWeight = any(), |
Siyamed Sinir | 2179601 | 2018-12-26 13:51:17 -0800 | [diff] [blame] | 915 | fontStyle = any(), |
| 916 | fontSynthesis = any() |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 917 | ) |
| 918 | assertThat(paragraph.textPaint.typeface, nullValue()) |
| 919 | } |
| 920 | |
| 921 | @Test |
| 922 | fun testEmptyFontFamily_withBoldFontWeightSelection() { |
| 923 | val typefaceAdapter = spy(TypefaceAdapter()) |
| 924 | |
| 925 | val paragraph = simpleParagraph( |
| 926 | text = "abc", |
| 927 | fontFamily = null, |
| 928 | fontWeight = FontWeight.bold, |
| 929 | typefaceAdapter = typefaceAdapter |
| 930 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 931 | paragraph.layout(Float.MAX_VALUE) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 932 | |
| 933 | verify(typefaceAdapter, times(1)).create( |
| 934 | fontFamily = eq(null), |
| 935 | fontWeight = eq(FontWeight.bold), |
Siyamed Sinir | adf982d | 2019-05-20 17:35:13 -0700 | [diff] [blame] | 936 | fontStyle = eq(FontStyle.Normal), |
Siyamed Sinir | 934a344 | 2019-06-18 12:57:13 -0700 | [diff] [blame] | 937 | fontSynthesis = eq(FontSynthesis.All) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 938 | ) |
| 939 | |
| 940 | val typeface = paragraph.textPaint.typeface |
| 941 | assertThat(typeface, not(nullValue())) |
| 942 | assertThat(typeface.isBold, equalTo(true)) |
| 943 | assertThat(typeface.isItalic, equalTo(false)) |
| 944 | } |
| 945 | |
| 946 | @Test |
| 947 | fun testEmptyFontFamily_withFontStyleSelection() { |
| 948 | val typefaceAdapter = spy(TypefaceAdapter()) |
| 949 | val paragraph = simpleParagraph( |
| 950 | text = "abc", |
| 951 | fontFamily = null, |
Siyamed Sinir | adf982d | 2019-05-20 17:35:13 -0700 | [diff] [blame] | 952 | fontStyle = FontStyle.Italic, |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 953 | typefaceAdapter = typefaceAdapter |
| 954 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 955 | paragraph.layout(Float.MAX_VALUE) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 956 | |
| 957 | verify(typefaceAdapter, times(1)).create( |
| 958 | fontFamily = eq(null), |
| 959 | fontWeight = eq(FontWeight.normal), |
Siyamed Sinir | adf982d | 2019-05-20 17:35:13 -0700 | [diff] [blame] | 960 | fontStyle = eq(FontStyle.Italic), |
Siyamed Sinir | 934a344 | 2019-06-18 12:57:13 -0700 | [diff] [blame] | 961 | fontSynthesis = eq(FontSynthesis.All) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 962 | ) |
| 963 | |
| 964 | val typeface = paragraph.textPaint.typeface |
| 965 | assertThat(typeface, not(nullValue())) |
| 966 | assertThat(typeface.isBold, equalTo(false)) |
| 967 | assertThat(typeface.isItalic, equalTo(true)) |
| 968 | } |
| 969 | |
| 970 | @Test |
| 971 | fun testFontFamily_withGenericFamilyName() { |
| 972 | val typefaceAdapter = spy(TypefaceAdapter()) |
| 973 | val fontFamily = FontFamily("sans-serif") |
| 974 | |
| 975 | val paragraph = simpleParagraph( |
| 976 | text = "abc", |
| 977 | fontFamily = fontFamily, |
| 978 | typefaceAdapter = typefaceAdapter |
| 979 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 980 | paragraph.layout(Float.MAX_VALUE) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 981 | |
| 982 | verify(typefaceAdapter, times(1)).create( |
| 983 | fontFamily = eq(fontFamily), |
| 984 | fontWeight = eq(FontWeight.normal), |
Siyamed Sinir | adf982d | 2019-05-20 17:35:13 -0700 | [diff] [blame] | 985 | fontStyle = eq(FontStyle.Normal), |
Siyamed Sinir | 934a344 | 2019-06-18 12:57:13 -0700 | [diff] [blame] | 986 | fontSynthesis = eq(FontSynthesis.All) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 987 | ) |
| 988 | |
| 989 | val typeface = paragraph.textPaint.typeface |
| 990 | assertThat(typeface, not(nullValue())) |
| 991 | assertThat(typeface.isBold, equalTo(false)) |
| 992 | assertThat(typeface.isItalic, equalTo(false)) |
| 993 | } |
| 994 | |
| 995 | @Test |
| 996 | fun testFontFamily_withCustomFont() { |
| 997 | val typefaceAdapter = spy(TypefaceAdapter()) |
| 998 | val paragraph = simpleParagraph( |
| 999 | text = "abc", |
| 1000 | fontFamily = fontFamily, |
| 1001 | typefaceAdapter = typefaceAdapter |
| 1002 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 1003 | paragraph.layout(Float.MAX_VALUE) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 1004 | |
| 1005 | verify(typefaceAdapter, times(1)).create( |
| 1006 | fontFamily = eq(fontFamily), |
| 1007 | fontWeight = eq(FontWeight.normal), |
Siyamed Sinir | adf982d | 2019-05-20 17:35:13 -0700 | [diff] [blame] | 1008 | fontStyle = eq(FontStyle.Normal), |
Siyamed Sinir | 934a344 | 2019-06-18 12:57:13 -0700 | [diff] [blame] | 1009 | fontSynthesis = eq(FontSynthesis.All) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 1010 | ) |
| 1011 | val typeface = paragraph.textPaint.typeface |
| 1012 | assertThat(typeface.isBold, equalTo(false)) |
| 1013 | assertThat(typeface.isItalic, equalTo(false)) |
| 1014 | } |
| 1015 | |
Haoyu Zhang | c11ef5a | 2019-01-14 16:35:05 -0800 | [diff] [blame] | 1016 | @Test |
| 1017 | fun testEllipsis_withMaxLineEqualsNull_doesNotEllipsis() { |
| 1018 | val text = "abc" |
| 1019 | val fontSize = 20f |
| 1020 | val paragraphWidth = (text.length - 1) * fontSize |
| 1021 | val paragraph = simpleParagraph( |
| 1022 | text = text, |
| 1023 | fontFamily = fontFamily, |
| 1024 | fontSize = fontSize, |
| 1025 | ellipsis = true |
| 1026 | ) |
| 1027 | paragraph.layout(paragraphWidth) |
| 1028 | for (i in 0 until paragraph.lineCount) { |
| 1029 | assertFalse(paragraph.isEllipsisApplied(i)) |
| 1030 | } |
| 1031 | } |
| 1032 | |
| 1033 | @Test |
| 1034 | fun testEllipsis_withMaxLinesLessThanTextLines_doesEllipsis() { |
| 1035 | val text = "abcde" |
| 1036 | val fontSize = 100f |
| 1037 | // Note that on API 21, if the next line only contains 1 character, ellipsis won't work |
| 1038 | val paragraphWidth = (text.length - 1.5f) * fontSize |
| 1039 | val paragraph = simpleParagraph( |
| 1040 | text = text, |
| 1041 | fontFamily = fontFamily, |
| 1042 | fontSize = fontSize, |
| 1043 | ellipsis = true, |
| 1044 | maxLines = 1 |
| 1045 | ) |
| 1046 | paragraph.layout(paragraphWidth) |
| 1047 | |
| 1048 | assertTrue(paragraph.isEllipsisApplied(0)) |
| 1049 | } |
| 1050 | |
| 1051 | @Test |
| 1052 | fun testEllipsis_withMaxLinesMoreThanTextLines_doesNotEllipsis() { |
| 1053 | val text = "abc" |
| 1054 | val fontSize = 100f |
| 1055 | val paragraphWidth = (text.length - 1) * fontSize |
| 1056 | val maxLines = ceil(text.length * fontSize / paragraphWidth).toInt() |
| 1057 | val paragraph = simpleParagraph( |
| 1058 | text = text, |
| 1059 | fontFamily = fontFamily, |
| 1060 | fontSize = fontSize, |
| 1061 | ellipsis = true, |
| 1062 | maxLines = maxLines |
| 1063 | ) |
| 1064 | paragraph.layout(paragraphWidth) |
| 1065 | |
| 1066 | for (i in 0 until paragraph.lineCount) { |
| 1067 | assertFalse(paragraph.isEllipsisApplied(i)) |
| 1068 | } |
| 1069 | } |
| 1070 | |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 1071 | private fun simpleParagraph( |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 1072 | text: String = "", |
| 1073 | textStyles: List<AnnotatedString.Item<TextStyle>> = listOf(), |
Siyamed Sinir | 2690d53 | 2019-06-21 13:16:00 -0700 | [diff] [blame] | 1074 | textIndent: TextIndent? = null, |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 1075 | textAlign: TextAlign? = null, |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 1076 | fontSize: Float? = null, |
Haoyu Zhang | c11ef5a | 2019-01-14 16:35:05 -0800 | [diff] [blame] | 1077 | ellipsis: Boolean? = null, |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 1078 | maxLines: Int? = null, |
| 1079 | fontFamily: FontFamily? = null, |
| 1080 | fontWeight: FontWeight? = null, |
| 1081 | fontStyle: FontStyle? = null, |
| 1082 | typefaceAdapter: TypefaceAdapter = TypefaceAdapter() |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 1083 | ): ParagraphAndroid { |
| 1084 | return ParagraphAndroid( |
Haoyu Zhang | 2742989 | 2019-06-11 13:19:57 -0700 | [diff] [blame] | 1085 | text = text, |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 1086 | textStyles = textStyles, |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 1087 | typefaceAdapter = typefaceAdapter, |
Siyamed Sinir | 883e023 | 2019-06-25 12:38:12 -0700 | [diff] [blame^] | 1088 | style = TextStyle( |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 1089 | fontFamily = fontFamily, |
| 1090 | fontSize = fontSize, |
| 1091 | fontWeight = fontWeight, |
| 1092 | fontStyle = fontStyle |
haoyu | a8e48a5 | 2019-06-25 12:23:27 -0700 | [diff] [blame] | 1093 | ), |
Siyamed Sinir | 883e023 | 2019-06-25 12:38:12 -0700 | [diff] [blame^] | 1094 | paragraphStyle = ParagraphStyle( |
| 1095 | textAlign = textAlign, |
| 1096 | textIndent = textIndent, |
| 1097 | ellipsis = ellipsis, |
| 1098 | maxLines = maxLines |
| 1099 | ) |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 1100 | ) |
| 1101 | } |
Siyamed Sinir | 2690d53 | 2019-06-21 13:16:00 -0700 | [diff] [blame] | 1102 | } |