|
| 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 <gtest/gtest.h> |
| 9 | +#include <yoga/Yoga.h> |
| 10 | +#include <yoga/node/Node.h> |
| 11 | +#include <cstdio> |
| 12 | + |
| 13 | +TEST(YogaTest, layoutable_children_single_contents_node) { |
| 14 | + YGNodeRef root = YGNodeNew(); |
| 15 | + |
| 16 | + YGNodeRef root_child0 = YGNodeNew(); |
| 17 | + YGNodeRef root_child1 = YGNodeNew(); |
| 18 | + YGNodeRef root_child2 = YGNodeNew(); |
| 19 | + |
| 20 | + YGNodeRef root_grandchild0 = YGNodeNew(); |
| 21 | + YGNodeRef root_grandchild1 = YGNodeNew(); |
| 22 | + |
| 23 | + YGNodeInsertChild(root, root_child0, 0); |
| 24 | + YGNodeInsertChild(root, root_child1, 1); |
| 25 | + YGNodeInsertChild(root, root_child2, 2); |
| 26 | + |
| 27 | + YGNodeInsertChild(root_child1, root_grandchild0, 0); |
| 28 | + YGNodeInsertChild(root_child1, root_grandchild1, 1); |
| 29 | + |
| 30 | + YGNodeStyleSetDisplay(root_child1, YGDisplayContents); |
| 31 | + |
| 32 | + std::vector<facebook::yoga::Node*> order = { |
| 33 | + facebook::yoga::resolveRef(root_child0), |
| 34 | + facebook::yoga::resolveRef(root_grandchild0), |
| 35 | + facebook::yoga::resolveRef(root_grandchild1), |
| 36 | + facebook::yoga::resolveRef(root_child2), |
| 37 | + }; |
| 38 | + auto correctOrderIt = order.begin(); |
| 39 | + |
| 40 | + for (auto node : facebook::yoga::resolveRef(root)->getLayoutChildren()) { |
| 41 | + ASSERT_EQ(node, *correctOrderIt); |
| 42 | + correctOrderIt++; |
| 43 | + } |
| 44 | + |
| 45 | + YGNodeFreeRecursive(root); |
| 46 | +} |
| 47 | + |
| 48 | +TEST(YogaTest, layoutable_children_multiple_contents_nodes) { |
| 49 | + YGNodeRef root = YGNodeNew(); |
| 50 | + |
| 51 | + YGNodeRef root_child0 = YGNodeNew(); |
| 52 | + YGNodeRef root_child1 = YGNodeNew(); |
| 53 | + YGNodeRef root_child2 = YGNodeNew(); |
| 54 | + |
| 55 | + YGNodeRef root_grandchild0 = YGNodeNew(); |
| 56 | + YGNodeRef root_grandchild1 = YGNodeNew(); |
| 57 | + YGNodeRef root_grandchild2 = YGNodeNew(); |
| 58 | + YGNodeRef root_grandchild3 = YGNodeNew(); |
| 59 | + YGNodeRef root_grandchild4 = YGNodeNew(); |
| 60 | + YGNodeRef root_grandchild5 = YGNodeNew(); |
| 61 | + |
| 62 | + YGNodeInsertChild(root, root_child0, 0); |
| 63 | + YGNodeInsertChild(root, root_child1, 1); |
| 64 | + YGNodeInsertChild(root, root_child2, 2); |
| 65 | + |
| 66 | + YGNodeInsertChild(root_child0, root_grandchild0, 0); |
| 67 | + YGNodeInsertChild(root_child0, root_grandchild1, 1); |
| 68 | + YGNodeInsertChild(root_child1, root_grandchild2, 0); |
| 69 | + YGNodeInsertChild(root_child1, root_grandchild3, 1); |
| 70 | + YGNodeInsertChild(root_child2, root_grandchild4, 0); |
| 71 | + YGNodeInsertChild(root_child2, root_grandchild5, 1); |
| 72 | + |
| 73 | + YGNodeStyleSetDisplay(root_child0, YGDisplayContents); |
| 74 | + YGNodeStyleSetDisplay(root_child1, YGDisplayContents); |
| 75 | + YGNodeStyleSetDisplay(root_child2, YGDisplayContents); |
| 76 | + |
| 77 | + std::vector<facebook::yoga::Node*> order = { |
| 78 | + facebook::yoga::resolveRef(root_grandchild0), |
| 79 | + facebook::yoga::resolveRef(root_grandchild1), |
| 80 | + facebook::yoga::resolveRef(root_grandchild2), |
| 81 | + facebook::yoga::resolveRef(root_grandchild3), |
| 82 | + facebook::yoga::resolveRef(root_grandchild4), |
| 83 | + facebook::yoga::resolveRef(root_grandchild5), |
| 84 | + }; |
| 85 | + auto correctOrderIt = order.begin(); |
| 86 | + |
| 87 | + for (auto node : facebook::yoga::resolveRef(root)->getLayoutChildren()) { |
| 88 | + ASSERT_EQ(node, *correctOrderIt); |
| 89 | + correctOrderIt++; |
| 90 | + } |
| 91 | + |
| 92 | + YGNodeFreeRecursive(root); |
| 93 | +} |
| 94 | + |
| 95 | +TEST(YogaTest, layoutable_children_nested_contents_nodes) { |
| 96 | + YGNodeRef root = YGNodeNew(); |
| 97 | + |
| 98 | + YGNodeRef root_child0 = YGNodeNew(); |
| 99 | + YGNodeRef root_child1 = YGNodeNew(); |
| 100 | + YGNodeRef root_child2 = YGNodeNew(); |
| 101 | + |
| 102 | + YGNodeRef root_grandchild0 = YGNodeNew(); |
| 103 | + YGNodeRef root_grandchild1 = YGNodeNew(); |
| 104 | + |
| 105 | + YGNodeRef root_great_grandchild0 = YGNodeNew(); |
| 106 | + YGNodeRef root_great_grandchild1 = YGNodeNew(); |
| 107 | + |
| 108 | + YGNodeInsertChild(root, root_child0, 0); |
| 109 | + YGNodeInsertChild(root, root_child1, 1); |
| 110 | + YGNodeInsertChild(root, root_child2, 2); |
| 111 | + |
| 112 | + YGNodeInsertChild(root_child1, root_grandchild0, 0); |
| 113 | + YGNodeInsertChild(root_child1, root_grandchild1, 1); |
| 114 | + |
| 115 | + YGNodeInsertChild(root_grandchild1, root_great_grandchild0, 0); |
| 116 | + YGNodeInsertChild(root_grandchild1, root_great_grandchild1, 1); |
| 117 | + |
| 118 | + YGNodeStyleSetDisplay(root_child1, YGDisplayContents); |
| 119 | + YGNodeStyleSetDisplay(root_grandchild1, YGDisplayContents); |
| 120 | + |
| 121 | + std::vector<facebook::yoga::Node*> order = { |
| 122 | + facebook::yoga::resolveRef(root_child0), |
| 123 | + facebook::yoga::resolveRef(root_grandchild0), |
| 124 | + facebook::yoga::resolveRef(root_great_grandchild0), |
| 125 | + facebook::yoga::resolveRef(root_great_grandchild1), |
| 126 | + facebook::yoga::resolveRef(root_child2), |
| 127 | + }; |
| 128 | + auto correctOrderIt = order.begin(); |
| 129 | + |
| 130 | + for (auto node : facebook::yoga::resolveRef(root)->getLayoutChildren()) { |
| 131 | + ASSERT_EQ(node, *correctOrderIt); |
| 132 | + correctOrderIt++; |
| 133 | + } |
| 134 | + |
| 135 | + YGNodeFreeRecursive(root); |
| 136 | +} |
| 137 | + |
| 138 | +TEST(YogaTest, layoutable_children_contents_leaf_node) { |
| 139 | + YGNodeRef root = YGNodeNew(); |
| 140 | + |
| 141 | + YGNodeRef root_child0 = YGNodeNew(); |
| 142 | + YGNodeRef root_child1 = YGNodeNew(); |
| 143 | + YGNodeRef root_child2 = YGNodeNew(); |
| 144 | + |
| 145 | + YGNodeInsertChild(root, root_child0, 0); |
| 146 | + YGNodeInsertChild(root, root_child1, 1); |
| 147 | + YGNodeInsertChild(root, root_child2, 2); |
| 148 | + |
| 149 | + YGNodeStyleSetDisplay(root_child1, YGDisplayContents); |
| 150 | + |
| 151 | + std::vector<facebook::yoga::Node*> order = { |
| 152 | + facebook::yoga::resolveRef(root_child0), |
| 153 | + facebook::yoga::resolveRef(root_child2), |
| 154 | + }; |
| 155 | + auto correctOrderIt = order.begin(); |
| 156 | + |
| 157 | + for (auto node : facebook::yoga::resolveRef(root)->getLayoutChildren()) { |
| 158 | + ASSERT_EQ(node, *correctOrderIt); |
| 159 | + correctOrderIt++; |
| 160 | + } |
| 161 | + |
| 162 | + YGNodeFreeRecursive(root); |
| 163 | +} |
| 164 | + |
| 165 | +TEST(YogaTest, layoutable_children_contents_root_node) { |
| 166 | + YGNodeRef root = YGNodeNew(); |
| 167 | + |
| 168 | + YGNodeRef root_child0 = YGNodeNew(); |
| 169 | + YGNodeRef root_child1 = YGNodeNew(); |
| 170 | + YGNodeRef root_child2 = YGNodeNew(); |
| 171 | + |
| 172 | + YGNodeInsertChild(root, root_child0, 0); |
| 173 | + YGNodeInsertChild(root, root_child1, 1); |
| 174 | + YGNodeInsertChild(root, root_child2, 2); |
| 175 | + |
| 176 | + YGNodeStyleSetDisplay(root, YGDisplayContents); |
| 177 | + |
| 178 | + std::vector<facebook::yoga::Node*> order = { |
| 179 | + facebook::yoga::resolveRef(root_child0), |
| 180 | + facebook::yoga::resolveRef(root_child1), |
| 181 | + facebook::yoga::resolveRef(root_child2), |
| 182 | + }; |
| 183 | + auto correctOrderIt = order.begin(); |
| 184 | + |
| 185 | + for (auto node : facebook::yoga::resolveRef(root)->getLayoutChildren()) { |
| 186 | + ASSERT_EQ(node, *correctOrderIt); |
| 187 | + correctOrderIt++; |
| 188 | + } |
| 189 | + |
| 190 | + YGNodeFreeRecursive(root); |
| 191 | +} |
0 commit comments