|
| 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