File: YGCloneNodeTest.cpp

package info (click to toggle)
yoga 3.2.1%2Bds-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 984 kB
  • sloc: cpp: 12,985; python: 260; sh: 32; makefile: 6
file content (89 lines) | stat: -rw-r--r-- 3,248 bytes parent folder | download | duplicates (2)
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
/*
 * 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 <gtest/gtest.h>
#include <yoga/Yoga.h>

static void recursivelyAssertProperNodeOwnership(YGNodeRef node) {
  for (size_t i = 0; i < YGNodeGetChildCount(node); ++i) {
    const auto child = YGNodeGetChild(node, i);
    ASSERT_EQ(node, YGNodeGetOwner(child));
    recursivelyAssertProperNodeOwnership(child);
  }
}

TEST(YogaTest, absolute_node_cloned_with_static_parent) {
  YGNodeRef root = YGNodeNew();
  YGNodeStyleSetWidth(root, 100);
  YGNodeStyleSetHeight(root, 100);

  YGNodeRef root_child0 = YGNodeNew();
  YGNodeStyleSetPositionType(root_child0, YGPositionTypeStatic);
  YGNodeStyleSetWidth(root_child0, 10);
  YGNodeStyleSetHeight(root_child0, 10);
  YGNodeInsertChild(root, root_child0, 0);

  YGNodeRef root_child0_child0 = YGNodeNew();
  YGNodeStyleSetPositionType(root_child0_child0, YGPositionTypeAbsolute);
  YGNodeStyleSetWidthPercent(root_child0_child0, 1);
  YGNodeStyleSetHeight(root_child0_child0, 1);
  YGNodeInsertChild(root_child0, root_child0_child0, 0);

  YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

  YGNodeRef clonedRoot = YGNodeClone(root);
  YGNodeStyleSetWidth(clonedRoot, 110);
  YGNodeCalculateLayout(clonedRoot, YGUndefined, YGUndefined, YGDirectionLTR);

  recursivelyAssertProperNodeOwnership(clonedRoot);

  YGNodeFreeRecursive(root);
  YGNodeFreeRecursive(clonedRoot);
}

TEST(YogaTest, absolute_node_cloned_with_static_ancestors) {
  YGNodeRef root = YGNodeNew();
  YGNodeStyleSetWidth(root, 100);
  YGNodeStyleSetHeight(root, 100);

  YGNodeRef root_child0 = YGNodeNew();
  YGNodeStyleSetPositionType(root_child0, YGPositionTypeStatic);
  YGNodeStyleSetWidth(root_child0, 50);
  YGNodeStyleSetHeight(root_child0, 50);
  YGNodeInsertChild(root, root_child0, 0);

  YGNodeRef root_child0_child0 = YGNodeNew();
  YGNodeStyleSetPositionType(root_child0_child0, YGPositionTypeStatic);
  YGNodeStyleSetWidth(root_child0_child0, 40);
  YGNodeStyleSetHeight(root_child0_child0, 40);
  YGNodeInsertChild(root_child0, root_child0_child0, 0);

  YGNodeRef root_child0_child0_child0 = YGNodeNew();
  YGNodeStyleSetPositionType(root_child0_child0_child0, YGPositionTypeStatic);
  YGNodeStyleSetWidth(root_child0_child0_child0, 30);
  YGNodeStyleSetHeight(root_child0_child0_child0, 30);
  YGNodeInsertChild(root_child0_child0, root_child0_child0_child0, 0);

  YGNodeRef root_child0_child0_child0_child0 = YGNodeNew();
  YGNodeStyleSetPositionType(
      root_child0_child0_child0_child0, YGPositionTypeAbsolute);
  YGNodeStyleSetWidthPercent(root_child0_child0_child0_child0, 1);
  YGNodeStyleSetHeight(root_child0_child0_child0_child0, 1);
  YGNodeInsertChild(
      root_child0_child0_child0, root_child0_child0_child0_child0, 0);

  YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);

  YGNodeRef clonedRoot = YGNodeClone(root);
  YGNodeStyleSetWidth(clonedRoot, 110);
  YGNodeCalculateLayout(clonedRoot, YGUndefined, YGUndefined, YGDirectionLTR);

  recursivelyAssertProperNodeOwnership(clonedRoot);

  YGNodeFreeRecursive(root);
  YGNodeFreeRecursive(clonedRoot);
}