mirror of
https://github.com/facebook/react-native.git
synced 2025-11-01 09:14:26 +00:00
Convert BaseJavaModuleTest to Kotlin (#37822)
Summary: As part of the effort to Kotlin-fy React Native tests, I've converted [BaseJavaModuleTest](https://github.com/facebook/react-native/blob/main/packages/react-native/ReactAndroid/src/test/java/com/facebook/react/bridge/BaseJavaModuleTest.java) to Kotlin. ## Changelog: [Internal] [Changed] - Convert BaseJavaModuleTest to Kotlin Pull Request resolved: https://github.com/facebook/react-native/pull/37822 Test Plan: Tests pass: ./gradlew :packages:react-native:ReactAndroid:test Formatted with [KtFmt](https://facebook.github.io/ktfmt/) Reviewed By: cortinico Differential Revision: D46639573 Pulled By: rshest fbshipit-source-id: d971d3a86ad05195885b8fbed8a165ab9efa9e78
This commit is contained in:
committed by
Facebook GitHub Bot
parent
cd56347dca
commit
8ddb334bb0
-131
@@ -1,131 +0,0 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.react.bridge;
|
||||
|
||||
import static org.mockito.Mockito.when;
|
||||
|
||||
import com.facebook.react.turbomodule.core.interfaces.TurboModule;
|
||||
import com.facebook.testutils.shadows.ShadowSoLoader;
|
||||
import java.util.List;
|
||||
import org.junit.Before;
|
||||
import org.junit.Test;
|
||||
import org.junit.runner.RunWith;
|
||||
import org.mockito.Mockito;
|
||||
import org.robolectric.RobolectricTestRunner;
|
||||
import org.robolectric.annotation.Config;
|
||||
|
||||
/** Tests for {@link BaseJavaModule} and {@link JavaModuleWrapper} */
|
||||
@Config(
|
||||
shadows = {
|
||||
ShadowSoLoader.class,
|
||||
})
|
||||
@RunWith(RobolectricTestRunner.class)
|
||||
public class BaseJavaModuleTest {
|
||||
private List<JavaModuleWrapper.MethodDescriptor> mMethods;
|
||||
private JavaModuleWrapper mModuleWrapper;
|
||||
|
||||
private List<JavaModuleWrapper.MethodDescriptor> mGeneratedMethods;
|
||||
private JavaModuleWrapper mGeneratedModuleWrapper;
|
||||
|
||||
private ReadableNativeArray mArguments;
|
||||
|
||||
@Before
|
||||
public void setup() {
|
||||
ModuleHolder moduleHolder = new ModuleHolder(new MethodsModule());
|
||||
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);
|
||||
}
|
||||
|
||||
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 {
|
||||
int methodId = findMethod("regularMethod", mMethods);
|
||||
when(mArguments.size()).thenReturn(1);
|
||||
mModuleWrapper.invoke(methodId, mArguments);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCallMethodWithEnoughArgs() {
|
||||
int methodId = findMethod("regularMethod", mMethods);
|
||||
when(mArguments.size()).thenReturn(2);
|
||||
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);
|
||||
mModuleWrapper.invoke(methodId, mArguments);
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testCallSyncMethod() {
|
||||
int methodId = findMethod("syncMethod", mMethods);
|
||||
when(mArguments.size()).thenReturn(2);
|
||||
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 {
|
||||
@Override
|
||||
public String getName() {
|
||||
return "Methods";
|
||||
}
|
||||
|
||||
@ReactMethod
|
||||
public void regularMethod(String a, int b) {}
|
||||
|
||||
@ReactMethod
|
||||
public void asyncMethod(int a, Promise p) {}
|
||||
|
||||
@ReactMethod(isBlockingSynchronousMethod = true)
|
||||
public int syncMethod(int a, int b) {
|
||||
return a + b;
|
||||
}
|
||||
}
|
||||
|
||||
private abstract class NativeTestGeneratedModuleSpec extends BaseJavaModule
|
||||
implements TurboModule {
|
||||
@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) {}
|
||||
}
|
||||
}
|
||||
+99
@@ -0,0 +1,99 @@
|
||||
/*
|
||||
* Copyright (c) Meta Platforms, Inc. and affiliates.
|
||||
*
|
||||
* This source code is licensed under the MIT license found in the
|
||||
* LICENSE file in the root directory of this source tree.
|
||||
*/
|
||||
|
||||
package com.facebook.react.bridge
|
||||
|
||||
import com.facebook.react.turbomodule.core.interfaces.TurboModule
|
||||
import com.facebook.testutils.shadows.ShadowSoLoader
|
||||
import org.junit.Before
|
||||
import org.junit.Test
|
||||
import org.junit.runner.RunWith
|
||||
import org.mockito.Mockito.mock
|
||||
import org.mockito.Mockito.`when` as whenever
|
||||
import org.robolectric.RobolectricTestRunner
|
||||
import org.robolectric.annotation.Config
|
||||
|
||||
/** Tests for [BaseJavaModule] and [JavaModuleWrapper] */
|
||||
@Config(shadows = [ShadowSoLoader::class])
|
||||
@RunWith(RobolectricTestRunner::class)
|
||||
class BaseJavaModuleTest {
|
||||
private lateinit var methods: List<JavaModuleWrapper.MethodDescriptor>
|
||||
private lateinit var moduleWrapper: JavaModuleWrapper
|
||||
private lateinit var generatedMethods: List<JavaModuleWrapper.MethodDescriptor>
|
||||
private lateinit var generatedModuleWrapper: JavaModuleWrapper
|
||||
private lateinit var arguments: ReadableNativeArray
|
||||
|
||||
@Before
|
||||
fun setup() {
|
||||
val moduleHolder = ModuleHolder(MethodsModule())
|
||||
moduleWrapper = JavaModuleWrapper(null, moduleHolder)
|
||||
methods = moduleWrapper.methodDescriptors
|
||||
val generatedModuleHolder = ModuleHolder(GeneratedMethodsModule())
|
||||
generatedModuleWrapper = JavaModuleWrapper(null, generatedModuleHolder)
|
||||
generatedMethods = generatedModuleWrapper.methodDescriptors
|
||||
arguments = mock(ReadableNativeArray::class.java)
|
||||
}
|
||||
|
||||
private fun findMethod(mname: String, methods: List<JavaModuleWrapper.MethodDescriptor>): Int =
|
||||
methods.indexOfFirst({ it.name === mname })
|
||||
|
||||
@Test(expected = NativeArgumentsParseException::class)
|
||||
fun testCallMethodWithoutEnoughArgs() {
|
||||
val methodId = findMethod("regularMethod", methods)
|
||||
whenever(arguments.size()).thenReturn(1)
|
||||
moduleWrapper.invoke(methodId, arguments)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCallMethodWithEnoughArgs() {
|
||||
val methodId = findMethod("regularMethod", methods)
|
||||
whenever(arguments.size()).thenReturn(2)
|
||||
moduleWrapper.invoke(methodId, arguments)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCallAsyncMethodWithEnoughArgs() {
|
||||
// Promise block evaluates to 2 args needing to be passed from JS
|
||||
val methodId = findMethod("asyncMethod", methods)
|
||||
whenever(arguments.size()).thenReturn(3)
|
||||
moduleWrapper.invoke(methodId, arguments)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCallSyncMethod() {
|
||||
val methodId = findMethod("syncMethod", methods)
|
||||
whenever(arguments.size()).thenReturn(2)
|
||||
moduleWrapper.invoke(methodId, arguments)
|
||||
}
|
||||
|
||||
@Test
|
||||
fun testCallGeneratedMethod() {
|
||||
val methodId = findMethod("generatedMethod", generatedMethods)
|
||||
whenever(arguments.size()).thenReturn(2)
|
||||
generatedModuleWrapper.invoke(methodId, arguments)
|
||||
}
|
||||
|
||||
private class MethodsModule : BaseJavaModule() {
|
||||
override fun getName(): String = "Methods"
|
||||
|
||||
@ReactMethod fun regularMethod(a: String?, b: Int?) {}
|
||||
|
||||
@ReactMethod fun asyncMethod(a: Int, p: Promise) {}
|
||||
|
||||
@ReactMethod(isBlockingSynchronousMethod = true) fun syncMethod(a: Int, b: Int): Int = a + b
|
||||
}
|
||||
|
||||
private abstract inner class NativeTestGeneratedModuleSpec : BaseJavaModule(), TurboModule {
|
||||
@ReactMethod abstract fun generatedMethod(a: String?, b: Int?)
|
||||
}
|
||||
|
||||
private inner class GeneratedMethodsModule : NativeTestGeneratedModuleSpec() {
|
||||
override fun getName(): String = "GeneratedMethods"
|
||||
|
||||
override fun generatedMethod(a: String?, b: Int?) {}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user