Skip to content

Commit 442ed50

Browse files
committed
Fix PlatformColor theme attributes that resolve to a ColorStateList on Android
1 parent 25b05ac commit 442ed50

2 files changed

Lines changed: 57 additions & 0 deletions

File tree

‎packages/react-native/ReactAndroid/src/main/java/com/facebook/react/bridge/ColorPropConverter.kt‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -226,6 +226,9 @@ public object ColorPropConverter {
226226
val theme = context.theme
227227

228228
if (theme.resolveAttribute(resourceId, outValue, true)) {
229+
if (outValue.type == TypedValue.TYPE_STRING) {
230+
return ResourcesCompat.getColor(context.resources, outValue.resourceId, theme)
231+
}
229232
return outValue.data
230233
}
231234

Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
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.bridge
9+
10+
import android.content.Context
11+
import android.view.ContextThemeWrapper
12+
import org.assertj.core.api.Assertions.assertThat
13+
import org.junit.Before
14+
import org.junit.Test
15+
import org.junit.runner.RunWith
16+
import org.robolectric.RobolectricTestRunner
17+
import org.robolectric.RuntimeEnvironment
18+
19+
/** Tests for [ColorPropConverter] */
20+
@RunWith(RobolectricTestRunner::class)
21+
class ColorPropConverterTest {
22+
23+
private lateinit var context: Context
24+
25+
@Before
26+
fun setUp() {
27+
context =
28+
ContextThemeWrapper(
29+
RuntimeEnvironment.getApplication(),
30+
androidx.appcompat.R.style.Theme_AppCompat,
31+
)
32+
}
33+
34+
@Test
35+
fun themeAttributeResolvesColor() {
36+
val result = ColorPropConverter.resolveResourcePath(context, "?android:attr/colorAccent")
37+
38+
assertThat(result).isEqualTo(themeColor(android.R.attr.colorAccent))
39+
}
40+
41+
@Test
42+
fun themeAttributeResolvesColorStateListToDefaultColor() {
43+
val result = ColorPropConverter.resolveResourcePath(context, "?attr/textColorPrimary")
44+
45+
assertThat(result).isEqualTo(themeColor(android.R.attr.textColorPrimary))
46+
}
47+
48+
private fun themeColor(attr: Int): Int {
49+
val typedArray = context.obtainStyledAttributes(intArrayOf(attr))
50+
val color = typedArray.getColor(0, 0)
51+
typedArray.recycle()
52+
return color
53+
}
54+
}

0 commit comments

Comments
 (0)