From 5960dd888da3bb8d5300830c206a8e89ac2af6e3 Mon Sep 17 00:00:00 2001 From: Sidharth Guglani Date: Wed, 13 Nov 2019 07:24:16 -0800 Subject: [PATCH] Add test for margin top not calculated correctly if defined in percentage Summary: Unit tests for margin percent scenario where height was being used instead of width ## Changelog: [Internal] [Yoga] - Added unit tests for margin percent Reviewed By: alickbass Differential Revision: D18448365 fbshipit-source-id: f4404b0c493938fc04dc685e97effa08bfd553b0 --- tests/YGMinMaxDimensionTest.cpp | 79 +++++++++++++++++++++++++++++++++ tests/YGPercentageTest.cpp | 78 ++++++++++++++++++++++++++++++++ 2 files changed, 157 insertions(+) diff --git a/tests/YGMinMaxDimensionTest.cpp b/tests/YGMinMaxDimensionTest.cpp index c253bdff..621aff4d 100644 --- a/tests/YGMinMaxDimensionTest.cpp +++ b/tests/YGMinMaxDimensionTest.cpp @@ -1295,3 +1295,82 @@ TEST(YogaTest, min_max_percent_no_width_height) { YGConfigFree(config); } + +static YGSize _measureCk_test_label_shrink_based_on_height( + YGNodeRef node, + float width, + YGMeasureMode widthMode, + float height, + YGMeasureMode heightMode) { + + if (heightMode == YGMeasureModeAtMost) { + return YGSize{ + .width = 290, + .height = 103, + }; + } else { + return YGSize{ + .width = 290, + .height = height, + }; + } +} + +TEST(YogaTest, min_max_percent_margin_percent_no_width_height) { + const YGConfigRef config = YGConfigNew(); + + const YGNodeRef root = YGNodeNewWithConfig(config); + YGNodeStyleSetWidth(root, 320); + + const YGNodeRef root_child0 = YGNodeNewWithConfig(config); + YGNodeInsertChild(root, root_child0, 0); + + const YGNodeRef root_child0_child0 = YGNodeNewWithConfig(config); + YGNodeStyleSetHeight(root_child0_child0, 450); + YGNodeInsertChild(root_child0, root_child0_child0, 0); + + const YGNodeRef root_child0_child0_child0 = YGNodeNewWithConfig(config); + YGNodeStyleSetFlexDirection(root_child0_child0_child0, YGFlexDirectionRow); + YGNodeStyleSetMarginPercent(root_child0_child0_child0, YGEdgeTop, 5); + YGNodeStyleSetMaxHeightPercent(root_child0_child0_child0, 10); + YGNodeInsertChild(root_child0_child0, root_child0_child0_child0, 0); + + const YGNodeRef root_child0_child0_child0_child0 = + YGNodeNewWithConfig(config); + YGNodeSetMeasureFunc( + root_child0_child0_child0_child0, + _measureCk_test_label_shrink_based_on_height); + YGNodeInsertChild( + root_child0_child0_child0, root_child0_child0_child0_child0, 0); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root)); + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root)); + ASSERT_FLOAT_EQ(320, YGNodeLayoutGetWidth(root)); + ASSERT_FLOAT_EQ(450, YGNodeLayoutGetHeight(root)); + + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0)); + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0)); + ASSERT_FLOAT_EQ(320, YGNodeLayoutGetWidth(root_child0)); + ASSERT_FLOAT_EQ(450, YGNodeLayoutGetHeight(root_child0)); + + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0_child0)); + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0_child0)); + ASSERT_FLOAT_EQ(320, YGNodeLayoutGetWidth(root_child0_child0)); + ASSERT_FLOAT_EQ(450, YGNodeLayoutGetHeight(root_child0_child0)); + + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0_child0_child0)); + ASSERT_FLOAT_EQ(16, YGNodeLayoutGetTop(root_child0_child0_child0)); + ASSERT_FLOAT_EQ(320, YGNodeLayoutGetWidth(root_child0_child0_child0)); + ASSERT_FLOAT_EQ(45, YGNodeLayoutGetHeight(root_child0_child0_child0)); + + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0_child0_child0_child0)); + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0_child0_child0_child0)); + ASSERT_FLOAT_EQ(290, YGNodeLayoutGetWidth(root_child0_child0_child0_child0)); + ASSERT_FLOAT_EQ(45, YGNodeLayoutGetHeight(root_child0_child0_child0_child0)); + + YGNodeFreeRecursive(root); + + YGConfigFree(config); +} diff --git a/tests/YGPercentageTest.cpp b/tests/YGPercentageTest.cpp index 2b9144dd..805df24b 100644 --- a/tests/YGPercentageTest.cpp +++ b/tests/YGPercentageTest.cpp @@ -1193,3 +1193,81 @@ TEST(YogaTest, percent_absolute_position) { YGConfigFree(config); } + +static YGSize _measureCk_test_label_shrink_based_on_height( + YGNodeRef node, + float width, + YGMeasureMode widthMode, + float height, + YGMeasureMode heightMode) { + + if (heightMode == YGMeasureModeAtMost) { + return YGSize{ + .width = 290, + .height = 103, + }; + } else { + return YGSize{ + .width = 290, + .height = height, + }; + } +} + +TEST(YogaTest, margin_percent_with_measure_func) { + const YGConfigRef config = YGConfigNew(); + + const YGNodeRef root = YGNodeNewWithConfig(config); + YGNodeStyleSetWidth(root, 320); + + const YGNodeRef root_child0 = YGNodeNewWithConfig(config); + YGNodeInsertChild(root, root_child0, 0); + + const YGNodeRef root_child0_child0 = YGNodeNewWithConfig(config); + YGNodeStyleSetHeight(root_child0_child0, 450); + YGNodeInsertChild(root_child0, root_child0_child0, 0); + + const YGNodeRef root_child0_child0_child0 = YGNodeNewWithConfig(config); + YGNodeStyleSetFlexDirection(root_child0_child0_child0, YGFlexDirectionRow); + YGNodeStyleSetMarginPercent(root_child0_child0_child0, YGEdgeTop, 5); + YGNodeInsertChild(root_child0_child0, root_child0_child0_child0, 0); + + const YGNodeRef root_child0_child0_child0_child0 = + YGNodeNewWithConfig(config); + YGNodeSetMeasureFunc( + root_child0_child0_child0_child0, + _measureCk_test_label_shrink_based_on_height); + YGNodeInsertChild( + root_child0_child0_child0, root_child0_child0_child0_child0, 0); + + YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR); + + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root)); + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root)); + ASSERT_FLOAT_EQ(320, YGNodeLayoutGetWidth(root)); + ASSERT_FLOAT_EQ(450, YGNodeLayoutGetHeight(root)); + + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0)); + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0)); + ASSERT_FLOAT_EQ(320, YGNodeLayoutGetWidth(root_child0)); + ASSERT_FLOAT_EQ(450, YGNodeLayoutGetHeight(root_child0)); + + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0_child0)); + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0_child0)); + ASSERT_FLOAT_EQ(320, YGNodeLayoutGetWidth(root_child0_child0)); + ASSERT_FLOAT_EQ(450, YGNodeLayoutGetHeight(root_child0_child0)); + + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0_child0_child0)); + ASSERT_FLOAT_EQ(16, YGNodeLayoutGetTop(root_child0_child0_child0)); + ASSERT_FLOAT_EQ(320, YGNodeLayoutGetWidth(root_child0_child0_child0)); + ASSERT_FLOAT_EQ(103, YGNodeLayoutGetHeight(root_child0_child0_child0)); + + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetLeft(root_child0_child0_child0_child0)); + ASSERT_FLOAT_EQ(0, YGNodeLayoutGetTop(root_child0_child0_child0_child0)); + ASSERT_FLOAT_EQ(290, YGNodeLayoutGetWidth(root_child0_child0_child0_child0)); + ASSERT_FLOAT_EQ(103, YGNodeLayoutGetHeight(root_child0_child0_child0_child0)); + + YGNodeFreeRecursive(root); + + YGConfigFree(config); +}