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
|
/*
* 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>
TEST(YogaTest, scale_change_invalidates_layout) {
YGConfigRef config = YGConfigNew();
YGNodeRef root = YGNodeNewWithConfig(config);
YGConfigSetPointScaleFactor(config, 1.0f);
YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
YGNodeStyleSetWidth(root, 50);
YGNodeStyleSetHeight(root, 50);
YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeStyleSetFlexGrow(root_child0, 1);
YGNodeInsertChild(root, root_child0, 0);
YGNodeRef root_child1 = YGNodeNewWithConfig(config);
YGNodeStyleSetFlexGrow(root_child1, 1);
YGNodeInsertChild(root, root_child1, 1);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_FLOAT_EQ(25, YGNodeLayoutGetLeft(root_child1));
YGConfigSetPointScaleFactor(config, 1.5f);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0));
// Left should change due to pixel alignment of new scale factor
ASSERT_FLOAT_EQ(25.333334f, YGNodeLayoutGetLeft(root_child1));
YGNodeFreeRecursive(root);
YGConfigFree(config);
}
TEST(YogaTest, errata_config_change_relayout) {
YGConfig* config = YGConfigNew();
YGConfigSetErrata(config, YGErrataStretchFlexBasis);
YGNodeRef root = YGNodeNewWithConfig(config);
YGNodeStyleSetWidth(root, 500);
YGNodeStyleSetHeight(root, 500);
YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeStyleSetAlignItems(root_child0, YGAlignFlexStart);
YGNodeInsertChild(root, root_child0, 0);
YGNodeRef root_child0_child0 = YGNodeNewWithConfig(config);
YGNodeStyleSetFlexGrow(root_child0_child0, 1);
YGNodeStyleSetFlexShrink(root_child0_child0, 1);
YGNodeInsertChild(root_child0, root_child0_child0, 0);
YGNodeRef root_child0_child0_child0 = YGNodeNewWithConfig(config);
YGNodeStyleSetFlexGrow(root_child0_child0_child0, 1);
YGNodeStyleSetFlexShrink(root_child0_child0_child0, 1);
YGNodeInsertChild(root_child0_child0, root_child0_child0_child0, 0);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root));
ASSERT_FLOAT_EQ(500, YGNodeLayoutGetWidth(root));
ASSERT_FLOAT_EQ(500, YGNodeLayoutGetHeight(root));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_FLOAT_EQ(500, YGNodeLayoutGetWidth(root_child0));
ASSERT_FLOAT_EQ(500, YGNodeLayoutGetHeight(root_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetWidth(root_child0_child0));
ASSERT_FLOAT_EQ(500, YGNodeLayoutGetHeight(root_child0_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0_child0_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0_child0_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetWidth(root_child0_child0_child0));
ASSERT_FLOAT_EQ(500, YGNodeLayoutGetHeight(root_child0_child0_child0));
YGConfigSetErrata(config, YGErrataNone);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root));
ASSERT_FLOAT_EQ(500, YGNodeLayoutGetWidth(root));
ASSERT_FLOAT_EQ(500, YGNodeLayoutGetHeight(root));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0));
ASSERT_FLOAT_EQ(500, YGNodeLayoutGetWidth(root_child0));
// This should be modified by the lack of the errata
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetHeight(root_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetWidth(root_child0_child0));
// This should be modified by the lack of the errata
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetHeight(root_child0_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0_child0_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0_child0_child0));
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetWidth(root_child0_child0_child0));
// This should be modified by the lack of the errata
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetHeight(root_child0_child0_child0));
YGNodeFreeRecursive(root);
YGConfigFree(config);
}
TEST(YogaTest, setting_compatible_config_maintains_layout_cache) {
static uint32_t measureCallCount = 0;
auto measureCustom = [](YGNodeConstRef /*node*/,
float /*width*/,
YGMeasureMode /*widthMode*/,
float /*height*/,
YGMeasureMode /*heightMode*/) {
measureCallCount++;
return YGSize{
.width = 25.0f,
.height = 25.0f,
};
};
YGConfigRef config = YGConfigNew();
YGNodeRef root = YGNodeNewWithConfig(config);
YGConfigSetPointScaleFactor(config, 1.0f);
YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow);
YGNodeStyleSetWidth(root, 50);
YGNodeStyleSetHeight(root, 50);
YGNodeRef root_child0 = YGNodeNewWithConfig(config);
EXPECT_EQ(0, measureCallCount);
YGNodeSetMeasureFunc(root_child0, measureCustom);
YGNodeInsertChild(root, root_child0, 0);
YGNodeRef root_child1 = YGNodeNewWithConfig(config);
YGNodeStyleSetFlexGrow(root_child1, 1);
YGNodeInsertChild(root, root_child1, 1);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
EXPECT_EQ(1, measureCallCount);
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_FLOAT_EQ(25, YGNodeLayoutGetLeft(root_child1));
YGConfigRef config2 = YGConfigNew();
// Calling YGConfigSetPointScaleFactor multiple times, ensures that config2
// gets a different config version that config1
YGConfigSetPointScaleFactor(config2, 1.0f);
YGConfigSetPointScaleFactor(config2, 1.5f);
YGConfigSetPointScaleFactor(config2, 1.0f);
YGNodeSetConfig(root, config2);
YGNodeSetConfig(root_child0, config2);
YGNodeSetConfig(root_child1, config2);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
// Measure should not be called again, as layout should have been cached since
// config is functionally the same as before
EXPECT_EQ(1, measureCallCount);
ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0));
ASSERT_FLOAT_EQ(25, YGNodeLayoutGetLeft(root_child1));
YGNodeFreeRecursive(root);
YGConfigFree(config);
YGConfigFree(config2);
}
|