Skip to content

Commit

Permalink
add unit test for method reflection on generated native modules (#36671)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #36671

Changelog: [Internal]

BaseJavaModuleTest tests the reflection of the native module methods wrapped by the JavaModuleWrapper abstraction, this abstraction is used by JNI in the C++ layer.

however, though we already have logic that supports the reflection of our codegenned modules (turbomodules), we didn't have a test for it yet, so i'm adding it in this change.

Reviewed By: javache, cortinico

Differential Revision: D44450433

fbshipit-source-id: 44abcf188d4b0559c684a95b058bd55e082b5531
  • Loading branch information
philIip authored and facebook-github-bot committed Mar 29, 2023
1 parent a7f7f8a commit e4a0a02
Showing 1 changed file with 39 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -40,14 +40,23 @@ public static boolean loadLibrary(String shortName) {
@RunWith(RobolectricTestRunner.class)
public class BaseJavaModuleTest {
private List<JavaModuleWrapper.MethodDescriptor> mMethods;
private JavaModuleWrapper mWrapper;
private JavaModuleWrapper mModuleWrapper;

private List<JavaModuleWrapper.MethodDescriptor> mGeneratedMethods;
private JavaModuleWrapper mGeneratedModuleWrapper;

private ReadableNativeArray mArguments;

@Before
public void setup() {
ModuleHolder moduleHolder = new ModuleHolder(new MethodsModule());
mWrapper = new JavaModuleWrapper(null, moduleHolder);
mMethods = mWrapper.getMethodDescriptors();
mModuleWrapper = new JavaModuleWrapper(null, moduleHolder);
mMethods = mModuleWrapper.getMethodDescriptors();

ModuleHolder generatedModuleHolder = new ModuleHolder(new GeneratedMethodsModule());
mGeneratedModuleWrapper = new JavaModuleWrapper(null, generatedModuleHolder);
mGeneratedMethods = mGeneratedModuleWrapper.getMethodDescriptors();

mArguments = Mockito.mock(ReadableNativeArray.class);
}

Expand All @@ -67,29 +76,36 @@ private int findMethod(String mname, List<JavaModuleWrapper.MethodDescriptor> me
public void testCallMethodWithoutEnoughArgs() throws Exception {
int methodId = findMethod("regularMethod", mMethods);
when(mArguments.size()).thenReturn(1);
mWrapper.invoke(methodId, mArguments);
mModuleWrapper.invoke(methodId, mArguments);
}

@Test
public void testCallMethodWithEnoughArgs() {
int methodId = findMethod("regularMethod", mMethods);
when(mArguments.size()).thenReturn(2);
mWrapper.invoke(methodId, mArguments);
mModuleWrapper.invoke(methodId, mArguments);
}

@Test
public void testCallAsyncMethodWithEnoughArgs() {
// Promise block evaluates to 2 args needing to be passed from JS
int methodId = findMethod("asyncMethod", mMethods);
when(mArguments.size()).thenReturn(3);
mWrapper.invoke(methodId, mArguments);
mModuleWrapper.invoke(methodId, mArguments);
}

@Test
public void testCallSyncMethod() {
int methodId = findMethod("syncMethod", mMethods);
when(mArguments.size()).thenReturn(2);
mWrapper.invoke(methodId, mArguments);
mModuleWrapper.invoke(methodId, mArguments);
}

@Test
public void testCallGeneratedMethod() {
int methodId = findMethod("generatedMethod", mGeneratedMethods);
when(mArguments.size()).thenReturn(2);
mGeneratedModuleWrapper.invoke(methodId, mArguments);
}

private static class MethodsModule extends BaseJavaModule {
Expand All @@ -109,4 +125,20 @@ public int syncMethod(int a, int b) {
return a + b;
}
}

private abstract class NativeTestGeneratedModuleSpec extends BaseJavaModule
implements ReactModuleWithSpec {
@ReactMethod
public abstract void generatedMethod(String a, int b);
}

private class GeneratedMethodsModule extends NativeTestGeneratedModuleSpec {
@Override
public String getName() {
return "GeneratedMethods";
}

@Override
public void generatedMethod(String a, int b) {}
}
}

0 comments on commit e4a0a02

Please sign in to comment.