-
Notifications
You must be signed in to change notification settings - Fork 24k
/
ViewTest.cpp
221 lines (187 loc) · 7.35 KB
/
ViewTest.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
/*
* 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 <algorithm>
#include <memory>
#include <gtest/gtest.h>
#include <react/components/root/RootComponentDescriptor.h>
#include <react/components/scrollview/ScrollViewComponentDescriptor.h>
#include <react/components/view/ViewComponentDescriptor.h>
#include <react/element/ComponentBuilder.h>
#include <react/element/Element.h>
#include <react/element/testUtils.h>
#include <react/uimanager/ComponentDescriptorProviderRegistry.h>
namespace facebook {
namespace react {
class YogaDirtyFlagTest : public ::testing::Test {
protected:
ComponentBuilder builder_;
std::shared_ptr<RootShadowNode> rootShadowNode_;
std::shared_ptr<ViewShadowNode> innerShadowNode_;
std::shared_ptr<ScrollViewShadowNode> scrollViewShadowNode_;
YogaDirtyFlagTest() : builder_(simpleComponentBuilder()) {
// clang-format off
auto element =
Element<RootShadowNode>()
.reference(rootShadowNode_)
.tag(1)
.children({
Element<ViewShadowNode>()
.tag(2),
Element<ViewShadowNode>()
.tag(3)
.reference(innerShadowNode_)
.children({
Element<ViewShadowNode>()
.tag(4)
.props([] {
/*
* Some non-default props.
*/
auto mutableViewProps = std::make_shared<ViewProps>();
auto &props = *mutableViewProps;
props.nativeId = "native Id";
props.opacity = 0.5;
props.yogaStyle.alignContent() = YGAlignBaseline;
props.yogaStyle.flexDirection() = YGFlexDirectionRowReverse;
return mutableViewProps;
}),
Element<ViewShadowNode>()
.tag(5),
Element<ViewShadowNode>()
.tag(6),
Element<ScrollViewShadowNode>()
.reference(scrollViewShadowNode_)
.tag(7)
})
});
// clang-format on
builder_.build(element);
/*
* Yoga nodes are dirty right after creation.
*/
EXPECT_TRUE(rootShadowNode_->layoutIfNeeded());
/*
* Yoga nodes are clean (not dirty) right after layout pass.
*/
EXPECT_FALSE(rootShadowNode_->layoutIfNeeded());
}
};
TEST_F(YogaDirtyFlagTest, cloningPropsWithoutChangingThem) {
/*
* Cloning props without changing them must *not* dirty a Yoga node.
*/
auto newRootShadowNode = rootShadowNode_->cloneTree(
innerShadowNode_->getFamily(), [](ShadowNode const &oldShadowNode) {
auto &componentDescriptor = oldShadowNode.getComponentDescriptor();
auto props = componentDescriptor.cloneProps(
oldShadowNode.getProps(), RawProps());
return oldShadowNode.clone(ShadowNodeFragment{props});
});
EXPECT_FALSE(
static_cast<RootShadowNode &>(*newRootShadowNode).layoutIfNeeded());
}
TEST_F(YogaDirtyFlagTest, changingNonLayoutSubPropsMustNotDirtyYogaNode) {
/*
* Changing *non-layout* sub-props must *not* dirty a Yoga node.
*/
auto newRootShadowNode = rootShadowNode_->cloneTree(
innerShadowNode_->getFamily(), [](ShadowNode const &oldShadowNode) {
auto viewProps = std::make_shared<ViewProps>();
auto &props = *viewProps;
props.nativeId = "some new native Id";
props.foregroundColor = whiteColor();
props.backgroundColor = blackColor();
props.opacity = props.opacity + 0.042;
props.zIndex = props.zIndex + 42;
props.shouldRasterize = !props.shouldRasterize;
props.collapsable = !props.collapsable;
return oldShadowNode.clone(ShadowNodeFragment{viewProps});
});
EXPECT_FALSE(
static_cast<RootShadowNode &>(*newRootShadowNode).layoutIfNeeded());
}
TEST_F(YogaDirtyFlagTest, changingLayoutSubPropsMustDirtyYogaNode) {
/*
* Changing *layout* sub-props *must* dirty a Yoga node.
*/
auto newRootShadowNode = rootShadowNode_->cloneTree(
innerShadowNode_->getFamily(), [](ShadowNode const &oldShadowNode) {
auto viewProps = std::make_shared<ViewProps>();
auto &props = *viewProps;
props.yogaStyle.alignContent() = YGAlignBaseline;
props.yogaStyle.display() = YGDisplayNone;
return oldShadowNode.clone(ShadowNodeFragment{viewProps});
});
EXPECT_TRUE(
static_cast<RootShadowNode &>(*newRootShadowNode).layoutIfNeeded());
}
TEST_F(YogaDirtyFlagTest, removingAllChildrenMustDirtyYogaNode) {
/*
* Removing all children *must* dirty a Yoga node.
*/
auto newRootShadowNode = rootShadowNode_->cloneTree(
innerShadowNode_->getFamily(), [](ShadowNode const &oldShadowNode) {
return oldShadowNode.clone(
{ShadowNodeFragment::propsPlaceholder(),
ShadowNode::emptySharedShadowNodeSharedList()});
});
EXPECT_TRUE(
static_cast<RootShadowNode &>(*newRootShadowNode).layoutIfNeeded());
}
TEST_F(YogaDirtyFlagTest, removingLastChildMustDirtyYogaNode) {
/*
* Removing the last child *must* dirty the Yoga node.
*/
auto newRootShadowNode = rootShadowNode_->cloneTree(
innerShadowNode_->getFamily(), [](ShadowNode const &oldShadowNode) {
auto children = oldShadowNode.getChildren();
children.pop_back();
std::reverse(children.begin(), children.end());
return oldShadowNode.clone(
{ShadowNodeFragment::propsPlaceholder(),
std::make_shared<ShadowNode::ListOfShared const>(children)});
});
EXPECT_TRUE(
static_cast<RootShadowNode &>(*newRootShadowNode).layoutIfNeeded());
}
TEST_F(YogaDirtyFlagTest, reversingListOfChildrenMustDirtyYogaNode) {
/*
* Reversing a list of children *must* dirty a Yoga node.
*/
auto newRootShadowNode = rootShadowNode_->cloneTree(
innerShadowNode_->getFamily(), [](ShadowNode const &oldShadowNode) {
auto children = oldShadowNode.getChildren();
std::reverse(children.begin(), children.end());
return oldShadowNode.clone(
{ShadowNodeFragment::propsPlaceholder(),
std::make_shared<ShadowNode::ListOfShared const>(children)});
});
EXPECT_TRUE(
static_cast<RootShadowNode &>(*newRootShadowNode).layoutIfNeeded());
}
TEST_F(YogaDirtyFlagTest, updatingStateForScrollViewMistNotDirtyYogaNode) {
/*
* Updating a state for *some* (not all!) components must *not* dirty Yoga
* nodes.
*/
auto newRootShadowNode = rootShadowNode_->cloneTree(
scrollViewShadowNode_->getFamily(), [](ShadowNode const &oldShadowNode) {
auto state = ScrollViewState{};
state.contentOffset = Point{42, 9000};
auto &componentDescriptor = oldShadowNode.getComponentDescriptor();
auto newState = componentDescriptor.createState(
oldShadowNode.getFamily(),
std::make_shared<ScrollViewState>(state));
return oldShadowNode.clone({ShadowNodeFragment::propsPlaceholder(),
ShadowNodeFragment::childrenPlaceholder(),
newState});
});
EXPECT_FALSE(
static_cast<RootShadowNode &>(*newRootShadowNode).layoutIfNeeded());
}
} // namespace react
} // namespace facebook