Skip to content

Commit 01145c9

Browse files
authored
Clear the border path effect when borderStyle is removed on Android (#59607)
1 parent 13c29f0 commit 01145c9

2 files changed

Lines changed: 82 additions & 10 deletions

File tree

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/uimanager/drawable/BorderDrawable.kt‎

Lines changed: 6 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -566,19 +566,15 @@ internal class BorderDrawable(
566566

567567
private fun updatePathEffect() {
568568
/** Used for rounded border and rounded background. */
569-
this.borderStyle?.let { style ->
570-
val pathEffectForBorderStyle =
571-
if (this.borderStyle != null) getPathEffect(style, getFullBorderWidth()) else null
572-
borderPaint.setPathEffect(pathEffectForBorderStyle)
573-
}
569+
val pathEffectForBorderStyle =
570+
this.borderStyle?.let { style -> getPathEffect(style, getFullBorderWidth()) }
571+
borderPaint.setPathEffect(pathEffectForBorderStyle)
574572
}
575573

576574
private fun updatePathEffect(borderWidth: Int) {
577-
this.borderStyle?.let { style ->
578-
val pathEffectForBorderStyle =
579-
if (this.borderStyle != null) getPathEffect(style, borderWidth.toFloat()) else null
580-
borderPaint.setPathEffect(pathEffectForBorderStyle)
581-
}
575+
val pathEffectForBorderStyle =
576+
this.borderStyle?.let { style -> getPathEffect(style, borderWidth.toFloat()) }
577+
borderPaint.setPathEffect(pathEffectForBorderStyle)
582578
}
583579

584580
private fun getPathEffect(style: BorderStyle, borderWidth: Float): PathEffect? {
Lines changed: 76 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
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.drawable
9+
10+
import android.graphics.Canvas
11+
import android.graphics.Color
12+
import android.graphics.DashPathEffect
13+
import android.graphics.Paint
14+
import android.graphics.Path
15+
import android.util.DisplayMetrics
16+
import com.facebook.react.uimanager.DisplayMetricsHolder
17+
import com.facebook.react.uimanager.Spacing
18+
import com.facebook.react.uimanager.style.BorderInsets
19+
import com.facebook.react.uimanager.style.BorderStyle
20+
import com.facebook.react.uimanager.style.LogicalEdge
21+
import org.assertj.core.api.Assertions.assertThat
22+
import org.junit.After
23+
import org.junit.Before
24+
import org.junit.Test
25+
import org.junit.runner.RunWith
26+
import org.mockito.kotlin.any
27+
import org.mockito.kotlin.argumentCaptor
28+
import org.mockito.kotlin.atLeastOnce
29+
import org.mockito.kotlin.mock
30+
import org.mockito.kotlin.verify
31+
import org.robolectric.RobolectricTestRunner
32+
import org.robolectric.RuntimeEnvironment
33+
34+
@RunWith(RobolectricTestRunner::class)
35+
class BorderDrawableTest {
36+
37+
@Before
38+
fun setUp() {
39+
DisplayMetricsHolder.setScreenDisplayMetrics(DisplayMetrics().apply { density = 1f })
40+
}
41+
42+
@After
43+
fun tearDown() {
44+
DisplayMetricsHolder.setScreenDisplayMetrics(null)
45+
}
46+
47+
@Test
48+
fun removingBorderStyleClearsDashedPathEffect() {
49+
val borderInsets = BorderInsets().apply { setBorderWidth(LogicalEdge.ALL, 8f) }
50+
val drawable =
51+
BorderDrawable(
52+
context = RuntimeEnvironment.getApplication(),
53+
borderWidth = Spacing(0f),
54+
borderRadius = null,
55+
borderInsets = borderInsets,
56+
borderStyle = BorderStyle.DASHED,
57+
)
58+
drawable.setBounds(0, 0, 240, 120)
59+
drawable.setBorderWidth(Spacing.ALL, 8f)
60+
drawable.setBorderColor(LogicalEdge.ALL, Color.RED)
61+
62+
assertThat(drawBorderPaint(drawable).pathEffect).isInstanceOf(DashPathEffect::class.java)
63+
64+
drawable.borderStyle = null
65+
66+
assertThat(drawBorderPaint(drawable).pathEffect).isNull()
67+
}
68+
69+
private fun drawBorderPaint(drawable: BorderDrawable): Paint {
70+
val canvas = mock<Canvas>()
71+
drawable.draw(canvas)
72+
val paint = argumentCaptor<Paint>()
73+
verify(canvas, atLeastOnce()).drawPath(any<Path>(), paint.capture())
74+
return paint.lastValue
75+
}
76+
}

0 commit comments

Comments
 (0)