diff --git a/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/test/TurboModuleManagerTest.cpp b/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/test/TurboModuleManagerTest.cpp new file mode 100644 index 000000000000..e2ce87d73edf --- /dev/null +++ b/packages/react-native/ReactAndroid/src/main/jni/react/turbomodule/test/TurboModuleManagerTest.cpp @@ -0,0 +1,70 @@ +/* + * 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 + +#include + +#include + +namespace facebook::react { + +namespace { + +class TestCallInvoker final : public CallInvoker { + public: + void invokeAsync(CallFunc&& /*func*/) noexcept override {} + void invokeSync(CallFunc&& /*func*/) override {} +}; + +class TestNativeMethodCallInvoker final : public NativeMethodCallInvoker { + public: + void invokeAsync( + const std::string& /*methodName*/, + NativeMethodCallFunc&& /*func*/) noexcept override {} + void invokeSync( + const std::string& /*methodName*/, + NativeMethodCallFunc&& /*func*/) override {} +}; + +} // namespace + +TEST(TurboModuleManagerTest, testInitHybridRetainsInvokersFromHolders) { + std::weak_ptr weakJsCallInvoker; + std::weak_ptr weakNativeMethodCallInvoker; + + auto hybridData = [&]() { + auto jsCallInvoker = std::make_shared(); + auto nativeMethodCallInvoker = + std::make_shared(); + weakJsCallInvoker = jsCallInvoker; + weakNativeMethodCallInvoker = nativeMethodCallInvoker; + + auto jsCallInvokerHolder = + CallInvokerHolder::newObjectCxxArgs(jsCallInvoker); + auto nativeMethodCallInvokerHolder = + NativeMethodCallInvokerHolder::newObjectCxxArgs( + nativeMethodCallInvoker); + facebook::jni::alias_ref unusedJavaPart; + facebook::jni::alias_ref + nullDelegate; + + return TurboModuleManager::initHybrid( + unusedJavaPart, + jsCallInvokerHolder, + nativeMethodCallInvokerHolder, + nullDelegate); + }(); + + ASSERT_NE(nullptr, hybridData); + // The holders and original shared_ptrs died inside the factory lambda. + // The returned HybridData owns the manager that must retain both invokers. + EXPECT_FALSE(weakJsCallInvoker.expired()); + EXPECT_FALSE(weakNativeMethodCallInvoker.expired()); +} + +} // namespace facebook::react