|
| 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.text.Layout |
| 12 | +import android.text.SpannableString |
| 13 | +import android.text.TextPaint |
| 14 | +import android.text.TextUtils |
| 15 | +import com.facebook.react.common.annotations.UnstableReactNativeAPI |
| 16 | +import com.facebook.react.common.mapbuffer.WritableMapBuffer |
| 17 | +import com.facebook.yoga.YogaMeasureMode |
| 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.RuntimeEnvironment |
| 23 | +import org.robolectric.annotation.Config |
| 24 | + |
| 25 | +/** |
| 26 | + * Regression test for prepared text layout exposing truncated text to accessibility / the view |
| 27 | + * hierarchy. |
| 28 | + * |
| 29 | + * When a prepared [Layout] is ellipsized, `layout.text` is an Android `Ellipsizer` whose |
| 30 | + * `toString()` is only the on-screen (truncated) string. [PreparedLayoutTextView.getText] — read by |
| 31 | + * the accessibility node and by black-box E2E view-hierarchy tooling — must instead return the full |
| 32 | + * logical text, matching a non-prepared `TextView` whose `getText()` returns the full source. |
| 33 | + */ |
| 34 | +@OptIn(UnstableReactNativeAPI::class) |
| 35 | +@RunWith(RobolectricTestRunner::class) |
| 36 | +@SuppressLint("NewApi") |
| 37 | +class PreparedLayoutTextViewAccessibilityTextTest { |
| 38 | + |
| 39 | + private val fullText = |
| 40 | + listOf( |
| 41 | + "This is an order created from the data builder", |
| 42 | + "second line of the description", |
| 43 | + "third line of the description", |
| 44 | + ) |
| 45 | + .joinToString("\n") |
| 46 | + |
| 47 | + @Test |
| 48 | + @Config(sdk = [34]) |
| 49 | + fun `getText returns the full logical text when the prepared layout is ellipsized`() { |
| 50 | + val layout = createEllipsizedLayout() |
| 51 | + |
| 52 | + // Guard: the scenario is genuinely ellipsized, so layout.text is the truncated on-screen text. |
| 53 | + assertThat(layout.getEllipsisCount(layout.lineCount - 1)).isGreaterThan(0) |
| 54 | + assertThat(layout.text.toString()).contains("…") |
| 55 | + assertThat(layout.text.toString()).isNotEqualTo(fullText) |
| 56 | + |
| 57 | + val view = PreparedLayoutTextView(RuntimeEnvironment.getApplication()) |
| 58 | + view.preparedLayout = |
| 59 | + PreparedLayout( |
| 60 | + layout, |
| 61 | + /* maximumNumberOfLines = */ 1, |
| 62 | + /* verticalOffset = */ 0f, |
| 63 | + /* reactTags = */ IntArray(0), |
| 64 | + /* textBreakStrategy = */ Layout.BREAK_STRATEGY_HIGH_QUALITY, |
| 65 | + /* justificationMode = */ 0, |
| 66 | + baseTextAttributes = TextAttributeProps.fromMapBuffer(WritableMapBuffer()), |
| 67 | + /* sourceText = */ fullText, |
| 68 | + ) |
| 69 | + |
| 70 | + assertThat(view.text?.toString()).isEqualTo(fullText) |
| 71 | + assertThat(view.text?.toString()).doesNotContain("…") |
| 72 | + } |
| 73 | + |
| 74 | + private fun createEllipsizedLayout(): Layout = |
| 75 | + TextLayoutManager.createLayout( |
| 76 | + SpannableString(fullText), |
| 77 | + /* boring = */ null, |
| 78 | + /* width = */ 200f, |
| 79 | + YogaMeasureMode.EXACTLY, |
| 80 | + /* includeFontPadding = */ false, |
| 81 | + /* textBreakStrategy = */ Layout.BREAK_STRATEGY_HIGH_QUALITY, |
| 82 | + /* hyphenationFrequency = */ Layout.HYPHENATION_FREQUENCY_NONE, |
| 83 | + Layout.Alignment.ALIGN_NORMAL, |
| 84 | + /* justificationMode = */ 0, |
| 85 | + /* ellipsizeMode = */ TextUtils.TruncateAt.END, |
| 86 | + /* maxNumberOfLines = */ 1, |
| 87 | + TextPaint(TextPaint.ANTI_ALIAS_FLAG).apply { textSize = 26f }, |
| 88 | + ) |
| 89 | +} |
0 commit comments