Skip to content

Commit

Permalink
Allow ReactInstrumentationTest to use custom JSIModules
Browse files Browse the repository at this point in the history
Summary:
Update ReactInstrumentationTest to allow passing in custom JSIModules

Also: 'Fixed' some improper ordering of Nullable annotations

Changelog:
Internal

Reviewed By: cortinico

Differential Revision: D35961806

fbshipit-source-id: aa1513bee34e04ac3aae37f794d9d2771e7cc501
  • Loading branch information
christophpurrer authored and facebook-github-bot committed Apr 28, 2022
1 parent 4ea593b commit eb2a83b
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 4 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@
import com.facebook.react.testing.idledetection.ReactBridgeIdleSignaler;
import com.facebook.react.testing.idledetection.ReactIdleDetectionUtil;
import com.facebook.react.uimanager.ViewManagerRegistry;
import java.util.Arrays;
import java.util.ArrayList;
import java.util.List;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
Expand Down Expand Up @@ -250,7 +250,8 @@ public void loadBundle(
public List<JSIModuleSpec> getJSIModules(
final ReactApplicationContext reactApplicationContext,
final JavaScriptContextHolder jsContext) {
return Arrays.<JSIModuleSpec>asList(
List<JSIModuleSpec> packages = new ArrayList<>();
packages.add(
new JSIModuleSpec() {
@Override
public JSIModuleType getJSIModuleType() {
Expand All @@ -276,6 +277,10 @@ public UIManager get() {
};
}
});
if (spec.getJSIModuleBuilder() != null) {
packages.addAll(spec.getJSIModuleBuilder().build(reactApplicationContext));
}
return packages;
}
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@
import androidx.annotation.Nullable;
import com.facebook.react.ReactPackage;
import com.facebook.react.ReactPackageTurboModuleManagerDelegate;
import com.facebook.react.bridge.JSIModuleSpec;
import com.facebook.react.bridge.JavaScriptExecutorFactory;
import com.facebook.react.bridge.JavaScriptModule;
import com.facebook.react.bridge.NativeModule;
import com.facebook.react.bridge.NativeModuleCallExceptionHandler;
import com.facebook.react.bridge.ReactApplicationContext;
import com.facebook.react.uimanager.ViewManager;
import java.util.ArrayList;
import java.util.Arrays;
Expand All @@ -28,6 +30,11 @@
@SuppressLint("JavatestsIncorrectFolder")
public class ReactInstanceSpecForTest {

public abstract static class JSIModuleBuilder {
@Nullable
protected abstract List<JSIModuleSpec> build(ReactApplicationContext context);
}

private final List<NativeModule> mNativeModules =
new ArrayList<NativeModule>(Arrays.asList(new FakeWebSocketModule()));
private final List<Class<? extends JavaScriptModule>> mJSModuleSpecs = new ArrayList<>();
Expand All @@ -37,6 +44,7 @@ public class ReactInstanceSpecForTest {
@Nullable private FabricUIManagerFactory mFabricUIManagerFactory = null;
@Nullable private JavaScriptExecutorFactory mJavaScriptExecutorFactory = null;
@Nullable private ReactPackageTurboModuleManagerDelegate.Builder mTMMDelegateBuilder = null;
@Nullable private JSIModuleBuilder mJSIModuleBuilder = null;

public ReactInstanceSpecForTest addNativeModule(NativeModule module) {
mNativeModules.add(module);
Expand Down Expand Up @@ -85,11 +93,22 @@ public ReactInstanceSpecForTest setReactPackageTurboModuleManagerDelegateBuilder
return this;
}

protected @Nullable ReactPackageTurboModuleManagerDelegate.Builder
@Nullable
protected ReactPackageTurboModuleManagerDelegate.Builder
getReactPackageTurboModuleManagerDelegateBuilder() {
return mTMMDelegateBuilder;
}

public ReactInstanceSpecForTest setJSIModuleBuilder(@Nullable JSIModuleBuilder builder) {
mJSIModuleBuilder = builder;
return this;
}

@Nullable
protected JSIModuleBuilder getJSIModuleBuilder() {
return mJSIModuleBuilder;
}

public ReactInstanceSpecForTest addPackages(List<ReactPackage> reactPackages) {
mReactPackages.addAll(reactPackages);
return this;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -96,11 +96,17 @@ protected <T extends JavaScriptModule> T getJSModule(Class<T> jsInterface) {
return getReactContext().getJSModule(jsInterface);
}

protected @Nullable ReactPackageTurboModuleManagerDelegate.Builder
@Nullable
protected ReactPackageTurboModuleManagerDelegate.Builder
getReactPackageTurboModuleManagerDelegateBuilder() {
return null;
}

@Nullable
protected ReactInstanceSpecForTest.JSIModuleBuilder getJSIModuleBuilder() {
return null;
}

/** Override this method to provide extra native modules to be loaded before the app starts */
protected ReactInstanceSpecForTest createReactInstanceSpecForTest() {
ReactInstanceSpecForTest reactInstanceSpecForTest =
Expand All @@ -113,6 +119,7 @@ protected ReactInstanceSpecForTest createReactInstanceSpecForTest() {
}
reactInstanceSpecForTest.setReactPackageTurboModuleManagerDelegateBuilder(
getReactPackageTurboModuleManagerDelegateBuilder());
reactInstanceSpecForTest.setJSIModuleBuilder(getJSIModuleBuilder());
return reactInstanceSpecForTest;
}

Expand Down

0 comments on commit eb2a83b

Please sign in to comment.