|
| 1 | +/* |
| 2 | + * Copyright (c) Facebook, Inc. and its affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include <exception> |
| 9 | + |
| 10 | +#include <gtest/gtest.h> |
| 11 | +#include "TestComponent.h" |
| 12 | + |
| 13 | +using namespace facebook::react; |
| 14 | + |
| 15 | +TEST(ShadowNodeFamilyTest, sealObjectCorrectly) { |
| 16 | + /* |
| 17 | + * The structure: |
| 18 | + * <A> |
| 19 | + * <AA> |
| 20 | + * <AAA/> |
| 21 | + * </AA> |
| 22 | + * </A> |
| 23 | + */ |
| 24 | + SurfaceId surfaceId = 1; |
| 25 | + auto eventDispatcher = std::shared_ptr<EventDispatcher const>(); |
| 26 | + auto componentDescriptor = TestComponentDescriptor({eventDispatcher}); |
| 27 | + auto props = std::make_shared<const TestProps>(); |
| 28 | + |
| 29 | + auto familyAAA = std::make_shared<ShadowNodeFamily>( |
| 30 | + ShadowNodeFamilyFragment{ |
| 31 | + /* .tag = */ 12, |
| 32 | + /* .surfaceId = */ surfaceId, |
| 33 | + /* .eventEmitter = */ nullptr, |
| 34 | + }, |
| 35 | + componentDescriptor); |
| 36 | + |
| 37 | + auto nodeAAA = std::make_shared<TestShadowNode>( |
| 38 | + ShadowNodeFragment{ |
| 39 | + /* .props = */ props, |
| 40 | + /* .children = */ ShadowNode::emptySharedShadowNodeSharedList(), |
| 41 | + }, |
| 42 | + familyAAA, |
| 43 | + ShadowNodeTraits{}); |
| 44 | + |
| 45 | + auto nodeAAChildren = |
| 46 | + std::make_shared<SharedShadowNodeList>(SharedShadowNodeList{nodeAAA}); |
| 47 | + auto familyAA = std::make_shared<ShadowNodeFamily>( |
| 48 | + ShadowNodeFamilyFragment{ |
| 49 | + /* .tag = */ 11, |
| 50 | + /* .surfaceId = */ surfaceId, |
| 51 | + /* .eventEmitter = */ nullptr, |
| 52 | + }, |
| 53 | + componentDescriptor); |
| 54 | + auto nodeAA = std::make_shared<TestShadowNode>( |
| 55 | + ShadowNodeFragment{ |
| 56 | + /* .props = */ props, |
| 57 | + /* .children = */ nodeAAChildren, |
| 58 | + }, |
| 59 | + familyAA, |
| 60 | + ShadowNodeTraits{}); |
| 61 | + |
| 62 | + auto nodeAChildren = |
| 63 | + std::make_shared<SharedShadowNodeList>(SharedShadowNodeList{nodeAA}); |
| 64 | + |
| 65 | + auto familyA = std::make_shared<ShadowNodeFamily>( |
| 66 | + ShadowNodeFamilyFragment{ |
| 67 | + /* .tag = */ 17, |
| 68 | + /* .surfaceId = */ surfaceId, |
| 69 | + /* .eventEmitter = */ nullptr, |
| 70 | + }, |
| 71 | + componentDescriptor); |
| 72 | + auto nodeA = std::make_shared<TestShadowNode>( |
| 73 | + ShadowNodeFragment{ |
| 74 | + /* .props = */ props, |
| 75 | + /* .children = */ nodeAChildren, |
| 76 | + }, |
| 77 | + familyA, |
| 78 | + ShadowNodeTraits{}); |
| 79 | + |
| 80 | + auto familyZ = std::make_shared<ShadowNodeFamily>( |
| 81 | + ShadowNodeFamilyFragment{ |
| 82 | + /* .tag = */ 18, |
| 83 | + /* .surfaceId = */ surfaceId, |
| 84 | + /* .eventEmitter = */ nullptr, |
| 85 | + }, |
| 86 | + componentDescriptor); |
| 87 | + auto nodeZ = std::make_shared<TestShadowNode>( |
| 88 | + ShadowNodeFragment{ |
| 89 | + /* .props = */ props, |
| 90 | + /* .children = */ ShadowNode::emptySharedShadowNodeSharedList(), |
| 91 | + }, |
| 92 | + familyZ, |
| 93 | + ShadowNodeTraits{}); |
| 94 | + |
| 95 | + // Negative case: |
| 96 | + auto ancestors1 = nodeZ->getFamily().getAncestors(*nodeA); |
| 97 | + EXPECT_EQ(ancestors1.size(), 0); |
| 98 | + |
| 99 | + // Positive case: |
| 100 | + auto ancestors2 = nodeAAA->getFamily().getAncestors(*nodeA); |
| 101 | + EXPECT_EQ(ancestors2.size(), 2); |
| 102 | + EXPECT_EQ(&ancestors2[0].first.get(), nodeA.get()); |
| 103 | + EXPECT_EQ(&ancestors2[1].first.get(), nodeAA.get()); |
| 104 | +} |
0 commit comments