Migrate BlobCollector to Kotlin (#49148)

Summary:
Migrate com.facebook.react.modules.blob.BlobCollector to Kotlin

## Changelog:

[INTERNAL] - Migrate com.facebook.react.modules.blob.BlobCollector to Kotlin

Pull Request resolved: https://github.com/facebook/react-native/pull/49148

Test Plan:
```bash
yarn test-android
yarn android
```

Reviewed By: cortinico, fabriziocucci

Differential Revision: D69522998

Pulled By: javache

fbshipit-source-id: a0255d1cdb51ee1f8aa73a07419e228871e56538
This commit is contained in:
Mateo Guzmán
2025-02-12 09:04:48 -08:00
committed by Facebook GitHub Bot
parent c5b9e8e7d4
commit 1b066bac9a
3 changed files with 97 additions and 34 deletions
@@ -1,34 +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.modules.blob;
import com.facebook.react.bridge.JavaScriptContextHolder;
import com.facebook.react.bridge.ReactContext;
import com.facebook.soloader.SoLoader;
/* package */ class BlobCollector {
static {
SoLoader.loadLibrary("reactnativeblob");
}
static void install(final ReactContext reactContext, final BlobModule blobModule) {
reactContext.runOnJSQueueThread(
new Runnable() {
@Override
public void run() {
JavaScriptContextHolder jsContext = reactContext.getJavaScriptContextHolder();
// When debugging in chrome the JS context is not available.
if (jsContext != null && jsContext.get() != 0) {
nativeInstall(blobModule, jsContext.get());
}
}
});
}
private static native void nativeInstall(Object blobModule, long jsContext);
}
@@ -0,0 +1,31 @@
/*
* 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.modules.blob
import com.facebook.react.bridge.JavaScriptContextHolder
import com.facebook.react.bridge.ReactContext
import com.facebook.soloader.SoLoader
internal object BlobCollector {
init {
SoLoader.loadLibrary("reactnativeblob")
}
@JvmStatic
public fun install(reactContext: ReactContext, blobModule: BlobModule) {
reactContext.runOnJSQueueThread {
val jsContext: JavaScriptContextHolder? = reactContext.getJavaScriptContextHolder()
// When debugging in chrome the JS context is not available.
if (jsContext != null && jsContext.get() != 0L) {
nativeInstall(blobModule, jsContext.get())
}
}
}
private external fun nativeInstall(blobModule: Any, jsContext: Long)
}
@@ -0,0 +1,66 @@
/*
* 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.modules.blob
import com.facebook.react.bridge.JavaScriptContextHolder
import com.facebook.react.bridge.ReactContext
import com.facebook.soloader.SoLoader
import org.junit.After
import org.junit.Before
import org.junit.Test
import org.mockito.MockedStatic
import org.mockito.Mockito.mockStatic
import org.mockito.kotlin.any
import org.mockito.kotlin.mock
import org.mockito.kotlin.verify
import org.mockito.kotlin.whenever
class BlobCollectorTest {
private lateinit var reactContext: ReactContext
private lateinit var mockedStaticSoLoader: MockedStatic<SoLoader>
@Before
fun setUp() {
reactContext = mock()
mockedStaticSoLoader = mockStatic(SoLoader::class.java)
mockedStaticSoLoader
.`when`<Boolean> { SoLoader.loadLibrary("reactnativeblob") }
.thenReturn(true)
}
@After
fun tearDown() {
mockedStaticSoLoader.close()
}
@Test
fun testInstallWithValidJsContext() {
val jsContextHolder = mock<JavaScriptContextHolder>()
val jsContext = 1234L
whenever(reactContext.getJavaScriptContextHolder()).thenReturn(jsContextHolder)
whenever(jsContextHolder.get()).thenReturn(jsContext)
BlobCollector.install(reactContext, mock())
verify(reactContext).runOnJSQueueThread(any())
}
@Test
fun testInstallWithInvalidOrNullJsContext() {
val jsContextHolder = mock<JavaScriptContextHolder>()
whenever(reactContext.getJavaScriptContextHolder()).thenReturn(jsContextHolder)
whenever(jsContextHolder.get()).thenReturn(0L)
BlobCollector.install(reactContext, mock())
verify(reactContext).runOnJSQueueThread(any())
}
}