@Override public void onDestroy() { Log.d(TAG, "onDestroy"); super.onDestroy(); mReactInstanceManager.onPause(); mReactInstanceManager.onDestroy(); mReactInstanceManager = null; }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mReactRootView = new ReactRootView(this); mImagePicker = new ImagePickerPackage(this); mReactInstanceManager = ReactInstanceManager.builder() .setApplication(getApplication()) .setBundleAssetName("index.android.bundle") .setJSMainModuleName("index.android") .addPackage(new MainReactPackage()) .addPackage(new ReactMaterialKitPackage()) .addPackage(new ImageResizerPackage()) .addPackage(new RNNetworkingManagerModule()) .addPackage(new GestureImagePackage()) .addPackage(mImagePicker) .setUseDeveloperSupport(BuildConfig.DEBUG) .setInitialLifecycleState(LifecycleState.RESUMED) .build(); mReactRootView.startReactApplication(mReactInstanceManager, "civil", null); setContentView(mReactRootView); }
@Override public boolean onKeyUp(int keyCode, KeyEvent event) { if (keyCode == KeyEvent.KEYCODE_MENU && mReactInstanceManager != null) { mReactInstanceManager.showDevOptionsDialog(); return true; } return super.onKeyUp(keyCode, event); }
@Override protected void onDestroy() { super.onDestroy(); if (mReactInstanceManager != null) { mReactInstanceManager.onHostDestroy(this); } }
@Override public void onBackPressed() { if (mReactInstanceManager != null) { mReactInstanceManager.onBackPressed(); } else { super.onBackPressed(); } }
@Override protected void onPause() { super.onPause(); if (mReactInstanceManager != null) { mReactInstanceManager.onPause(); } }
@Override protected void onResume() { super.onResume(); if (mReactInstanceManager != null) { mReactInstanceManager.onResume(this); } }
public void resetRootViewForScreenshotTests() { if (mReactInstanceManager != null) { mReactInstanceManager.onDestroy(); mReactInstanceManager = null; } mReactRootView = new ReactRootView(this); mScreenshotingFrameLayout.removeAllViews(); mScreenshotingFrameLayout.addView(mReactRootView); }
@Override protected void onDestroy() { super.onDestroy(); mDestroyCountDownLatch.countDown(); if (mReactInstanceManager != null) { mReactInstanceManager.onDestroy(); } }
@Override protected void onResume() { super.onResume(); mLifecycleState = LifecycleState.RESUMED; if (mReactInstanceManager != null) { mReactInstanceManager.onResume(this, this); } }
@Override protected void onPause() { super.onPause(); mLifecycleState = LifecycleState.BEFORE_RESUME; overridePendingTransition(0, 0); if (mReactInstanceManager != null) { mReactInstanceManager.onPause(); } }
private UIManagerModule createUIManager(ReactApplicationContext reactContext) { ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_START); Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "createUIManagerModule"); try { List<ViewManager> viewManagersList = mReactInstanceManager.createAllViewManagers(reactContext); return new UIManagerModule(reactContext, viewManagersList, mUIImplementationProvider); } finally { Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); ReactMarker.logMarker(CREATE_UI_MANAGER_MODULE_END); } }
@Override public List<NativeModule> createNativeModules( ReactApplicationContext catalystApplicationContext) { Systrace.beginSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE, "createUIManagerModule"); UIManagerModule uiManagerModule; try { List<ViewManager> viewManagersList = mReactInstanceManager.createAllViewManagers(catalystApplicationContext); uiManagerModule = new UIManagerModule( catalystApplicationContext, viewManagersList, mUIImplementationProvider.createUIImplementation( catalystApplicationContext, viewManagersList)); } finally { Systrace.endSection(Systrace.TRACE_TAG_REACT_JAVA_BRIDGE); } List<NativeModule> nativeModulesList = new ArrayList<>( Arrays.<NativeModule>asList( new AnimationsDebugModule( catalystApplicationContext, mReactInstanceManager.getDevSupportManager().getDevSettings()), new AndroidInfoModule(), new DeviceEventManagerModule(catalystApplicationContext, mHardwareBackBtnHandler), new ExceptionsManagerModule(mReactInstanceManager.getDevSupportManager()), new Timing( catalystApplicationContext, mReactInstanceManager.getDevSupportManager()), new SourceCodeModule(mReactInstanceManager.getSourceUrl()), uiManagerModule, new DebugComponentOwnershipModule(catalystApplicationContext))); if (ReactBuildConfig.DEBUG) { nativeModulesList.add(new JSCHeapCapture(catalystApplicationContext)); } return nativeModulesList; }
// Because react context is created asynchronously, we may have to wait until it is available. // It's simpler than exposing synchronosition mechanism to notify listener than react context // creation has completed. private ReactContext waitForReactContext() { Assertions.assertNotNull(mReactInstanceManager); try { while (true) { ReactContext reactContext = mReactInstanceManager.getCurrentReactContext(); if (reactContext != null) { return reactContext; } Thread.sleep(100); } } catch (InterruptedException e) { throw new RuntimeException(e); } }
public void loadApp( String appKey, ReactInstanceSpecForTest spec, @Nullable Bundle initialProps, String bundleName, boolean useDevSupport) { final CountDownLatch currentLayoutEvent = mLayoutEvent = new CountDownLatch(1); mBridgeIdleSignaler = new ReactBridgeIdleSignaler(); ReactInstanceManager.Builder builder = ReactTestHelper.getReactTestFactory() .getReactInstanceManagerBuilder() .setApplication(getApplication()) .setBundleAssetName(bundleName) // By not setting a JS module name, we force the bundle to be always loaded from // assets, not the devserver, even if dev mode is enabled (such as when testing // redboxes). // This makes sense because we never run the devserver in tests. // .setJSMainModuleName() .addPackage( spec.getAlternativeReactPackageForTest() != null ? spec.getAlternativeReactPackageForTest() : new MainReactPackage()) .addPackage(new InstanceSpecForTestPackage(spec)) .setUseDeveloperSupport(useDevSupport) .setBridgeIdleDebugListener(mBridgeIdleSignaler) .setInitialLifecycleState(mLifecycleState); mReactInstanceManager = builder.build(); mReactInstanceManager.onResume(this, this); Assertions.assertNotNull(mReactRootView) .getViewTreeObserver() .addOnGlobalLayoutListener( new ViewTreeObserver.OnGlobalLayoutListener() { @Override public void onGlobalLayout() { currentLayoutEvent.countDown(); } }); Assertions.assertNotNull(mReactRootView) .startReactApplication(mReactInstanceManager, appKey, initialProps); }
@Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); mReactRootView = new ReactRootView(this); mReactInstanceManager = ReactInstanceManager.builder() .setApplication(getApplication()) .setBundleAssetName("index.android.bundle") .setJSMainModuleName("index.android") .addPackage(new MainReactPackage()) .setUseDeveloperSupport(BuildConfig.DEBUG) .setInitialLifecycleState(LifecycleState.RESUMED) .build(); mReactRootView.startReactApplication(mReactInstanceManager, "Example1", null); setContentView(mReactRootView); }
@Test public void testTouchEmitter() { ReactInstanceManager instanceManager = mock(ReactInstanceManager.class); when(instanceManager.getCurrentReactContext()).thenReturn(mReactContext); UIManagerModule uiManager = mock(UIManagerModule.class); EventDispatcher eventDispatcher = mock(EventDispatcher.class); RCTEventEmitter eventEmitterModuleMock = mock(RCTEventEmitter.class); when(mCatalystInstanceMock.getNativeModule(UIManagerModule.class)).thenReturn(uiManager); when(uiManager.getEventDispatcher()).thenReturn(eventDispatcher); int rootViewId = 7; ReactRootView rootView = new ReactRootView(mReactContext); rootView.setId(rootViewId); rootView.startReactApplication(instanceManager, ""); rootView.simulateAttachForTesting(); long ts = new Date().getTime(); // Test ACTION_DOWN event rootView.onTouchEvent(MotionEvent.obtain(100, ts, MotionEvent.ACTION_DOWN, 0, 0, 0)); ArgumentCaptor<Event> downEventCaptor = ArgumentCaptor.forClass(Event.class); verify(eventDispatcher).dispatchEvent(downEventCaptor.capture()); verifyNoMoreInteractions(eventDispatcher); downEventCaptor.getValue().dispatch(eventEmitterModuleMock); ArgumentCaptor<SimpleArray> downActionTouchesArgCaptor = ArgumentCaptor.forClass(SimpleArray.class); verify(eventEmitterModuleMock) .receiveTouches( eq("topTouchStart"), downActionTouchesArgCaptor.capture(), any(SimpleArray.class)); verifyNoMoreInteractions(eventEmitterModuleMock); assertThat(downActionTouchesArgCaptor.getValue().size()).isEqualTo(1); assertThat(downActionTouchesArgCaptor.getValue().getMap(0)) .isEqualTo( SimpleMap.of( "pageX", 0., "pageY", 0., "locationX", 0., "locationY", 0., "target", rootViewId, "timeStamp", (double) ts, "identifier", 0.)); // Test ACTION_UP event reset(eventEmitterModuleMock, eventDispatcher); ArgumentCaptor<Event> upEventCaptor = ArgumentCaptor.forClass(Event.class); ArgumentCaptor<SimpleArray> upActionTouchesArgCaptor = ArgumentCaptor.forClass(SimpleArray.class); rootView.onTouchEvent(MotionEvent.obtain(50, ts, MotionEvent.ACTION_UP, 0, 0, 0)); verify(eventDispatcher).dispatchEvent(upEventCaptor.capture()); verifyNoMoreInteractions(eventDispatcher); upEventCaptor.getValue().dispatch(eventEmitterModuleMock); verify(eventEmitterModuleMock) .receiveTouches( eq("topTouchEnd"), upActionTouchesArgCaptor.capture(), any(WritableArray.class)); verifyNoMoreInteractions(eventEmitterModuleMock); assertThat(upActionTouchesArgCaptor.getValue().size()).isEqualTo(1); assertThat(upActionTouchesArgCaptor.getValue().getMap(0)) .isEqualTo( SimpleMap.of( "pageX", 0., "pageY", 0., "locationX", 0., "locationY", 0., "target", rootViewId, "timeStamp", (double) ts, "identifier", 0.)); // Test other action reset(eventDispatcher); rootView.onTouchEvent( MotionEvent.obtain(50, new Date().getTime(), MotionEvent.ACTION_HOVER_MOVE, 0, 0, 0)); verifyNoMoreInteractions(eventDispatcher); }
@Override public ReactInstanceManager.Builder getReactInstanceManagerBuilder() { return ReactInstanceManager.builder(); }