|
| 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 | + |
| 10 | +#include <yoga/Yoga.h> |
| 11 | + |
| 12 | +namespace { |
| 13 | + |
| 14 | +class FlexLineTest : public ::testing::Test { |
| 15 | + protected: |
| 16 | + void SetUp() override { |
| 17 | + config_ = YGConfigNew(); |
| 18 | + YGConfigSetPointScaleFactor(config_, 0.0f); |
| 19 | + root_ = YGNodeNewWithConfig(config_); |
| 20 | + YGNodeStyleSetFlexDirection(root_, YGFlexDirectionRow); |
| 21 | + } |
| 22 | + |
| 23 | + void TearDown() override { |
| 24 | + YGNodeFreeRecursive(root_); |
| 25 | + YGConfigFree(config_); |
| 26 | + } |
| 27 | + |
| 28 | + YGNodeRef appendChildWithSize(float width, float height) { |
| 29 | + auto child = YGNodeNewWithConfig(config_); |
| 30 | + YGNodeStyleSetWidth(child, width); |
| 31 | + YGNodeStyleSetHeight(child, height); |
| 32 | + YGNodeInsertChild(root_, child, YGNodeGetChildCount(root_)); |
| 33 | + return child; |
| 34 | + } |
| 35 | + |
| 36 | + void calculateLayout(float width) { |
| 37 | + YGNodeStyleSetWidth(root_, width); |
| 38 | + YGNodeCalculateLayout(root_, YGUndefined, YGUndefined, YGDirectionLTR); |
| 39 | + } |
| 40 | + |
| 41 | + YGConfigRef config_{nullptr}; |
| 42 | + YGNodeRef root_{nullptr}; |
| 43 | +}; |
| 44 | + |
| 45 | +TEST_F(FlexLineTest, testCalculateFlexLineStopsBeforeWrappedOverflow) { |
| 46 | + YGNodeStyleSetFlexWrap(root_, YGWrapWrap); |
| 47 | + YGNodeStyleSetGap(root_, YGGutterColumn, 10.0f); |
| 48 | + |
| 49 | + auto firstChild = appendChildWithSize(40.0f, 10.0f); |
| 50 | + auto secondChild = appendChildWithSize(40.0f, 20.0f); |
| 51 | + auto overflowingChild = appendChildWithSize(20.0f, 30.0f); |
| 52 | + |
| 53 | + calculateLayout(90.0f); |
| 54 | + |
| 55 | + EXPECT_FLOAT_EQ(YGNodeLayoutGetLeft(firstChild), 0.0f); |
| 56 | + EXPECT_FLOAT_EQ(YGNodeLayoutGetTop(firstChild), 0.0f); |
| 57 | + EXPECT_FLOAT_EQ(YGNodeLayoutGetLeft(secondChild), 50.0f); |
| 58 | + EXPECT_FLOAT_EQ(YGNodeLayoutGetTop(secondChild), 0.0f); |
| 59 | + EXPECT_FLOAT_EQ(YGNodeLayoutGetLeft(overflowingChild), 0.0f); |
| 60 | + EXPECT_FLOAT_EQ(YGNodeLayoutGetTop(overflowingChild), 20.0f); |
| 61 | +} |
| 62 | + |
| 63 | +TEST_F(FlexLineTest, testCalculateFlexLineSkipsNonFlowChildren) { |
| 64 | + YGNodeStyleSetGap(root_, YGGutterColumn, 5.0f); |
| 65 | + |
| 66 | + auto displayNoneChild = appendChildWithSize(100.0f, 10.0f); |
| 67 | + YGNodeStyleSetDisplay(displayNoneChild, YGDisplayNone); |
| 68 | + |
| 69 | + auto absoluteChild = appendChildWithSize(100.0f, 10.0f); |
| 70 | + YGNodeStyleSetPositionType(absoluteChild, YGPositionTypeAbsolute); |
| 71 | + |
| 72 | + auto firstInFlowChild = appendChildWithSize(30.0f, 10.0f); |
| 73 | + auto secondInFlowChild = appendChildWithSize(20.0f, 10.0f); |
| 74 | + YGNodeStyleSetMargin(secondInFlowChild, YGEdgeLeft, 4.0f); |
| 75 | + YGNodeStyleSetMargin(secondInFlowChild, YGEdgeRight, 6.0f); |
| 76 | + |
| 77 | + calculateLayout(500.0f); |
| 78 | + |
| 79 | + EXPECT_FLOAT_EQ(YGNodeLayoutGetLeft(firstInFlowChild), 0.0f); |
| 80 | + EXPECT_FLOAT_EQ(YGNodeLayoutGetWidth(firstInFlowChild), 30.0f); |
| 81 | + EXPECT_FLOAT_EQ(YGNodeLayoutGetLeft(secondInFlowChild), 39.0f); |
| 82 | + EXPECT_FLOAT_EQ(YGNodeLayoutGetWidth(secondInFlowChild), 20.0f); |
| 83 | + EXPECT_FLOAT_EQ(YGNodeLayoutGetWidth(displayNoneChild), 0.0f); |
| 84 | + EXPECT_FLOAT_EQ(YGNodeLayoutGetLeft(absoluteChild), 0.0f); |
| 85 | +} |
| 86 | + |
| 87 | +TEST_F(FlexLineTest, testCalculateFlexLineFloorsTotalGrowFactor) { |
| 88 | + auto firstChild = appendChildWithSize(10.0f, 10.0f); |
| 89 | + YGNodeStyleSetFlexGrow(firstChild, 0.25f); |
| 90 | + YGNodeStyleSetFlexBasis(firstChild, 10.0f); |
| 91 | + |
| 92 | + auto secondChild = appendChildWithSize(20.0f, 10.0f); |
| 93 | + YGNodeStyleSetFlexGrow(secondChild, 0.5f); |
| 94 | + YGNodeStyleSetFlexBasis(secondChild, 20.0f); |
| 95 | + |
| 96 | + calculateLayout(100.0f); |
| 97 | + |
| 98 | + EXPECT_FLOAT_EQ(YGNodeLayoutGetWidth(firstChild), 27.5f); |
| 99 | + EXPECT_FLOAT_EQ(YGNodeLayoutGetLeft(secondChild), 27.5f); |
| 100 | + EXPECT_FLOAT_EQ(YGNodeLayoutGetWidth(secondChild), 55.0f); |
| 101 | +} |
| 102 | + |
| 103 | +} // namespace |
0 commit comments