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
This commit is contained in:
Sidharth Guglani
2019-11-13 07:24:16 -08:00
committed by Facebook Github Bot
parent b0a0007d6e
commit 5960dd888d
2 changed files with 157 additions and 0 deletions

View File

@@ -1295,3 +1295,82 @@ TEST(YogaTest, min_max_percent_no_width_height) {
YGConfigFree(config); 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);
}

View File

@@ -1193,3 +1193,81 @@ TEST(YogaTest, percent_absolute_position) {
YGConfigFree(config); 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);
}