|
| 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.graphics.drawable.ColorDrawable |
| 11 | +import android.view.View |
| 12 | +import com.facebook.react.bridge.CatalystInstance |
| 13 | +import com.facebook.react.bridge.JavaOnlyMap |
| 14 | +import com.facebook.react.bridge.ReactApplicationContext |
| 15 | +import com.facebook.react.bridge.ReactTestHelper.createMockCatalystInstance |
| 16 | +import com.facebook.react.bridge.ReadableMap |
| 17 | +import com.facebook.react.touch.JSResponderHandler |
| 18 | +import com.facebook.react.uimanager.annotations.ReactProp |
| 19 | +import org.assertj.core.api.Assertions |
| 20 | +import org.junit.Before |
| 21 | +import org.junit.Rule |
| 22 | +import org.junit.Test |
| 23 | +import org.junit.runner.RunWith |
| 24 | +import org.powermock.core.classloader.annotations.PowerMockIgnore |
| 25 | +import org.powermock.modules.junit4.rule.PowerMockRule |
| 26 | +import org.robolectric.RobolectricTestRunner |
| 27 | +import org.robolectric.RuntimeEnvironment |
| 28 | + |
| 29 | +/** Verify [View] view property being applied properly by [SimpleViewManager] */ |
| 30 | +@RunWith(RobolectricTestRunner::class) |
| 31 | +@PowerMockIgnore("org.mockito.*", "org.robolectric.*", "androidx.*", "android.*") |
| 32 | +class SimpleViewPropertyTest { |
| 33 | + |
| 34 | + @get:Rule var rule = PowerMockRule() |
| 35 | + |
| 36 | + private class ConcreteViewManager : SimpleViewManager<View?>() { |
| 37 | + @ReactProp(name = "foo") fun setFoo(view: View, foo: Boolean) {} |
| 38 | + |
| 39 | + @ReactProp(name = "bar") fun setBar(view: View, bar: ReadableMap?) {} |
| 40 | + |
| 41 | + override fun createViewInstance(reactContext: ThemedReactContext): View { |
| 42 | + return View(reactContext) |
| 43 | + } |
| 44 | + |
| 45 | + override fun getName(): String { |
| 46 | + return "View" |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + private lateinit var context: ReactApplicationContext |
| 51 | + private lateinit var catalystInstanceMock: CatalystInstance |
| 52 | + private lateinit var themedContext: ThemedReactContext |
| 53 | + private lateinit var manager: ConcreteViewManager |
| 54 | + |
| 55 | + @Before |
| 56 | + fun setup() { |
| 57 | + context = ReactApplicationContext(RuntimeEnvironment.getApplication()) |
| 58 | + catalystInstanceMock = createMockCatalystInstance() |
| 59 | + context.initializeWithInstance(catalystInstanceMock) |
| 60 | + themedContext = ThemedReactContext(context, context) |
| 61 | + manager = ConcreteViewManager() |
| 62 | + } |
| 63 | + |
| 64 | + fun buildStyles(vararg keysAndValues: Any?): ReactStylesDiffMap { |
| 65 | + return ReactStylesDiffMap(JavaOnlyMap.of(*keysAndValues)) |
| 66 | + } |
| 67 | + |
| 68 | + @Test |
| 69 | + fun testOpacity() { |
| 70 | + val view = manager.createView(viewTag, themedContext, buildStyles(), null, JSResponderHandler()) |
| 71 | + manager.updateProperties(view, buildStyles()) |
| 72 | + Assertions.assertThat(view.alpha).isEqualTo(1.0f) |
| 73 | + manager.updateProperties(view, buildStyles("opacity", 0.31)) |
| 74 | + Assertions.assertThat(view.alpha).isEqualTo(0.31f, Assertions.offset(1e-5f)) |
| 75 | + manager.updateProperties(view, buildStyles("opacity", null)) |
| 76 | + Assertions.assertThat(view.alpha).isEqualTo(1.0f) |
| 77 | + } |
| 78 | + |
| 79 | + @Test |
| 80 | + fun testBackgroundColor() { |
| 81 | + val view = manager.createView(viewTag, themedContext, buildStyles(), null, JSResponderHandler()) |
| 82 | + manager.updateProperties(view, buildStyles()) |
| 83 | + Assertions.assertThat(view.background).isEqualTo(null) |
| 84 | + manager.updateProperties(view, buildStyles("backgroundColor", 12)) |
| 85 | + Assertions.assertThat((view.background as ColorDrawable).color).isEqualTo(12) |
| 86 | + manager.updateProperties(view, buildStyles("backgroundColor", null)) |
| 87 | + Assertions.assertThat((view.background as ColorDrawable).color).isEqualTo(0) |
| 88 | + } |
| 89 | + |
| 90 | + @Test |
| 91 | + fun testGetNativeProps() { |
| 92 | + val nativeProps = manager.nativeProps |
| 93 | + Assertions.assertThat(nativeProps["foo"]).isEqualTo("boolean") |
| 94 | + Assertions.assertThat(nativeProps["bar"]).isEqualTo("Map") |
| 95 | + } |
| 96 | + |
| 97 | + companion object { |
| 98 | + private const val viewTag = 2 |
| 99 | + } |
| 100 | +} |
0 commit comments