diff --git a/ReactCommon/fabric/core/BUCK b/ReactCommon/fabric/core/BUCK index 2bab70459c730c..7a1cd7884c1506 100644 --- a/ReactCommon/fabric/core/BUCK +++ b/ReactCommon/fabric/core/BUCK @@ -85,6 +85,9 @@ fb_xplat_cxx_test( deps = [ "fbsource//xplat/folly:molly", "fbsource//xplat/js/react-native-github/ReactCommon/fabric/element:element", + react_native_xplat_target("fabric/components/view:view"), + react_native_xplat_target("fabric/components/scrollview:scrollview"), + react_native_xplat_target("fabric/components/text:text"), "fbsource//xplat/third-party/gmock:gtest", react_native_xplat_target("fabric/components/view:view"), ":core", diff --git a/ReactCommon/fabric/core/tests/traitCastTest.cpp b/ReactCommon/fabric/core/tests/traitCastTest.cpp new file mode 100644 index 00000000000000..49a2f64d47a21c --- /dev/null +++ b/ReactCommon/fabric/core/tests/traitCastTest.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) Facebook, Inc. and its 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 +#include +#include +#include + +#include +#include + +using namespace facebook::react; + +TEST(traitCastTest, testOne) { + auto builder = simpleComponentBuilder(); + + auto viewShadowNode = std::shared_ptr{}; + auto scrollViewShadowNode = std::shared_ptr{}; + auto paragraphShadowNode = std::shared_ptr{}; + auto textShadowNode = std::shared_ptr{}; + auto rawTextShadowNode = std::shared_ptr{}; + + // clang-format off + auto element = + Element() + .reference(scrollViewShadowNode) + .children({ + Element() + .reference(paragraphShadowNode) + .children({ + Element() + .reference(textShadowNode), + Element() + .reference(rawTextShadowNode) + }), + Element() + .reference(viewShadowNode), + }); + // clang-format on + + auto rootShadowNode = builder.build(element); + + // Casting `nullptr` returns `nullptrs`. + EXPECT_FALSE(traitCast(nullptr)); + EXPECT_FALSE(traitCast(nullptr)); + + // `ViewShadowNode` is `LayoutableShadowNode` and `YogaLayoutableShadowNode`. + EXPECT_TRUE(traitCast(viewShadowNode.get())); + EXPECT_TRUE( + traitCast(viewShadowNode.get())); + EXPECT_NO_FATAL_FAILURE( + traitCast(*viewShadowNode)); + EXPECT_NO_FATAL_FAILURE( + traitCast(*viewShadowNode)); + + // `ScrollViewShadowNode` is `LayoutableShadowNode` and + // `YogaLayoutableShadowNode`. + EXPECT_TRUE( + traitCast(scrollViewShadowNode.get())); + EXPECT_TRUE( + traitCast(scrollViewShadowNode.get())); + EXPECT_NO_FATAL_FAILURE( + traitCast(*scrollViewShadowNode)); + EXPECT_NO_FATAL_FAILURE( + traitCast(*scrollViewShadowNode)); + + // `ParagraphShadowNode` is `LayoutableShadowNode` and + // `YogaLayoutableShadowNode`. + EXPECT_TRUE( + traitCast(paragraphShadowNode.get())); + EXPECT_TRUE( + traitCast(paragraphShadowNode.get())); + EXPECT_NO_FATAL_FAILURE( + traitCast(*paragraphShadowNode)); + EXPECT_NO_FATAL_FAILURE( + traitCast(*paragraphShadowNode)); + + // `TextShadowNode` is *not* `LayoutableShadowNode` nor + // `YogaLayoutableShadowNode`. + EXPECT_FALSE(traitCast(textShadowNode.get())); + EXPECT_FALSE( + traitCast(textShadowNode.get())); + EXPECT_DEATH_IF_SUPPORTED( + traitCast(*textShadowNode), ""); + EXPECT_DEATH_IF_SUPPORTED( + traitCast(*textShadowNode), ""); + + // `RawTextShadowNode` is *not* `LayoutableShadowNode` nor + // `YogaLayoutableShadowNode`. + EXPECT_FALSE( + traitCast(rawTextShadowNode.get())); + EXPECT_FALSE( + traitCast(rawTextShadowNode.get())); + EXPECT_DEATH_IF_SUPPORTED( + traitCast(*rawTextShadowNode), ""); + EXPECT_DEATH_IF_SUPPORTED( + traitCast(*rawTextShadowNode), ""); +} diff --git a/ReactCommon/fabric/element/BUCK b/ReactCommon/fabric/element/BUCK index db36226e8d7fec..d40596e59c4ec5 100644 --- a/ReactCommon/fabric/element/BUCK +++ b/ReactCommon/fabric/element/BUCK @@ -55,6 +55,7 @@ rn_xplat_cxx_library( react_native_xplat_target("fabric/components/root:root"), react_native_xplat_target("fabric/components/view:view"), react_native_xplat_target("fabric/components/scrollview:scrollview"), + react_native_xplat_target("fabric/components/text:text"), react_native_xplat_target("fabric/uimanager:uimanager"), react_native_xplat_target("fabric/core:core"), react_native_xplat_target("fabric/debug:debug"), diff --git a/ReactCommon/fabric/element/testUtils.h b/ReactCommon/fabric/element/testUtils.h index f600ab758e81f6..008786913c1b77 100644 --- a/ReactCommon/fabric/element/testUtils.h +++ b/ReactCommon/fabric/element/testUtils.h @@ -9,6 +9,9 @@ #include #include +#include +#include +#include #include #include #include @@ -29,6 +32,12 @@ inline ComponentBuilder simpleComponentBuilder() { concreteComponentDescriptorProvider()); componentDescriptorProviderRegistry.add( concreteComponentDescriptorProvider()); + componentDescriptorProviderRegistry.add( + concreteComponentDescriptorProvider()); + componentDescriptorProviderRegistry.add( + concreteComponentDescriptorProvider()); + componentDescriptorProviderRegistry.add( + concreteComponentDescriptorProvider()); return ComponentBuilder{componentDescriptorRegistry}; }