From 95ca59eb9432395a8be63cdeab062b3945dda590 Mon Sep 17 00:00:00 2001 From: Nick Lefever Date: Thu, 17 Apr 2025 06:18:36 -0700 Subject: [PATCH] Add unit test for shadow node state on clone (#50774) Summary: Pull Request resolved: https://github.com/facebook/react-native/pull/50774 Adding a Fantom test for the `useShadowNodeStateOnClone` feature flag, showing that the shadow node state is maintained after commit hook processing only when the feature flag is enabled. Changelog: [Internal] Reviewed By: rshest Differential Revision: D73121159 fbshipit-source-id: bbd7f46fbf49394ca5015e825a59d2fb1b519ee5 --- .../State-ForcedCloneCommitHook-itest.js | 59 +++++++++++++ .../NativeFantomForcedCloneCommitHook.cpp | 84 +++++++++++++++++++ .../NativeFantomForcedCloneCommitHook.h | 30 +++++++ .../NativeFantomForcedCloneCommitHook.js | 21 +++++ 4 files changed, 194 insertions(+) create mode 100644 packages/react-native/Libraries/ReactNative/__tests__/State-ForcedCloneCommitHook-itest.js create mode 100644 packages/react-native/ReactCommon/react/nativemodule/fantomforcedclonecommithook/NativeFantomForcedCloneCommitHook.cpp create mode 100644 packages/react-native/ReactCommon/react/nativemodule/fantomforcedclonecommithook/NativeFantomForcedCloneCommitHook.h create mode 100644 packages/react-native/src/private/testing/fantom/specs/NativeFantomForcedCloneCommitHook.js diff --git a/packages/react-native/Libraries/ReactNative/__tests__/State-ForcedCloneCommitHook-itest.js b/packages/react-native/Libraries/ReactNative/__tests__/State-ForcedCloneCommitHook-itest.js new file mode 100644 index 00000000000..5430a4f470c --- /dev/null +++ b/packages/react-native/Libraries/ReactNative/__tests__/State-ForcedCloneCommitHook-itest.js @@ -0,0 +1,59 @@ +/** + * 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. + * + * @flow strict-local + * @format + * @oncall react_native + * @fantom_flags useShadowNodeStateOnClone:true + */ + +import 'react-native/Libraries/Core/InitializeCore'; + +import ensureInstance from '../../../src/private/__tests__/utilities/ensureInstance'; +import * as Fantom from '@react-native/fantom'; +import * as React from 'react'; +import {ScrollView, View} from 'react-native'; +import NativeFantomForcedCloneCommitHook from 'react-native/src/private/testing/fantom/specs/NativeFantomForcedCloneCommitHook'; +import ReactNativeElement from 'react-native/src/private/webapis/dom/nodes/ReactNativeElement'; + +NativeFantomForcedCloneCommitHook.setup(); + +describe('ScrollViewShadowNode', () => { + it('maintains state after commit hook processing', () => { + const root = Fantom.createRoot(); + let maybeScrollViewNode; + + Fantom.runTask(() => { + root.render( + + { + maybeScrollViewNode = node; + }} + /> + + , + ); + }); + + const scrollViewElement = ensureInstance( + maybeScrollViewNode, + ReactNativeElement, + ); + + // Scrolling triggers a state update to store the scroll position + // - the state update gets committed + // - during the commit, the shadow tree gets processed by the commit hook + // - the commit hook clones the View with nativeID set to 'to-be-cloned-in-the-commit-hook' + // - the parent YogaLayoutableShadowNode receives the changed children + // - this leads to all direct children to be adopted + // - the ScrollViewShadowNode gets cloned as part of the child adoption + Fantom.scrollTo(scrollViewElement, {x: 0, y: 777}); + + // With the useShadowNodeStateOnClone feature flag enabled, state is maintained after the commit hook processing + expect(scrollViewElement.scrollTop).toBe(777); + }); +}); diff --git a/packages/react-native/ReactCommon/react/nativemodule/fantomforcedclonecommithook/NativeFantomForcedCloneCommitHook.cpp b/packages/react-native/ReactCommon/react/nativemodule/fantomforcedclonecommithook/NativeFantomForcedCloneCommitHook.cpp new file mode 100644 index 00000000000..ed4afe29fad --- /dev/null +++ b/packages/react-native/ReactCommon/react/nativemodule/fantomforcedclonecommithook/NativeFantomForcedCloneCommitHook.cpp @@ -0,0 +1,84 @@ +/* + * 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 "NativeFantomForcedCloneCommitHook.h" +#include +#include +#include +#include +#include +#include +#include + +#include "Plugins.h" + +std::shared_ptr +NativeFantomForcedCloneCommitHookModuleProvider( + std::shared_ptr jsInvoker) { + return std::make_shared( + std::move(jsInvoker)); +} + +namespace facebook::react { + +struct FantomForcedCloneCommitHook : public UIManagerCommitHook { + void commitHookWasRegistered( + const UIManager& /*uiManager*/) noexcept override {} + void commitHookWasUnregistered( + const UIManager& /*uiManager*/) noexcept override {} + RootShadowNode::Unshared shadowTreeWillCommit( + const ShadowTree& shadowTree, + const RootShadowNode::Shared& oldRootShadowNode, + const RootShadowNode::Unshared& newRootShadowNode) noexcept override; +}; + +ShadowNode::Shared findAndClone(const ShadowNode::Shared& node) { + if (node->getProps()->nativeId == "to-be-cloned-in-the-commit-hook") { + return node->clone({}); + } + + auto children = node->getChildren(); + for (int i = 0; i < children.size(); i++) { + auto& child = children[i]; + auto maybeClone = findAndClone(child); + if (maybeClone != child) { + children[i] = maybeClone; + return node->clone( + {ShadowNodeFragment::propsPlaceholder(), + std::make_shared(children)}); + } + } + + return node; +} + +RootShadowNode::Unshared FantomForcedCloneCommitHook::shadowTreeWillCommit( + const ShadowTree& /*shadowTree*/, + const RootShadowNode::Shared& /*oldRootShadowNode*/, + const RootShadowNode::Unshared& newRootShadowNode) noexcept { + auto result = findAndClone(newRootShadowNode); + + return std::static_pointer_cast( + std::const_pointer_cast(result)); +} + +static UIManager& getUIManagerFromRuntime(jsi::Runtime& runtime) { + return UIManagerBinding::getBinding(runtime)->getUIManager(); +} + +NativeFantomForcedCloneCommitHook::NativeFantomForcedCloneCommitHook( + std::shared_ptr jsInvoker) + : NativeFantomForcedCloneCommitHookCxxSpec(std::move(jsInvoker)), + fantomForcedCloneCommitHook_( + std::make_shared()) {} + +void NativeFantomForcedCloneCommitHook::setup(jsi::Runtime& runtime) { + auto& uiManager = getUIManagerFromRuntime(runtime); + uiManager.registerCommitHook(*fantomForcedCloneCommitHook_); +} + +} // namespace facebook::react diff --git a/packages/react-native/ReactCommon/react/nativemodule/fantomforcedclonecommithook/NativeFantomForcedCloneCommitHook.h b/packages/react-native/ReactCommon/react/nativemodule/fantomforcedclonecommithook/NativeFantomForcedCloneCommitHook.h new file mode 100644 index 00000000000..9644e5ef765 --- /dev/null +++ b/packages/react-native/ReactCommon/react/nativemodule/fantomforcedclonecommithook/NativeFantomForcedCloneCommitHook.h @@ -0,0 +1,30 @@ +/* + * 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. + */ + +#pragma once + +#include +#include + +namespace facebook::react { + +struct FantomForcedCloneCommitHook; + +class NativeFantomForcedCloneCommitHook + : public NativeFantomForcedCloneCommitHookCxxSpec< + NativeFantomForcedCloneCommitHook> { + public: + explicit NativeFantomForcedCloneCommitHook( + std::shared_ptr jsInvoker); + + void setup(jsi::Runtime& runtime); + + private: + std::shared_ptr fantomForcedCloneCommitHook_{}; +}; + +} // namespace facebook::react diff --git a/packages/react-native/src/private/testing/fantom/specs/NativeFantomForcedCloneCommitHook.js b/packages/react-native/src/private/testing/fantom/specs/NativeFantomForcedCloneCommitHook.js new file mode 100644 index 00000000000..bc8da85b91f --- /dev/null +++ b/packages/react-native/src/private/testing/fantom/specs/NativeFantomForcedCloneCommitHook.js @@ -0,0 +1,21 @@ +/** + * 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. + * + * @flow strict + * @format + */ + +import type {TurboModule} from '../../../../../Libraries/TurboModule/RCTExport'; + +import * as TurboModuleRegistry from '../../../../../Libraries/TurboModule/TurboModuleRegistry'; + +export interface Spec extends TurboModule { + +setup: () => void; +} + +export default (TurboModuleRegistry.getEnforcing( + 'NativeFantomForcedCloneCommitHookCxx', +): Spec);