From 5e9db574ee3aee6c44cbc270120e57d43e38cc74 Mon Sep 17 00:00:00 2001 From: Aaron Chiu Date: Fri, 20 Jan 2017 15:53:42 -0800 Subject: [PATCH] access view managers as Native Modules Reviewed By: achen1 Differential Revision: D4338782 fbshipit-source-id: 1573e45ee3af5a44d033a166424e556b2c091fb6 --- Libraries/ReactNative/UIManager.js | 6 ++- ...alystNativeJSToJavaParametersTestCase.java | 3 +- ...talystNativeJavaToJSArgumentsTestCase.java | 3 +- .../tests/CatalystUIManagerTestCase.java | 3 +- .../facebook/react/tests/JSLocaleTest.java | 3 +- .../react/tests/ProgressBarTestCase.java | 3 +- .../react/tests/ViewRenderingTestCase.java | 3 +- .../facebook/react/CoreModulesPackage.java | 8 +++- .../react/XReactInstanceManagerImpl.java | 6 ++- .../react/uimanager/UIManagerModule.java | 13 ++++-- .../UIManagerModuleConstantsHelper.java | 5 ++- .../uimanager/ReactPropConstantsTest.java | 3 +- .../UIManagerModuleConstantsTest.java | 42 +++++++++++-------- .../react/uimanager/UIManagerModuleTest.java | 3 +- .../react/views/text/ReactTextTest.java | 3 +- .../react/views/textinput/TextInputTest.java | 3 +- 16 files changed, 73 insertions(+), 37 deletions(-) diff --git a/Libraries/ReactNative/UIManager.js b/Libraries/ReactNative/UIManager.js index 2f8926488..ee2fc4b86 100644 --- a/Libraries/ReactNative/UIManager.js +++ b/Libraries/ReactNative/UIManager.js @@ -101,7 +101,11 @@ if (Platform.OS === 'ios') { } }); } else if (Platform.OS === 'android' && UIManager.AndroidLazyViewManagersEnabled) { - // TODO fill this out + UIManager.ViewManagerNames.forEach(viewManagerName => { + defineLazyObjectProperty(UIManager, viewManagerName, { + get: () => NativeModules[viewManagerName.replace(/^(RCT|RK)/, '')], + }); + }); } module.exports = UIManager; diff --git a/ReactAndroid/src/androidTest/java/com/facebook/react/tests/CatalystNativeJSToJavaParametersTestCase.java b/ReactAndroid/src/androidTest/java/com/facebook/react/tests/CatalystNativeJSToJavaParametersTestCase.java index 7b691d277..56e209bfb 100644 --- a/ReactAndroid/src/androidTest/java/com/facebook/react/tests/CatalystNativeJSToJavaParametersTestCase.java +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/tests/CatalystNativeJSToJavaParametersTestCase.java @@ -81,7 +81,8 @@ public class CatalystNativeJSToJavaParametersTestCase extends ReactIntegrationTe final UIManagerModule mUIManager = new UIManagerModule( getContext(), viewManagers, - new UIImplementationProvider()); + new UIImplementationProvider(), + false); UiThreadUtil.runOnUiThread( new Runnable() { @Override diff --git a/ReactAndroid/src/androidTest/java/com/facebook/react/tests/CatalystNativeJavaToJSArgumentsTestCase.java b/ReactAndroid/src/androidTest/java/com/facebook/react/tests/CatalystNativeJavaToJSArgumentsTestCase.java index 4b89f5c57..1b377e56b 100644 --- a/ReactAndroid/src/androidTest/java/com/facebook/react/tests/CatalystNativeJavaToJSArgumentsTestCase.java +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/tests/CatalystNativeJavaToJSArgumentsTestCase.java @@ -62,7 +62,8 @@ public class CatalystNativeJavaToJSArgumentsTestCase extends ReactIntegrationTes final UIManagerModule mUIManager = new UIManagerModule( getContext(), viewManagers, - new UIImplementationProvider()); + new UIImplementationProvider(), + false); UiThreadUtil.runOnUiThread( new Runnable() { @Override diff --git a/ReactAndroid/src/androidTest/java/com/facebook/react/tests/CatalystUIManagerTestCase.java b/ReactAndroid/src/androidTest/java/com/facebook/react/tests/CatalystUIManagerTestCase.java index f73efaf46..5345064b3 100644 --- a/ReactAndroid/src/androidTest/java/com/facebook/react/tests/CatalystUIManagerTestCase.java +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/tests/CatalystUIManagerTestCase.java @@ -82,7 +82,8 @@ public class CatalystUIManagerTestCase extends ReactIntegrationTestCase { uiManager = new UIManagerModule( getContext(), viewManagers, - new UIImplementationProvider()); + new UIImplementationProvider(), + false); UiThreadUtil.runOnUiThread(new Runnable() { @Override public void run() { diff --git a/ReactAndroid/src/androidTest/java/com/facebook/react/tests/JSLocaleTest.java b/ReactAndroid/src/androidTest/java/com/facebook/react/tests/JSLocaleTest.java index 3dceaefbd..498d587e6 100644 --- a/ReactAndroid/src/androidTest/java/com/facebook/react/tests/JSLocaleTest.java +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/tests/JSLocaleTest.java @@ -46,7 +46,8 @@ public class JSLocaleTest extends ReactIntegrationTestCase { final UIManagerModule mUIManager = new UIManagerModule( getContext(), viewManagers, - new UIImplementationProvider()); + new UIImplementationProvider(), + false); UiThreadUtil.runOnUiThread( new Runnable() { @Override diff --git a/ReactAndroid/src/androidTest/java/com/facebook/react/tests/ProgressBarTestCase.java b/ReactAndroid/src/androidTest/java/com/facebook/react/tests/ProgressBarTestCase.java index 1c9b9e368..40cc0b8f5 100644 --- a/ReactAndroid/src/androidTest/java/com/facebook/react/tests/ProgressBarTestCase.java +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/tests/ProgressBarTestCase.java @@ -72,7 +72,8 @@ public class ProgressBarTestCase extends ReactIntegrationTestCase { mUIManager = new UIManagerModule( getContext(), viewManagers, - new UIImplementationProvider()); + new UIImplementationProvider(), + false); UiThreadUtil.runOnUiThread( new Runnable() { @Override diff --git a/ReactAndroid/src/androidTest/java/com/facebook/react/tests/ViewRenderingTestCase.java b/ReactAndroid/src/androidTest/java/com/facebook/react/tests/ViewRenderingTestCase.java index 4e2d65a30..71e22fa99 100644 --- a/ReactAndroid/src/androidTest/java/com/facebook/react/tests/ViewRenderingTestCase.java +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/tests/ViewRenderingTestCase.java @@ -53,7 +53,8 @@ public class ViewRenderingTestCase extends ReactIntegrationTestCase { final UIManagerModule uiManager = new UIManagerModule( getContext(), viewManagers, - new UIImplementationProvider()); + new UIImplementationProvider(), + false); UiThreadUtil.runOnUiThread( new Runnable() { @Override diff --git a/ReactAndroid/src/main/java/com/facebook/react/CoreModulesPackage.java b/ReactAndroid/src/main/java/com/facebook/react/CoreModulesPackage.java index acc5ef4db..7451ae6de 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/CoreModulesPackage.java +++ b/ReactAndroid/src/main/java/com/facebook/react/CoreModulesPackage.java @@ -73,14 +73,17 @@ import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_M private final ReactInstanceManager mReactInstanceManager; private final DefaultHardwareBackBtnHandler mHardwareBackBtnHandler; private final UIImplementationProvider mUIImplementationProvider; + private final boolean mLazyViewManagersEnabled; CoreModulesPackage( ReactInstanceManager reactInstanceManager, DefaultHardwareBackBtnHandler hardwareBackBtnHandler, - UIImplementationProvider uiImplementationProvider) { + UIImplementationProvider uiImplementationProvider, + boolean lazyViewManagersEnabled) { mReactInstanceManager = reactInstanceManager; mHardwareBackBtnHandler = hardwareBackBtnHandler; mUIImplementationProvider = uiImplementationProvider; + mLazyViewManagersEnabled = lazyViewManagersEnabled; } @Override @@ -208,7 +211,8 @@ import static com.facebook.react.bridge.ReactMarkerConstants.CREATE_UI_MANAGER_M return new UIManagerModule( reactContext, viewManagersList, - mUIImplementationProvider); + mUIImplementationProvider, + mLazyViewManagersEnabled); } finally { Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_END); diff --git a/ReactAndroid/src/main/java/com/facebook/react/XReactInstanceManagerImpl.java b/ReactAndroid/src/main/java/com/facebook/react/XReactInstanceManagerImpl.java index f798af3ed..e3e33064b 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/XReactInstanceManagerImpl.java +++ b/ReactAndroid/src/main/java/com/facebook/react/XReactInstanceManagerImpl.java @@ -841,7 +841,11 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE; "createAndProcessCoreModulesPackage"); try { CoreModulesPackage coreModulesPackage = - new CoreModulesPackage(this, mBackBtnHandler, mUIImplementationProvider); + new CoreModulesPackage( + this, + mBackBtnHandler, + mUIImplementationProvider, + mLazyViewManagersEnabled); processPackage( coreModulesPackage, reactContext, diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java index 633e8ba6a..79cfc7ff3 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModule.java @@ -89,11 +89,12 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements public UIManagerModule( ReactApplicationContext reactContext, List viewManagerList, - UIImplementationProvider uiImplementationProvider) { + UIImplementationProvider uiImplementationProvider, + boolean lazyViewManagersEnabled) { super(reactContext); DisplayMetricsHolder.initDisplayMetricsIfNotInitialized(reactContext); mEventDispatcher = new EventDispatcher(reactContext); - mModuleConstants = createConstants(viewManagerList); + mModuleConstants = createConstants(viewManagerList, lazyViewManagersEnabled); mUIImplementation = uiImplementationProvider .createUIImplementation(reactContext, viewManagerList, mEventDispatcher); @@ -147,11 +148,15 @@ public class UIManagerModule extends ReactContextBaseJavaModule implements YogaNodePool.get().clear(); } - private static Map createConstants(List viewManagerList) { + private static Map createConstants( + List viewManagerList, + boolean lazyViewManagersEnabled) { ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_CONSTANTS_START); Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "CreateUIManagerConstants"); try { - return UIManagerModuleConstantsHelper.createConstants(viewManagerList); + return UIManagerModuleConstantsHelper.createConstants( + viewManagerList, + lazyViewManagersEnabled); } finally { Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_CONSTANTS_END); diff --git a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstantsHelper.java b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstantsHelper.java index 2d725c2c2..9c1afe2b2 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstantsHelper.java +++ b/ReactAndroid/src/main/java/com/facebook/react/uimanager/UIManagerModuleConstantsHelper.java @@ -40,7 +40,9 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE; * {@link UIManagerModuleConstants}. * TODO(6845124): Create a test for this */ - /* package */ static Map createConstants(List viewManagers) { + /* package */ static Map createConstants( + List viewManagers, + boolean lazyViewManagersEnabled) { Map constants = UIManagerModuleConstants.getConstants(); Map bubblingEventTypesConstants = UIManagerModuleConstants.getBubblingEventTypeConstants(); Map directEventTypesConstants = UIManagerModuleConstants.getDirectEventTypeConstants(); @@ -81,6 +83,7 @@ import static com.facebook.systrace.Systrace.TRACE_TAG_REACT_JAVA_BRIDGE; constants.put(CUSTOM_BUBBLING_EVENT_TYPES_KEY, bubblingEventTypesConstants); constants.put(CUSTOM_DIRECT_EVENT_TYPES_KEY, directEventTypesConstants); + constants.put("AndroidLazyViewManagersEnabled", lazyViewManagersEnabled); return constants; } diff --git a/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactPropConstantsTest.java b/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactPropConstantsTest.java index 91873e56c..8cc25c3b2 100644 --- a/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactPropConstantsTest.java +++ b/ReactAndroid/src/test/java/com/facebook/react/uimanager/ReactPropConstantsTest.java @@ -148,7 +148,8 @@ public class ReactPropConstantsTest { UIManagerModule uiManagerModule = new UIManagerModule( reactContext, viewManagers, - new UIImplementationProvider()); + new UIImplementationProvider(), + false); Map constants = (Map) valueAtPath(uiManagerModule.getConstants(), "SomeView", "NativeProps"); assertThat(constants).isEqualTo( diff --git a/ReactAndroid/src/test/java/com/facebook/react/uimanager/UIManagerModuleConstantsTest.java b/ReactAndroid/src/test/java/com/facebook/react/uimanager/UIManagerModuleConstantsTest.java index 6bed0fe7b..ab272af5e 100644 --- a/ReactAndroid/src/test/java/com/facebook/react/uimanager/UIManagerModuleConstantsTest.java +++ b/ReactAndroid/src/test/java/com/facebook/react/uimanager/UIManagerModuleConstantsTest.java @@ -69,9 +69,10 @@ public class UIManagerModuleConstantsTest { public void testNoCustomConstants() { List viewManagers = Arrays.asList(mock(ViewManager.class)); UIManagerModule uiManagerModule = new UIManagerModule( - mReactContext, - viewManagers, - mUIImplementationProvider); + mReactContext, + viewManagers, + mUIImplementationProvider, + false); Map constants = uiManagerModule.getConstants(); assertThat(constants) .containsKey(CUSTOM_BUBBLING_EVENT_TYPES) @@ -86,9 +87,10 @@ public class UIManagerModuleConstantsTest { when(mockViewManager.getExportedCustomBubblingEventTypeConstants()) .thenReturn(MapBuilder.of("onTwirl", TWIRL_BUBBLING_EVENT_MAP)); UIManagerModule uiManagerModule = new UIManagerModule( - mReactContext, - viewManagers, - mUIImplementationProvider); + mReactContext, + viewManagers, + mUIImplementationProvider, + false); Map constants = uiManagerModule.getConstants(); assertThat((Map) constants.get(CUSTOM_BUBBLING_EVENT_TYPES)) .contains(MapEntry.entry("onTwirl", TWIRL_BUBBLING_EVENT_MAP)) @@ -102,9 +104,10 @@ public class UIManagerModuleConstantsTest { when(mockViewManager.getExportedCustomDirectEventTypeConstants()) .thenReturn(MapBuilder.of("onTwirl", TWIRL_DIRECT_EVENT_MAP)); UIManagerModule uiManagerModule = new UIManagerModule( - mReactContext, - viewManagers, - mUIImplementationProvider); + mReactContext, + viewManagers, + mUIImplementationProvider, + false); Map constants = uiManagerModule.getConstants(); assertThat((Map) constants.get(CUSTOM_DIRECT_EVENT_TYPES)) .contains(MapEntry.entry("onTwirl", TWIRL_DIRECT_EVENT_MAP)) @@ -119,9 +122,10 @@ public class UIManagerModuleConstantsTest { when(mockViewManager.getExportedViewConstants()) .thenReturn(MapBuilder.of("PhotoSizeType", MapBuilder.of("Small", 1, "Large", 2))); UIManagerModule uiManagerModule = new UIManagerModule( - mReactContext, - viewManagers, - mUIImplementationProvider); + mReactContext, + viewManagers, + mUIImplementationProvider, + false); Map constants = uiManagerModule.getConstants(); assertThat(constants).containsKey("RedPandaPhotoOfTheDayView"); assertThat((Map) constants.get("RedPandaPhotoOfTheDayView")).containsKey("Constants"); @@ -137,9 +141,10 @@ public class UIManagerModuleConstantsTest { when(mockViewManager.getNativeProps()) .thenReturn(MapBuilder.of("fooProp", "number")); UIManagerModule uiManagerModule = new UIManagerModule( - mReactContext, - viewManagers, - mUIImplementationProvider); + mReactContext, + viewManagers, + mUIImplementationProvider, + false); Map constants = uiManagerModule.getConstants(); assertThat((String) valueAtPath(constants, "SomeView", "NativeProps", "fooProp")) .isEqualTo("number"); @@ -171,9 +176,10 @@ public class UIManagerModuleConstantsTest { List viewManagers = Arrays.asList(managerX, managerY); UIManagerModule uiManagerModule = new UIManagerModule( - mReactContext, - viewManagers, - mUIImplementationProvider); + mReactContext, + viewManagers, + mUIImplementationProvider, + false); Map constants = uiManagerModule.getConstants(); assertThat((Map) constants.get(CUSTOM_DIRECT_EVENT_TYPES)).containsKey("onTwirl"); diff --git a/ReactAndroid/src/test/java/com/facebook/react/uimanager/UIManagerModuleTest.java b/ReactAndroid/src/test/java/com/facebook/react/uimanager/UIManagerModuleTest.java index 7cdcabf53..e11caf3e7 100644 --- a/ReactAndroid/src/test/java/com/facebook/react/uimanager/UIManagerModuleTest.java +++ b/ReactAndroid/src/test/java/com/facebook/react/uimanager/UIManagerModuleTest.java @@ -820,7 +820,8 @@ public class UIManagerModuleTest { UIManagerModule uiManagerModule = new UIManagerModule( mReactContext, viewManagers, - new UIImplementationProvider()); + new UIImplementationProvider(), + false); uiManagerModule.onHostResume(); return uiManagerModule; } diff --git a/ReactAndroid/src/test/java/com/facebook/react/views/text/ReactTextTest.java b/ReactAndroid/src/test/java/com/facebook/react/views/text/ReactTextTest.java index b2008d0ed..ad0721e4e 100644 --- a/ReactAndroid/src/test/java/com/facebook/react/views/text/ReactTextTest.java +++ b/ReactAndroid/src/test/java/com/facebook/react/views/text/ReactTextTest.java @@ -434,7 +434,8 @@ public class ReactTextTest { UIManagerModule uiManagerModule = new UIManagerModule( reactContext, viewManagers, - new UIImplementationProvider()); + new UIImplementationProvider(), + false); uiManagerModule.onHostResume(); return uiManagerModule; } diff --git a/ReactAndroid/src/test/java/com/facebook/react/views/textinput/TextInputTest.java b/ReactAndroid/src/test/java/com/facebook/react/views/textinput/TextInputTest.java index 4e783a227..2105d8f90 100644 --- a/ReactAndroid/src/test/java/com/facebook/react/views/textinput/TextInputTest.java +++ b/ReactAndroid/src/test/java/com/facebook/react/views/textinput/TextInputTest.java @@ -186,7 +186,8 @@ public class TextInputTest { UIManagerModule uiManagerModule = new UIManagerModule( reactContext, viewManagers, - new UIImplementationProvider()); + new UIImplementationProvider(), + false); uiManagerModule.onHostResume(); return uiManagerModule; }