|
| 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.text.Spannable |
| 11 | +import android.text.style.ClickableSpan |
| 12 | +import com.facebook.react.common.mapbuffer.WritableMapBuffer |
| 13 | +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlags |
| 14 | +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsDefaults |
| 15 | +import com.facebook.react.internal.featureflags.ReactNativeFeatureFlagsForTests |
| 16 | +import com.facebook.react.uimanager.DisplayMetricsHolder |
| 17 | +import com.facebook.react.uimanager.ReactAccessibilityDelegate.Role |
| 18 | +import org.assertj.core.api.Assertions.assertThat |
| 19 | +import org.junit.After |
| 20 | +import org.junit.Before |
| 21 | +import org.junit.Test |
| 22 | +import org.junit.runner.RunWith |
| 23 | +import org.robolectric.RobolectricTestRunner |
| 24 | +import org.robolectric.RuntimeEnvironment |
| 25 | +import org.robolectric.annotation.Config |
| 26 | + |
| 27 | +@RunWith(RobolectricTestRunner::class) |
| 28 | +@Config(sdk = [34]) |
| 29 | +class TextLayoutManagerRelaxedLinkRoleTest { |
| 30 | + |
| 31 | + @Before |
| 32 | + fun setUp() { |
| 33 | + ReactNativeFeatureFlagsForTests.setUp() |
| 34 | + DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(RuntimeEnvironment.getApplication()) |
| 35 | + } |
| 36 | + |
| 37 | + @After |
| 38 | + fun tearDown() { |
| 39 | + DisplayMetricsHolder.setScreenDisplayMetrics(null) |
| 40 | + ReactNativeFeatureFlags.dangerouslyReset() |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun `role=button becomes a link span when enableRelaxedLinkRole is on`() { |
| 45 | + setRelaxedLinkRole(true) |
| 46 | + assertThat(spannable(role = Role.BUTTON).clickableSpanCount()).isEqualTo(1) |
| 47 | + } |
| 48 | + |
| 49 | + @Test |
| 50 | + fun `role=button stays plain when enableRelaxedLinkRole is off`() { |
| 51 | + setRelaxedLinkRole(false) |
| 52 | + assertThat(spannable(role = Role.BUTTON).clickableSpanCount()).isZero() |
| 53 | + } |
| 54 | + |
| 55 | + @Test |
| 56 | + fun `heading role with link accessibilityRole is detected when enableRelaxedLinkRole is on`() { |
| 57 | + setRelaxedLinkRole(true) |
| 58 | + assertThat(spannable(role = Role.HEADING, accessibilityRole = "link").clickableSpanCount()) |
| 59 | + .isEqualTo(1) |
| 60 | + } |
| 61 | + |
| 62 | + @Test |
| 63 | + fun `heading role with link accessibilityRole stays plain when enableRelaxedLinkRole is off`() { |
| 64 | + setRelaxedLinkRole(false) |
| 65 | + assertThat(spannable(role = Role.HEADING, accessibilityRole = "link").clickableSpanCount()) |
| 66 | + .isZero() |
| 67 | + } |
| 68 | + |
| 69 | + private fun setRelaxedLinkRole(enabled: Boolean) = |
| 70 | + ReactNativeFeatureFlags.override( |
| 71 | + object : ReactNativeFeatureFlagsDefaults() { |
| 72 | + override fun enableRelaxedLinkRole(): Boolean = enabled |
| 73 | + } |
| 74 | + ) |
| 75 | + |
| 76 | + private fun Spannable.clickableSpanCount(): Int = |
| 77 | + getSpans(0, length, ClickableSpan::class.java).size |
| 78 | + |
| 79 | + private fun spannable(role: Role? = null, accessibilityRole: String? = null): Spannable { |
| 80 | + val attrs = |
| 81 | + WritableMapBuffer().apply { |
| 82 | + put(TextAttributeProps.TA_KEY_FONT_SIZE, 16.0) |
| 83 | + role?.let { put(TextAttributeProps.TA_KEY_ROLE, it.ordinal) } |
| 84 | + accessibilityRole?.let { put(TextAttributeProps.TA_KEY_ACCESSIBILITY_ROLE, it) } |
| 85 | + } |
| 86 | + val fragment = |
| 87 | + WritableMapBuffer().apply { |
| 88 | + put(TextLayoutManager.FR_KEY_STRING, "Follow") |
| 89 | + put(TextLayoutManager.FR_KEY_REACT_TAG, 1) |
| 90 | + put(TextLayoutManager.FR_KEY_TEXT_ATTRIBUTES, attrs) |
| 91 | + } |
| 92 | + val attributedString = |
| 93 | + WritableMapBuffer().apply { |
| 94 | + put(TextLayoutManager.AS_KEY_STRING, "Follow") |
| 95 | + put(TextLayoutManager.AS_KEY_FRAGMENTS, WritableMapBuffer().put(0, fragment)) |
| 96 | + } |
| 97 | + return TextLayoutManager.getOrCreateSpannableForText( |
| 98 | + RuntimeEnvironment.getApplication().assets, |
| 99 | + attributedString, |
| 100 | + null, |
| 101 | + ) |
| 102 | + } |
| 103 | +} |
0 commit comments