[go: nahoru, domu]

blob: daa87d7802d252214910c2ac5ddc2c8d0a6dab70 [file] [log] [blame]
Haoyu Zhang10fc6502018-11-09 15:50:11 -08001package androidx.ui.port.engine.text.platform
2
Haoyu Zhang10fc6502018-11-09 15:50:11 -08003import android.graphics.Paint
Haoyu Zhang10fc6502018-11-09 15:50:11 -08004import android.text.TextPaint
Haoyu Zhangbb51ccf2018-12-17 15:07:43 -08005import android.text.style.AbsoluteSizeSpan
Haoyu Zhang47f57a22018-12-12 13:16:40 -08006import android.text.style.ForegroundColorSpan
Haoyu Zhangb5677202018-12-19 14:22:18 -08007import android.text.style.StrikethroughSpan
8import android.text.style.UnderlineSpan
Haoyu Zhang10fc6502018-11-09 15:50:11 -08009import androidx.test.filters.SmallTest
10import androidx.test.platform.app.InstrumentationRegistry
Haoyu Zhang2eb38cd2018-11-21 15:05:28 -080011import androidx.text.StaticLayoutCompat
Haoyu Zhang1588ba0f2018-12-19 16:47:10 -080012import androidx.text.style.LetterSpacingSpan
Siyamed Sinir70f336a2018-12-27 10:37:09 -080013import androidx.text.style.TypefaceSpan
Siyamed Sinirf18dcca2018-12-20 18:25:10 -080014import androidx.ui.engine.text.FontStyle
Siyamed Sinir21796012018-12-26 13:51:17 -080015import androidx.ui.engine.text.FontSynthesis
Siyamed Sinirf18dcca2018-12-20 18:25:10 -080016import androidx.ui.engine.text.FontWeight
Haoyu Zhang47f57a22018-12-12 13:16:40 -080017import androidx.ui.engine.text.ParagraphBuilder
Haoyu Zhang10fc6502018-11-09 15:50:11 -080018import androidx.ui.engine.text.ParagraphStyle
19import androidx.ui.engine.text.TextAlign
Haoyu Zhangb5677202018-12-19 14:22:18 -080020import androidx.ui.engine.text.TextDecoration
Haoyu Zhang47f57a22018-12-12 13:16:40 -080021import androidx.ui.engine.text.TextStyle
Siyamed Sinirf18dcca2018-12-20 18:25:10 -080022import androidx.ui.engine.text.font.FontFamily
23import androidx.ui.engine.text.font.asFontFamily
Haoyu Zhang10fc6502018-11-09 15:50:11 -080024import androidx.ui.engine.text.platform.ParagraphAndroid
Siyamed Sinirf18dcca2018-12-20 18:25:10 -080025import androidx.ui.engine.text.platform.TypefaceAdapter
Haoyu Zhang47f57a22018-12-12 13:16:40 -080026import androidx.ui.painting.Color
Siyamed Sinirf18dcca2018-12-20 18:25:10 -080027import androidx.ui.port.engine.text.FontTestData.Companion.BASIC_MEASURE_FONT
Haoyu Zhang10fc6502018-11-09 15:50:11 -080028import androidx.ui.port.matchers.equalToBitmap
Haoyu Zhang47f57a22018-12-12 13:16:40 -080029import androidx.ui.port.matchers.hasSpan
30import androidx.ui.port.matchers.hasSpanOnTop
Siyamed Sinirf18dcca2018-12-20 18:25:10 -080031import com.nhaarman.mockitokotlin2.any
32import com.nhaarman.mockitokotlin2.eq
33import com.nhaarman.mockitokotlin2.mock
34import com.nhaarman.mockitokotlin2.never
35import com.nhaarman.mockitokotlin2.spy
36import com.nhaarman.mockitokotlin2.times
37import com.nhaarman.mockitokotlin2.verify
Haoyu Zhangb5677202018-12-19 14:22:18 -080038import org.hamcrest.Matchers.equalTo
Siyamed Sinirf18dcca2018-12-20 18:25:10 -080039import org.hamcrest.Matchers.not
40import org.hamcrest.Matchers.nullValue
Haoyu Zhang47f57a22018-12-12 13:16:40 -080041import org.junit.Assert.assertThat
Haoyu Zhang10fc6502018-11-09 15:50:11 -080042import org.junit.Before
43import org.junit.Test
44import org.junit.runner.RunWith
45import org.junit.runners.JUnit4
Haoyu Zhang10fc6502018-11-09 15:50:11 -080046import kotlin.math.ceil
47
48@RunWith(JUnit4::class)
49@SmallTest
50class ParagraphAndroidTest {
Siyamed Sinirf18dcca2018-12-20 18:25:10 -080051 private lateinit var fontFamily: FontFamily
Haoyu Zhang10fc6502018-11-09 15:50:11 -080052
53 @Before
54 fun setup() {
Haoyu Zhang47f57a22018-12-12 13:16:40 -080055 // 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 Sinirf18dcca2018-12-20 18:25:10 -080059 fontFamily = BASIC_MEASURE_FONT.asFontFamily()
60 fontFamily.context = InstrumentationRegistry.getInstrumentation().context
Haoyu Zhang10fc6502018-11-09 15:50:11 -080061 }
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 Sinirf18dcca2018-12-20 18:25:10 -080069 fontSize = fontSize,
70 fontFamily = fontFamily
Haoyu Zhang10fc6502018-11-09 15:50:11 -080071 )
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 Sinirf18dcca2018-12-20 18:25:10 -080078 textPaint.typeface = TypefaceAdapter().create(fontFamily)
Haoyu Zhang10fc6502018-11-09 15:50:11 -080079
Haoyu Zhang2eb38cd2018-11-21 15:05:28 -080080 val staticLayout = StaticLayoutCompat.Builder(
81 text,
82 textPaint,
83 ceil(paragraphAndroid.width).toInt()
Haoyu Zhang10fc6502018-11-09 15:50:11 -080084 )
Haoyu Zhang2eb38cd2018-11-21 15:05:28 -080085 .setEllipsizedWidth(ceil(paragraphAndroid.width).toInt())
86 .build()
Haoyu Zhang47f57a22018-12-12 13:16:40 -080087 assertThat(paragraphAndroid.bitmap(), equalToBitmap(staticLayout.bitmap()))
Haoyu Zhang10fc6502018-11-09 15:50:11 -080088 }
89 }
90
Haoyu Zhang47f57a22018-12-12 13:16:40 -080091 @Test
92 fun textStyle_setColorOnWholeText() {
93 val text = "abcde"
94 val fontSize = 20.0
Haoyu Zhangbb51ccf2018-12-17 15:07:43 -080095 val paragraphWidth = text.length * fontSize
Haoyu Zhang47f57a22018-12-12 13:16:40 -080096 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 Zhangbb51ccf2018-12-17 15:07:43 -0800103 paragraph.layout(paragraphWidth)
Haoyu Zhang47f57a22018-12-12 13:16:40 -0800104
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 Zhangbb51ccf2018-12-17 15:07:43 -0800112 val paragraphWidth = text.length * fontSize
Haoyu Zhang47f57a22018-12-12 13:16:40 -0800113 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 Zhangbb51ccf2018-12-17 15:07:43 -0800120 paragraph.layout(paragraphWidth)
Haoyu Zhang47f57a22018-12-12 13:16:40 -0800121
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 Zhangbb51ccf2018-12-17 15:07:43 -0800129 val paragraphWidth = text.length * fontSize
Haoyu Zhang47f57a22018-12-12 13:16:40 -0800130 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 Zhangbb51ccf2018-12-17 15:07:43 -0800141 paragraph.layout(paragraphWidth)
Haoyu Zhang47f57a22018-12-12 13:16:40 -0800142
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 Zhangbb51ccf2018-12-17 15:07:43 -0800151 @Test
Haoyu Zhangb5677202018-12-19 14:22:18 -0800152 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 Zhangbb51ccf2018-12-17 15:07:43 -0800232 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 Zhang1588ba0f2018-12-19 16:47:10 -0800289 @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 Sinir70f336a2018-12-27 10:37:09 -0800341 @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 Sinir37298412018-12-27 19:47:15 -0800354 val expectedStart = 0
355 val expectedEnd = "abc".length
Siyamed Sinir70f336a2018-12-27 10:37:09 -0800356
357 val paragraph = simpleParagraph(
358 text = text,
Siyamed Sinir37298412018-12-27 19:47:15 -0800359 textStyles = listOf(
360 ParagraphBuilder.TextStyleIndex(
361 textStyle,
362 expectedStart,
363 expectedEnd
364 )
365 )
Siyamed Sinir70f336a2018-12-27 10:37:09 -0800366 )
367 paragraph.layout(100.0)
368
369 assertThat(paragraph.underlyingText.toString(), equalTo(text))
370 assertThat(
371 paragraph.underlyingText,
Siyamed Sinir37298412018-12-27 19:47:15 -0800372 hasSpan(TypefaceSpan::class, expectedStart, expectedEnd) { span ->
Siyamed Sinir70f336a2018-12-27 10:37:09 -0800373 span.typeface == expectedTypeface
Siyamed Sinir37298412018-12-27 19:47:15 -0800374 })
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 Sinir70f336a2018-12-27 10:37:09 -0800385 )
Siyamed Sinir37298412018-12-27 19:47:15 -0800386 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 Sinir70f336a2018-12-27 10:37:09 -0800413 }
414
415 @Test
Siyamed Sinirf18dcca2018-12-20 18:25:10 -0800416 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 Sinir21796012018-12-26 13:51:17 -0800427 fontStyle = any(),
428 fontSynthesis = any()
Siyamed Sinirf18dcca2018-12-20 18:25:10 -0800429 )
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 Sinir21796012018-12-26 13:51:17 -0800448 fontStyle = eq(FontStyle.normal),
449 fontSynthesis = eq(FontSynthesis.all)
Siyamed Sinirf18dcca2018-12-20 18:25:10 -0800450 )
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 Sinir21796012018-12-26 13:51:17 -0800472 fontStyle = eq(FontStyle.italic),
473 fontSynthesis = eq(FontSynthesis.all)
Siyamed Sinirf18dcca2018-12-20 18:25:10 -0800474 )
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 Sinir21796012018-12-26 13:51:17 -0800497 fontStyle = eq(FontStyle.normal),
498 fontSynthesis = eq(FontSynthesis.all)
Siyamed Sinirf18dcca2018-12-20 18:25:10 -0800499 )
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 Sinir21796012018-12-26 13:51:17 -0800520 fontStyle = eq(FontStyle.normal),
521 fontSynthesis = eq(FontSynthesis.all)
Siyamed Sinirf18dcca2018-12-20 18:25:10 -0800522 )
523 val typeface = paragraph.textPaint.typeface
524 assertThat(typeface.isBold, equalTo(false))
525 assertThat(typeface.isItalic, equalTo(false))
526 }
527
Haoyu Zhang10fc6502018-11-09 15:50:11 -0800528 private fun simpleParagraph(
529 text: CharSequence = "",
Haoyu Zhang47f57a22018-12-12 13:16:40 -0800530 textStyles: List<ParagraphBuilder.TextStyleIndex> = listOf(),
Haoyu Zhang10fc6502018-11-09 15:50:11 -0800531 textAlign: TextAlign? = null,
532 fontSize: Double? = null,
Siyamed Sinirf18dcca2018-12-20 18:25:10 -0800533 maxLines: Int? = null,
534 fontFamily: FontFamily? = null,
535 fontWeight: FontWeight? = null,
536 fontStyle: FontStyle? = null,
537 typefaceAdapter: TypefaceAdapter = TypefaceAdapter()
Haoyu Zhang10fc6502018-11-09 15:50:11 -0800538 ): ParagraphAndroid {
539 return ParagraphAndroid(
540 text = StringBuilder(text),
Haoyu Zhang47f57a22018-12-12 13:16:40 -0800541 textStyles = textStyles,
Siyamed Sinirf18dcca2018-12-20 18:25:10 -0800542 typefaceAdapter = typefaceAdapter,
Haoyu Zhang10fc6502018-11-09 15:50:11 -0800543 paragraphStyle = ParagraphStyle(
544 textAlign = textAlign,
545 maxLines = maxLines,
Siyamed Sinirf18dcca2018-12-20 18:25:10 -0800546 fontFamily = fontFamily,
547 fontSize = fontSize,
548 fontWeight = fontWeight,
549 fontStyle = fontStyle
Haoyu Zhang10fc6502018-11-09 15:50:11 -0800550 )
551 )
552 }
553}