Skip to content

Commit a2c39e6

Browse files
javachefacebook-github-bot
authored andcommitted
Modernize StateReconciliationTest
Differential Revision: D48969984 fbshipit-source-id: cb8fe8262a23b655cf783a2aed77699a266dcc04
1 parent a228b0f commit a2c39e6

File tree

1 file changed

+68
-61
lines changed

1 file changed

+68
-61
lines changed

packages/react-native/ReactCommon/react/renderer/mounting/tests/StateReconciliationTest.cpp

Lines changed: 68 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,8 @@
2222

2323
using namespace facebook::react;
2424

25+
namespace {
26+
2527
class DummyShadowTreeDelegate : public ShadowTreeDelegate {
2628
public:
2729
RootShadowNode::Unshared shadowTreeWillCommit(
@@ -36,7 +38,7 @@ class DummyShadowTreeDelegate : public ShadowTreeDelegate {
3638
bool mountSynchronously) const override{};
3739
};
3840

39-
inline const ShadowNode* findDescendantNode(
41+
const ShadowNode* findDescendantNode(
4042
const ShadowNode& shadowNode,
4143
const ShadowNodeFamily& family) {
4244
const ShadowNode* result = nullptr;
@@ -47,82 +49,87 @@ inline const ShadowNode* findDescendantNode(
4749
return result;
4850
}
4951

50-
inline const ShadowNode* findDescendantNode(
52+
const ShadowNode* findDescendantNode(
5153
const ShadowTree& shadowTree,
5254
const ShadowNodeFamily& family) {
5355
return findDescendantNode(
5456
*shadowTree.getCurrentRevision().rootShadowNode, family);
5557
}
5658

57-
TEST(StateReconciliationTest, testStateReconciliation) {
58-
auto builder = simpleComponentBuilder();
59-
60-
auto shadowNodeA = std::shared_ptr<RootShadowNode>{};
61-
auto shadowNodeAA = std::shared_ptr<ViewShadowNode>{};
62-
auto shadowNodeAB = std::shared_ptr<ScrollViewShadowNode>{};
63-
auto shadowNodeABA = std::shared_ptr<ViewShadowNode>{};
64-
auto shadowNodeABB = std::shared_ptr<ViewShadowNode>{};
65-
auto shadowNodeABC = std::shared_ptr<ViewShadowNode>{};
66-
67-
// clang-format off
68-
auto element =
69-
Element<RootShadowNode>()
70-
.reference(shadowNodeA)
71-
.finalize([](RootShadowNode &shadowNode){
72-
shadowNode.sealRecursive();
73-
})
74-
.children({
75-
Element<ViewShadowNode>()
76-
.reference(shadowNodeAA),
77-
Element<ScrollViewShadowNode>()
78-
.reference(shadowNodeAB)
79-
.children({
80-
Element<ViewShadowNode>()
59+
} // namespace
60+
61+
class StateReconciliationTest : public ::testing::Test {
62+
public:
63+
StateReconciliationTest()
64+
: shadowTree_(
65+
SurfaceId{11},
66+
LayoutConstraints{},
67+
LayoutContext{},
68+
shadowTreeDelegate_,
69+
contextContainer_) {
70+
// clang-format off
71+
auto element =
72+
Element<RootShadowNode>()
73+
.reference(shadowNodeA_)
74+
.finalize([](RootShadowNode &shadowNode){
75+
shadowNode.sealRecursive();
76+
})
77+
.children({
78+
Element<ViewShadowNode>()
79+
.reference(shadowNodeAA_),
80+
Element<ScrollViewShadowNode>()
81+
.reference(shadowNodeAB_)
8182
.children({
8283
Element<ViewShadowNode>()
83-
.reference(shadowNodeABA),
84-
Element<ViewShadowNode>()
85-
.reference(shadowNodeABB),
86-
Element<ViewShadowNode>()
87-
.reference(shadowNodeABC)
84+
.children({
85+
Element<ViewShadowNode>()
86+
.reference(shadowNodeABA_),
87+
Element<ViewShadowNode>()
88+
.reference(shadowNodeABB_),
89+
Element<ViewShadowNode>()
90+
.reference(shadowNodeABC_)
91+
})
8892
})
89-
})
90-
});
91-
// clang-format on
92-
93-
ContextContainer contextContainer{};
94-
95-
auto shadowNode = builder.build(element);
96-
97-
auto rootShadowNodeState1 = shadowNode->ShadowNode::clone({});
93+
});
94+
// clang-format on
95+
componentBuilder_.build(element);
96+
}
97+
98+
ComponentBuilder componentBuilder_ = simpleComponentBuilder();
99+
ContextContainer contextContainer_;
100+
DummyShadowTreeDelegate shadowTreeDelegate_;
101+
ShadowTree shadowTree_;
102+
103+
std::shared_ptr<RootShadowNode> shadowNodeA_;
104+
std::shared_ptr<ViewShadowNode> shadowNodeAA_;
105+
std::shared_ptr<ScrollViewShadowNode> shadowNodeAB_;
106+
std::shared_ptr<ViewShadowNode> shadowNodeABA_;
107+
std::shared_ptr<ViewShadowNode> shadowNodeABB_;
108+
std::shared_ptr<ViewShadowNode> shadowNodeABC_;
109+
};
98110

99-
auto& scrollViewComponentDescriptor = shadowNodeAB->getComponentDescriptor();
100-
auto& family = shadowNodeAB->getFamily();
101-
auto state1 = shadowNodeAB->getState();
102-
auto shadowTreeDelegate = DummyShadowTreeDelegate{};
103-
ShadowTree shadowTree{
104-
SurfaceId{11},
105-
LayoutConstraints{},
106-
LayoutContext{},
107-
shadowTreeDelegate,
108-
contextContainer};
111+
TEST_F(StateReconciliationTest, testStateReconciliation) {
112+
// Cloning to get an unsealed shadowNode
113+
auto rootShadowNodeState1 = shadowNodeA_->ShadowNode::clone({});
114+
auto state1 = shadowNodeAB_->getState();
115+
auto& family = shadowNodeAB_->getFamily();
109116

110-
shadowTree.commit(
117+
shadowTree_.commit(
111118
[&](const RootShadowNode& /*oldRootShadowNode*/) {
112119
return std::static_pointer_cast<RootShadowNode>(rootShadowNodeState1);
113120
},
114-
{true});
121+
{/* .enableStateReconciliation = */ true});
115122

116123
EXPECT_EQ(state1->getMostRecentState(), state1);
117-
118124
EXPECT_EQ(
119125
findDescendantNode(*rootShadowNodeState1, family)->getState(), state1);
120126

127+
auto& scrollViewComponentDescriptor = shadowNodeAB_->getComponentDescriptor();
121128
auto state2 = scrollViewComponentDescriptor.createState(
122129
family, std::make_shared<const ScrollViewState>());
123130

124131
auto rootShadowNodeState2 =
125-
shadowNode->cloneTree(family, [&](const ShadowNode& oldShadowNode) {
132+
shadowNodeA_->cloneTree(family, [&](const ShadowNode& oldShadowNode) {
126133
return oldShadowNode.clone(
127134
{ShadowNodeFragment::propsPlaceholder(),
128135
ShadowNodeFragment::childrenPlaceholder(),
@@ -132,11 +139,11 @@ TEST(StateReconciliationTest, testStateReconciliation) {
132139
EXPECT_EQ(
133140
findDescendantNode(*rootShadowNodeState2, family)->getState(), state2);
134141

135-
shadowTree.commit(
142+
shadowTree_.commit(
136143
[&](const RootShadowNode& /*oldRootShadowNode*/) {
137144
return std::static_pointer_cast<RootShadowNode>(rootShadowNodeState2);
138145
},
139-
{true});
146+
{/* .enableStateReconciliation = */ true});
140147

141148
EXPECT_EQ(state1->getMostRecentState(), state2);
142149
EXPECT_EQ(state2->getMostRecentState(), state2);
@@ -155,13 +162,13 @@ TEST(StateReconciliationTest, testStateReconciliation) {
155162
EXPECT_EQ(
156163
findDescendantNode(*rootShadowNodeState3, family)->getState(), state3);
157164

158-
shadowTree.commit(
165+
shadowTree_.commit(
159166
[&](const RootShadowNode& /*oldRootShadowNode*/) {
160167
return std::static_pointer_cast<RootShadowNode>(rootShadowNodeState3);
161168
},
162-
{true});
169+
{/* .enableStateReconciliation = */ true});
163170

164-
EXPECT_EQ(findDescendantNode(shadowTree, family)->getState(), state3);
171+
EXPECT_EQ(findDescendantNode(shadowTree_, family)->getState(), state3);
165172

166173
EXPECT_EQ(state1->getMostRecentState(), state3);
167174
EXPECT_EQ(state2->getMostRecentState(), state3);
@@ -170,11 +177,11 @@ TEST(StateReconciliationTest, testStateReconciliation) {
170177
// This is the core part of the whole test.
171178
// Here we commit the old tree but we expect that the state associated with
172179
// the node will stay the same (newer that the old tree has).
173-
shadowTree.commit(
180+
shadowTree_.commit(
174181
[&](const RootShadowNode& /*oldRootShadowNode*/) {
175182
return std::static_pointer_cast<RootShadowNode>(rootShadowNodeState2);
176183
},
177184
{true});
178185

179-
EXPECT_EQ(findDescendantNode(shadowTree, family)->getState(), state3);
186+
EXPECT_EQ(findDescendantNode(shadowTree_, family)->getState(), state3);
180187
}

0 commit comments

Comments
 (0)