|
5 | 5 | * LICENSE file in the root directory of this source tree. |
6 | 6 | */ |
7 | 7 |
|
| 8 | +@file:Suppress("DEPRECATION") // DisplayMetrics.scaledDensity, to simulate a system font scale |
| 9 | + |
8 | 10 | package com.facebook.react.views.text |
9 | 11 |
|
| 12 | +import android.util.DisplayMetrics |
10 | 13 | import android.view.Gravity |
11 | 14 | import com.facebook.react.bridge.JavaOnlyMap |
| 15 | +import com.facebook.react.common.mapbuffer.WritableMapBuffer |
12 | 16 | import com.facebook.react.uimanager.DisplayMetricsHolder |
13 | 17 | import com.facebook.react.uimanager.ReactStylesDiffMap |
14 | 18 | import org.assertj.core.api.Assertions.assertThat |
| 19 | +import org.assertj.core.api.Assertions.within |
15 | 20 | import org.junit.After |
16 | 21 | import org.junit.Before |
17 | 22 | import org.junit.Test |
@@ -84,11 +89,133 @@ class TextAttributePropsTest { |
84 | 89 | assertThat(textAlignment("end", isRTL = true)).isEqualTo(Gravity.LEFT) |
85 | 90 | } |
86 | 91 |
|
| 92 | + @Test |
| 93 | + fun lineHeightIsCappedByMaxFontSizeMultiplier() { |
| 94 | + setFontScale(2f) |
| 95 | + |
| 96 | + val textAttributes = |
| 97 | + TextAttributeProps.fromReadableMap( |
| 98 | + ReactStylesDiffMap( |
| 99 | + JavaOnlyMap.of("fontSize", 14.0, "lineHeight", 20.0, "maxFontSizeMultiplier", 1.3) |
| 100 | + ), |
| 101 | + ) |
| 102 | + |
| 103 | + assertThat(textAttributes.lineHeight).isCloseTo(20f * DENSITY * 1.3f, within(0.001f)) |
| 104 | + } |
| 105 | + |
| 106 | + @Test |
| 107 | + fun lineHeightIsCappedByMaxFontSizeMultiplierFromMapBuffer() { |
| 108 | + setFontScale(2f) |
| 109 | + |
| 110 | + // Keys are iterated in ascending order, so lineHeight is set before maxFontSizeMultiplier. |
| 111 | + val textAttributes = |
| 112 | + TextAttributeProps.fromMapBuffer( |
| 113 | + WritableMapBuffer() |
| 114 | + .put(TextAttributeProps.TA_KEY_FONT_SIZE, 14.0) |
| 115 | + .put(TextAttributeProps.TA_KEY_LINE_HEIGHT, 20.0) |
| 116 | + .put(TextAttributeProps.TA_KEY_MAX_FONT_SIZE_MULTIPLIER, 1.3), |
| 117 | + ) |
| 118 | + |
| 119 | + assertThat(textAttributes.lineHeight).isCloseTo(20f * DENSITY * 1.3f, within(0.001f)) |
| 120 | + } |
| 121 | + |
| 122 | + @Test |
| 123 | + fun letterSpacingIsCappedByMaxFontSizeMultiplier() { |
| 124 | + setFontScale(2f) |
| 125 | + |
| 126 | + val textAttributes = |
| 127 | + TextAttributeProps.fromReadableMap( |
| 128 | + ReactStylesDiffMap( |
| 129 | + JavaOnlyMap.of("fontSize", 14.0, "letterSpacing", 2.0, "maxFontSizeMultiplier", 1.3) |
| 130 | + ), |
| 131 | + ) |
| 132 | + |
| 133 | + assertThat(textAttributes.fontSize).isEqualTo(37) // ceil(14 * 2 * 1.3) |
| 134 | + assertThat(textAttributes.letterSpacing) |
| 135 | + .isCloseTo(2f * DENSITY * 1.3f / textAttributes.fontSize, within(0.001f)) |
| 136 | + } |
| 137 | + |
| 138 | + @Test |
| 139 | + fun lineHeightScalesWithFontScaleWithoutMaxFontSizeMultiplier() { |
| 140 | + setFontScale(2f) |
| 141 | + |
| 142 | + val textAttributes = |
| 143 | + TextAttributeProps.fromReadableMap( |
| 144 | + ReactStylesDiffMap(JavaOnlyMap.of("fontSize", 14.0, "lineHeight", 20.0)), |
| 145 | + ) |
| 146 | + |
| 147 | + assertThat(textAttributes.lineHeight).isCloseTo(20f * DENSITY * 2f, within(0.001f)) |
| 148 | + } |
| 149 | + |
| 150 | + @Test |
| 151 | + fun lineHeightIsNotCappedWhenFontScaleIsBelowMaxFontSizeMultiplier() { |
| 152 | + setFontScale(1.2f) |
| 153 | + |
| 154 | + val textAttributes = |
| 155 | + TextAttributeProps.fromReadableMap( |
| 156 | + ReactStylesDiffMap( |
| 157 | + JavaOnlyMap.of("fontSize", 14.0, "lineHeight", 20.0, "maxFontSizeMultiplier", 1.5) |
| 158 | + ), |
| 159 | + ) |
| 160 | + |
| 161 | + assertThat(textAttributes.lineHeight).isCloseTo(20f * DENSITY * 1.2f, within(0.001f)) |
| 162 | + } |
| 163 | + |
| 164 | + @Test |
| 165 | + fun lineHeightIsNotCappedWhenMaxFontSizeMultiplierIsZero() { |
| 166 | + setFontScale(2f) |
| 167 | + |
| 168 | + val textAttributes = |
| 169 | + TextAttributeProps.fromReadableMap( |
| 170 | + ReactStylesDiffMap( |
| 171 | + JavaOnlyMap.of("fontSize", 14.0, "lineHeight", 20.0, "maxFontSizeMultiplier", 0.0) |
| 172 | + ), |
| 173 | + ) |
| 174 | + |
| 175 | + assertThat(textAttributes.lineHeight).isCloseTo(20f * DENSITY * 2f, within(0.001f)) |
| 176 | + } |
| 177 | + |
| 178 | + @Test |
| 179 | + fun lineHeightIgnoresFontScaleWhenFontScalingIsDisabled() { |
| 180 | + setFontScale(2f) |
| 181 | + |
| 182 | + val textAttributes = |
| 183 | + TextAttributeProps.fromReadableMap( |
| 184 | + ReactStylesDiffMap( |
| 185 | + JavaOnlyMap.of( |
| 186 | + "fontSize", |
| 187 | + 14.0, |
| 188 | + "lineHeight", |
| 189 | + 20.0, |
| 190 | + "maxFontSizeMultiplier", |
| 191 | + 1.3, |
| 192 | + "allowFontScaling", |
| 193 | + false, |
| 194 | + ) |
| 195 | + ), |
| 196 | + ) |
| 197 | + |
| 198 | + assertThat(textAttributes.lineHeight).isCloseTo(20f * DENSITY, within(0.001f)) |
| 199 | + } |
| 200 | + |
| 201 | + private fun setFontScale(fontScale: Float) { |
| 202 | + DisplayMetricsHolder.setScreenDisplayMetrics( |
| 203 | + DisplayMetrics().apply { |
| 204 | + density = DENSITY |
| 205 | + scaledDensity = DENSITY * fontScale |
| 206 | + }, |
| 207 | + ) |
| 208 | + } |
| 209 | + |
87 | 210 | private fun textAlignment(textAlign: String, isRTL: Boolean): Int { |
88 | 211 | return TextAttributeProps.getTextAlignment( |
89 | 212 | ReactStylesDiffMap(JavaOnlyMap.of("textAlign", textAlign)), |
90 | 213 | isRTL, |
91 | 214 | Gravity.CENTER_HORIZONTAL, |
92 | 215 | ) |
93 | 216 | } |
| 217 | + |
| 218 | + private companion object { |
| 219 | + const val DENSITY = 2f |
| 220 | + } |
94 | 221 | } |
0 commit comments