Files
react-native/ReactCommon/fabric/core/tests/ShadowNodeFamilyTest.cpp
T
Valentin Shergin b39c75f20e Fabric: Element<> now returns (and accepts) mutable (non const) shared pointers
Summary:
It makes perfect sense because `Builder` builds totally new shadow trees, so those are not sealed or used by anyone yet.
Assigning it to const shared pointer will do logical sealing (and it does not requires const-cast).
Fewer const-casts in the code, fewer bugs.

Changelog: [Internal] Fabric-specific internal change.

Reviewed By: sammy-SC

Differential Revision: D19596284

fbshipit-source-id: 75d1c706034958ba7e4bc80a68af75a57c46eb6f
2020-01-30 19:45:39 -08:00

80 lines
2.3 KiB
C++

/*
* 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 <exception>
#include <gtest/gtest.h>
#include <react/components/view/ViewComponentDescriptor.h>
#include <react/element/ComponentBuilder.h>
#include <react/element/Element.h>
#include <react/uimanager/ComponentDescriptorProviderRegistry.h>
using namespace facebook::react;
TEST(ShadowNodeFamilyTest, sealObjectCorrectly) {
/*
* The structure:
* <A>
* <AA>
* <AAA/>
* </AA>
* </A>
*/
ComponentDescriptorProviderRegistry componentDescriptorProviderRegistry{};
auto eventDispatcher = EventDispatcher::Shared{};
auto componentDescriptorRegistry =
componentDescriptorProviderRegistry.createComponentDescriptorRegistry(
ComponentDescriptorParameters{eventDispatcher, nullptr, nullptr});
componentDescriptorProviderRegistry.add(
concreteComponentDescriptorProvider<ViewComponentDescriptor>());
auto builder = ComponentBuilder{componentDescriptorRegistry};
auto shadowNodeAAA = std::shared_ptr<ViewShadowNode>{};
auto shadowNodeAA = std::shared_ptr<ViewShadowNode>{};
// clang-format off
auto elementA =
Element<ViewShadowNode>()
.tag(1)
.finalize([](ViewShadowNode &shadowNode){
shadowNode.sealRecursive();
})
.children({
Element<ViewShadowNode>()
.tag(2)
.reference(shadowNodeAA)
.children({
Element<ViewShadowNode>()
.reference(shadowNodeAAA)
.tag(3)
})
});
auto elementB =
Element<ViewShadowNode>()
.tag(1)
.finalize([](ViewShadowNode &shadowNode){
shadowNode.sealRecursive();
});
// clang-format on
auto shadowNodeA = builder.build(elementA);
auto shadowNodeB = builder.build(elementB);
// Negative case:
auto ancestors1 = shadowNodeB->getFamily().getAncestors(*shadowNodeA);
EXPECT_EQ(ancestors1.size(), 0);
// Positive case:
auto ancestors2 = shadowNodeAAA->getFamily().getAncestors(*shadowNodeA);
EXPECT_EQ(ancestors2.size(), 2);
EXPECT_EQ(&ancestors2[0].first.get(), shadowNodeA.get());
EXPECT_EQ(&ancestors2[1].first.get(), shadowNodeAA.get());
}