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 | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 6 | import android.text.style.ForegroundColorSpan |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 7 | import android.text.style.StrikethroughSpan |
| 8 | import android.text.style.UnderlineSpan |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 9 | import androidx.test.filters.SmallTest |
| 10 | import androidx.test.platform.app.InstrumentationRegistry |
Haoyu Zhang | 2eb38cd | 2018-11-21 15:05:28 -0800 | [diff] [blame] | 11 | import androidx.text.StaticLayoutCompat |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 12 | import androidx.text.style.LetterSpacingSpan |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 13 | import androidx.text.style.TypefaceSpan |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 14 | import androidx.ui.engine.text.FontStyle |
Siyamed Sinir | 2179601 | 2018-12-26 13:51:17 -0800 | [diff] [blame] | 15 | import androidx.ui.engine.text.FontSynthesis |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 16 | import androidx.ui.engine.text.FontWeight |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 17 | import androidx.ui.engine.text.ParagraphBuilder |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 18 | import androidx.ui.engine.text.ParagraphStyle |
| 19 | import androidx.ui.engine.text.TextAlign |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 20 | import androidx.ui.engine.text.TextDecoration |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 21 | import androidx.ui.engine.text.TextStyle |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 22 | import androidx.ui.engine.text.font.FontFamily |
| 23 | import androidx.ui.engine.text.font.asFontFamily |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 24 | import androidx.ui.engine.text.platform.ParagraphAndroid |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 25 | import androidx.ui.engine.text.platform.TypefaceAdapter |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 26 | import androidx.ui.painting.Color |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 27 | import androidx.ui.port.engine.text.FontTestData.Companion.BASIC_MEASURE_FONT |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 28 | import androidx.ui.port.matchers.equalToBitmap |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 29 | import androidx.ui.port.matchers.hasSpan |
| 30 | import androidx.ui.port.matchers.hasSpanOnTop |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 31 | import com.nhaarman.mockitokotlin2.any |
| 32 | import com.nhaarman.mockitokotlin2.eq |
| 33 | import com.nhaarman.mockitokotlin2.mock |
| 34 | import com.nhaarman.mockitokotlin2.never |
| 35 | import com.nhaarman.mockitokotlin2.spy |
| 36 | import com.nhaarman.mockitokotlin2.times |
| 37 | import com.nhaarman.mockitokotlin2.verify |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 38 | import org.hamcrest.Matchers.equalTo |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 39 | import org.hamcrest.Matchers.not |
| 40 | import org.hamcrest.Matchers.nullValue |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 41 | import org.junit.Assert.assertThat |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 42 | import org.junit.Before |
| 43 | import org.junit.Test |
| 44 | import org.junit.runner.RunWith |
| 45 | import org.junit.runners.JUnit4 |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 46 | import kotlin.math.ceil |
| 47 | |
| 48 | @RunWith(JUnit4::class) |
| 49 | @SmallTest |
| 50 | class ParagraphAndroidTest { |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 51 | private lateinit var fontFamily: FontFamily |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 52 | |
| 53 | @Before |
| 54 | fun setup() { |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 55 | // This sample font provides the following features: |
| 56 | // 1. The width of most of visible characters equals to font size. |
| 57 | // 2. The LTR/RTL characters are rendered as â–¶/â—€. |
| 58 | // 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] | 59 | fontFamily = BASIC_MEASURE_FONT.asFontFamily() |
| 60 | fontFamily.context = InstrumentationRegistry.getInstrumentation().context |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 61 | } |
| 62 | |
| 63 | @Test |
| 64 | fun draw_with_newline_and_line_break_default_values() { |
| 65 | val fontSize = 50.0 |
| 66 | for (text in arrayOf("abc\ndef", "\u05D0\u05D1\u05D2\n\u05D3\u05D4\u05D5")) { |
| 67 | val paragraphAndroid = simpleParagraph( |
| 68 | text = StringBuilder(text), |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 69 | fontSize = fontSize, |
| 70 | fontFamily = fontFamily |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 71 | ) |
| 72 | |
| 73 | // 2 chars width |
| 74 | paragraphAndroid.layout(width = 2 * fontSize) |
| 75 | |
| 76 | val textPaint = TextPaint(Paint.ANTI_ALIAS_FLAG) |
| 77 | textPaint.textSize = fontSize.toFloat() |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 78 | textPaint.typeface = TypefaceAdapter().create(fontFamily) |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 79 | |
Haoyu Zhang | 2eb38cd | 2018-11-21 15:05:28 -0800 | [diff] [blame] | 80 | val staticLayout = StaticLayoutCompat.Builder( |
| 81 | text, |
| 82 | textPaint, |
| 83 | ceil(paragraphAndroid.width).toInt() |
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 | .setEllipsizedWidth(ceil(paragraphAndroid.width).toInt()) |
| 86 | .build() |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 87 | assertThat(paragraphAndroid.bitmap(), equalToBitmap(staticLayout.bitmap())) |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 88 | } |
| 89 | } |
| 90 | |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 91 | @Test |
| 92 | fun textStyle_setColorOnWholeText() { |
| 93 | val text = "abcde" |
| 94 | val fontSize = 20.0 |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 95 | val paragraphWidth = text.length * fontSize |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 96 | val textStyle = TextStyle(color = Color(0xFF0000FF.toInt())) |
| 97 | |
| 98 | val paragraph = simpleParagraph( |
| 99 | text = text, |
| 100 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length)), |
| 101 | fontSize = fontSize |
| 102 | ) |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 103 | paragraph.layout(paragraphWidth) |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 104 | |
| 105 | assertThat(paragraph.underlyingText, hasSpan(ForegroundColorSpan::class, 0, text.length)) |
| 106 | } |
| 107 | |
| 108 | @Test |
| 109 | fun textStyle_setColorOnPartOfText() { |
| 110 | val text = "abcde" |
| 111 | val fontSize = 20.0 |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 112 | val paragraphWidth = text.length * fontSize |
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, |
| 117 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, "abc".length)), |
| 118 | fontSize = fontSize |
| 119 | ) |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 120 | paragraph.layout(paragraphWidth) |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 121 | |
| 122 | assertThat(paragraph.underlyingText, hasSpan(ForegroundColorSpan::class, 0, "abc".length)) |
| 123 | } |
| 124 | |
| 125 | @Test |
| 126 | fun textStyle_setColorTwice_lastOneOverwrite() { |
| 127 | val text = "abcde" |
| 128 | val fontSize = 20.0 |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 129 | val paragraphWidth = text.length * fontSize |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 130 | val textStyle = TextStyle(color = Color(0xFF0000FF.toInt())) |
| 131 | val textStyleOverwrite = TextStyle(color = Color(0xFF00FF00.toInt())) |
| 132 | |
| 133 | val paragraph = simpleParagraph( |
| 134 | text = text, |
| 135 | textStyles = listOf( |
| 136 | ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length), |
| 137 | ParagraphBuilder.TextStyleIndex(textStyleOverwrite, 0, "abc".length) |
| 138 | ), |
| 139 | fontSize = fontSize |
| 140 | ) |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 141 | paragraph.layout(paragraphWidth) |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 142 | |
| 143 | assertThat(paragraph.underlyingText, hasSpan(ForegroundColorSpan::class, 0, text.length)) |
| 144 | assertThat(paragraph.underlyingText, hasSpan(ForegroundColorSpan::class, 0, "abc".length)) |
| 145 | assertThat( |
| 146 | paragraph.underlyingText, |
| 147 | hasSpanOnTop(ForegroundColorSpan::class, 0, "abc".length) |
| 148 | ) |
| 149 | } |
| 150 | |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 151 | @Test |
Haoyu Zhang | b567720 | 2018-12-19 14:22:18 -0800 | [diff] [blame] | 152 | fun testStyle_setTextDecorationOnWholeText_withLineThrough() { |
| 153 | val text = "abcde" |
| 154 | val textStyle = TextStyle(decoration = TextDecoration.lineThrough) |
| 155 | |
| 156 | val paragraph = simpleParagraph( |
| 157 | text = text, |
| 158 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length)) |
| 159 | ) |
| 160 | paragraph.layout(100.0) |
| 161 | |
| 162 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 163 | assertThat(paragraph.underlyingText, hasSpan(StrikethroughSpan::class, 0, text.length)) |
| 164 | } |
| 165 | |
| 166 | @Test |
| 167 | fun testStyle_setTextDecorationOnWholeText_withUnderline() { |
| 168 | val text = "abcde" |
| 169 | val textStyle = TextStyle(decoration = TextDecoration.underline) |
| 170 | |
| 171 | val paragraph = simpleParagraph( |
| 172 | text = text, |
| 173 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length)) |
| 174 | ) |
| 175 | paragraph.layout(100.0) |
| 176 | |
| 177 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 178 | assertThat(paragraph.underlyingText, hasSpan(UnderlineSpan::class, 0, text.length)) |
| 179 | } |
| 180 | |
| 181 | @Test |
| 182 | fun testStyle_setTextDecorationOnPartText_withLineThrough() { |
| 183 | val text = "abcde" |
| 184 | val textStyle = TextStyle(decoration = TextDecoration.lineThrough) |
| 185 | |
| 186 | val paragraph = simpleParagraph( |
| 187 | text = text, |
| 188 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, "abc".length)) |
| 189 | ) |
| 190 | paragraph.layout(100.0) |
| 191 | |
| 192 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 193 | assertThat(paragraph.underlyingText, hasSpan(StrikethroughSpan::class, 0, "abc".length)) |
| 194 | } |
| 195 | |
| 196 | @Test |
| 197 | fun testStyle_setTextDecorationOnPartText_withUnderline() { |
| 198 | val text = "abcde" |
| 199 | val textStyle = TextStyle(decoration = TextDecoration.underline) |
| 200 | |
| 201 | val paragraph = simpleParagraph( |
| 202 | text = text, |
| 203 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, "abc".length)) |
| 204 | ) |
| 205 | paragraph.layout(100.0) |
| 206 | |
| 207 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 208 | assertThat(paragraph.underlyingText, hasSpan(UnderlineSpan::class, 0, "abc".length)) |
| 209 | } |
| 210 | |
| 211 | @Test |
| 212 | fun testStyle_setTextDecoration_withLineThroughAndUnderline() { |
| 213 | val text = "abcde" |
| 214 | val textStyle = TextStyle( |
| 215 | decoration = TextDecoration.combine( |
| 216 | listOf(TextDecoration.lineThrough, TextDecoration.underline) |
| 217 | ) |
| 218 | ) |
| 219 | |
| 220 | val paragraph = simpleParagraph( |
| 221 | text = text, |
| 222 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, "abc".length)) |
| 223 | ) |
| 224 | paragraph.layout(100.0) |
| 225 | |
| 226 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 227 | assertThat(paragraph.underlyingText, hasSpan(UnderlineSpan::class, 0, "abc".length)) |
| 228 | assertThat(paragraph.underlyingText, hasSpan(StrikethroughSpan::class, 0, "abc".length)) |
| 229 | } |
| 230 | |
| 231 | @Test |
Haoyu Zhang | bb51ccf | 2018-12-17 15:07:43 -0800 | [diff] [blame] | 232 | fun textStyle_setFontSizeOnWholeText() { |
| 233 | val text = "abcde" |
| 234 | val fontSize = 20.0 |
| 235 | val paragraphWidth = text.length * fontSize |
| 236 | val textStyle = TextStyle(fontSize = fontSize) |
| 237 | |
| 238 | val paragraph = simpleParagraph( |
| 239 | text = text, |
| 240 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length)) |
| 241 | ) |
| 242 | paragraph.layout(paragraphWidth) |
| 243 | |
| 244 | assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, text.length)) |
| 245 | } |
| 246 | |
| 247 | @Test |
| 248 | fun textStyle_setFontSizeOnPartText() { |
| 249 | val text = "abcde" |
| 250 | val fontSize = 20.0 |
| 251 | val paragraphWidth = text.length * fontSize |
| 252 | val textStyle = TextStyle(fontSize = fontSize) |
| 253 | |
| 254 | val paragraph = simpleParagraph( |
| 255 | text = text, |
| 256 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, "abc".length)) |
| 257 | ) |
| 258 | paragraph.layout(paragraphWidth) |
| 259 | |
| 260 | assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, "abc".length)) |
| 261 | } |
| 262 | |
| 263 | @Test |
| 264 | fun textStyle_setFontSizeTwice_lastOneOverwrite() { |
| 265 | val text = "abcde" |
| 266 | val fontSize = 20.0 |
| 267 | val fontSizeOverwrite = 30.0 |
| 268 | val paragraphWidth = text.length * fontSizeOverwrite |
| 269 | val textStyle = TextStyle(fontSize = fontSize) |
| 270 | val textStyleOverwrite = TextStyle(fontSize = fontSizeOverwrite) |
| 271 | |
| 272 | val paragraph = simpleParagraph( |
| 273 | text = text, |
| 274 | textStyles = listOf( |
| 275 | ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length), |
| 276 | ParagraphBuilder.TextStyleIndex(textStyleOverwrite, 0, "abc".length) |
| 277 | ) |
| 278 | ) |
| 279 | paragraph.layout(paragraphWidth) |
| 280 | |
| 281 | assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, text.length)) |
| 282 | assertThat(paragraph.underlyingText, hasSpan(AbsoluteSizeSpan::class, 0, "abc".length)) |
| 283 | assertThat( |
| 284 | paragraph.underlyingText, |
| 285 | hasSpanOnTop(AbsoluteSizeSpan::class, 0, "abc".length) |
| 286 | ) |
| 287 | } |
| 288 | |
Haoyu Zhang | 1588ba0f | 2018-12-19 16:47:10 -0800 | [diff] [blame] | 289 | @Test |
| 290 | fun textStyle_setLetterSpacingOnWholeText() { |
| 291 | val text = "abcde" |
| 292 | val letterSpacing = 2.0 |
| 293 | val textStyle = TextStyle(letterSpacing = letterSpacing) |
| 294 | |
| 295 | val paragraph = simpleParagraph( |
| 296 | text = text, |
| 297 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length)) |
| 298 | ) |
| 299 | paragraph.layout(100.0) |
| 300 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 301 | assertThat(paragraph.underlyingText, hasSpan(LetterSpacingSpan::class, 0, text.length)) |
| 302 | } |
| 303 | |
| 304 | @Test |
| 305 | fun textStyle_setLetterSpacingOnPartText() { |
| 306 | val text = "abcde" |
| 307 | val textStyle = TextStyle(letterSpacing = 2.0) |
| 308 | |
| 309 | val paragraph = simpleParagraph( |
| 310 | text = text, |
| 311 | textStyles = listOf(ParagraphBuilder.TextStyleIndex(textStyle, 0, "abc".length)) |
| 312 | ) |
| 313 | paragraph.layout(100.0) |
| 314 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 315 | assertThat(paragraph.underlyingText, hasSpan(LetterSpacingSpan::class, 0, "abc".length)) |
| 316 | } |
| 317 | |
| 318 | @Test |
| 319 | fun textStyle_setLetterTwice_lastOneOverwrite() { |
| 320 | val text = "abcde" |
| 321 | val textStyle = TextStyle(letterSpacing = 2.0) |
| 322 | val textStyleOverwrite = TextStyle(letterSpacing = 3.0) |
| 323 | |
| 324 | val paragraph = simpleParagraph( |
| 325 | text = text, |
| 326 | textStyles = listOf( |
| 327 | ParagraphBuilder.TextStyleIndex(textStyle, 0, text.length), |
| 328 | ParagraphBuilder.TextStyleIndex(textStyleOverwrite, 0, "abc".length) |
| 329 | ) |
| 330 | ) |
| 331 | paragraph.layout(100.0) |
| 332 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 333 | assertThat(paragraph.underlyingText, hasSpan(LetterSpacingSpan::class, 0, text.length)) |
| 334 | assertThat(paragraph.underlyingText, hasSpan(LetterSpacingSpan::class, 0, "abc".length)) |
| 335 | assertThat( |
| 336 | paragraph.underlyingText, |
| 337 | hasSpanOnTop(LetterSpacingSpan::class, 0, "abc".length) |
| 338 | ) |
| 339 | } |
| 340 | |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 341 | @Test |
| 342 | fun textStyle_fontFamily_addsTypefaceSpanWithCorrectTypeface() { |
| 343 | val text = "abcde" |
| 344 | val textStyle = TextStyle( |
| 345 | fontFamily = fontFamily, |
| 346 | fontStyle = FontStyle.italic, |
| 347 | fontWeight = FontWeight.bold |
| 348 | ) |
| 349 | val expectedTypeface = TypefaceAdapter().create( |
| 350 | fontFamily = fontFamily, |
| 351 | fontStyle = FontStyle.italic, |
| 352 | fontWeight = FontWeight.bold |
| 353 | ) |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame^] | 354 | val expectedStart = 0 |
| 355 | val expectedEnd = "abc".length |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 356 | |
| 357 | val paragraph = simpleParagraph( |
| 358 | text = text, |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame^] | 359 | textStyles = listOf( |
| 360 | ParagraphBuilder.TextStyleIndex( |
| 361 | textStyle, |
| 362 | expectedStart, |
| 363 | expectedEnd |
| 364 | ) |
| 365 | ) |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 366 | ) |
| 367 | paragraph.layout(100.0) |
| 368 | |
| 369 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 370 | assertThat( |
| 371 | paragraph.underlyingText, |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame^] | 372 | hasSpan(TypefaceSpan::class, expectedStart, expectedEnd) { span -> |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 373 | span.typeface == expectedTypeface |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame^] | 374 | }) |
| 375 | } |
| 376 | |
| 377 | @Test |
| 378 | fun textStyle_fontFamily_whenFontSynthesizeTurnedOff() { |
| 379 | val text = "abcde" |
| 380 | val textStyle = TextStyle( |
| 381 | fontFamily = fontFamily, |
| 382 | fontStyle = FontStyle.italic, |
| 383 | fontWeight = FontWeight.bold, |
| 384 | fontSynthesis = FontSynthesis.none |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 385 | ) |
Siyamed Sinir | 3729841 | 2018-12-27 19:47:15 -0800 | [diff] [blame^] | 386 | val expectedTypeface = TypefaceAdapter().create( |
| 387 | fontFamily = fontFamily, |
| 388 | fontStyle = FontStyle.italic, |
| 389 | fontWeight = FontWeight.bold, |
| 390 | fontSynthesis = FontSynthesis.none |
| 391 | ) |
| 392 | val expectedStart = 0 |
| 393 | val expectedEnd = "abc".length |
| 394 | |
| 395 | val paragraph = simpleParagraph( |
| 396 | text = text, |
| 397 | textStyles = listOf( |
| 398 | ParagraphBuilder.TextStyleIndex( |
| 399 | textStyle, |
| 400 | expectedStart, |
| 401 | expectedEnd |
| 402 | ) |
| 403 | ) |
| 404 | ) |
| 405 | paragraph.layout(100.0) |
| 406 | |
| 407 | assertThat(paragraph.underlyingText.toString(), equalTo(text)) |
| 408 | assertThat( |
| 409 | paragraph.underlyingText, |
| 410 | hasSpan(TypefaceSpan::class, expectedStart, expectedEnd) { span -> |
| 411 | span.typeface == expectedTypeface |
| 412 | }) |
Siyamed Sinir | 70f336a | 2018-12-27 10:37:09 -0800 | [diff] [blame] | 413 | } |
| 414 | |
| 415 | @Test |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 416 | fun testEmptyFontFamily() { |
| 417 | val typefaceAdapter = mock<TypefaceAdapter>() |
| 418 | val paragraph = simpleParagraph( |
| 419 | text = "abc", |
| 420 | typefaceAdapter = typefaceAdapter |
| 421 | ) |
| 422 | paragraph.layout(Double.MAX_VALUE) |
| 423 | |
| 424 | verify(typefaceAdapter, never()).create( |
| 425 | fontFamily = any(), |
| 426 | fontWeight = any(), |
Siyamed Sinir | 2179601 | 2018-12-26 13:51:17 -0800 | [diff] [blame] | 427 | fontStyle = any(), |
| 428 | fontSynthesis = any() |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 429 | ) |
| 430 | assertThat(paragraph.textPaint.typeface, nullValue()) |
| 431 | } |
| 432 | |
| 433 | @Test |
| 434 | fun testEmptyFontFamily_withBoldFontWeightSelection() { |
| 435 | val typefaceAdapter = spy(TypefaceAdapter()) |
| 436 | |
| 437 | val paragraph = simpleParagraph( |
| 438 | text = "abc", |
| 439 | fontFamily = null, |
| 440 | fontWeight = FontWeight.bold, |
| 441 | typefaceAdapter = typefaceAdapter |
| 442 | ) |
| 443 | paragraph.layout(Double.MAX_VALUE) |
| 444 | |
| 445 | verify(typefaceAdapter, times(1)).create( |
| 446 | fontFamily = eq(null), |
| 447 | fontWeight = eq(FontWeight.bold), |
Siyamed Sinir | 2179601 | 2018-12-26 13:51:17 -0800 | [diff] [blame] | 448 | fontStyle = eq(FontStyle.normal), |
| 449 | fontSynthesis = eq(FontSynthesis.all) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 450 | ) |
| 451 | |
| 452 | val typeface = paragraph.textPaint.typeface |
| 453 | assertThat(typeface, not(nullValue())) |
| 454 | assertThat(typeface.isBold, equalTo(true)) |
| 455 | assertThat(typeface.isItalic, equalTo(false)) |
| 456 | } |
| 457 | |
| 458 | @Test |
| 459 | fun testEmptyFontFamily_withFontStyleSelection() { |
| 460 | val typefaceAdapter = spy(TypefaceAdapter()) |
| 461 | val paragraph = simpleParagraph( |
| 462 | text = "abc", |
| 463 | fontFamily = null, |
| 464 | fontStyle = FontStyle.italic, |
| 465 | typefaceAdapter = typefaceAdapter |
| 466 | ) |
| 467 | paragraph.layout(Double.MAX_VALUE) |
| 468 | |
| 469 | verify(typefaceAdapter, times(1)).create( |
| 470 | fontFamily = eq(null), |
| 471 | fontWeight = eq(FontWeight.normal), |
Siyamed Sinir | 2179601 | 2018-12-26 13:51:17 -0800 | [diff] [blame] | 472 | fontStyle = eq(FontStyle.italic), |
| 473 | fontSynthesis = eq(FontSynthesis.all) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 474 | ) |
| 475 | |
| 476 | val typeface = paragraph.textPaint.typeface |
| 477 | assertThat(typeface, not(nullValue())) |
| 478 | assertThat(typeface.isBold, equalTo(false)) |
| 479 | assertThat(typeface.isItalic, equalTo(true)) |
| 480 | } |
| 481 | |
| 482 | @Test |
| 483 | fun testFontFamily_withGenericFamilyName() { |
| 484 | val typefaceAdapter = spy(TypefaceAdapter()) |
| 485 | val fontFamily = FontFamily("sans-serif") |
| 486 | |
| 487 | val paragraph = simpleParagraph( |
| 488 | text = "abc", |
| 489 | fontFamily = fontFamily, |
| 490 | typefaceAdapter = typefaceAdapter |
| 491 | ) |
| 492 | paragraph.layout(Double.MAX_VALUE) |
| 493 | |
| 494 | verify(typefaceAdapter, times(1)).create( |
| 495 | fontFamily = eq(fontFamily), |
| 496 | fontWeight = eq(FontWeight.normal), |
Siyamed Sinir | 2179601 | 2018-12-26 13:51:17 -0800 | [diff] [blame] | 497 | fontStyle = eq(FontStyle.normal), |
| 498 | fontSynthesis = eq(FontSynthesis.all) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 499 | ) |
| 500 | |
| 501 | val typeface = paragraph.textPaint.typeface |
| 502 | assertThat(typeface, not(nullValue())) |
| 503 | assertThat(typeface.isBold, equalTo(false)) |
| 504 | assertThat(typeface.isItalic, equalTo(false)) |
| 505 | } |
| 506 | |
| 507 | @Test |
| 508 | fun testFontFamily_withCustomFont() { |
| 509 | val typefaceAdapter = spy(TypefaceAdapter()) |
| 510 | val paragraph = simpleParagraph( |
| 511 | text = "abc", |
| 512 | fontFamily = fontFamily, |
| 513 | typefaceAdapter = typefaceAdapter |
| 514 | ) |
| 515 | paragraph.layout(Double.MAX_VALUE) |
| 516 | |
| 517 | verify(typefaceAdapter, times(1)).create( |
| 518 | fontFamily = eq(fontFamily), |
| 519 | fontWeight = eq(FontWeight.normal), |
Siyamed Sinir | 2179601 | 2018-12-26 13:51:17 -0800 | [diff] [blame] | 520 | fontStyle = eq(FontStyle.normal), |
| 521 | fontSynthesis = eq(FontSynthesis.all) |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 522 | ) |
| 523 | val typeface = paragraph.textPaint.typeface |
| 524 | assertThat(typeface.isBold, equalTo(false)) |
| 525 | assertThat(typeface.isItalic, equalTo(false)) |
| 526 | } |
| 527 | |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 528 | private fun simpleParagraph( |
| 529 | text: CharSequence = "", |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 530 | textStyles: List<ParagraphBuilder.TextStyleIndex> = listOf(), |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 531 | textAlign: TextAlign? = null, |
| 532 | fontSize: Double? = null, |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 533 | maxLines: Int? = null, |
| 534 | fontFamily: FontFamily? = null, |
| 535 | fontWeight: FontWeight? = null, |
| 536 | fontStyle: FontStyle? = null, |
| 537 | typefaceAdapter: TypefaceAdapter = TypefaceAdapter() |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 538 | ): ParagraphAndroid { |
| 539 | return ParagraphAndroid( |
| 540 | text = StringBuilder(text), |
Haoyu Zhang | 47f57a2 | 2018-12-12 13:16:40 -0800 | [diff] [blame] | 541 | textStyles = textStyles, |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 542 | typefaceAdapter = typefaceAdapter, |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 543 | paragraphStyle = ParagraphStyle( |
| 544 | textAlign = textAlign, |
| 545 | maxLines = maxLines, |
Siyamed Sinir | f18dcca | 2018-12-20 18:25:10 -0800 | [diff] [blame] | 546 | fontFamily = fontFamily, |
| 547 | fontSize = fontSize, |
| 548 | fontWeight = fontWeight, |
| 549 | fontStyle = fontStyle |
Haoyu Zhang | 10fc650 | 2018-11-09 15:50:11 -0800 | [diff] [blame] | 550 | ) |
| 551 | ) |
| 552 | } |
| 553 | } |