|
| 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 | + |
| 11 | +TEST(YogaTest, scale_change_invalidates_layout) { |
| 12 | + YGConfigRef config = YGConfigNew(); |
| 13 | + |
| 14 | + YGNodeRef root = YGNodeNewWithConfig(config); |
| 15 | + YGConfigSetPointScaleFactor(config, 1.0f); |
| 16 | + |
| 17 | + YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow); |
| 18 | + YGNodeStyleSetWidth(root, 50); |
| 19 | + YGNodeStyleSetHeight(root, 50); |
| 20 | + |
| 21 | + YGNodeRef root_child0 = YGNodeNewWithConfig(config); |
| 22 | + YGNodeStyleSetFlexGrow(root_child0, 1); |
| 23 | + YGNodeInsertChild(root, root_child0, 0); |
| 24 | + |
| 25 | + YGNodeRef root_child1 = YGNodeNewWithConfig(config); |
| 26 | + YGNodeStyleSetFlexGrow(root_child1, 1); |
| 27 | + YGNodeInsertChild(root, root_child1, 1); |
| 28 | + |
| 29 | + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); |
| 30 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0)); |
| 31 | + ASSERT_FLOAT_EQ(25, YGNodeLayoutGetLeft(root_child1)); |
| 32 | + |
| 33 | + YGConfigSetPointScaleFactor(config, 1.5f); |
| 34 | + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); |
| 35 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0)); |
| 36 | + // Left should change due to pixel alignment of new scale factor |
| 37 | + ASSERT_FLOAT_EQ(25.333334f, YGNodeLayoutGetLeft(root_child1)); |
| 38 | + |
| 39 | + YGNodeFreeRecursive(root); |
| 40 | + YGConfigFree(config); |
| 41 | +} |
| 42 | + |
| 43 | +TEST(YogaTest, errata_config_change_relayout) { |
| 44 | + YGConfig* config = YGConfigNew(); |
| 45 | + YGConfigSetErrata(config, YGErrataStretchFlexBasis); |
| 46 | + YGNodeRef root = YGNodeNewWithConfig(config); |
| 47 | + YGNodeStyleSetWidth(root, 500); |
| 48 | + YGNodeStyleSetHeight(root, 500); |
| 49 | + |
| 50 | + YGNodeRef root_child0 = YGNodeNewWithConfig(config); |
| 51 | + YGNodeStyleSetAlignItems(root_child0, YGAlignFlexStart); |
| 52 | + YGNodeInsertChild(root, root_child0, 0); |
| 53 | + |
| 54 | + YGNodeRef root_child0_child0 = YGNodeNewWithConfig(config); |
| 55 | + YGNodeStyleSetFlexGrow(root_child0_child0, 1); |
| 56 | + YGNodeStyleSetFlexShrink(root_child0_child0, 1); |
| 57 | + YGNodeInsertChild(root_child0, root_child0_child0, 0); |
| 58 | + |
| 59 | + YGNodeRef root_child0_child0_child0 = YGNodeNewWithConfig(config); |
| 60 | + YGNodeStyleSetFlexGrow(root_child0_child0_child0, 1); |
| 61 | + YGNodeStyleSetFlexShrink(root_child0_child0_child0, 1); |
| 62 | + YGNodeInsertChild(root_child0_child0, root_child0_child0_child0, 0); |
| 63 | + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); |
| 64 | + |
| 65 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root)); |
| 66 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root)); |
| 67 | + ASSERT_FLOAT_EQ(500, YGNodeLayoutGetWidth(root)); |
| 68 | + ASSERT_FLOAT_EQ(500, YGNodeLayoutGetHeight(root)); |
| 69 | + |
| 70 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0)); |
| 71 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0)); |
| 72 | + ASSERT_FLOAT_EQ(500, YGNodeLayoutGetWidth(root_child0)); |
| 73 | + ASSERT_FLOAT_EQ(500, YGNodeLayoutGetHeight(root_child0)); |
| 74 | + |
| 75 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0_child0)); |
| 76 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0_child0)); |
| 77 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetWidth(root_child0_child0)); |
| 78 | + ASSERT_FLOAT_EQ(500, YGNodeLayoutGetHeight(root_child0_child0)); |
| 79 | + |
| 80 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0_child0_child0)); |
| 81 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0_child0_child0)); |
| 82 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetWidth(root_child0_child0_child0)); |
| 83 | + ASSERT_FLOAT_EQ(500, YGNodeLayoutGetHeight(root_child0_child0_child0)); |
| 84 | + |
| 85 | + YGConfigSetErrata(config, YGErrataNone); |
| 86 | + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); |
| 87 | + |
| 88 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root)); |
| 89 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root)); |
| 90 | + ASSERT_FLOAT_EQ(500, YGNodeLayoutGetWidth(root)); |
| 91 | + ASSERT_FLOAT_EQ(500, YGNodeLayoutGetHeight(root)); |
| 92 | + |
| 93 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0)); |
| 94 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0)); |
| 95 | + ASSERT_FLOAT_EQ(500, YGNodeLayoutGetWidth(root_child0)); |
| 96 | + // This should be modified by the lack of the errata |
| 97 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetHeight(root_child0)); |
| 98 | + |
| 99 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0_child0)); |
| 100 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0_child0)); |
| 101 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetWidth(root_child0_child0)); |
| 102 | + // This should be modified by the lack of the errata |
| 103 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetHeight(root_child0_child0)); |
| 104 | + |
| 105 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0_child0_child0)); |
| 106 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0_child0_child0)); |
| 107 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetWidth(root_child0_child0_child0)); |
| 108 | + // This should be modified by the lack of the errata |
| 109 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetHeight(root_child0_child0_child0)); |
| 110 | + |
| 111 | + YGNodeFreeRecursive(root); |
| 112 | + |
| 113 | + YGConfigFree(config); |
| 114 | +} |
| 115 | + |
| 116 | +TEST(YogaTest, setting_compatible_config_maintains_layout_cache) { |
| 117 | + static uint32_t measureCallCount = 0; |
| 118 | + auto measureCustom = [](YGNodeConstRef /*node*/, |
| 119 | + float /*width*/, |
| 120 | + YGMeasureMode /*widthMode*/, |
| 121 | + float /*height*/, |
| 122 | + YGMeasureMode /*heightMode*/) { |
| 123 | + measureCallCount++; |
| 124 | + return YGSize{ |
| 125 | + .width = 25.0f, |
| 126 | + .height = 25.0f, |
| 127 | + }; |
| 128 | + }; |
| 129 | + |
| 130 | + YGConfigRef config = YGConfigNew(); |
| 131 | + |
| 132 | + YGNodeRef root = YGNodeNewWithConfig(config); |
| 133 | + YGConfigSetPointScaleFactor(config, 1.0f); |
| 134 | + |
| 135 | + YGNodeStyleSetFlexDirection(root, YGFlexDirectionRow); |
| 136 | + YGNodeStyleSetWidth(root, 50); |
| 137 | + YGNodeStyleSetHeight(root, 50); |
| 138 | + |
| 139 | + YGNodeRef root_child0 = YGNodeNewWithConfig(config); |
| 140 | + EXPECT_EQ(0, measureCallCount); |
| 141 | + |
| 142 | + YGNodeSetMeasureFunc(root_child0, measureCustom); |
| 143 | + YGNodeInsertChild(root, root_child0, 0); |
| 144 | + |
| 145 | + YGNodeRef root_child1 = YGNodeNewWithConfig(config); |
| 146 | + YGNodeStyleSetFlexGrow(root_child1, 1); |
| 147 | + YGNodeInsertChild(root, root_child1, 1); |
| 148 | + |
| 149 | + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); |
| 150 | + EXPECT_EQ(1, measureCallCount); |
| 151 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0)); |
| 152 | + ASSERT_FLOAT_EQ(25, YGNodeLayoutGetLeft(root_child1)); |
| 153 | + |
| 154 | + YGConfigRef config2 = YGConfigNew(); |
| 155 | + // Calling YGConfigSetPointScaleFactor multiple times, ensures that config2 |
| 156 | + // gets a different config version that config1 |
| 157 | + YGConfigSetPointScaleFactor(config2, 1.0f); |
| 158 | + YGConfigSetPointScaleFactor(config2, 1.5f); |
| 159 | + YGConfigSetPointScaleFactor(config2, 1.0f); |
| 160 | + |
| 161 | + YGNodeSetConfig(root, config2); |
| 162 | + YGNodeSetConfig(root_child0, config2); |
| 163 | + YGNodeSetConfig(root_child1, config2); |
| 164 | + |
| 165 | + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); |
| 166 | + |
| 167 | + // Measure should not be called again, as layout should have been cached since |
| 168 | + // config is functionally the same as before |
| 169 | + EXPECT_EQ(1, measureCallCount); |
| 170 | + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0)); |
| 171 | + ASSERT_FLOAT_EQ(25, YGNodeLayoutGetLeft(root_child1)); |
| 172 | + |
| 173 | + YGNodeFreeRecursive(root); |
| 174 | + YGConfigFree(config); |
| 175 | + YGConfigFree(config2); |
| 176 | +} |
0 commit comments