From 6fed4dfe3053e9b3a6aac354dc8b0915976f85ec Mon Sep 17 00:00:00 2001 From: Jakub Piasecki Date: Fri, 25 Oct 2024 18:01:20 -0700 Subject: [PATCH] Add tests for `LayoutableChildren` iterator (#1731) Summary: Adds unit tests that directly cover the order in which `LayoutableChildren` iterator goes over the descendant nodes. The covered cases are as follows (nodes with `display: contents` are marked green): ### Single `display: contents` node ```mermaid flowchart TD R((R)) --> A((A)) R --> B((B)) R --> C((C)) B --> D((D)) B --> E((E)) style B fill:https://github.com/facebook/yoga/issues/090 ``` Correct order: `A, D, E, C` ### Multiple `display: contents` nodes ```mermaid flowchart TD R((R)) --> A((A)) R --> B((B)) R --> C((C)) A --> D((D)) A --> E((E)) B --> F((F)) B --> G((G)) C --> H((H)) C --> I((I)) style A fill:https://github.com/facebook/yoga/issues/090 style B fill:https://github.com/facebook/yoga/issues/090 style C fill:https://github.com/facebook/yoga/issues/090 ``` Correct order: `D, E, F, G, H, I` ### Nested `display: contents` nodes ```mermaid flowchart TD R((R)) --> A((A)) R --> B((B)) R --> C((C)) B --> D((D)) B --> E((E)) E --> F((F)) E --> G((G)) style B fill:https://github.com/facebook/yoga/issues/090 style E fill:https://github.com/facebook/yoga/issues/090 ``` Correct order: `A, D, F, G, C` ### Leaf `display: contents` node ```mermaid flowchart TD R((R)) --> A((A)) R --> B((B)) R --> C((C)) style B fill:https://github.com/facebook/yoga/issues/090 ``` Correct order: `A, C` ### Root `display: contents` node ```mermaid flowchart TD R((R)) --> A((A)) R --> B((B)) R --> C((C)) style R fill:https://github.com/facebook/yoga/issues/090 ``` Correct order: `A, B, C` - `LayoutableChildren` goes over the children with `display: contents` property, setting it on the root node should have no effect. Changelog: [Internal] Pull Request resolved: https://github.com/facebook/yoga/pull/1731 Reviewed By: joevilches Differential Revision: D64981779 Pulled By: NickGerleman fbshipit-source-id: ee39759c663a40f96ad313f1b775d53ab68fb442 --- tests/YGLayoutableChildrenTest.cpp | 191 +++++++++++++++++++++++++++++ yoga/node/LayoutableChildren.h | 1 + 2 files changed, 192 insertions(+) create mode 100644 tests/YGLayoutableChildrenTest.cpp diff --git a/tests/YGLayoutableChildrenTest.cpp b/tests/YGLayoutableChildrenTest.cpp new file mode 100644 index 00000000..1a3a3f60 --- /dev/null +++ b/tests/YGLayoutableChildrenTest.cpp @@ -0,0 +1,191 @@ +/* + * 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 +#include +#include +#include + +TEST(YogaTest, layoutable_children_single_contents_node) { + YGNodeRef root = YGNodeNew(); + + YGNodeRef root_child0 = YGNodeNew(); + YGNodeRef root_child1 = YGNodeNew(); + YGNodeRef root_child2 = YGNodeNew(); + + YGNodeRef root_grandchild0 = YGNodeNew(); + YGNodeRef root_grandchild1 = YGNodeNew(); + + YGNodeInsertChild(root, root_child0, 0); + YGNodeInsertChild(root, root_child1, 1); + YGNodeInsertChild(root, root_child2, 2); + + YGNodeInsertChild(root_child1, root_grandchild0, 0); + YGNodeInsertChild(root_child1, root_grandchild1, 1); + + YGNodeStyleSetDisplay(root_child1, YGDisplayContents); + + std::vector order = { + facebook::yoga::resolveRef(root_child0), + facebook::yoga::resolveRef(root_grandchild0), + facebook::yoga::resolveRef(root_grandchild1), + facebook::yoga::resolveRef(root_child2), + }; + auto correctOrderIt = order.begin(); + + for (auto node : facebook::yoga::resolveRef(root)->getLayoutChildren()) { + ASSERT_EQ(node, *correctOrderIt); + correctOrderIt++; + } + + YGNodeFreeRecursive(root); +} + +TEST(YogaTest, layoutable_children_multiple_contents_nodes) { + YGNodeRef root = YGNodeNew(); + + YGNodeRef root_child0 = YGNodeNew(); + YGNodeRef root_child1 = YGNodeNew(); + YGNodeRef root_child2 = YGNodeNew(); + + YGNodeRef root_grandchild0 = YGNodeNew(); + YGNodeRef root_grandchild1 = YGNodeNew(); + YGNodeRef root_grandchild2 = YGNodeNew(); + YGNodeRef root_grandchild3 = YGNodeNew(); + YGNodeRef root_grandchild4 = YGNodeNew(); + YGNodeRef root_grandchild5 = YGNodeNew(); + + YGNodeInsertChild(root, root_child0, 0); + YGNodeInsertChild(root, root_child1, 1); + YGNodeInsertChild(root, root_child2, 2); + + YGNodeInsertChild(root_child0, root_grandchild0, 0); + YGNodeInsertChild(root_child0, root_grandchild1, 1); + YGNodeInsertChild(root_child1, root_grandchild2, 0); + YGNodeInsertChild(root_child1, root_grandchild3, 1); + YGNodeInsertChild(root_child2, root_grandchild4, 0); + YGNodeInsertChild(root_child2, root_grandchild5, 1); + + YGNodeStyleSetDisplay(root_child0, YGDisplayContents); + YGNodeStyleSetDisplay(root_child1, YGDisplayContents); + YGNodeStyleSetDisplay(root_child2, YGDisplayContents); + + std::vector order = { + facebook::yoga::resolveRef(root_grandchild0), + facebook::yoga::resolveRef(root_grandchild1), + facebook::yoga::resolveRef(root_grandchild2), + facebook::yoga::resolveRef(root_grandchild3), + facebook::yoga::resolveRef(root_grandchild4), + facebook::yoga::resolveRef(root_grandchild5), + }; + auto correctOrderIt = order.begin(); + + for (auto node : facebook::yoga::resolveRef(root)->getLayoutChildren()) { + ASSERT_EQ(node, *correctOrderIt); + correctOrderIt++; + } + + YGNodeFreeRecursive(root); +} + +TEST(YogaTest, layoutable_children_nested_contents_nodes) { + YGNodeRef root = YGNodeNew(); + + YGNodeRef root_child0 = YGNodeNew(); + YGNodeRef root_child1 = YGNodeNew(); + YGNodeRef root_child2 = YGNodeNew(); + + YGNodeRef root_grandchild0 = YGNodeNew(); + YGNodeRef root_grandchild1 = YGNodeNew(); + + YGNodeRef root_great_grandchild0 = YGNodeNew(); + YGNodeRef root_great_grandchild1 = YGNodeNew(); + + YGNodeInsertChild(root, root_child0, 0); + YGNodeInsertChild(root, root_child1, 1); + YGNodeInsertChild(root, root_child2, 2); + + YGNodeInsertChild(root_child1, root_grandchild0, 0); + YGNodeInsertChild(root_child1, root_grandchild1, 1); + + YGNodeInsertChild(root_grandchild1, root_great_grandchild0, 0); + YGNodeInsertChild(root_grandchild1, root_great_grandchild1, 1); + + YGNodeStyleSetDisplay(root_child1, YGDisplayContents); + YGNodeStyleSetDisplay(root_grandchild1, YGDisplayContents); + + std::vector order = { + facebook::yoga::resolveRef(root_child0), + facebook::yoga::resolveRef(root_grandchild0), + facebook::yoga::resolveRef(root_great_grandchild0), + facebook::yoga::resolveRef(root_great_grandchild1), + facebook::yoga::resolveRef(root_child2), + }; + auto correctOrderIt = order.begin(); + + for (auto node : facebook::yoga::resolveRef(root)->getLayoutChildren()) { + ASSERT_EQ(node, *correctOrderIt); + correctOrderIt++; + } + + YGNodeFreeRecursive(root); +} + +TEST(YogaTest, layoutable_children_contents_leaf_node) { + YGNodeRef root = YGNodeNew(); + + YGNodeRef root_child0 = YGNodeNew(); + YGNodeRef root_child1 = YGNodeNew(); + YGNodeRef root_child2 = YGNodeNew(); + + YGNodeInsertChild(root, root_child0, 0); + YGNodeInsertChild(root, root_child1, 1); + YGNodeInsertChild(root, root_child2, 2); + + YGNodeStyleSetDisplay(root_child1, YGDisplayContents); + + std::vector order = { + facebook::yoga::resolveRef(root_child0), + facebook::yoga::resolveRef(root_child2), + }; + auto correctOrderIt = order.begin(); + + for (auto node : facebook::yoga::resolveRef(root)->getLayoutChildren()) { + ASSERT_EQ(node, *correctOrderIt); + correctOrderIt++; + } + + YGNodeFreeRecursive(root); +} + +TEST(YogaTest, layoutable_children_contents_root_node) { + YGNodeRef root = YGNodeNew(); + + YGNodeRef root_child0 = YGNodeNew(); + YGNodeRef root_child1 = YGNodeNew(); + YGNodeRef root_child2 = YGNodeNew(); + + YGNodeInsertChild(root, root_child0, 0); + YGNodeInsertChild(root, root_child1, 1); + YGNodeInsertChild(root, root_child2, 2); + + YGNodeStyleSetDisplay(root, YGDisplayContents); + + std::vector order = { + facebook::yoga::resolveRef(root_child0), + facebook::yoga::resolveRef(root_child1), + facebook::yoga::resolveRef(root_child2), + }; + auto correctOrderIt = order.begin(); + + for (auto node : facebook::yoga::resolveRef(root)->getLayoutChildren()) { + ASSERT_EQ(node, *correctOrderIt); + correctOrderIt++; + } + + YGNodeFreeRecursive(root); +} diff --git a/yoga/node/LayoutableChildren.h b/yoga/node/LayoutableChildren.h index c8300be3..7d5598fb 100644 --- a/yoga/node/LayoutableChildren.h +++ b/yoga/node/LayoutableChildren.h @@ -4,6 +4,7 @@ * This source code is licensed under the MIT license found in the * LICENSE file in the root directory of this source tree. */ +#pragma once #include #include