Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 1 | package androidx.ui.port.engine.text.platform |
| 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 | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 8 | import android.text.style.LocaleSpan |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 9 | import android.text.style.StrikethroughSpan |
| 10 | import android.text.style.UnderlineSpan |
Haoyu Zhang | 97d5e72 | 2019-01-04 17:32:34 -0800 | [diff] [blame^] | 11 | import androidx.test.filters.SdkSuppress |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 12 | import androidx.test.filters.SmallTest |
| 13 | import androidx.test.platform.app.InstrumentationRegistry |
Haoyu Zhang | 2eb38cd | 2018-11-21 15:05:28 -0800 | [diff] [blame] | 14 | import androidx.text.StaticLayoutCompat |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 15 | import androidx.text.style.LetterSpacingSpan |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 16 | import androidx.text.style.TypefaceSpan |
Haoyu Zhang | 97d5e72 | 2019-01-04 17:32:34 -0800 | [diff] [blame^] | 17 | import androidx.text.style.WordSpacingSpan |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 18 | import androidx.ui.engine.text.FontStyle |
Siyamed Sinir | 2179601 | 2018-12-26 13:51:17 -0800 | [diff] [blame] | 19 | import androidx.ui.engine.text.FontSynthesis |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 20 | import androidx.ui.engine.text.FontWeight |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 21 | import androidx.ui.engine.text.ParagraphBuilder |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 22 | import androidx.ui.engine.text.ParagraphStyle |
| 23 | import androidx.ui.engine.text.TextAlign |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 24 | import androidx.ui.engine.text.TextDecoration |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 25 | import androidx.ui.engine.text.TextStyle |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 26 | import androidx.ui.engine.text.font.FontFamily |
| 27 | import androidx.ui.engine.text.font.asFontFamily |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 28 | import androidx.ui.engine.text.platform.ParagraphAndroid |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 29 | import androidx.ui.engine.text.platform.TypefaceAdapter |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 30 | import androidx.ui.engine.window.Locale |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 31 | import androidx.ui.painting.Color |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 32 | import androidx.ui.port.engine.text.FontTestData.Companion.BASIC_MEASURE_FONT |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 33 | import androidx.ui.port.matchers.equalToBitmap |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 34 | import androidx.ui.port.matchers.hasSpan |
| 35 | import androidx.ui.port.matchers.hasSpanOnTop |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 36 | import com.nhaarman.mockitokotlin2.any |
| 37 | import com.nhaarman.mockitokotlin2.eq |
| 38 | import com.nhaarman.mockitokotlin2.mock |
| 39 | import com.nhaarman.mockitokotlin2.never |
| 40 | import com.nhaarman.mockitokotlin2.spy |
| 41 | import com.nhaarman.mockitokotlin2.times |
| 42 | import com.nhaarman.mockitokotlin2.verify |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 43 | import org.hamcrest.Matchers.equalTo |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 44 | import org.hamcrest.Matchers.not |
| 45 | import org.hamcrest.Matchers.nullValue |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 46 | import org.junit.Assert.assertThat |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 47 | import org.junit.Before |
| 48 | import org.junit.Test |
| 49 | import org.junit.runner.RunWith |
| 50 | import org.junit.runners.JUnit4 |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 51 | import kotlin.math.ceil |
| 52 | |
| 53 | @RunWith(JUnit4::class) |
| 54 | @SmallTest |
| 55 | class ParagraphAndroidTest { |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 56 | private lateinit var fontFamily: FontFamily |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 57 | |
| 58 | @Before |
| 59 | fun setup() { |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 60 | // This sample font provides the following features: |
| 61 | // 1. The width of most of visible characters equals to font size. |
| 62 | // 2. The LTR/RTL characters are rendered as â–¶/â—€. |
| 63 | // 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] | 64 | fontFamily = BASIC_MEASURE_FONT.asFontFamily() |
| 65 | fontFamily.context = InstrumentationRegistry.getInstrumentation().context |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | @Test |
| 69 | fun draw_with_newline_and_line_break_default_values() { |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 70 | val fontSize = 50.0f |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 71 | for (text in arrayOf("abc\ndef", "\u05D0\u05D1\u05D2\n\u05D3\u05D4\u05D5")) { |
| 72 | val paragraphAndroid = simpleParagraph( |
| 73 | text = StringBuilder(text), |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 74 | fontSize = fontSize, |
| 75 | fontFamily = fontFamily |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 76 | ) |
| 77 | |
| 78 | // 2 chars width |
| 79 | paragraphAndroid.layout(width = 2 * fontSize) |
| 80 | |
| 81 | val textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 82 | textPaint.textSize = fontSize |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 83 | textPaint.typeface = TypefaceAdapter().create(fontFamily) |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 84 | |
Haoyu Zhang | 2eb38cd | 2018-11-21 15:05:28 -0800 | [diff] [blame] | 85 | val staticLayout = StaticLayoutCompat.Builder( |
| 86 | text, |
| 87 | textPaint, |
| 88 | ceil(paragraphAndroid.width).toInt() |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 89 | ) |
Haoyu Zhang | 2eb38cd | 2018-11-21 15:05:28 -0800 | [diff] [blame] | 90 | .setEllipsizedWidth(ceil(paragraphAndroid.width).toInt()) |
| 91 | .build() |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 92 | assertThat(paragraphAndroid.bitmap(), equalToBitmap(staticLayout.bitmap())) |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 93 | } |
| 94 | } |
| 95 | |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 96 | @Test |
| 97 | fun textStyle_setColorOnWholeText() { |
| 98 | val text = "abcde" |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 99 | val textStyle = TextStyle(color = Color(0xFF0000FF.toInt())) |
| 100 | |
| 101 | val paragraph = simpleParagraph( |
| 102 | text = text, |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 103 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length)) |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 104 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 105 | paragraph.layout(100.0f) |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 106 | |
| 107 | assertThat(paragraph.underlyingText, hasSpan(ForegroundColorSpan::class, 0, text.length)) |
| 108 | } |
| 109 | |
| 110 | @Test |
| 111 | fun textStyle_setColorOnPartOfText() { |
| 112 | val text = "abcde" |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 113 | val textStyle = TextStyle(color = Color(0xFF0000FF.toInt())) |
| 114 | |
| 115 | val paragraph = simpleParagraph( |
| 116 | text = text, |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 117 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, "abc".length)) |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 118 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 119 | paragraph.layout(100.0f) |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 120 | |
| 121 | assertThat(paragraph.underlyingText, hasSpan(ForegroundColorSpan::class, 0, "abc".length)) |
| 122 | } |
| 123 | |
| 124 | @Test |
| 125 | fun textStyle_setColorTwice_lastOneOverwrite() { |
| 126 | val text = "abcde" |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 127 | val textStyle = TextStyle(color = Color(0xFF0000FF.toInt())) |
| 128 | val textStyleOverwrite = TextStyle(color = Color(0xFF00FF00.toInt())) |
| 129 | |
| 130 | val paragraph = simpleParagraph( |
| 131 | text = text, |
| 132 | textStyles = listOf( |
| 133 | ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length), |
| 134 | ParagraphBuilder.TextStyleIndex(textStyleOverwrite, 0, "abc".length) |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 135 | ) |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 136 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 137 | paragraph.layout(100.0f) |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 138 | |
| 139 | assertThat(paragraph.underlyingText, hasSpan(ForegroundColorSpan::class, 0, text.length)) |
| 140 | assertThat(paragraph.underlyingText, hasSpan(ForegroundColorSpan::class, 0, "abc".length)) |
| 141 | assertThat( |
| 142 | paragraph.underlyingText, |
| 143 | hasSpanOnTop(ForegroundColorSpan::class, 0, "abc".length) |
| 144 | ) |
| 145 | } |
| 146 | |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 147 | @Test |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 148 | fun testStyle_setTextDecorationOnWholeText_withLineThrough() { |
| 149 | val text = "abcde" |
| 150 | val textStyle = TextStyle(decoration = TextDecoration.lineThrough) |
| 151 | |
| 152 | val paragraph = simpleParagraph( |
| 153 | text = text, |
| 154 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length)) |
| 155 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 156 | paragraph.layout(100.0f) |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 157 | |
| 158 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 159 | assertThat(paragraph.underlyingText, hasSpan(StrikethroughSpan::class, 0, text.length)) |
| 160 | } |
| 161 | |
| 162 | @Test |
| 163 | fun testStyle_setTextDecorationOnWholeText_withUnderline() { |
| 164 | val text = "abcde" |
| 165 | val textStyle = TextStyle(decoration = TextDecoration.underline) |
| 166 | |
| 167 | val paragraph = simpleParagraph( |
| 168 | text = text, |
| 169 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length)) |
| 170 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 171 | paragraph.layout(100.0f) |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 172 | |
| 173 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 174 | assertThat(paragraph.underlyingText, hasSpan(UnderlineSpan::class, 0, text.length)) |
| 175 | } |
| 176 | |
| 177 | @Test |
| 178 | fun testStyle_setTextDecorationOnPartText_withLineThrough() { |
| 179 | val text = "abcde" |
| 180 | val textStyle = TextStyle(decoration = TextDecoration.lineThrough) |
| 181 | |
| 182 | val paragraph = simpleParagraph( |
| 183 | text = text, |
| 184 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, "abc".length)) |
| 185 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 186 | paragraph.layout(100.0f) |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 187 | |
| 188 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 189 | assertThat(paragraph.underlyingText, hasSpan(StrikethroughSpan::class, 0, "abc".length)) |
| 190 | } |
| 191 | |
| 192 | @Test |
| 193 | fun testStyle_setTextDecorationOnPartText_withUnderline() { |
| 194 | val text = "abcde" |
| 195 | val textStyle = TextStyle(decoration = TextDecoration.underline) |
| 196 | |
| 197 | val paragraph = simpleParagraph( |
| 198 | text = text, |
| 199 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, "abc".length)) |
| 200 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 201 | paragraph.layout(100.0f) |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 202 | |
| 203 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 204 | assertThat(paragraph.underlyingText, hasSpan(UnderlineSpan::class, 0, "abc".length)) |
| 205 | } |
| 206 | |
| 207 | @Test |
| 208 | fun testStyle_setTextDecoration_withLineThroughAndUnderline() { |
| 209 | val text = "abcde" |
| 210 | val textStyle = TextStyle( |
| 211 | decoration = TextDecoration.combine( |
| 212 | listOf(TextDecoration.lineThrough, TextDecoration.underline) |
| 213 | ) |
| 214 | ) |
| 215 | |
| 216 | val paragraph = simpleParagraph( |
| 217 | text = text, |
| 218 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, "abc".length)) |
| 219 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 220 | paragraph.layout(100.0f) |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 221 | |
| 222 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 223 | assertThat(paragraph.underlyingText, hasSpan(UnderlineSpan::class, 0, "abc".length)) |
| 224 | assertThat(paragraph.underlyingText, hasSpan(StrikethroughSpan::class, 0, "abc".length)) |
| 225 | } |
| 226 | |
| 227 | @Test |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 228 | fun textStyle_setFontSizeOnWholeText() { |
| 229 | val text = "abcde" |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 230 | val fontSize = 20.0f |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 231 | val paragraphWidth = text.length * fontSize |
| 232 | val textStyle = TextStyle(fontSize = fontSize) |
| 233 | |
| 234 | val paragraph = simpleParagraph( |
| 235 | text = text, |
| 236 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length)) |
| 237 | ) |
| 238 | paragraph.layout(paragraphWidth) |
| 239 | |
| 240 | assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, text.length)) |
| 241 | } |
| 242 | |
| 243 | @Test |
| 244 | fun textStyle_setFontSizeOnPartText() { |
| 245 | val text = "abcde" |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 246 | val fontSize = 20.0f |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 247 | val paragraphWidth = text.length * fontSize |
| 248 | val textStyle = TextStyle(fontSize = fontSize) |
| 249 | |
| 250 | val paragraph = simpleParagraph( |
| 251 | text = text, |
| 252 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, "abc".length)) |
| 253 | ) |
| 254 | paragraph.layout(paragraphWidth) |
| 255 | |
| 256 | assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, "abc".length)) |
| 257 | } |
| 258 | |
| 259 | @Test |
| 260 | fun textStyle_setFontSizeTwice_lastOneOverwrite() { |
| 261 | val text = "abcde" |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 262 | val fontSize = 20.0f |
| 263 | val fontSizeOverwrite = 30.0f |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 264 | val paragraphWidth = text.length * fontSizeOverwrite |
| 265 | val textStyle = TextStyle(fontSize = fontSize) |
| 266 | val textStyleOverwrite = TextStyle(fontSize = fontSizeOverwrite) |
| 267 | |
| 268 | val paragraph = simpleParagraph( |
| 269 | text = text, |
| 270 | textStyles = listOf( |
| 271 | ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length), |
| 272 | ParagraphBuilder.TextStyleIndex(textStyleOverwrite, 0, "abc".length) |
| 273 | ) |
| 274 | ) |
| 275 | paragraph.layout(paragraphWidth) |
| 276 | |
| 277 | assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, text.length)) |
| 278 | assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, "abc".length)) |
| 279 | assertThat( |
| 280 | paragraph.underlyingText, |
| 281 | hasSpanOnTop(AbsoluteSizeSpan::class, 0, "abc".length) |
| 282 | ) |
| 283 | } |
| 284 | |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 285 | @Test |
| 286 | fun textStyle_setLetterSpacingOnWholeText() { |
| 287 | val text = "abcde" |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 288 | val letterSpacing = 2.0f |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 289 | val textStyle = TextStyle(letterSpacing = letterSpacing) |
| 290 | |
| 291 | val paragraph = simpleParagraph( |
| 292 | text = text, |
| 293 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length)) |
| 294 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 295 | paragraph.layout(100.0f) |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 296 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 297 | assertThat(paragraph.underlyingText, hasSpan(LetterSpacingSpan::class, 0, text.length)) |
| 298 | } |
| 299 | |
| 300 | @Test |
| 301 | fun textStyle_setLetterSpacingOnPartText() { |
| 302 | val text = "abcde" |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 303 | val textStyle = TextStyle(letterSpacing = 2.0f) |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 304 | |
| 305 | val paragraph = simpleParagraph( |
| 306 | text = text, |
| 307 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, "abc".length)) |
| 308 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 309 | paragraph.layout(100.0f) |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 310 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 311 | assertThat(paragraph.underlyingText, hasSpan(LetterSpacingSpan::class, 0, "abc".length)) |
| 312 | } |
| 313 | |
| 314 | @Test |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 315 | fun textStyle_setLetterSpacingTwice_lastOneOverwrite() { |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 316 | val text = "abcde" |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 317 | val textStyle = TextStyle(letterSpacing = 2.0f) |
| 318 | val textStyleOverwrite = TextStyle(letterSpacing = 3.0f) |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 319 | |
| 320 | val paragraph = simpleParagraph( |
| 321 | text = text, |
| 322 | textStyles = listOf( |
| 323 | ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length), |
| 324 | ParagraphBuilder.TextStyleIndex(textStyleOverwrite, 0, "abc".length) |
| 325 | ) |
| 326 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 327 | paragraph.layout(100.0f) |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 328 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 329 | assertThat(paragraph.underlyingText, hasSpan(LetterSpacingSpan::class, 0, text.length)) |
| 330 | assertThat(paragraph.underlyingText, hasSpan(LetterSpacingSpan::class, 0, "abc".length)) |
| 331 | assertThat( |
| 332 | paragraph.underlyingText, |
| 333 | hasSpanOnTop(LetterSpacingSpan::class, 0, "abc".length) |
| 334 | ) |
| 335 | } |
| 336 | |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 337 | @Test |
Haoyu Zhang | 97d5e72 | 2019-01-04 17:32:34 -0800 | [diff] [blame^] | 338 | @SdkSuppress(minSdkVersion = 28) |
| 339 | fun textStyle_setWordSpacingOnWholeText() { |
| 340 | val text = "ab cd" |
| 341 | val wordSpacing = 2.0f |
| 342 | val textStyle = TextStyle(wordSpacing = wordSpacing) |
| 343 | |
| 344 | val paragraph = simpleParagraph( |
| 345 | text = text, |
| 346 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length)) |
| 347 | ) |
| 348 | // Notice that the width doesn't matter for this test. |
| 349 | paragraph.layout(100.0f) |
| 350 | |
| 351 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 352 | assertThat( |
| 353 | paragraph.underlyingText, |
| 354 | hasSpan(WordSpacingSpan::class, 0, text.length) { span -> |
| 355 | span.wordSpacing == wordSpacing.toFloat() |
| 356 | } |
| 357 | ) |
| 358 | } |
| 359 | |
| 360 | @Test |
| 361 | @SdkSuppress(minSdkVersion = 28) |
| 362 | fun textStyle_setWordSpacingOnPartText() { |
| 363 | val text = "abc d" |
| 364 | val wordSpacing = 2.0f |
| 365 | val textStyle = TextStyle(wordSpacing = wordSpacing) |
| 366 | |
| 367 | val paragraph = simpleParagraph( |
| 368 | text = text, |
| 369 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, "abc".length)) |
| 370 | ) |
| 371 | // Notice that the width doesn't matter for this test. |
| 372 | paragraph.layout(100.0f) |
| 373 | |
| 374 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 375 | assertThat( |
| 376 | paragraph.underlyingText, |
| 377 | hasSpan(WordSpacingSpan::class, 0, "abc".length) { span -> |
| 378 | span.wordSpacing == wordSpacing.toFloat() |
| 379 | } |
| 380 | ) |
| 381 | } |
| 382 | |
| 383 | @Test |
| 384 | @SdkSuppress(minSdkVersion = 28) |
| 385 | fun textStyle_setWordSpacingTwice_lastOneOverwrite() { |
| 386 | val text = "abc d" |
| 387 | val wordSpacing = 2.0f |
| 388 | val textStyle = TextStyle(wordSpacing = wordSpacing) |
| 389 | val wordSpacingOverwrite = 3.0f |
| 390 | val textStyleOverwrite = TextStyle(wordSpacing = wordSpacingOverwrite) |
| 391 | |
| 392 | val paragraph = simpleParagraph( |
| 393 | text = text, |
| 394 | textStyles = listOf( |
| 395 | ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length), |
| 396 | ParagraphBuilder.TextStyleIndex(textStyleOverwrite, 0, "abc".length) |
| 397 | ) |
| 398 | ) |
| 399 | // Notice that the width doesn't matter for this test. |
| 400 | paragraph.layout(100.0f) |
| 401 | |
| 402 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 403 | assertThat( |
| 404 | paragraph.underlyingText, |
| 405 | hasSpan(WordSpacingSpan::class, 0, text.length) { span -> |
| 406 | span.wordSpacing == wordSpacing.toFloat() |
| 407 | } |
| 408 | ) |
| 409 | assertThat( |
| 410 | paragraph.underlyingText, |
| 411 | hasSpan(WordSpacingSpan::class, 0, "abc".length) { span -> |
| 412 | span.wordSpacing == wordSpacingOverwrite.toFloat() |
| 413 | } |
| 414 | ) |
| 415 | assertThat( |
| 416 | paragraph.underlyingText, |
| 417 | hasSpanOnTop(WordSpacingSpan::class, 0, "abc".length) { span -> |
| 418 | span.wordSpacing == wordSpacingOverwrite.toFloat() |
| 419 | } |
| 420 | ) |
| 421 | } |
| 422 | |
| 423 | @Test |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 424 | fun textStyle_setBackgroundOnWholeText() { |
| 425 | val text = "abcde" |
| 426 | val color = Color(0xFF0000FF.toInt()) |
Haoyu Zhang | 437d945 | 2019-01-04 17:34:04 -0800 | [diff] [blame] | 427 | val textStyle = TextStyle(background = color) |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 428 | |
| 429 | val paragraph = simpleParagraph( |
| 430 | text = text, |
| 431 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length)) |
| 432 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 433 | paragraph.layout(100.0f) |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 434 | |
| 435 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 436 | assertThat(paragraph.underlyingText, |
| 437 | hasSpan(BackgroundColorSpan::class, 0, text.length) { span -> |
| 438 | span.backgroundColor == color.value |
| 439 | } |
| 440 | ) |
| 441 | } |
| 442 | |
| 443 | @Test |
| 444 | fun textStyle_setBackgroundOnPartText() { |
| 445 | val text = "abcde" |
| 446 | val color = Color(0xFF0000FF.toInt()) |
Haoyu Zhang | 437d945 | 2019-01-04 17:34:04 -0800 | [diff] [blame] | 447 | val textStyle = TextStyle(background = color) |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 448 | |
| 449 | val paragraph = simpleParagraph( |
| 450 | text = text, |
| 451 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, "abc".length)) |
| 452 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 453 | paragraph.layout(100.0f) |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 454 | |
| 455 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 456 | assertThat(paragraph.underlyingText, |
| 457 | hasSpan(BackgroundColorSpan::class, 0, "abc".length) { span -> |
| 458 | span.backgroundColor == color.value |
| 459 | } |
| 460 | ) |
| 461 | } |
| 462 | |
| 463 | @Test |
| 464 | fun textStyle_setBackgroundTwice_lastOneOverwrite() { |
| 465 | val text = "abcde" |
| 466 | val color = Color(0xFF0000FF.toInt()) |
Haoyu Zhang | 437d945 | 2019-01-04 17:34:04 -0800 | [diff] [blame] | 467 | val textStyle = TextStyle(background = color) |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 468 | val colorOverwrite = Color(0xFF00FF00.toInt()) |
Haoyu Zhang | 437d945 | 2019-01-04 17:34:04 -0800 | [diff] [blame] | 469 | val textStyleOverwrite = TextStyle(background = colorOverwrite) |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 470 | |
| 471 | val paragraph = simpleParagraph( |
| 472 | text = text, |
| 473 | textStyles = listOf( |
| 474 | ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length), |
| 475 | ParagraphBuilder.TextStyleIndex(textStyleOverwrite, 0, "abc".length) |
| 476 | ) |
| 477 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 478 | paragraph.layout(100.0f) |
Haoyu Zhang | 0c4fd02 | 2019-01-02 13:53:10 -0800 | [diff] [blame] | 479 | |
| 480 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 481 | assertThat(paragraph.underlyingText, |
| 482 | hasSpan(BackgroundColorSpan::class, 0, text.length) { span -> |
| 483 | span.backgroundColor == color.value |
| 484 | } |
| 485 | ) |
| 486 | assertThat(paragraph.underlyingText, |
| 487 | hasSpan(BackgroundColorSpan::class, 0, "abc".length) { span -> |
| 488 | span.backgroundColor == colorOverwrite.value |
| 489 | } |
| 490 | ) |
| 491 | assertThat( |
| 492 | paragraph.underlyingText, |
| 493 | hasSpanOnTop(BackgroundColorSpan::class, 0, "abc".length) { span -> |
| 494 | span.backgroundColor == colorOverwrite.value |
| 495 | } |
| 496 | ) |
| 497 | } |
| 498 | |
| 499 | @Test |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 500 | fun textStyle_setLocaleOnWholeText() { |
| 501 | val text = "abcde" |
| 502 | val locale = Locale("en", "US") |
| 503 | val textStyle = TextStyle(locale = locale) |
| 504 | |
| 505 | val paragraph = simpleParagraph( |
| 506 | text = text, |
| 507 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length)) |
| 508 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 509 | paragraph.layout(100.0f) |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 510 | |
| 511 | assertThat(paragraph.underlyingText, hasSpan(LocaleSpan::class, 0, text.length)) |
| 512 | } |
| 513 | |
| 514 | @Test |
| 515 | fun textStyle_setLocaleOnPartText() { |
| 516 | val text = "abcde" |
| 517 | val locale = Locale("en", "US") |
| 518 | val textStyle = TextStyle(locale = locale) |
| 519 | |
| 520 | val paragraph = simpleParagraph( |
| 521 | text = text, |
| 522 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, "abc".length)) |
| 523 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 524 | paragraph.layout(100.0f) |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 525 | |
| 526 | assertThat(paragraph.underlyingText, hasSpan(LocaleSpan::class, 0, "abc".length)) |
| 527 | } |
| 528 | |
| 529 | @Test |
| 530 | fun textStyle_setLocaleTwice_lastOneOverwrite() { |
| 531 | val text = "abcde" |
| 532 | val textStyle = TextStyle(locale = Locale("en", "US")) |
| 533 | val textStyleOverwrite = TextStyle(locale = Locale("ja", "JP")) |
| 534 | |
| 535 | val paragraph = simpleParagraph( |
| 536 | text = text, |
| 537 | textStyles = listOf( |
| 538 | ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length), |
| 539 | ParagraphBuilder.TextStyleIndex(textStyleOverwrite, 0, "abc".length) |
| 540 | ) |
| 541 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 542 | paragraph.layout(100.0f) |
Haoyu Zhang | 4b201d2 | 2018-12-18 14:27:38 -0800 | [diff] [blame] | 543 | |
| 544 | assertThat(paragraph.underlyingText, hasSpan(LocaleSpan::class, 0, text.length)) |
| 545 | assertThat(paragraph.underlyingText, hasSpan(LocaleSpan::class, 0, "abc".length)) |
| 546 | assertThat( |
| 547 | paragraph.underlyingText, |
| 548 | hasSpanOnTop(LocaleSpan::class, 0, "abc".length) |
| 549 | ) |
| 550 | } |
| 551 | |
| 552 | @Test |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 553 | fun textStyle_fontFamily_addsTypefaceSpanWithCorrectTypeface() { |
| 554 | val text = "abcde" |
| 555 | val textStyle = TextStyle( |
| 556 | fontFamily = fontFamily, |
| 557 | fontStyle = FontStyle.italic, |
| 558 | fontWeight = FontWeight.bold |
| 559 | ) |
| 560 | val expectedTypeface = TypefaceAdapter().create( |
| 561 | fontFamily = fontFamily, |
| 562 | fontStyle = FontStyle.italic, |
| 563 | fontWeight = FontWeight.bold |
| 564 | ) |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame] | 565 | val expectedStart = 0 |
| 566 | val expectedEnd = "abc".length |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 567 | |
| 568 | val paragraph = simpleParagraph( |
| 569 | text = text, |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame] | 570 | textStyles = listOf( |
| 571 | ParagraphBuilder.TextStyleIndex( |
| 572 | textStyle, |
| 573 | expectedStart, |
| 574 | expectedEnd |
| 575 | ) |
| 576 | ) |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 577 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 578 | paragraph.layout(100.0f) |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 579 | |
| 580 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 581 | assertThat( |
| 582 | paragraph.underlyingText, |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame] | 583 | hasSpan(TypefaceSpan::class, expectedStart, expectedEnd) { span -> |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 584 | span.typeface == expectedTypeface |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame] | 585 | }) |
| 586 | } |
| 587 | |
| 588 | @Test |
| 589 | fun textStyle_fontFamily_whenFontSynthesizeTurnedOff() { |
| 590 | val text = "abcde" |
| 591 | val textStyle = TextStyle( |
| 592 | fontFamily = fontFamily, |
| 593 | fontStyle = FontStyle.italic, |
| 594 | fontWeight = FontWeight.bold, |
| 595 | fontSynthesis = FontSynthesis.none |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 596 | ) |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame] | 597 | val expectedTypeface = TypefaceAdapter().create( |
| 598 | fontFamily = fontFamily, |
| 599 | fontStyle = FontStyle.italic, |
| 600 | fontWeight = FontWeight.bold, |
| 601 | fontSynthesis = FontSynthesis.none |
| 602 | ) |
| 603 | val expectedStart = 0 |
| 604 | val expectedEnd = "abc".length |
| 605 | |
| 606 | val paragraph = simpleParagraph( |
| 607 | text = text, |
| 608 | textStyles = listOf( |
| 609 | ParagraphBuilder.TextStyleIndex( |
| 610 | textStyle, |
| 611 | expectedStart, |
| 612 | expectedEnd |
| 613 | ) |
| 614 | ) |
| 615 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 616 | paragraph.layout(100.0f) |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame] | 617 | |
| 618 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 619 | assertThat( |
| 620 | paragraph.underlyingText, |
| 621 | hasSpan(TypefaceSpan::class, expectedStart, expectedEnd) { span -> |
| 622 | span.typeface == expectedTypeface |
| 623 | }) |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 624 | } |
| 625 | |
| 626 | @Test |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 627 | fun testEmptyFontFamily() { |
| 628 | val typefaceAdapter = mock<TypefaceAdapter>() |
| 629 | val paragraph = simpleParagraph( |
| 630 | text = "abc", |
| 631 | typefaceAdapter = typefaceAdapter |
| 632 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 633 | paragraph.layout(Float.MAX_VALUE) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 634 | |
| 635 | verify(typefaceAdapter, never()).create( |
| 636 | fontFamily = any(), |
| 637 | fontWeight = any(), |
Siyamed Sinir | 2179601 | 2018-12-26 13:51:17 -0800 | [diff] [blame] | 638 | fontStyle = any(), |
| 639 | fontSynthesis = any() |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 640 | ) |
| 641 | assertThat(paragraph.textPaint.typeface, nullValue()) |
| 642 | } |
| 643 | |
| 644 | @Test |
| 645 | fun testEmptyFontFamily_withBoldFontWeightSelection() { |
| 646 | val typefaceAdapter = spy(TypefaceAdapter()) |
| 647 | |
| 648 | val paragraph = simpleParagraph( |
| 649 | text = "abc", |
| 650 | fontFamily = null, |
| 651 | fontWeight = FontWeight.bold, |
| 652 | typefaceAdapter = typefaceAdapter |
| 653 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 654 | paragraph.layout(Float.MAX_VALUE) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 655 | |
| 656 | verify(typefaceAdapter, times(1)).create( |
| 657 | fontFamily = eq(null), |
| 658 | fontWeight = eq(FontWeight.bold), |
Siyamed Sinir | 2179601 | 2018-12-26 13:51:17 -0800 | [diff] [blame] | 659 | fontStyle = eq(FontStyle.normal), |
| 660 | fontSynthesis = eq(FontSynthesis.all) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 661 | ) |
| 662 | |
| 663 | val typeface = paragraph.textPaint.typeface |
| 664 | assertThat(typeface, not(nullValue())) |
| 665 | assertThat(typeface.isBold, equalTo(true)) |
| 666 | assertThat(typeface.isItalic, equalTo(false)) |
| 667 | } |
| 668 | |
| 669 | @Test |
| 670 | fun testEmptyFontFamily_withFontStyleSelection() { |
| 671 | val typefaceAdapter = spy(TypefaceAdapter()) |
| 672 | val paragraph = simpleParagraph( |
| 673 | text = "abc", |
| 674 | fontFamily = null, |
| 675 | fontStyle = FontStyle.italic, |
| 676 | typefaceAdapter = typefaceAdapter |
| 677 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 678 | paragraph.layout(Float.MAX_VALUE) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 679 | |
| 680 | verify(typefaceAdapter, times(1)).create( |
| 681 | fontFamily = eq(null), |
| 682 | fontWeight = eq(FontWeight.normal), |
Siyamed Sinir | 2179601 | 2018-12-26 13:51:17 -0800 | [diff] [blame] | 683 | fontStyle = eq(FontStyle.italic), |
| 684 | fontSynthesis = eq(FontSynthesis.all) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 685 | ) |
| 686 | |
| 687 | val typeface = paragraph.textPaint.typeface |
| 688 | assertThat(typeface, not(nullValue())) |
| 689 | assertThat(typeface.isBold, equalTo(false)) |
| 690 | assertThat(typeface.isItalic, equalTo(true)) |
| 691 | } |
| 692 | |
| 693 | @Test |
| 694 | fun testFontFamily_withGenericFamilyName() { |
| 695 | val typefaceAdapter = spy(TypefaceAdapter()) |
| 696 | val fontFamily = FontFamily("sans-serif") |
| 697 | |
| 698 | val paragraph = simpleParagraph( |
| 699 | text = "abc", |
| 700 | fontFamily = fontFamily, |
| 701 | typefaceAdapter = typefaceAdapter |
| 702 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 703 | paragraph.layout(Float.MAX_VALUE) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 704 | |
| 705 | verify(typefaceAdapter, times(1)).create( |
| 706 | fontFamily = eq(fontFamily), |
| 707 | fontWeight = eq(FontWeight.normal), |
Siyamed Sinir | 2179601 | 2018-12-26 13:51:17 -0800 | [diff] [blame] | 708 | fontStyle = eq(FontStyle.normal), |
| 709 | fontSynthesis = eq(FontSynthesis.all) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 710 | ) |
| 711 | |
| 712 | val typeface = paragraph.textPaint.typeface |
| 713 | assertThat(typeface, not(nullValue())) |
| 714 | assertThat(typeface.isBold, equalTo(false)) |
| 715 | assertThat(typeface.isItalic, equalTo(false)) |
| 716 | } |
| 717 | |
| 718 | @Test |
| 719 | fun testFontFamily_withCustomFont() { |
| 720 | val typefaceAdapter = spy(TypefaceAdapter()) |
| 721 | val paragraph = simpleParagraph( |
| 722 | text = "abc", |
| 723 | fontFamily = fontFamily, |
| 724 | typefaceAdapter = typefaceAdapter |
| 725 | ) |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 726 | paragraph.layout(Float.MAX_VALUE) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 727 | |
| 728 | verify(typefaceAdapter, times(1)).create( |
| 729 | fontFamily = eq(fontFamily), |
| 730 | fontWeight = eq(FontWeight.normal), |
Siyamed Sinir | 2179601 | 2018-12-26 13:51:17 -0800 | [diff] [blame] | 731 | fontStyle = eq(FontStyle.normal), |
| 732 | fontSynthesis = eq(FontSynthesis.all) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 733 | ) |
| 734 | val typeface = paragraph.textPaint.typeface |
| 735 | assertThat(typeface.isBold, equalTo(false)) |
| 736 | assertThat(typeface.isItalic, equalTo(false)) |
| 737 | } |
| 738 | |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 739 | private fun simpleParagraph( |
| 740 | text: CharSequence = "", |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 741 | textStyles: List<ParagraphBuilder.TextStyleIndex> = listOf(), |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 742 | textAlign: TextAlign? = null, |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 743 | fontSize: Float? = null, |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 744 | maxLines: Int? = null, |
| 745 | fontFamily: FontFamily? = null, |
| 746 | fontWeight: FontWeight? = null, |
| 747 | fontStyle: FontStyle? = null, |
| 748 | typefaceAdapter: TypefaceAdapter = TypefaceAdapter() |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 749 | ): ParagraphAndroid { |
| 750 | return ParagraphAndroid( |
| 751 | text = StringBuilder(text), |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 752 | textStyles = textStyles, |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 753 | typefaceAdapter = typefaceAdapter, |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 754 | paragraphStyle = ParagraphStyle( |
| 755 | textAlign = textAlign, |
| 756 | maxLines = maxLines, |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 757 | fontFamily = fontFamily, |
| 758 | fontSize = fontSize, |
| 759 | fontWeight = fontWeight, |
| 760 | fontStyle = fontStyle |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 761 | ) |
| 762 | ) |
| 763 | } |
George Mount | b1cdd86 | 2018-12-21 10:54:42 -0800 | [diff] [blame] | 764 | } |