-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathYGNodeChildTest.cpp
More file actions
110 lines (83 loc) · 3.43 KB
/
Copy pathYGNodeChildTest.cpp
File metadata and controls
110 lines (83 loc) · 3.43 KB
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
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <stdexcept>
#include <gtest/gtest.h>
#include <yoga/Yoga.h>
TEST(YogaTest, reset_layout_when_child_removed) {
YGNodeRef root = YGNodeNew();
YGNodeRef root_child0 = YGNodeNew();
YGNodeStyleSetWidth(root_child0, 100);
YGNodeStyleSetHeight(root_child0, 100);
YGNodeInsertChild(root, root_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_EQ(100, YGNodeLayoutGetWidth(root_child0));
ASSERT_EQ(100, YGNodeLayoutGetHeight(root_child0));
YGNodeRemoveChild(root, root_child0);
ASSERT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_TRUE(YGFloatIsUndefined(YGNodeLayoutGetWidth(root_child0)));
ASSERT_TRUE(YGFloatIsUndefined(YGNodeLayoutGetHeight(root_child0)));
YGNodeFreeRecursive(root);
YGNodeFreeRecursive(root_child0);
}
TEST(YogaTest, removed_child_can_be_reused_with_valid_layout) {
YGNodeRef root = YGNodeNew();
YGNodeStyleSetWidth(root, 200);
YGNodeStyleSetHeight(root, 200);
YGNodeRef child = YGNodeNew();
YGNodeStyleSetWidth(child, 100);
YGNodeStyleSetHeight(child, 100);
YGNodeInsertChild(root, child, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_EQ(100, YGNodeLayoutGetWidth(child));
ASSERT_EQ(100, YGNodeLayoutGetHeight(child));
// Remove child - layout should be cleared and child marked dirty
YGNodeRemoveChild(root, child);
ASSERT_TRUE(YGFloatIsUndefined(YGNodeLayoutGetWidth(child)));
ASSERT_TRUE(YGFloatIsUndefined(YGNodeLayoutGetHeight(child)));
ASSERT_TRUE(YGNodeIsDirty(child));
// Reinsert the child and recalculate - layout should be valid again
YGNodeInsertChild(root, child, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_EQ(100, YGNodeLayoutGetWidth(child));
ASSERT_EQ(100, YGNodeLayoutGetHeight(child));
ASSERT_FALSE(YGNodeIsDirty(child));
YGNodeFreeRecursive(root);
}
TEST(YogaTest, swap_child_with_out_of_bounds_index_is_rejected) {
YGNodeRef root = YGNodeNew();
YGNodeRef child = YGNodeNew();
YGNodeInsertChild(root, child, 0);
// Replacing an existing index works.
YGNodeRef replacement = YGNodeNew();
YGNodeSwapChild(root, replacement, 0);
ASSERT_EQ(replacement, YGNodeGetChild(root, 0));
// An index past the last child is rejected rather than reading and writing
// out of bounds on the underlying children vector.
YGNodeRef stray = YGNodeNew();
ASSERT_THROW(YGNodeSwapChild(root, stray, 1), std::logic_error);
YGNodeFree(stray);
YGNodeFree(child);
YGNodeFreeRecursive(root);
}
TEST(YogaTest, insert_child_with_out_of_bounds_index_is_rejected) {
YGNodeRef root = YGNodeNew();
YGNodeRef first = YGNodeNew();
YGNodeInsertChild(root, first, 0);
// Appending at the end (index == child count) is allowed.
YGNodeRef second = YGNodeNew();
YGNodeInsertChild(root, second, 1);
ASSERT_EQ(2u, YGNodeGetChildCount(root));
// An index beyond the end is rejected rather than constructing an invalid
// iterator into the children vector.
YGNodeRef stray = YGNodeNew();
ASSERT_THROW(YGNodeInsertChild(root, stray, 5), std::logic_error);
YGNodeFree(stray);
YGNodeFreeRecursive(root);
}