Skip to content

Commit d8739e1

Browse files
jorge-cabfacebook-github-bot
authored andcommitted
Add unit tests for BorderRadiusStyle (#44964)
Summary: Pull Request resolved: #44964 Testing property priority and correct setting percentages for business logic of `BorderRadiusStyle.kt` To prevent issues like the one fixed by D57473482 Changelog: [Internal] Reviewed By: fkgozali Differential Revision: D58705515 fbshipit-source-id: 74e9a68fc0e3d1e88b8eebbb34a1ca8c29052c21
1 parent 3fc6963 commit d8739e1

File tree

3 files changed

+214
-0
lines changed

3 files changed

+214
-0
lines changed

packages/react-native/ReactAndroid/api/ReactAndroid.api

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5956,6 +5956,7 @@ public final class com/facebook/react/uimanager/style/ComputedBorderRadius {
59565956
public final fun copy (FFFF)Lcom/facebook/react/uimanager/style/ComputedBorderRadius;
59575957
public static synthetic fun copy$default (Lcom/facebook/react/uimanager/style/ComputedBorderRadius;FFFFILjava/lang/Object;)Lcom/facebook/react/uimanager/style/ComputedBorderRadius;
59585958
public fun equals (Ljava/lang/Object;)Z
5959+
public final fun get (Lcom/facebook/react/uimanager/style/ComputedBorderRadiusProp;)F
59595960
public final fun getBottomLeft ()F
59605961
public final fun getBottomRight ()F
59615962
public final fun getTopLeft ()F
@@ -5965,6 +5966,16 @@ public final class com/facebook/react/uimanager/style/ComputedBorderRadius {
59655966
public fun toString ()Ljava/lang/String;
59665967
}
59675968

5969+
public final class com/facebook/react/uimanager/style/ComputedBorderRadiusProp : java/lang/Enum {
5970+
public static final field COMPUTED_BORDER_BOTTOM_LEFT_RADIUS Lcom/facebook/react/uimanager/style/ComputedBorderRadiusProp;
5971+
public static final field COMPUTED_BORDER_BOTTOM_RIGHT_RADIUS Lcom/facebook/react/uimanager/style/ComputedBorderRadiusProp;
5972+
public static final field COMPUTED_BORDER_TOP_LEFT_RADIUS Lcom/facebook/react/uimanager/style/ComputedBorderRadiusProp;
5973+
public static final field COMPUTED_BORDER_TOP_RIGHT_RADIUS Lcom/facebook/react/uimanager/style/ComputedBorderRadiusProp;
5974+
public static fun getEntries ()Lkotlin/enums/EnumEntries;
5975+
public static fun valueOf (Ljava/lang/String;)Lcom/facebook/react/uimanager/style/ComputedBorderRadiusProp;
5976+
public static fun values ()[Lcom/facebook/react/uimanager/style/ComputedBorderRadiusProp;
5977+
}
5978+
59685979
public class com/facebook/react/uimanager/util/ReactFindViewUtil {
59695980
public fun <init> ()V
59705981
public static fun addViewListener (Lcom/facebook/react/uimanager/util/ReactFindViewUtil$OnViewFoundListener;)V

packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/style/ComputedBorderRadius.kt

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,14 @@
77

88
package com.facebook.react.uimanager.style
99

10+
/** Represents the collection of possible computed border radius style properties. */
11+
public enum class ComputedBorderRadiusProp {
12+
COMPUTED_BORDER_TOP_LEFT_RADIUS,
13+
COMPUTED_BORDER_TOP_RIGHT_RADIUS,
14+
COMPUTED_BORDER_BOTTOM_RIGHT_RADIUS,
15+
COMPUTED_BORDER_BOTTOM_LEFT_RADIUS,
16+
}
17+
1018
/** Phsysical edge lengths (in DIPs) for a border-radius. */
1119
public data class ComputedBorderRadius(
1220
val topLeft: Float,
@@ -18,5 +26,14 @@ public data class ComputedBorderRadius(
1826
return topLeft > 0f || topRight > 0f || bottomLeft > 0f || bottomRight > 0f
1927
}
2028

29+
public fun get(property: ComputedBorderRadiusProp): Float {
30+
return when (property) {
31+
ComputedBorderRadiusProp.COMPUTED_BORDER_TOP_LEFT_RADIUS -> topLeft
32+
ComputedBorderRadiusProp.COMPUTED_BORDER_TOP_RIGHT_RADIUS -> topRight
33+
ComputedBorderRadiusProp.COMPUTED_BORDER_BOTTOM_LEFT_RADIUS -> bottomLeft
34+
ComputedBorderRadiusProp.COMPUTED_BORDER_BOTTOM_RIGHT_RADIUS -> bottomRight
35+
}
36+
}
37+
2138
public constructor() : this(0f, 0f, 0f, 0f)
2239
}
Lines changed: 186 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,186 @@
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

Comments
 (0)