From e0921e987b7b1f769fb3882a26354ade035f5b26 Mon Sep 17 00:00:00 2001 From: generatedunixname1563563004708334 Date: Thu, 3 Sep 2026 01:15:06 -0700 Subject: [PATCH] xplat/js/react-native-github/packages/react-native/ReactAndroid/src/main/jni/react/reactnativeblob/BlobCollector.cpp (#58300) Summary: Pull Request resolved: https://github.com/react/react-native/pull/58300 Reviewed By: javache Differential Revision: D118604015 fbshipit-source-id: 61769d6987c620e1912f23fbee0dd92bd8a762fd --- .../test/BlobCollectorTest.cpp | 151 ++++++++++++++++++ 1 file changed, 151 insertions(+) create mode 100644 packages/react-native/ReactAndroid/src/main/jni/react/reactnativeblob/test/BlobCollectorTest.cpp diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/reactnativeblob/test/BlobCollectorTest.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/reactnativeblob/test/BlobCollectorTest.cpp new file mode 100644 index 00000000000..a813451dcf9 --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/jni/react/reactnativeblob/test/BlobCollectorTest.cpp @@ -0,0 +1,151 @@ +/* + * 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. + */ + +#include "BlobCollector.h" + +#include +#include + +#include +#include +#include + +namespace facebook::react { +namespace { + +class JContext : public jni::JavaClass { + public: + static constexpr auto kJavaDescriptor = "Landroid/content/Context;"; +}; + +class JInstrumentation : public jni::JavaClass { + public: + static constexpr auto kJavaDescriptor = "Landroid/app/Instrumentation;"; + + jni::local_ref getTargetContext() { + static const auto method = + javaClassStatic()->getMethod( + "getTargetContext"); + return method(self()); + } +}; + +class JInstrumentationRegistry + : public jni::JavaClass { + public: + static constexpr auto kJavaDescriptor = + "Landroidx/test/platform/app/InstrumentationRegistry;"; + + static jni::local_ref getInstrumentation() { + static const auto method = + javaClassStatic()->getStaticMethod( + "getInstrumentation"); + return method(javaClassStatic()); + } +}; + +class JReactApplicationContext + : public jni::JavaClass { + public: + static constexpr auto kJavaDescriptor = + "Lcom/facebook/react/bridge/ReactApplicationContext;"; +}; + +class JBridgeReactContext + : public jni::JavaClass { + public: + static constexpr auto kJavaDescriptor = + "Lcom/facebook/react/bridge/BridgeReactContext;"; + + static jni::local_ref create( + jni::alias_ref context) { + static const auto constructor = + javaClassStatic() + ->getConstructor(); + return jni::static_ref_cast( + javaClassStatic()->newObject(constructor, context.get())); + } +}; + +class JBlobModule : public jni::JavaClass { + public: + static constexpr auto kJavaDescriptor = + "Lcom/facebook/react/modules/blob/BlobModule;"; + + static jni::local_ref create( + jni::alias_ref reactContext) { + static const auto constructor = + javaClassStatic() + ->getConstructor(); + return javaClassStatic()->newObject(constructor, reactContext.get()); + } + + void store(jni::alias_ref data, const std::string& blobId) { + static const auto method = + javaClassStatic()->getMethod("store"); + method(self(), data.get(), jni::make_jstring(blobId).get()); + } + + jlong getLengthOfBlob(const std::string& blobId) { + static const auto method = + javaClassStatic()->getMethod("getLengthOfBlob"); + return method(self(), jni::make_jstring(blobId).get()); + } +}; + +jni::local_ref makeByteArray(const jbyte* bytes, jsize size) { + auto javaBytes = jni::JArrayByte::newArray(size); + javaBytes->setRegion(0, size, bytes); + return javaBytes; +} + +class BlobCollectorTest : public ::testing::Test { + protected: + void SetUp() override { + jni::ThreadScope::WithClassLoader([&] { + auto instrumentation = JInstrumentationRegistry::getInstrumentation(); + auto context = + JBridgeReactContext::create(instrumentation->getTargetContext()); + blobModule_ = jni::make_global(JBlobModule::create(context)); + }); + } + + jni::global_ref blobModule_; +}; + +/* + * Bug this catches: the collector must ask Java for the blob's actual byte + * length and must release the same blob id when the JS host object is + * collected. A mismatch in method names, descriptor, object lifetime, or blob + * id plumbing would either report the wrong external-memory pressure to Hermes + * or leak blob storage after GC. + */ +TEST_F( + BlobCollectorTest, + testBlobCollectorReportsStoredLengthAndRemovesBlobOnDestruction) { + const std::string blobId{"native-collector-test"}; + constexpr jsize blobLength = 7; + const std::array bytes{0, 1, 1, 2, 3, 5, 8}; + + jni::ThreadScope::WithClassLoader([&] { + blobModule_->store(makeByteArray(bytes.data(), blobLength), blobId); + ASSERT_EQ(blobModule_->getLengthOfBlob(blobId), blobLength); + + { + BlobCollector collector{blobModule_, blobId}; + + EXPECT_EQ(collector.getBlobLength(), blobLength); + } + + EXPECT_EQ(blobModule_->getLengthOfBlob(blobId), 0); + }); +} + +} // namespace +} // namespace facebook::react