From baa2714648890fe85bb9c22c451df50b4b0b0075 Mon Sep 17 00:00:00 2001 From: Madhusudan Sapkota Date: Tue, 8 Aug 2023 20:14:34 -0700 Subject: [PATCH] SimpleViewPropertyTest.java => SimpleViewPropertyTest.kt (#38856) Summary: This PR converts SimpleViewPropertyTest into Kotlin as requested in [https://github.com/facebook/react-native/issues/38835](https://github.com/facebook/react-native/issues/38825#issuecomment-1669634951) ## Changelog: [INTERNAL] [CHANGED] - Convert to SimpleViewPropertyTest Kotlin Pull Request resolved: https://github.com/facebook/react-native/pull/38856 Test Plan: 1. Run `./gradlew :packages:react-native:ReactAndroid:test.` 2. All tests should pass. Reviewed By: NickGerleman Differential Revision: D48171939 Pulled By: mdvacca fbshipit-source-id: eaa93a696faba1793b85186e6a072a7d3f043d0c --- .../uimanager/SimpleViewPropertyTest.java | 118 ------------------ .../react/uimanager/SimpleViewPropertyTest.kt | 100 +++++++++++++++ 2 files changed, 100 insertions(+), 118 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/SimpleViewPropertyTest.java create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/SimpleViewPropertyTest.kt diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/SimpleViewPropertyTest.java b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/SimpleViewPropertyTest.java deleted file mode 100644 index 69badeea839e15..00000000000000 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/SimpleViewPropertyTest.java +++ /dev/null @@ -1,118 +0,0 @@ -/* - * Copyright (c) Meta Platforms, Inc. and affiliates. - * - * This source code is licensed under the MIT license found in the - * LICENSE file in the root directory of this source tree. - */ - -package com.facebook.react.uimanager; - -import static org.assertj.core.api.Assertions.assertThat; -import static org.assertj.core.api.Assertions.offset; - -import android.graphics.drawable.ColorDrawable; -import android.view.View; -import com.facebook.react.bridge.CatalystInstance; -import com.facebook.react.bridge.JavaOnlyMap; -import com.facebook.react.bridge.ReactApplicationContext; -import com.facebook.react.bridge.ReactTestHelper; -import com.facebook.react.bridge.ReadableMap; -import com.facebook.react.touch.JSResponderHandler; -import com.facebook.react.uimanager.annotations.ReactProp; -import java.util.Map; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Rule; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.powermock.core.classloader.annotations.PowerMockIgnore; -import org.powermock.modules.junit4.rule.PowerMockRule; -import org.robolectric.RobolectricTestRunner; -import org.robolectric.RuntimeEnvironment; - -/** Verify {@link View} view property being applied properly by {@link SimpleViewManager} */ -@RunWith(RobolectricTestRunner.class) -@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "androidx.*", "android.*"}) -@Ignore // TODO T14964130 -public class SimpleViewPropertyTest { - - @Rule public PowerMockRule rule = new PowerMockRule(); - - private static int sViewTag = 2; - - private static class ConcreteViewManager extends SimpleViewManager { - - @ReactProp(name = "foo") - public void setFoo(View view, boolean foo) {} - - @ReactProp(name = "bar") - public void setBar(View view, ReadableMap bar) {} - - @Override - protected View createViewInstance(ThemedReactContext reactContext) { - return new View(reactContext); - } - - @Override - public String getName() { - return "View"; - } - } - - private ReactApplicationContext mContext; - private CatalystInstance mCatalystInstanceMock; - private ThemedReactContext mThemedContext; - private ConcreteViewManager mManager; - - @Before - public void setup() { - mContext = new ReactApplicationContext(RuntimeEnvironment.getApplication()); - mCatalystInstanceMock = ReactTestHelper.createMockCatalystInstance(); - mContext.initializeWithInstance(mCatalystInstanceMock); - mThemedContext = new ThemedReactContext(mContext, mContext); - mManager = new ConcreteViewManager(); - } - - public ReactStylesDiffMap buildStyles(Object... keysAndValues) { - return new ReactStylesDiffMap(JavaOnlyMap.of(keysAndValues)); - } - - @Test - public void testOpacity() { - View view = - mManager.createView( - sViewTag, mThemedContext, buildStyles(), null, new JSResponderHandler()); - - mManager.updateProperties(view, buildStyles()); - assertThat(view.getAlpha()).isEqualTo(1.0f); - - mManager.updateProperties(view, buildStyles("opacity", 0.31)); - assertThat(view.getAlpha()).isEqualTo(0.31f, offset(1e-5f)); - - mManager.updateProperties(view, buildStyles("opacity", null)); - assertThat(view.getAlpha()).isEqualTo(1.0f); - } - - @Test - public void testBackgroundColor() { - View view = - mManager.createView( - sViewTag, mThemedContext, buildStyles(), null, new JSResponderHandler()); - - mManager.updateProperties(view, buildStyles()); - assertThat(view.getBackground()).isEqualTo(null); - - mManager.updateProperties(view, buildStyles("backgroundColor", 12)); - assertThat(((ColorDrawable) view.getBackground()).getColor()).isEqualTo(12); - - mManager.updateProperties(view, buildStyles("backgroundColor", null)); - assertThat(((ColorDrawable) view.getBackground()).getColor()).isEqualTo(0); - } - - @Test - public void testGetNativeProps() { - Map nativeProps = mManager.getNativeProps(); - assertThat(nativeProps.get("foo")).isEqualTo("boolean"); - assertThat(nativeProps.get("bar")).isEqualTo("Map"); - } -} diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/SimpleViewPropertyTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/SimpleViewPropertyTest.kt new file mode 100644 index 00000000000000..313e7f65c0ee4a --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/SimpleViewPropertyTest.kt @@ -0,0 +1,100 @@ +/* + * Copyright (c) Meta Platforms, Inc. and affiliates. + * + * This source code is licensed under the MIT license found in the + * LICENSE file in the root directory of this source tree. + */ + +package com.facebook.react.uimanager + +import android.graphics.drawable.ColorDrawable +import android.view.View +import com.facebook.react.bridge.CatalystInstance +import com.facebook.react.bridge.JavaOnlyMap +import com.facebook.react.bridge.ReactApplicationContext +import com.facebook.react.bridge.ReactTestHelper.createMockCatalystInstance +import com.facebook.react.bridge.ReadableMap +import com.facebook.react.touch.JSResponderHandler +import com.facebook.react.uimanager.annotations.ReactProp +import org.assertj.core.api.Assertions +import org.junit.Before +import org.junit.Rule +import org.junit.Test +import org.junit.runner.RunWith +import org.powermock.core.classloader.annotations.PowerMockIgnore +import org.powermock.modules.junit4.rule.PowerMockRule +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment + +/** Verify [View] view property being applied properly by [SimpleViewManager] */ +@RunWith(RobolectricTestRunner::class) +@PowerMockIgnore("org.mockito.*", "org.robolectric.*", "androidx.*", "android.*") +class SimpleViewPropertyTest { + + @get:Rule var rule = PowerMockRule() + + private class ConcreteViewManager : SimpleViewManager() { + @ReactProp(name = "foo") fun setFoo(view: View, foo: Boolean) {} + + @ReactProp(name = "bar") fun setBar(view: View, bar: ReadableMap?) {} + + override fun createViewInstance(reactContext: ThemedReactContext): View { + return View(reactContext) + } + + override fun getName(): String { + return "View" + } + } + + private lateinit var context: ReactApplicationContext + private lateinit var catalystInstanceMock: CatalystInstance + private lateinit var themedContext: ThemedReactContext + private lateinit var manager: ConcreteViewManager + + @Before + fun setup() { + context = ReactApplicationContext(RuntimeEnvironment.getApplication()) + catalystInstanceMock = createMockCatalystInstance() + context.initializeWithInstance(catalystInstanceMock) + themedContext = ThemedReactContext(context, context) + manager = ConcreteViewManager() + } + + fun buildStyles(vararg keysAndValues: Any?): ReactStylesDiffMap { + return ReactStylesDiffMap(JavaOnlyMap.of(*keysAndValues)) + } + + @Test + fun testOpacity() { + val view = manager.createView(viewTag, themedContext, buildStyles(), null, JSResponderHandler()) + manager.updateProperties(view, buildStyles()) + Assertions.assertThat(view.alpha).isEqualTo(1.0f) + manager.updateProperties(view, buildStyles("opacity", 0.31)) + Assertions.assertThat(view.alpha).isEqualTo(0.31f, Assertions.offset(1e-5f)) + manager.updateProperties(view, buildStyles("opacity", null)) + Assertions.assertThat(view.alpha).isEqualTo(1.0f) + } + + @Test + fun testBackgroundColor() { + val view = manager.createView(viewTag, themedContext, buildStyles(), null, JSResponderHandler()) + manager.updateProperties(view, buildStyles()) + Assertions.assertThat(view.background).isEqualTo(null) + manager.updateProperties(view, buildStyles("backgroundColor", 12)) + Assertions.assertThat((view.background as ColorDrawable).color).isEqualTo(12) + manager.updateProperties(view, buildStyles("backgroundColor", null)) + Assertions.assertThat((view.background as ColorDrawable).color).isEqualTo(0) + } + + @Test + fun testGetNativeProps() { + val nativeProps = manager.nativeProps + Assertions.assertThat(nativeProps["foo"]).isEqualTo("boolean") + Assertions.assertThat(nativeProps["bar"]).isEqualTo("Map") + } + + companion object { + private const val viewTag = 2 + } +}