|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and 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 <react/fabric/MountItem.h> |
| 9 | + |
| 10 | +#include <gtest/gtest.h> |
| 11 | + |
| 12 | +namespace facebook::react { |
| 13 | + |
| 14 | +namespace { |
| 15 | + |
| 16 | +ShadowView |
| 17 | +makeShadowView(ComponentName componentName, SurfaceId surfaceId, Tag tag) { |
| 18 | + auto shadowView = ShadowView{}; |
| 19 | + shadowView.componentName = componentName; |
| 20 | + shadowView.surfaceId = surfaceId; |
| 21 | + shadowView.tag = tag; |
| 22 | + return shadowView; |
| 23 | +} |
| 24 | + |
| 25 | +void expectSameShadowViewIdentity( |
| 26 | + const ShadowView& expected, |
| 27 | + const ShadowView& actual) { |
| 28 | + EXPECT_EQ(expected.componentName, actual.componentName); |
| 29 | + EXPECT_EQ(expected.surfaceId, actual.surfaceId); |
| 30 | + EXPECT_EQ(expected.tag, actual.tag); |
| 31 | +} |
| 32 | + |
| 33 | +void expectEmptyShadowView(const ShadowView& shadowView) { |
| 34 | + EXPECT_EQ(nullptr, shadowView.componentName); |
| 35 | + EXPECT_EQ(SurfaceId{}, shadowView.surfaceId); |
| 36 | + EXPECT_EQ(Tag{}, shadowView.tag); |
| 37 | +} |
| 38 | + |
| 39 | +} // namespace |
| 40 | + |
| 41 | +class MountItemTest : public ::testing::Test {}; |
| 42 | + |
| 43 | +TEST_F(MountItemTest, testCreateAndDeleteUseOppositeShadowViewSlots) { |
| 44 | + const auto shadowView = makeShadowView("View", /*surfaceId=*/11, /*tag=*/101); |
| 45 | + |
| 46 | + const auto createItem = CppMountItem::CreateMountItem(shadowView); |
| 47 | + const auto deleteItem = CppMountItem::DeleteMountItem(shadowView); |
| 48 | + |
| 49 | + EXPECT_EQ(CppMountItem::Type::Create, createItem.type); |
| 50 | + expectSameShadowViewIdentity(shadowView, createItem.newChildShadowView); |
| 51 | + expectEmptyShadowView(createItem.oldChildShadowView); |
| 52 | + EXPECT_EQ(-1, createItem.index); |
| 53 | + |
| 54 | + EXPECT_EQ(CppMountItem::Type::Delete, deleteItem.type); |
| 55 | + expectSameShadowViewIdentity(shadowView, deleteItem.oldChildShadowView); |
| 56 | + expectEmptyShadowView(deleteItem.newChildShadowView); |
| 57 | + EXPECT_EQ(-1, deleteItem.index); |
| 58 | +} |
| 59 | + |
| 60 | +TEST_F(MountItemTest, testInsertAndRemovePreserveParentIndexAndChildSlot) { |
| 61 | + constexpr Tag kParentTag = 501; |
| 62 | + constexpr int kIndex = 3; |
| 63 | + const auto shadowView = makeShadowView("Text", /*surfaceId=*/22, /*tag=*/202); |
| 64 | + |
| 65 | + const auto insertItem = |
| 66 | + CppMountItem::InsertMountItem(kParentTag, shadowView, kIndex); |
| 67 | + const auto removeItem = |
| 68 | + CppMountItem::RemoveMountItem(kParentTag, shadowView, kIndex); |
| 69 | + |
| 70 | + EXPECT_EQ(CppMountItem::Type::Insert, insertItem.type); |
| 71 | + EXPECT_EQ(kParentTag, insertItem.parentTag); |
| 72 | + EXPECT_EQ(kIndex, insertItem.index); |
| 73 | + expectSameShadowViewIdentity(shadowView, insertItem.newChildShadowView); |
| 74 | + expectEmptyShadowView(insertItem.oldChildShadowView); |
| 75 | + |
| 76 | + EXPECT_EQ(CppMountItem::Type::Remove, removeItem.type); |
| 77 | + EXPECT_EQ(kParentTag, removeItem.parentTag); |
| 78 | + EXPECT_EQ(kIndex, removeItem.index); |
| 79 | + expectSameShadowViewIdentity(shadowView, removeItem.oldChildShadowView); |
| 80 | + expectEmptyShadowView(removeItem.newChildShadowView); |
| 81 | +} |
| 82 | + |
| 83 | +TEST_F(MountItemTest, testUpdatePropsPreservesOldAndNewShadowViews) { |
| 84 | + const auto oldShadowView = |
| 85 | + makeShadowView("RawText", /*surfaceId=*/33, /*tag=*/303); |
| 86 | + const auto newShadowView = |
| 87 | + makeShadowView("RawText", /*surfaceId=*/33, /*tag=*/404); |
| 88 | + |
| 89 | + const auto item = |
| 90 | + CppMountItem::UpdatePropsMountItem(oldShadowView, newShadowView); |
| 91 | + |
| 92 | + EXPECT_EQ(CppMountItem::Type::UpdateProps, item.type); |
| 93 | + expectSameShadowViewIdentity(oldShadowView, item.oldChildShadowView); |
| 94 | + expectSameShadowViewIdentity(newShadowView, item.newChildShadowView); |
| 95 | + EXPECT_EQ(-1, item.index); |
| 96 | +} |
| 97 | + |
| 98 | +TEST_F(MountItemTest, testUpdateLayoutPreservesParentTagWithShadowView) { |
| 99 | + constexpr Tag kParentTag = 604; |
| 100 | + const auto shadowView = |
| 101 | + makeShadowView("ScrollView", /*surfaceId=*/44, /*tag=*/404); |
| 102 | + |
| 103 | + const auto item = CppMountItem::UpdateLayoutMountItem(shadowView, kParentTag); |
| 104 | + |
| 105 | + EXPECT_EQ(CppMountItem::Type::UpdateLayout, item.type); |
| 106 | + EXPECT_EQ(kParentTag, item.parentTag); |
| 107 | + expectSameShadowViewIdentity(shadowView, item.newChildShadowView); |
| 108 | + expectEmptyShadowView(item.oldChildShadowView); |
| 109 | + EXPECT_EQ(-1, item.index); |
| 110 | +} |
| 111 | + |
| 112 | +TEST_F(MountItemTest, testNonLayoutUpdatesTargetNewShadowViewOnly) { |
| 113 | + const auto shadowView = |
| 114 | + makeShadowView("Image", /*surfaceId=*/55, /*tag=*/505); |
| 115 | + |
| 116 | + const auto stateItem = CppMountItem::UpdateStateMountItem(shadowView); |
| 117 | + const auto eventEmitterItem = |
| 118 | + CppMountItem::UpdateEventEmitterMountItem(shadowView); |
| 119 | + const auto paddingItem = CppMountItem::UpdatePaddingMountItem(shadowView); |
| 120 | + const auto overflowInsetItem = |
| 121 | + CppMountItem::UpdateOverflowInsetMountItem(shadowView); |
| 122 | + |
| 123 | + EXPECT_EQ(CppMountItem::Type::UpdateState, stateItem.type); |
| 124 | + expectSameShadowViewIdentity(shadowView, stateItem.newChildShadowView); |
| 125 | + expectEmptyShadowView(stateItem.oldChildShadowView); |
| 126 | + |
| 127 | + EXPECT_EQ(CppMountItem::Type::UpdateEventEmitter, eventEmitterItem.type); |
| 128 | + expectSameShadowViewIdentity(shadowView, eventEmitterItem.newChildShadowView); |
| 129 | + expectEmptyShadowView(eventEmitterItem.oldChildShadowView); |
| 130 | + |
| 131 | + EXPECT_EQ(CppMountItem::Type::UpdatePadding, paddingItem.type); |
| 132 | + expectSameShadowViewIdentity(shadowView, paddingItem.newChildShadowView); |
| 133 | + expectEmptyShadowView(paddingItem.oldChildShadowView); |
| 134 | + |
| 135 | + EXPECT_EQ(CppMountItem::Type::UpdateOverflowInset, overflowInsetItem.type); |
| 136 | + expectSameShadowViewIdentity( |
| 137 | + shadowView, overflowInsetItem.newChildShadowView); |
| 138 | + expectEmptyShadowView(overflowInsetItem.oldChildShadowView); |
| 139 | +} |
| 140 | + |
| 141 | +} // namespace facebook::react |
0 commit comments