From ea88338d75d38e6639636eca58e824ed7e796403 Mon Sep 17 00:00:00 2001 From: "huzhanbo.luc" Date: Tue, 3 Oct 2023 13:53:06 -0700 Subject: [PATCH] convert ReactPropAnnotationSetterTest to Kotlin (#39706) Summary: fix and convert `ReactPropAnnotationSetterTest` to Kotlin for https://github.com/facebook/react-native/issues/38825 ## Changelog: [ANDROID] [CHANGED] - Rewrite ReactPropAnnotationSetterTest to Kotlin, Pull Request resolved: https://github.com/facebook/react-native/pull/39706 Test Plan: yarn && ./gradlew :packages:react-native:ReactAndroid:test cortinico mdvacca Reviewed By: NickGerleman Differential Revision: D49839905 Pulled By: cortinico fbshipit-source-id: 4afda5cdccd722f0e16e2ab517e32ecee0b845a4 --- .../ReactPropAnnotationSetterTest.java | 495 ------------------ .../ReactPropAnnotationSetterTest.kt | 426 +++++++++++++++ .../ReactPropForShadowNodeSetterTest.java | 2 +- 3 files changed, 427 insertions(+), 496 deletions(-) delete mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactPropAnnotationSetterTest.java create mode 100644 packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactPropAnnotationSetterTest.kt diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactPropAnnotationSetterTest.java b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactPropAnnotationSetterTest.java deleted file mode 100644 index af01cd11adad1e..00000000000000 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactPropAnnotationSetterTest.java +++ /dev/null @@ -1,495 +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.fail; -import static org.mockito.Mockito.mock; -import static org.mockito.Mockito.reset; -import static org.mockito.Mockito.verify; -import static org.mockito.Mockito.verifyNoMoreInteractions; - -import android.view.View; -import com.facebook.react.bridge.JSApplicationIllegalArgumentException; -import com.facebook.react.bridge.JavaOnlyArray; -import com.facebook.react.bridge.JavaOnlyMap; -import com.facebook.react.bridge.ReadableArray; -import com.facebook.react.bridge.ReadableMap; -import com.facebook.react.uimanager.annotations.ReactProp; -import com.facebook.react.uimanager.annotations.ReactPropGroup; -import org.junit.Before; -import org.junit.Ignore; -import org.junit.Test; -import org.junit.runner.RunWith; -import org.robolectric.RobolectricTestRunner; - -/** - * Test updating view through {@link ViewManager} with {@link ReactProp} and {@link ReactPropGroup} - * annotations. - */ -@RunWith(RobolectricTestRunner.class) -@Ignore // TODO T14964130 -public class ReactPropAnnotationSetterTest { - - public interface ViewManagerUpdatesReceiver { - void onBooleanSetterCalled(boolean value); - - void onIntSetterCalled(int value); - - void onDoubleSetterCalled(double value); - - void onFloatSetterCalled(float value); - - void onStringSetterCalled(String value); - - void onBoxedBooleanSetterCalled(Boolean value); - - void onBoxedIntSetterCalled(Integer value); - - void onArraySetterCalled(ReadableArray value); - - void onMapSetterCalled(ReadableMap value); - - void onFloatGroupPropSetterCalled(int index, float value); - - void onIntGroupPropSetterCalled(int index, int value); - - void onBoxedIntGroupPropSetterCalled(int index, Integer value); - } - - private class ViewManagerUnderTest extends ViewManager { - - final ViewManagerUpdatesReceiver mViewManagerUpdatesReceiver; - - private ViewManagerUnderTest(ViewManagerUpdatesReceiver viewManagerUpdatesReceiver) { - mViewManagerUpdatesReceiver = viewManagerUpdatesReceiver; - } - - @Override - public String getName() { - return "RedpandasLivestreamVideoView"; - } - - @Override - public ReactShadowNode createShadowNodeInstance() { - fail("This method should not be executed as a part of this test"); - return null; - } - - @Override - public Class getShadowNodeClass() { - return ReactShadowNode.class; - } - - @Override - protected View createViewInstance(ThemedReactContext reactContext) { - fail("This method should not be executed as a part of this test"); - return null; - } - - @Override - public void updateExtraData(View root, Object extraData) { - fail("This method should not be executed as a part of this test"); - } - - @ReactProp(name = "boolProp") - public void setBoolProp(View v, boolean value) { - mViewManagerUpdatesReceiver.onBooleanSetterCalled(value); - } - - @ReactProp(name = "boolPropWithDefault", defaultBoolean = true) - public void setBoolPropWithDefault(View v, boolean value) { - mViewManagerUpdatesReceiver.onBooleanSetterCalled(value); - } - - @ReactProp(name = "intProp") - public void setIntProp(View v, int value) { - mViewManagerUpdatesReceiver.onIntSetterCalled(value); - } - - @ReactProp(name = "intPropWithDefault", defaultInt = 7168) - public void setIntPropWithDefault(View v, int value) { - mViewManagerUpdatesReceiver.onIntSetterCalled(value); - } - - @ReactProp(name = "floatProp") - public void setFloatProp(View v, float value) { - mViewManagerUpdatesReceiver.onFloatSetterCalled(value); - } - - @ReactProp(name = "floatPropWithDefault", defaultFloat = 14.0f) - public void setFloatPropWithDefault(View v, float value) { - mViewManagerUpdatesReceiver.onFloatSetterCalled(value); - } - - @ReactProp(name = "doubleProp") - public void setDoubleProp(View v, double value) { - mViewManagerUpdatesReceiver.onDoubleSetterCalled(value); - } - - @ReactProp(name = "doublePropWithDefault", defaultDouble = -88.0) - public void setDoublePropWithDefault(View v, double value) { - mViewManagerUpdatesReceiver.onDoubleSetterCalled(value); - } - - @ReactProp(name = "stringProp") - public void setStringProp(View v, String value) { - mViewManagerUpdatesReceiver.onStringSetterCalled(value); - } - - @ReactProp(name = "boxedBoolProp") - public void setBoxedBoolProp(View v, Boolean value) { - mViewManagerUpdatesReceiver.onBoxedBooleanSetterCalled(value); - } - - @ReactProp(name = "boxedIntProp") - public void setBoxedIntProp(View v, Integer value) { - mViewManagerUpdatesReceiver.onBoxedIntSetterCalled(value); - } - - @ReactProp(name = "arrayProp") - public void setArrayProp(View v, ReadableArray value) { - mViewManagerUpdatesReceiver.onArraySetterCalled(value); - } - - @ReactProp(name = "mapProp") - public void setMapProp(View v, ReadableMap value) { - mViewManagerUpdatesReceiver.onMapSetterCalled(value); - } - - @ReactPropGroup( - names = { - "floatGroupPropFirst", - "floatGroupPropSecond", - }) - public void setFloatGroupProp(View v, int index, float value) { - mViewManagerUpdatesReceiver.onFloatGroupPropSetterCalled(index, value); - } - - @ReactPropGroup( - names = { - "floatGroupPropWithDefaultFirst", - "floatGroupPropWithDefaultSecond", - }, - defaultFloat = -100.0f) - public void setFloatGroupPropWithDefault(View v, int index, float value) { - mViewManagerUpdatesReceiver.onFloatGroupPropSetterCalled(index, value); - } - - @ReactPropGroup(names = {"intGroupPropFirst", "intGroupPropSecond"}) - public void setIntGroupProp(View v, int index, int value) { - mViewManagerUpdatesReceiver.onIntGroupPropSetterCalled(index, value); - } - - @ReactPropGroup( - names = {"intGroupPropWithDefaultFirst", "intGroupPropWithDefaultSecond"}, - defaultInt = 555) - public void setIntGroupPropWithDefault(View v, int index, int value) { - mViewManagerUpdatesReceiver.onIntGroupPropSetterCalled(index, value); - } - - @ReactPropGroup( - names = { - "boxedIntGroupPropFirst", - "boxedIntGroupPropSecond", - }) - public void setBoxedIntGroupProp(View v, int index, Integer value) { - mViewManagerUpdatesReceiver.onBoxedIntGroupPropSetterCalled(index, value); - } - } - - public static ReactStylesDiffMap buildStyles(Object... keysAndValues) { - return new ReactStylesDiffMap(JavaOnlyMap.of(keysAndValues)); - } - - private ViewManagerUnderTest mViewManager; - private ViewManagerUpdatesReceiver mUpdatesReceiverMock; - - @Before - public void setup() { - mUpdatesReceiverMock = mock(ViewManagerUpdatesReceiver.class); - mViewManager = new ViewManagerUnderTest(mUpdatesReceiverMock); - } - - @Test - public void testBooleanSetter() { - mViewManager.updateProperties(null, buildStyles("boolProp", true)); - verify(mUpdatesReceiverMock).onBooleanSetterCalled(true); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("boolProp", false)); - verify(mUpdatesReceiverMock).onBooleanSetterCalled(false); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("boolProp", null)); - verify(mUpdatesReceiverMock).onBooleanSetterCalled(false); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("boolPropWithDefault", false)); - verify(mUpdatesReceiverMock).onBooleanSetterCalled(false); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("boolPropWithDefault", null)); - verify(mUpdatesReceiverMock).onBooleanSetterCalled(true); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - } - - @Test - public void testIntSetter() { - mViewManager.updateProperties(null, buildStyles("intProp", 13)); - verify(mUpdatesReceiverMock).onIntSetterCalled(13); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("intProp", null)); - verify(mUpdatesReceiverMock).onIntSetterCalled(0); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("intPropWithDefault", -1)); - verify(mUpdatesReceiverMock).onIntSetterCalled(-1); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("intPropWithDefault", null)); - verify(mUpdatesReceiverMock).onIntSetterCalled(7168); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - } - - @Test - public void testDoubleSetter() { - mViewManager.updateProperties(null, buildStyles("doubleProp", 13.0)); - verify(mUpdatesReceiverMock).onDoubleSetterCalled(13.0); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("doubleProp", null)); - verify(mUpdatesReceiverMock).onDoubleSetterCalled(0.0); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("doublePropWithDefault", -1.0)); - verify(mUpdatesReceiverMock).onDoubleSetterCalled(-1.0); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("doublePropWithDefault", null)); - verify(mUpdatesReceiverMock).onDoubleSetterCalled(-88.0); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - } - - @Test - public void testFloatSetter() { - mViewManager.updateProperties(null, buildStyles("floatProp", 13.0)); - verify(mUpdatesReceiverMock).onFloatSetterCalled(13.0f); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("floatProp", null)); - verify(mUpdatesReceiverMock).onFloatSetterCalled(0.0f); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("floatPropWithDefault", -1.0)); - verify(mUpdatesReceiverMock).onFloatSetterCalled(-1.0f); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("floatPropWithDefault", null)); - verify(mUpdatesReceiverMock).onFloatSetterCalled(14.0f); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - } - - @Test - public void testStringSetter() { - mViewManager.updateProperties(null, buildStyles("stringProp", "someRandomString")); - verify(mUpdatesReceiverMock).onStringSetterCalled("someRandomString"); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("stringProp", null)); - verify(mUpdatesReceiverMock).onStringSetterCalled(null); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - } - - @Test - public void testBoxedBooleanSetter() { - mViewManager.updateProperties(null, buildStyles("boxedBoolProp", true)); - verify(mUpdatesReceiverMock).onBoxedBooleanSetterCalled(Boolean.TRUE); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("boxedBoolProp", false)); - verify(mUpdatesReceiverMock).onBoxedBooleanSetterCalled(Boolean.FALSE); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("boxedBoolProp", null)); - verify(mUpdatesReceiverMock).onBoxedBooleanSetterCalled(null); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - } - - @Test - public void testBoxedIntSetter() { - mViewManager.updateProperties(null, buildStyles("boxedIntProp", 55)); - verify(mUpdatesReceiverMock).onBoxedIntSetterCalled(Integer.valueOf(55)); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("boxedIntProp", null)); - verify(mUpdatesReceiverMock).onBoxedIntSetterCalled(null); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - } - - @Test - public void testArraySetter() { - ReadableArray array = new JavaOnlyArray(); - mViewManager.updateProperties(null, buildStyles("arrayProp", array)); - verify(mUpdatesReceiverMock).onArraySetterCalled(array); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("arrayProp", null)); - verify(mUpdatesReceiverMock).onArraySetterCalled(null); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - } - - @Test - public void testMapSetter() { - ReadableMap map = new JavaOnlyMap(); - mViewManager.updateProperties(null, buildStyles("mapProp", map)); - verify(mUpdatesReceiverMock).onMapSetterCalled(map); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("mapProp", null)); - verify(mUpdatesReceiverMock).onMapSetterCalled(null); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - } - - @Test - public void testFloatGroupSetter() { - mViewManager.updateProperties(null, buildStyles("floatGroupPropFirst", 11.0)); - verify(mUpdatesReceiverMock).onFloatGroupPropSetterCalled(0, 11.0f); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("floatGroupPropSecond", -111.0)); - verify(mUpdatesReceiverMock).onFloatGroupPropSetterCalled(1, -111.0f); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("floatGroupPropSecond", null)); - verify(mUpdatesReceiverMock).onFloatGroupPropSetterCalled(1, 0.0f); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("floatGroupPropWithDefaultFirst", null)); - verify(mUpdatesReceiverMock).onFloatGroupPropSetterCalled(0, -100.0f); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - } - - @Test - public void testIntGroupSetter() { - mViewManager.updateProperties(null, buildStyles("intGroupPropFirst", -7)); - verify(mUpdatesReceiverMock).onIntGroupPropSetterCalled(0, -7); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("intGroupPropSecond", -77)); - verify(mUpdatesReceiverMock).onIntGroupPropSetterCalled(1, -77); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("intGroupPropSecond", null)); - verify(mUpdatesReceiverMock).onIntGroupPropSetterCalled(1, 0); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("intGroupPropWithDefaultFirst", 5)); - verify(mUpdatesReceiverMock).onIntGroupPropSetterCalled(0, 5); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("intGroupPropWithDefaultFirst", null)); - verify(mUpdatesReceiverMock).onIntGroupPropSetterCalled(0, 555); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("intGroupPropWithDefaultSecond", null)); - verify(mUpdatesReceiverMock).onIntGroupPropSetterCalled(1, 555); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - } - - @Test - public void testStringGroupSetter() { - mViewManager.updateProperties(null, buildStyles("boxedIntGroupPropFirst", -7)); - verify(mUpdatesReceiverMock).onBoxedIntGroupPropSetterCalled(0, -7); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("boxedIntGroupPropSecond", 12345)); - verify(mUpdatesReceiverMock).onBoxedIntGroupPropSetterCalled(1, 12345); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - - mViewManager.updateProperties(null, buildStyles("boxedIntGroupPropSecond", null)); - verify(mUpdatesReceiverMock).onBoxedIntGroupPropSetterCalled(1, null); - verifyNoMoreInteractions(mUpdatesReceiverMock); - reset(mUpdatesReceiverMock); - } - - @Test(expected = JSApplicationIllegalArgumentException.class) - public void testFailToUpdateBoolPropWithMap() { - mViewManager.updateProperties(null, buildStyles("boolProp", new JavaOnlyMap())); - } - - @Test(expected = JSApplicationIllegalArgumentException.class) - public void testFailToUpdateStringPropWithDouble() { - mViewManager.updateProperties(null, buildStyles("stringProp", 14.5)); - } - - @Test(expected = JSApplicationIllegalArgumentException.class) - public void testFailToUpdateDoublePropWithString() { - mViewManager.updateProperties(null, buildStyles("doubleProp", "hello")); - } - - @Test(expected = JSApplicationIllegalArgumentException.class) - public void testFailToUpdateIntPropWithDouble() { - mViewManager.updateProperties(null, buildStyles("intProp", -7.4)); - } - - @Test(expected = JSApplicationIllegalArgumentException.class) - public void testFailToUpdateArrayPropWithBool() { - mViewManager.updateProperties(null, buildStyles("arrayProp", false)); - } - - @Test(expected = JSApplicationIllegalArgumentException.class) - public void testFailToUpdateMapPropWithArray() { - mViewManager.updateProperties(null, buildStyles("mapProp", new JavaOnlyArray())); - } - - @Test(expected = JSApplicationIllegalArgumentException.class) - public void testFailToUpdateBoxedIntPropWithBoxedDouble() { - mViewManager.updateProperties(null, buildStyles("boxedIntProp", Double.valueOf(1))); - } -} diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactPropAnnotationSetterTest.kt b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactPropAnnotationSetterTest.kt new file mode 100644 index 00000000000000..996a761743249f --- /dev/null +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactPropAnnotationSetterTest.kt @@ -0,0 +1,426 @@ +/* + * 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.view.View +import com.facebook.react.bridge.JSApplicationIllegalArgumentException +import com.facebook.react.bridge.JavaOnlyArray +import com.facebook.react.bridge.JavaOnlyMap +import com.facebook.react.bridge.ReadableArray +import com.facebook.react.bridge.ReadableMap +import com.facebook.react.uimanager.annotations.ReactProp +import com.facebook.react.uimanager.annotations.ReactPropGroup +import org.junit.Before +import org.junit.Test +import org.junit.runner.RunWith +import org.mockito.Mockito +import org.robolectric.RobolectricTestRunner +import org.robolectric.RuntimeEnvironment + +/** Test updating view through [ViewManager] with [ReactProp] and [ReactPropGroup] annotations. */ +@RunWith(RobolectricTestRunner::class) +class ReactPropAnnotationSetterTest { + interface ViewManagerUpdatesReceiver { + fun onBooleanSetterCalled(value: Boolean) + + fun onIntSetterCalled(value: Int) + + fun onDoubleSetterCalled(value: Double) + + fun onFloatSetterCalled(value: Float) + + fun onStringSetterCalled(value: String?) + + fun onBoxedBooleanSetterCalled(value: Boolean?) + + fun onBoxedIntSetterCalled(value: Int?) + + fun onArraySetterCalled(value: ReadableArray?) + + fun onMapSetterCalled(value: ReadableMap?) + + fun onFloatGroupPropSetterCalled(index: Int, value: Float) + + fun onIntGroupPropSetterCalled(index: Int, value: Int) + + fun onBoxedIntGroupPropSetterCalled(index: Int, value: Int?) + } + + @Suppress("UNUSED_PARAMETER") + private inner class ViewManagerUnderTest( + val viewManagerUpdatesReceiver: ViewManagerUpdatesReceiver + ) : ViewManager>() { + override fun getName() = "RedpandasLivestreamVideoView" + + override fun createShadowNodeInstance(): ReactShadowNode<*> = + error("This method should not be executed as a part of this test") + + override fun getShadowNodeClass(): Class> = ReactShadowNode::class.java + + override fun createViewInstance(reactContext: ThemedReactContext): View = + error("This method should not be executed as a part of this test") + + override fun updateExtraData(root: View, extraData: Any) = + error("This method should not be executed as a part of this test") + + @ReactProp(name = "boolProp") + fun setBoolProp(v: View?, value: Boolean) { + viewManagerUpdatesReceiver.onBooleanSetterCalled(value) + } + + @ReactProp(name = "boolPropWithDefault", defaultBoolean = true) + fun setBoolPropWithDefault(v: View?, value: Boolean) { + viewManagerUpdatesReceiver.onBooleanSetterCalled(value) + } + + @ReactProp(name = "intProp") + fun setIntProp(v: View?, value: Int) { + viewManagerUpdatesReceiver.onIntSetterCalled(value) + } + + @ReactProp(name = "intPropWithDefault", defaultInt = 7168) + fun setIntPropWithDefault(v: View?, value: Int) { + viewManagerUpdatesReceiver.onIntSetterCalled(value) + } + + @ReactProp(name = "floatProp") + fun setFloatProp(v: View?, value: Float) { + viewManagerUpdatesReceiver.onFloatSetterCalled(value) + } + + @ReactProp(name = "floatPropWithDefault", defaultFloat = 14.0f) + fun setFloatPropWithDefault(v: View?, value: Float) { + viewManagerUpdatesReceiver.onFloatSetterCalled(value) + } + + @ReactProp(name = "doubleProp") + fun setDoubleProp(v: View?, value: Double) { + viewManagerUpdatesReceiver.onDoubleSetterCalled(value) + } + + @ReactProp(name = "doublePropWithDefault", defaultDouble = -88.0) + fun setDoublePropWithDefault(v: View?, value: Double) { + viewManagerUpdatesReceiver.onDoubleSetterCalled(value) + } + + @ReactProp(name = "stringProp") + fun setStringProp(v: View?, value: String?) { + viewManagerUpdatesReceiver.onStringSetterCalled(value) + } + + @ReactProp(name = "boxedBoolProp") + fun setBoxedBoolProp(v: View?, value: Boolean?) { + viewManagerUpdatesReceiver.onBoxedBooleanSetterCalled(value) + } + + @ReactProp(name = "boxedIntProp") + fun setBoxedIntProp(v: View?, value: Int?) { + viewManagerUpdatesReceiver.onBoxedIntSetterCalled(value) + } + + @ReactProp(name = "arrayProp") + fun setArrayProp(v: View?, value: ReadableArray?) { + viewManagerUpdatesReceiver.onArraySetterCalled(value) + } + + @ReactProp(name = "mapProp") + fun setMapProp(v: View?, value: ReadableMap?) { + viewManagerUpdatesReceiver.onMapSetterCalled(value) + } + + @ReactPropGroup(names = ["floatGroupPropFirst", "floatGroupPropSecond"]) + fun setFloatGroupProp(v: View?, index: Int, value: Float) { + viewManagerUpdatesReceiver.onFloatGroupPropSetterCalled(index, value) + } + + @ReactPropGroup( + names = ["floatGroupPropWithDefaultFirst", "floatGroupPropWithDefaultSecond"], + defaultFloat = -100.0f) + fun setFloatGroupPropWithDefault(v: View?, index: Int, value: Float) { + viewManagerUpdatesReceiver.onFloatGroupPropSetterCalled(index, value) + } + + @ReactPropGroup(names = ["intGroupPropFirst", "intGroupPropSecond"]) + fun setIntGroupProp(v: View?, index: Int, value: Int) { + viewManagerUpdatesReceiver.onIntGroupPropSetterCalled(index, value) + } + + @ReactPropGroup( + names = ["intGroupPropWithDefaultFirst", "intGroupPropWithDefaultSecond"], defaultInt = 555) + fun setIntGroupPropWithDefault(v: View?, index: Int, value: Int) { + viewManagerUpdatesReceiver.onIntGroupPropSetterCalled(index, value) + } + + @ReactPropGroup(names = ["boxedIntGroupPropFirst", "boxedIntGroupPropSecond"]) + fun setBoxedIntGroupProp(v: View?, index: Int, value: Int?) { + viewManagerUpdatesReceiver.onBoxedIntGroupPropSetterCalled(index, value) + } + } + + private lateinit var viewManager: ViewManagerUnderTest + private lateinit var updatesReceiverMock: ViewManagerUpdatesReceiver + private lateinit var targetView: View + + @Before + fun setup() { + updatesReceiverMock = Mockito.mock(ViewManagerUpdatesReceiver::class.java) + viewManager = ViewManagerUnderTest(updatesReceiverMock) + targetView = View(RuntimeEnvironment.getApplication()) + } + + @Test + fun testBooleanSetter() { + viewManager.updateProperties(targetView, buildStyles("boolProp", true)) + Mockito.verify(updatesReceiverMock).onBooleanSetterCalled(true) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("boolProp", false)) + Mockito.verify(updatesReceiverMock).onBooleanSetterCalled(false) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("boolProp", null)) + Mockito.verify(updatesReceiverMock).onBooleanSetterCalled(false) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("boolPropWithDefault", false)) + Mockito.verify(updatesReceiverMock).onBooleanSetterCalled(false) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("boolPropWithDefault", null)) + Mockito.verify(updatesReceiverMock).onBooleanSetterCalled(true) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + } + + @Test + fun testIntSetter() { + viewManager.updateProperties(targetView, buildStyles("intProp", 13)) + Mockito.verify(updatesReceiverMock).onIntSetterCalled(13) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("intProp", null)) + Mockito.verify(updatesReceiverMock).onIntSetterCalled(0) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("intProp", -7.4)) + Mockito.verify(updatesReceiverMock).onIntSetterCalled(-7) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("intPropWithDefault", -1)) + Mockito.verify(updatesReceiverMock).onIntSetterCalled(-1) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("intPropWithDefault", null)) + Mockito.verify(updatesReceiverMock).onIntSetterCalled(7168) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + } + + @Test + fun testDoubleSetter() { + viewManager.updateProperties(targetView, buildStyles("doubleProp", 13.0)) + Mockito.verify(updatesReceiverMock).onDoubleSetterCalled(13.0) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("doubleProp", null)) + Mockito.verify(updatesReceiverMock).onDoubleSetterCalled(0.0) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("doublePropWithDefault", -1.0)) + Mockito.verify(updatesReceiverMock).onDoubleSetterCalled(-1.0) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("doublePropWithDefault", null)) + Mockito.verify(updatesReceiverMock).onDoubleSetterCalled(-88.0) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + } + + @Test + fun testFloatSetter() { + viewManager.updateProperties(targetView, buildStyles("floatProp", 13.0)) + Mockito.verify(updatesReceiverMock).onFloatSetterCalled(13.0f) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("floatProp", null)) + Mockito.verify(updatesReceiverMock).onFloatSetterCalled(0.0f) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("floatPropWithDefault", -1.0)) + Mockito.verify(updatesReceiverMock).onFloatSetterCalled(-1.0f) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("floatPropWithDefault", null)) + Mockito.verify(updatesReceiverMock).onFloatSetterCalled(14.0f) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + } + + @Test + fun testStringSetter() { + viewManager.updateProperties(targetView, buildStyles("stringProp", "someRandomString")) + Mockito.verify(updatesReceiverMock).onStringSetterCalled("someRandomString") + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("stringProp", null)) + Mockito.verify(updatesReceiverMock).onStringSetterCalled(null) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + } + + @Test + fun testBoxedBooleanSetter() { + viewManager.updateProperties(targetView, buildStyles("boxedBoolProp", true)) + Mockito.verify(updatesReceiverMock).onBoxedBooleanSetterCalled(java.lang.Boolean.TRUE) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("boxedBoolProp", false)) + Mockito.verify(updatesReceiverMock).onBoxedBooleanSetterCalled(java.lang.Boolean.FALSE) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("boxedBoolProp", null)) + Mockito.verify(updatesReceiverMock).onBoxedBooleanSetterCalled(null) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + } + + @Test + fun testBoxedIntSetter() { + viewManager.updateProperties(targetView, buildStyles("boxedIntProp", 55)) + Mockito.verify(updatesReceiverMock).onBoxedIntSetterCalled(55) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("boxedIntProp", 1)) + Mockito.verify(updatesReceiverMock).onBoxedIntSetterCalled(1) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("boxedIntProp", null)) + Mockito.verify(updatesReceiverMock).onBoxedIntSetterCalled(null) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + } + + @Test + fun testArraySetter() { + val array: ReadableArray = JavaOnlyArray() + viewManager.updateProperties(targetView, buildStyles("arrayProp", array)) + Mockito.verify(updatesReceiverMock).onArraySetterCalled(array) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("arrayProp", null)) + Mockito.verify(updatesReceiverMock).onArraySetterCalled(null) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + } + + @Test + fun testMapSetter() { + val map: ReadableMap = JavaOnlyMap() + viewManager.updateProperties(targetView, buildStyles("mapProp", map)) + Mockito.verify(updatesReceiverMock).onMapSetterCalled(map) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("mapProp", null)) + Mockito.verify(updatesReceiverMock).onMapSetterCalled(null) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + } + + @Test + fun testFloatGroupSetter() { + viewManager.updateProperties(targetView, buildStyles("floatGroupPropFirst", 11.0)) + Mockito.verify(updatesReceiverMock).onFloatGroupPropSetterCalled(0, 11.0f) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("floatGroupPropSecond", -111.0)) + Mockito.verify(updatesReceiverMock).onFloatGroupPropSetterCalled(1, -111.0f) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("floatGroupPropSecond", null)) + Mockito.verify(updatesReceiverMock).onFloatGroupPropSetterCalled(1, 0.0f) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("floatGroupPropWithDefaultFirst", null)) + Mockito.verify(updatesReceiverMock).onFloatGroupPropSetterCalled(0, -100.0f) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + } + + @Test + fun testIntGroupSetter() { + viewManager.updateProperties(targetView, buildStyles("intGroupPropFirst", -7)) + Mockito.verify(updatesReceiverMock).onIntGroupPropSetterCalled(0, -7) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("intGroupPropSecond", -77)) + Mockito.verify(updatesReceiverMock).onIntGroupPropSetterCalled(1, -77) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("intGroupPropSecond", null)) + Mockito.verify(updatesReceiverMock).onIntGroupPropSetterCalled(1, 0) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("intGroupPropWithDefaultFirst", 5)) + Mockito.verify(updatesReceiverMock).onIntGroupPropSetterCalled(0, 5) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("intGroupPropWithDefaultFirst", null)) + Mockito.verify(updatesReceiverMock).onIntGroupPropSetterCalled(0, 555) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("intGroupPropWithDefaultSecond", null)) + Mockito.verify(updatesReceiverMock).onIntGroupPropSetterCalled(1, 555) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + } + + @Test + fun testStringGroupSetter() { + viewManager.updateProperties(targetView, buildStyles("boxedIntGroupPropFirst", -7)) + Mockito.verify(updatesReceiverMock).onBoxedIntGroupPropSetterCalled(0, -7) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("boxedIntGroupPropSecond", 12345)) + Mockito.verify(updatesReceiverMock).onBoxedIntGroupPropSetterCalled(1, 12345) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + viewManager.updateProperties(targetView, buildStyles("boxedIntGroupPropSecond", null)) + Mockito.verify(updatesReceiverMock).onBoxedIntGroupPropSetterCalled(1, null) + Mockito.verifyNoMoreInteractions(updatesReceiverMock) + Mockito.reset(updatesReceiverMock) + } + + @Test(expected = JSApplicationIllegalArgumentException::class) + fun testFailToUpdateBoolPropWithMap() { + viewManager.updateProperties(targetView, buildStyles("boolProp", JavaOnlyMap())) + } + + @Test(expected = JSApplicationIllegalArgumentException::class) + fun testFailToUpdateStringPropWithDouble() { + viewManager.updateProperties(targetView, buildStyles("stringProp", 14.5)) + } + + @Test(expected = JSApplicationIllegalArgumentException::class) + fun testFailToUpdateDoublePropWithString() { + viewManager.updateProperties(targetView, buildStyles("doubleProp", "hello")) + } + + @Test(expected = JSApplicationIllegalArgumentException::class) + fun testFailToUpdateArrayPropWithBool() { + viewManager.updateProperties(targetView, buildStyles("arrayProp", false)) + } + + @Test(expected = JSApplicationIllegalArgumentException::class) + fun testFailToUpdateMapPropWithArray() { + viewManager.updateProperties(targetView, buildStyles("mapProp", JavaOnlyArray())) + } + + companion object { + fun buildStyles(vararg keysAndValues: Any?) = ReactStylesDiffMap(JavaOnlyMap.of(*keysAndValues)) + } +} diff --git a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactPropForShadowNodeSetterTest.java b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactPropForShadowNodeSetterTest.java index 335d3588915bb3..a6062a221d2a5e 100644 --- a/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactPropForShadowNodeSetterTest.java +++ b/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactPropForShadowNodeSetterTest.java @@ -59,7 +59,7 @@ public interface ViewManagerUpdatesReceiver { void onBoxedIntGroupPropSetterCalled(int index, Integer value); } - public static ReactStylesDiffMap buildStyles(Object... keysAndValues) { + private ReactStylesDiffMap buildStyles(Object... keysAndValues) { return new ReactStylesDiffMap(JavaOnlyMap.of(keysAndValues)); }