|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +package com.facebook.react.views.text |
| 9 | + |
| 10 | +import android.annotation.SuppressLint |
| 11 | +import android.graphics.text.LineBreakConfig |
| 12 | +import android.text.BoringLayout |
| 13 | +import android.text.Layout |
| 14 | +import android.text.SpannableString |
| 15 | +import android.text.TextPaint |
| 16 | +import com.facebook.yoga.YogaMeasureMode |
| 17 | +import kotlin.math.ceil |
| 18 | +import org.assertj.core.api.Assertions.assertThat |
| 19 | +import org.junit.Test |
| 20 | +import org.junit.runner.RunWith |
| 21 | +import org.robolectric.RobolectricTestRunner |
| 22 | +import org.robolectric.annotation.GraphicsMode |
| 23 | +import org.robolectric.annotation.TextLayoutMode |
| 24 | + |
| 25 | +@RunWith(RobolectricTestRunner::class) |
| 26 | +@GraphicsMode(GraphicsMode.Mode.NATIVE) |
| 27 | +@TextLayoutMode(TextLayoutMode.Mode.REALISTIC) |
| 28 | +class TextLayoutManagerPixelWidthTest { |
| 29 | + private val text = SpannableString("Add payment method") |
| 30 | + private val paint = TextPaint(TextPaint.ANTI_ALIAS_FLAG).apply { textSize = 16f } |
| 31 | + private val measuredWidth = ceil(Layout.getDesiredWidth(text, paint)).toInt() |
| 32 | + |
| 33 | + @Test |
| 34 | + fun `AT_MOST width a floating point error short of the measured width keeps one line`() { |
| 35 | + val widths = listOf(Math.nextDown(measuredWidth.toFloat()), measuredWidth - 0.0004f) |
| 36 | + for (width in widths) { |
| 37 | + for (boring in listOf(BoringLayout.isBoring(text, paint), null)) { |
| 38 | + val layout = createLayout(width, boring) |
| 39 | + |
| 40 | + assertThat(layout.width).isEqualTo(measuredWidth) |
| 41 | + assertThat(layout.lineCount).isEqualTo(1) |
| 42 | + } |
| 43 | + } |
| 44 | + } |
| 45 | + |
| 46 | + @Test |
| 47 | + fun `AT_MOST width a fraction of a pixel short of the measured width wraps`() { |
| 48 | + for (boring in listOf(BoringLayout.isBoring(text, paint), null)) { |
| 49 | + val layout = createLayout(measuredWidth - 0.5f, boring) |
| 50 | + |
| 51 | + assertThat(layout.width).isEqualTo(measuredWidth - 1) |
| 52 | + assertThat(layout.lineCount).isGreaterThan(1) |
| 53 | + } |
| 54 | + } |
| 55 | + |
| 56 | + @SuppressLint("InlinedApi") |
| 57 | + private fun createLayout(width: Float, boring: BoringLayout.Metrics?): Layout = |
| 58 | + TextLayoutManager.createLayout( |
| 59 | + text, |
| 60 | + boring, |
| 61 | + width, |
| 62 | + YogaMeasureMode.AT_MOST, |
| 63 | + /* includeFontPadding = */ false, |
| 64 | + /* textBreakStrategy = */ Layout.BREAK_STRATEGY_HIGH_QUALITY, |
| 65 | + /* hyphenationFrequency = */ Layout.HYPHENATION_FREQUENCY_NONE, |
| 66 | + /* lineBreakStyle = */ LineBreakConfig.LINE_BREAK_STYLE_NONE, |
| 67 | + /* lineBreakWordStyle = */ LineBreakConfig.LINE_BREAK_WORD_STYLE_NONE, |
| 68 | + Layout.Alignment.ALIGN_NORMAL, |
| 69 | + /* justificationMode = */ 0, |
| 70 | + /* ellipsizeMode = */ null, |
| 71 | + /* maxNumberOfLines = */ 0, |
| 72 | + paint, |
| 73 | + ) |
| 74 | +} |
0 commit comments