|
| 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 | +#include <yoga/algorithm/Baseline.h> |
| 12 | +#include <yoga/enums/Align.h> |
| 13 | +#include <yoga/enums/Dimension.h> |
| 14 | +#include <yoga/enums/FlexDirection.h> |
| 15 | +#include <yoga/enums/PhysicalEdge.h> |
| 16 | +#include <yoga/enums/PositionType.h> |
| 17 | +#include <yoga/node/Node.h> |
| 18 | + |
| 19 | +namespace facebook::yoga { |
| 20 | +namespace { |
| 21 | + |
| 22 | +void setMeasuredSize(Node& node, float width, float height) { |
| 23 | + node.setLayoutMeasuredDimension(width, Dimension::Width); |
| 24 | + node.setLayoutMeasuredDimension(height, Dimension::Height); |
| 25 | +} |
| 26 | + |
| 27 | +float baselineFromContext( |
| 28 | + YGNodeConstRef node, |
| 29 | + float /*width*/, |
| 30 | + float /*height*/) { |
| 31 | + return *static_cast<const float*>(resolveRef(node)->getContext()); |
| 32 | +} |
| 33 | + |
| 34 | +TEST(BaselineTest, testCalculateBaselineFallsBackToFirstNonAbsoluteChild) { |
| 35 | + Node root; |
| 36 | + Node absoluteChild; |
| 37 | + Node child; |
| 38 | + |
| 39 | + setMeasuredSize(root, 100.0f, 200.0f); |
| 40 | + setMeasuredSize(absoluteChild, 10.0f, 7.0f); |
| 41 | + setMeasuredSize(child, 30.0f, 20.0f); |
| 42 | + absoluteChild.style().setPositionType(PositionType::Absolute); |
| 43 | + child.setLayoutPosition(30.0f, PhysicalEdge::Top); |
| 44 | + root.setChildren({&absoluteChild, &child}); |
| 45 | + |
| 46 | + EXPECT_FLOAT_EQ(50.0f, calculateBaseline(&root)); |
| 47 | +} |
| 48 | + |
| 49 | +TEST(BaselineTest, testCalculateBaselineUsesReferenceBaselineChild) { |
| 50 | + Node root; |
| 51 | + Node firstChild; |
| 52 | + Node referenceChild; |
| 53 | + float referenceBaseline = 17.0f; |
| 54 | + |
| 55 | + setMeasuredSize(firstChild, 30.0f, 80.0f); |
| 56 | + setMeasuredSize(referenceChild, 30.0f, 40.0f); |
| 57 | + firstChild.setLayoutPosition(5.0f, PhysicalEdge::Top); |
| 58 | + referenceChild.setLayoutPosition(12.0f, PhysicalEdge::Top); |
| 59 | + referenceChild.setContext(&referenceBaseline); |
| 60 | + referenceChild.setBaselineFunc(baselineFromContext); |
| 61 | + referenceChild.setIsReferenceBaseline(true); |
| 62 | + root.setChildren({&firstChild, &referenceChild}); |
| 63 | + |
| 64 | + EXPECT_FLOAT_EQ(29.0f, calculateBaseline(&root)); |
| 65 | +} |
| 66 | + |
| 67 | +TEST(BaselineTest, testCalculateBaselineIgnoresChildrenAfterFirstLine) { |
| 68 | + Node root; |
| 69 | + Node firstLineChild; |
| 70 | + Node secondLineChild; |
| 71 | + float secondLineBaseline = 3.0f; |
| 72 | + |
| 73 | + setMeasuredSize(firstLineChild, 30.0f, 20.0f); |
| 74 | + setMeasuredSize(secondLineChild, 30.0f, 10.0f); |
| 75 | + firstLineChild.setLayoutPosition(4.0f, PhysicalEdge::Top); |
| 76 | + secondLineChild.setLayoutPosition(100.0f, PhysicalEdge::Top); |
| 77 | + secondLineChild.setLineIndex(1); |
| 78 | + secondLineChild.setContext(&secondLineBaseline); |
| 79 | + secondLineChild.setBaselineFunc(baselineFromContext); |
| 80 | + secondLineChild.setIsReferenceBaseline(true); |
| 81 | + root.setChildren({&firstLineChild, &secondLineChild}); |
| 82 | + |
| 83 | + EXPECT_FLOAT_EQ(24.0f, calculateBaseline(&root)); |
| 84 | +} |
| 85 | + |
| 86 | +TEST(BaselineTest, testIsBaselineLayoutRequiresRowAndParticipatingChild) { |
| 87 | + Node root; |
| 88 | + Node child; |
| 89 | + |
| 90 | + root.style().setFlexDirection(FlexDirection::Row); |
| 91 | + child.style().setAlignSelf(Align::Baseline); |
| 92 | + root.setChildren({&child}); |
| 93 | + |
| 94 | + EXPECT_TRUE(isBaselineLayout(&root)); |
| 95 | + |
| 96 | + child.style().setPositionType(PositionType::Absolute); |
| 97 | + EXPECT_FALSE(isBaselineLayout(&root)); |
| 98 | + |
| 99 | + child.style().setPositionType(PositionType::Relative); |
| 100 | + root.style().setFlexDirection(FlexDirection::Column); |
| 101 | + root.style().setAlignItems(Align::Baseline); |
| 102 | + EXPECT_FALSE(isBaselineLayout(&root)); |
| 103 | +} |
| 104 | + |
| 105 | +} // namespace |
| 106 | +} // namespace facebook::yoga |
0 commit comments