Files
yoga/tests/YGRoundingMeasureFuncTest.cpp
Joe Vilches 2ea4c043fd gentest support for position: static + initial test
Summary:
I am about to embark on supporting `position: static` in Yoga. The enum exists already (and is the default position type, lol) but does not actually do anything and just behaves like `position: relative`.

My approach here is to write a bunch of tests to test for the various behaviors of static positions and then develop on Yoga afterwards to get those tests passing. To do this, we need to make a few changes to the gentest files as there is not support for adding `position: static` at the moment:

* Make it so that the gentest code can physically write `YGPositionTypeStatic` if it encounters `position: static` in the style
* Make it so that gentest.js knows that Yoga's default is actually static. This way the code generated in the tests will actually label nodes for non default values
* Explicitly label the position type even when it is not declared in the style prop (with the exception of the default)
* Regenerate all the tests

Additionally I added the first, basic test: making sure insets do nothing on a statically positioned element.

Reviewed By: NickGerleman

Differential Revision: D50437855

fbshipit-source-id: 0e8bbf1c224d477ea4592b7563d0b70d2ffa79c8
2023-10-23 18:20:24 -07:00

140 lines
3.9 KiB
C++

/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/
#include <gtest/gtest.h>
#include <yoga/Yoga.h>
static YGSize _measureFloor(
YGNodeConstRef /*node*/,
float width,
YGMeasureMode /*widthMode*/,
float height,
YGMeasureMode /*heightMode*/) {
return YGSize{
width = 10.2f,
height = 10.2f,
};
}
static YGSize _measureCeil(
YGNodeConstRef /*node*/,
float width,
YGMeasureMode /*widthMode*/,
float height,
YGMeasureMode /*heightMode*/) {
return YGSize{
width = 10.5f,
height = 10.5f,
};
}
static YGSize _measureFractial(
YGNodeConstRef /*node*/,
float width,
YGMeasureMode /*widthMode*/,
float height,
YGMeasureMode /*heightMode*/) {
return YGSize{
width = 0.5f,
height = 0.5f,
};
}
TEST(YogaTest, rounding_feature_with_custom_measure_func_floor) {
const YGConfigRef config = YGConfigNew();
const YGNodeRef root = YGNodeNewWithConfig(config);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _measureFloor);
YGNodeInsertChild(root, root_child0, 0);
YGConfigSetPointScaleFactor(config, 0.0f);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionRTL);
ASSERT_FLOAT_EQ(10.2f, YGNodeLayoutGetWidth(root_child0));
ASSERT_FLOAT_EQ(10.2f, YGNodeLayoutGetHeight(root_child0));
YGConfigSetPointScaleFactor(config, 1.0f);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_FLOAT_EQ(11, YGNodeLayoutGetWidth(root_child0));
ASSERT_FLOAT_EQ(11, YGNodeLayoutGetHeight(root_child0));
YGConfigSetPointScaleFactor(config, 2.0f);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionRTL);
ASSERT_FLOAT_EQ(10.5, YGNodeLayoutGetWidth(root_child0));
ASSERT_FLOAT_EQ(10.5, YGNodeLayoutGetHeight(root_child0));
YGConfigSetPointScaleFactor(config, 4.0f);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_FLOAT_EQ(10.25, YGNodeLayoutGetWidth(root_child0));
ASSERT_FLOAT_EQ(10.25, YGNodeLayoutGetHeight(root_child0));
YGConfigSetPointScaleFactor(config, 1.0f / 3.0f);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionRTL);
ASSERT_FLOAT_EQ(12.0, YGNodeLayoutGetWidth(root_child0));
ASSERT_FLOAT_EQ(12.0, YGNodeLayoutGetHeight(root_child0));
YGNodeFreeRecursive(root);
YGConfigFree(config);
}
TEST(YogaTest, rounding_feature_with_custom_measure_func_ceil) {
const YGConfigRef config = YGConfigNew();
const YGNodeRef root = YGNodeNewWithConfig(config);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeSetMeasureFunc(root_child0, _measureCeil);
YGNodeInsertChild(root, root_child0, 0);
YGConfigSetPointScaleFactor(config, 1.0f);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_FLOAT_EQ(11, YGNodeLayoutGetWidth(root_child0));
ASSERT_FLOAT_EQ(11, YGNodeLayoutGetHeight(root_child0));
YGNodeFreeRecursive(root);
YGConfigFree(config);
}
TEST(
YogaTest,
rounding_feature_with_custom_measure_and_fractial_matching_scale) {
const YGConfigRef config = YGConfigNew();
const YGNodeRef root = YGNodeNewWithConfig(config);
YGNodeStyleSetPositionType(root, YGPositionTypeAbsolute);
const YGNodeRef root_child0 = YGNodeNewWithConfig(config);
YGNodeStyleSetPosition(root_child0, YGEdgeLeft, 73.625);
YGNodeStyleSetPositionType(root_child0, YGPositionTypeRelative);
YGNodeSetMeasureFunc(root_child0, _measureFractial);
YGNodeInsertChild(root, root_child0, 0);
YGConfigSetPointScaleFactor(config, 2.0f);
YGNodeCalculateLayout(root, YGUndefined, YGUndefined, YGDirectionLTR);
ASSERT_FLOAT_EQ(0.5, YGNodeLayoutGetWidth(root_child0));
ASSERT_FLOAT_EQ(0.5, YGNodeLayoutGetHeight(root_child0));
ASSERT_FLOAT_EQ(73.5, YGNodeLayoutGetLeft(root_child0));
YGNodeFreeRecursive(root);
YGConfigFree(config);
}