|
| 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.uimanager |
| 9 | + |
| 10 | +import android.content.Context |
| 11 | +import com.facebook.react.uimanager.style.BorderRadiusProp |
| 12 | +import com.facebook.react.uimanager.style.BorderRadiusStyle |
| 13 | +import com.facebook.react.uimanager.style.ComputedBorderRadiusProp |
| 14 | +import org.assertj.core.api.Assertions.* |
| 15 | +import org.junit.Test |
| 16 | +import org.junit.runner.RunWith |
| 17 | +import org.robolectric.RobolectricTestRunner |
| 18 | +import org.robolectric.RuntimeEnvironment |
| 19 | + |
| 20 | +/** Tests for [BorderRadiusStyle] */ |
| 21 | +@RunWith(RobolectricTestRunner::class) |
| 22 | +class BorderRadiusStyleTest { |
| 23 | + |
| 24 | + private val ctx: Context = RuntimeEnvironment.getApplication() |
| 25 | + |
| 26 | + @Test |
| 27 | + fun testCorrectPriorityLTR() { |
| 28 | + val propertyOrderMap = |
| 29 | + mapOf( |
| 30 | + ComputedBorderRadiusProp.COMPUTED_BORDER_TOP_LEFT_RADIUS to |
| 31 | + arrayOf( |
| 32 | + BorderRadiusProp.BORDER_RADIUS, |
| 33 | + BorderRadiusProp.BORDER_TOP_LEFT_RADIUS, |
| 34 | + BorderRadiusProp.BORDER_TOP_START_RADIUS, |
| 35 | + BorderRadiusProp.BORDER_START_START_RADIUS), |
| 36 | + ComputedBorderRadiusProp.COMPUTED_BORDER_TOP_RIGHT_RADIUS to |
| 37 | + arrayOf( |
| 38 | + BorderRadiusProp.BORDER_RADIUS, |
| 39 | + BorderRadiusProp.BORDER_TOP_RIGHT_RADIUS, |
| 40 | + BorderRadiusProp.BORDER_TOP_END_RADIUS, |
| 41 | + BorderRadiusProp.BORDER_END_START_RADIUS), |
| 42 | + ComputedBorderRadiusProp.COMPUTED_BORDER_BOTTOM_LEFT_RADIUS to |
| 43 | + arrayOf( |
| 44 | + BorderRadiusProp.BORDER_RADIUS, |
| 45 | + BorderRadiusProp.BORDER_BOTTOM_LEFT_RADIUS, |
| 46 | + BorderRadiusProp.BORDER_BOTTOM_START_RADIUS, |
| 47 | + BorderRadiusProp.BORDER_START_END_RADIUS), |
| 48 | + ComputedBorderRadiusProp.COMPUTED_BORDER_BOTTOM_RIGHT_RADIUS to |
| 49 | + arrayOf( |
| 50 | + BorderRadiusProp.BORDER_RADIUS, |
| 51 | + BorderRadiusProp.BORDER_BOTTOM_RIGHT_RADIUS, |
| 52 | + BorderRadiusProp.BORDER_BOTTOM_END_RADIUS, |
| 53 | + BorderRadiusProp.BORDER_END_END_RADIUS), |
| 54 | + ) |
| 55 | + |
| 56 | + propertyOrderMap.forEach { order -> |
| 57 | + val borderRadiusStyle = BorderRadiusStyle() |
| 58 | + // Starting count on 3 to test 0 override |
| 59 | + var count = 3f |
| 60 | + for (prop in order.value) { |
| 61 | + borderRadiusStyle.set(prop, LengthPercentage(count, LengthPercentageType.POINT)) |
| 62 | + val resolved = borderRadiusStyle.resolve(0, context = ctx, width = 100f, height = 100f) |
| 63 | + assertThat(resolved.get(order.key)).isEqualTo(count) |
| 64 | + count -= 1f |
| 65 | + } |
| 66 | + } |
| 67 | + } |
| 68 | + |
| 69 | + @Test |
| 70 | + fun testCorrectPriorityRTL() { |
| 71 | + setContextLeftAndRightSwap(ctx, true) |
| 72 | + val propertyOrderMap = |
| 73 | + mapOf( |
| 74 | + ComputedBorderRadiusProp.COMPUTED_BORDER_TOP_LEFT_RADIUS to |
| 75 | + arrayOf( |
| 76 | + BorderRadiusProp.BORDER_RADIUS, |
| 77 | + BorderRadiusProp.BORDER_TOP_RIGHT_RADIUS, |
| 78 | + BorderRadiusProp.BORDER_TOP_END_RADIUS, |
| 79 | + BorderRadiusProp.BORDER_END_START_RADIUS), |
| 80 | + ComputedBorderRadiusProp.COMPUTED_BORDER_TOP_RIGHT_RADIUS to |
| 81 | + arrayOf( |
| 82 | + BorderRadiusProp.BORDER_RADIUS, |
| 83 | + BorderRadiusProp.BORDER_TOP_LEFT_RADIUS, |
| 84 | + BorderRadiusProp.BORDER_TOP_START_RADIUS, |
| 85 | + BorderRadiusProp.BORDER_START_START_RADIUS), |
| 86 | + ComputedBorderRadiusProp.COMPUTED_BORDER_BOTTOM_LEFT_RADIUS to |
| 87 | + arrayOf( |
| 88 | + BorderRadiusProp.BORDER_RADIUS, |
| 89 | + BorderRadiusProp.BORDER_BOTTOM_RIGHT_RADIUS, |
| 90 | + BorderRadiusProp.BORDER_BOTTOM_START_RADIUS, |
| 91 | + BorderRadiusProp.BORDER_END_END_RADIUS), |
| 92 | + ComputedBorderRadiusProp.COMPUTED_BORDER_BOTTOM_RIGHT_RADIUS to |
| 93 | + arrayOf( |
| 94 | + BorderRadiusProp.BORDER_RADIUS, |
| 95 | + BorderRadiusProp.BORDER_BOTTOM_LEFT_RADIUS, |
| 96 | + BorderRadiusProp.BORDER_BOTTOM_END_RADIUS, |
| 97 | + BorderRadiusProp.BORDER_START_END_RADIUS), |
| 98 | + ) |
| 99 | + |
| 100 | + propertyOrderMap.forEach { order -> |
| 101 | + val borderRadiusStyle = BorderRadiusStyle() |
| 102 | + // Starting count on 3 to test 0 override |
| 103 | + var count = 3f |
| 104 | + for (prop in order.value) { |
| 105 | + borderRadiusStyle.set(prop, LengthPercentage(count, LengthPercentageType.POINT)) |
| 106 | + val resolved = borderRadiusStyle.resolve(1, context = ctx, width = 100f, height = 100f) |
| 107 | + assertThat(resolved.get(order.key)).isEqualTo(count) |
| 108 | + count -= 1f |
| 109 | + } |
| 110 | + } |
| 111 | + } |
| 112 | + |
| 113 | + @Test |
| 114 | + fun testCorrectPriorityRTLNoSwap() { |
| 115 | + setContextLeftAndRightSwap(ctx, false) |
| 116 | + val propertyOrderMap = |
| 117 | + mapOf( |
| 118 | + ComputedBorderRadiusProp.COMPUTED_BORDER_TOP_LEFT_RADIUS to |
| 119 | + arrayOf( |
| 120 | + BorderRadiusProp.BORDER_RADIUS, |
| 121 | + BorderRadiusProp.BORDER_TOP_LEFT_RADIUS, |
| 122 | + BorderRadiusProp.BORDER_TOP_END_RADIUS, |
| 123 | + BorderRadiusProp.BORDER_END_START_RADIUS), |
| 124 | + ComputedBorderRadiusProp.COMPUTED_BORDER_TOP_RIGHT_RADIUS to |
| 125 | + arrayOf( |
| 126 | + BorderRadiusProp.BORDER_RADIUS, |
| 127 | + BorderRadiusProp.BORDER_TOP_RIGHT_RADIUS, |
| 128 | + BorderRadiusProp.BORDER_TOP_START_RADIUS, |
| 129 | + BorderRadiusProp.BORDER_START_START_RADIUS), |
| 130 | + ComputedBorderRadiusProp.COMPUTED_BORDER_BOTTOM_LEFT_RADIUS to |
| 131 | + arrayOf( |
| 132 | + BorderRadiusProp.BORDER_RADIUS, |
| 133 | + BorderRadiusProp.BORDER_BOTTOM_LEFT_RADIUS, |
| 134 | + BorderRadiusProp.BORDER_BOTTOM_START_RADIUS, |
| 135 | + BorderRadiusProp.BORDER_END_END_RADIUS), |
| 136 | + ComputedBorderRadiusProp.COMPUTED_BORDER_BOTTOM_RIGHT_RADIUS to |
| 137 | + arrayOf( |
| 138 | + BorderRadiusProp.BORDER_RADIUS, |
| 139 | + BorderRadiusProp.BORDER_BOTTOM_RIGHT_RADIUS, |
| 140 | + BorderRadiusProp.BORDER_BOTTOM_END_RADIUS, |
| 141 | + BorderRadiusProp.BORDER_START_END_RADIUS), |
| 142 | + ) |
| 143 | + |
| 144 | + propertyOrderMap.forEach { order -> |
| 145 | + val borderRadiusStyle = BorderRadiusStyle() |
| 146 | + // Starting count on 3 to test 0 override |
| 147 | + var count = 3f |
| 148 | + for (prop in order.value) { |
| 149 | + borderRadiusStyle.set(prop, LengthPercentage(count, LengthPercentageType.POINT)) |
| 150 | + val resolved = borderRadiusStyle.resolve(1, context = ctx, width = 100f, height = 100f) |
| 151 | + assertThat(resolved.get(order.key)).isEqualTo(count) |
| 152 | + count -= 1f |
| 153 | + } |
| 154 | + } |
| 155 | + } |
| 156 | + |
| 157 | + @Test |
| 158 | + fun testBorderRadiusPercentages() { |
| 159 | + val borderRadiusStyle = |
| 160 | + BorderRadiusStyle( |
| 161 | + topLeft = LengthPercentage(0f, LengthPercentageType.PERCENT), |
| 162 | + topRight = LengthPercentage(10f, LengthPercentageType.PERCENT), |
| 163 | + bottomLeft = LengthPercentage(20f, LengthPercentageType.PERCENT), |
| 164 | + bottomRight = LengthPercentage(30f, LengthPercentageType.PERCENT), |
| 165 | + ) |
| 166 | + val resolved = borderRadiusStyle.resolve(0, context = ctx, width = 1000f, height = 1000f) |
| 167 | + |
| 168 | + assertThat(resolved.topLeft).isEqualTo(0f) |
| 169 | + assertThat(resolved.topRight).isEqualTo(100f) |
| 170 | + assertThat(resolved.bottomLeft).isEqualTo(200f) |
| 171 | + assertThat(resolved.bottomRight).isEqualTo(300f) |
| 172 | + } |
| 173 | + |
| 174 | + /* |
| 175 | + * Make I18nUtil.instance.doLeftAndRightSwapInRTL(context) return false |
| 176 | + * by setting context preference |
| 177 | + */ |
| 178 | + private fun setContextLeftAndRightSwap(context: Context, leftAndRightSwap: Boolean) { |
| 179 | + val sharedPrefs = |
| 180 | + context.getSharedPreferences( |
| 181 | + "com.facebook.react.modules.i18nmanager.I18nUtil", Context.MODE_PRIVATE) |
| 182 | + val editor = sharedPrefs.edit() |
| 183 | + editor.putBoolean("RCTI18nUtil_makeRTLFlipLeftAndRightStyles", leftAndRightSwap) |
| 184 | + editor.apply() |
| 185 | + } |
| 186 | +} |
0 commit comments