From dc22bd638fcc68ecab55d1175318822cf8972967 Mon Sep 17 00:00:00 2001 From: Spencer Ahrens Date: Fri, 25 Aug 2017 19:05:44 -0700 Subject: [PATCH] Add fb4a native modules to snapshot tests Reviewed By: achen1 Differential Revision: D5599659 fbshipit-source-id: ca9f8806212e6b3551eaebb95903bc168110b4ec --- .../react/testing/ReactAppTestActivity.java | 28 ++++++++------- .../testing/ReactInstanceSpecForTest.java | 35 +++++++++++++------ .../react/ReactInstanceManagerBuilder.java | 7 +++- 3 files changed, 46 insertions(+), 24 deletions(-) diff --git a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactAppTestActivity.java b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactAppTestActivity.java index 1fd51f877..1f60abde0 100644 --- a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactAppTestActivity.java +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactAppTestActivity.java @@ -8,18 +8,12 @@ package com.facebook.react.testing; -import javax.annotation.Nullable; - -import java.util.concurrent.CountDownLatch; -import java.util.concurrent.TimeUnit; - import android.graphics.Bitmap; import android.os.Bundle; import android.support.v4.app.FragmentActivity; import android.view.View; import android.view.ViewTreeObserver; import android.widget.FrameLayout; - import com.facebook.infer.annotation.Assertions; import com.facebook.react.ReactInstanceManager; import com.facebook.react.ReactInstanceManagerBuilder; @@ -31,6 +25,9 @@ import com.facebook.react.shell.MainReactPackage; import com.facebook.react.testing.idledetection.ReactBridgeIdleSignaler; import com.facebook.react.testing.idledetection.ReactIdleDetectionUtil; import com.facebook.react.uimanager.UIImplementationProvider; +import java.util.concurrent.CountDownLatch; +import java.util.concurrent.TimeUnit; +import javax.annotation.Nullable; public class ReactAppTestActivity extends FragmentActivity implements DefaultHardwareBackBtnHandler @@ -39,7 +36,7 @@ public class ReactAppTestActivity extends FragmentActivity implements private static final String DEFAULT_BUNDLE_NAME = "AndroidTestBundle.js"; private static final int ROOT_VIEW_ID = 8675309; // we need a bigger timeout for CI builds because they run on a slow emulator - private static final long IDLE_TIMEOUT_MS = 60000; + private static final long IDLE_TIMEOUT_MS = 120000; private CountDownLatch mLayoutEvent = new CountDownLatch(1); private @Nullable ReactBridgeIdleSignaler mBridgeIdleSignaler; @@ -164,16 +161,21 @@ public class ReactAppTestActivity extends FragmentActivity implements mBridgeIdleSignaler = new ReactBridgeIdleSignaler(); ReactInstanceManagerBuilder builder = - ReactTestHelper.getReactTestFactory().getReactInstanceManagerBuilder() - .setApplication(getApplication()) - .setBundleAssetName(bundleName) + ReactTestHelper.getReactTestFactory() + .getReactInstanceManagerBuilder() + .setApplication(getApplication()) + .setBundleAssetName(bundleName); + if (!spec.getAlternativeReactPackagesForTest().isEmpty()) { + builder.addPackages(spec.getAlternativeReactPackagesForTest()); + } else { + builder.addPackage(new MainReactPackage()); + } + builder + .addPackage(new InstanceSpecForTestPackage(spec)) // 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) diff --git a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactInstanceSpecForTest.java b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactInstanceSpecForTest.java index 0a17e29b7..f58efec73 100644 --- a/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactInstanceSpecForTest.java +++ b/ReactAndroid/src/androidTest/java/com/facebook/react/testing/ReactInstanceSpecForTest.java @@ -8,17 +8,15 @@ package com.facebook.react.testing; +import android.annotation.SuppressLint; +import com.facebook.react.ReactPackage; +import com.facebook.react.bridge.JavaScriptModule; +import com.facebook.react.bridge.NativeModule; +import com.facebook.react.uimanager.ViewManager; import java.util.ArrayList; import java.util.Arrays; import java.util.List; -import android.annotation.SuppressLint; - -import com.facebook.react.bridge.NativeModule; -import com.facebook.react.bridge.JavaScriptModule; -import com.facebook.react.uimanager.ViewManager; -import com.facebook.react.ReactPackage; - /** * A spec that allows a test to add additional NativeModules/JS modules to the ReactInstance. This * can also be used to stub out existing native modules by adding another module with the same name @@ -31,7 +29,7 @@ public class ReactInstanceSpecForTest { new ArrayList(Arrays.asList(new FakeWebSocketModule())); private final List> mJSModuleSpecs = new ArrayList<>(); private final List mViewManagers = new ArrayList<>(); - private ReactPackage mReactPackage = null; + private final ArrayList mReactPackages = new ArrayList<>(); public ReactInstanceSpecForTest addNativeModule(NativeModule module) { mNativeModules.add(module); @@ -39,7 +37,16 @@ public class ReactInstanceSpecForTest { } public ReactInstanceSpecForTest setPackage(ReactPackage reactPackage) { - mReactPackage = reactPackage; + if (!mReactPackages.isEmpty()) { + throw new IllegalStateException( + "setPackage is not allowed after addPackages. " + reactPackage); + } + mReactPackages.add(reactPackage); + return this; + } + + public ReactInstanceSpecForTest addPackages(List reactPackages) { + mReactPackages.addAll(reactPackages); return this; } @@ -53,7 +60,15 @@ public class ReactInstanceSpecForTest { } public ReactPackage getAlternativeReactPackageForTest() { - return mReactPackage; + if (mReactPackages.size() > 1) { + throw new IllegalStateException( + "Multiple packages were added - use getAlternativeReactPackagesForTest instead."); + } + return mReactPackages.get(0); + } + + public List getAlternativeReactPackagesForTest() { + return mReactPackages; } public List getExtraViewManagers() { diff --git a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java index 56cba793f..7344bcfb6 100644 --- a/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java +++ b/ReactAndroid/src/main/java/com/facebook/react/ReactInstanceManagerBuilder.java @@ -5,9 +5,9 @@ package com.facebook.react; import android.app.Activity; import android.app.Application; import com.facebook.infer.annotation.Assertions; -import com.facebook.react.bridge.JavaScriptExecutorFactory; import com.facebook.react.bridge.JSBundleLoader; import com.facebook.react.bridge.JSCJavaScriptExecutorFactory; +import com.facebook.react.bridge.JavaScriptExecutorFactory; import com.facebook.react.bridge.NativeModuleCallExceptionHandler; import com.facebook.react.bridge.NotThreadSafeBridgeIdleDebugListener; import com.facebook.react.common.LifecycleState; @@ -125,6 +125,11 @@ public class ReactInstanceManagerBuilder { return this; } + public ReactInstanceManagerBuilder addPackages(List reactPackages) { + mPackages.addAll(reactPackages); + return this; + } + public ReactInstanceManagerBuilder setBridgeIdleDebugListener( NotThreadSafeBridgeIdleDebugListener bridgeIdleDebugListener) { mBridgeIdleDebugListener = bridgeIdleDebugListener;