Delay module creation on call for constants when module has none

Reviewed By: AaaChiuuu

Differential Revision: D4810252

fbshipit-source-id: b2b98c3a8355dbb5775f254f25304a21f0bfee5b
This commit is contained in:
Kathy Gray
2017-04-10 03:01:00 -07:00
committed by Facebook Github Bot
parent 678679e009
commit 78ab4ee893
24 changed files with 101 additions and 29 deletions

View File

@@ -9,10 +9,13 @@ rn_robolectric_test(
"PUBLIC",
],
deps = [
react_native_dep("libraries/fbcore/src/test/java/com/facebook/powermock:powermock"),
react_native_dep("libraries/soloader/java/com/facebook/soloader:soloader"),
react_native_dep("third-party/java/fest:fest"),
react_native_dep("third-party/java/junit:junit"),
react_native_dep("third-party/java/mockito:mockito"),
react_native_dep("third-party/java/robolectric3/robolectric:robolectric"),
react_native_target("java/com/facebook/react/bridge:bridge"),
react_native_target("java/com/facebook/react/cxxbridge:bridge"),
react_native_tests_target("java/com/facebook/common/logging:logging"),
],

View File

@@ -7,11 +7,16 @@
* of patent rights can be found in the PATENTS file in the same directory.
*/
package com.facebook.react.bridge;
package com.facebook.react.cxxbridge;
import java.util.Map;
import javax.inject.Provider;
import java.util.List;
import com.facebook.react.bridge.BaseJavaModule;
import com.facebook.react.bridge.ReactMethod;
import com.facebook.react.bridge.ReadableNativeArray;
import com.facebook.react.bridge.Promise;
import com.facebook.react.bridge.NativeArgumentsParseException;
import org.junit.Before;
import org.junit.Rule;
@@ -27,7 +32,7 @@ import org.robolectric.RobolectricTestRunner;
import com.facebook.soloader.SoLoader;
/**
* Tests for {@link BaseJavaModule}
* Tests for {@link BaseJavaModule} and {@link JavaModuleWrapper}
*/
@PrepareForTest({ReadableNativeArray.class, SoLoader.class})
@PowerMockIgnore({"org.mockito.*", "org.robolectric.*", "android.*"})
@@ -37,43 +42,70 @@ public class BaseJavaModuleTest {
@Rule
public PowerMockRule rule = new PowerMockRule();
private Map<String, NativeModule.NativeMethod> mMethods;
private List<JavaModuleWrapper.MethodDescriptor> mMethods;
private JavaModuleWrapper mWrapper;
private ReadableNativeArray mArguments;
@Before
public void setup() {
mMethods = new MethodsModule().getMethods();
ModuleHolder moduleHolder = new ModuleHolder("MethodsModule",
false,
false,
false,
false,
new Provider<MethodsModule>() {
MethodsModule mModule;
@Override
public MethodsModule get() {
mModule = new MethodsModule();
return mModule;
}
});
mWrapper = new JavaModuleWrapper(null, MethodsModule.class, moduleHolder);
mMethods = mWrapper.getMethodDescriptors();
PowerMockito.mockStatic(SoLoader.class);
mArguments = PowerMockito.mock(ReadableNativeArray.class);
}
private int findMethod(String mname, List<JavaModuleWrapper.MethodDescriptor> methods) {
int posn = -1;
for (int i = 0; i< methods.size(); i++) {
JavaModuleWrapper.MethodDescriptor md = methods.get(i);
if (md.name == mname) {
posn = i;
break;
}
}
return posn;
}
@Test(expected = NativeArgumentsParseException.class)
public void testCallMethodWithoutEnoughArgs() throws Exception {
BaseJavaModule.NativeMethod regularMethod = mMethods.get("regularMethod");
int methodId = findMethod("regularMethod",mMethods);
Mockito.stub(mArguments.size()).toReturn(1);
regularMethod.invoke(null, null, mArguments);
mWrapper.invoke(null, methodId, mArguments);
}
@Test
public void testCallMethodWithEnoughArgs() {
BaseJavaModule.NativeMethod regularMethod = mMethods.get("regularMethod");
int methodId = findMethod("regularMethod", mMethods);
Mockito.stub(mArguments.size()).toReturn(2);
regularMethod.invoke(null, null, mArguments);
mWrapper.invoke(null, methodId, mArguments);
}
@Test
public void testCallAsyncMethodWithEnoughArgs() {
// Promise block evaluates to 2 args needing to be passed from JS
BaseJavaModule.NativeMethod asyncMethod = mMethods.get("asyncMethod");
int methodId = findMethod("asyncMethod", mMethods);
Mockito.stub(mArguments.size()).toReturn(3);
asyncMethod.invoke(null, null, mArguments);
mWrapper.invoke(null, methodId, mArguments);
}
@Test
public void testCallSyncMethod() {
BaseJavaModule.NativeMethod syncMethod = mMethods.get("syncMethod");
int methodId = findMethod("syncMethod", mMethods);
Mockito.stub(mArguments.size()).toReturn(2);
syncMethod.invoke(null, null, mArguments);
mWrapper.invoke(null, methodId, mArguments);
}
private static class MethodsModule extends BaseJavaModule {