Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions enums.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
"MaxContent",
"FitContent",
"Stretch",
"Dynamic",
],
"FlexDirection": ["Column", "ColumnReverse", "Row", "RowReverse"],
"Justify": [
Expand Down
4 changes: 3 additions & 1 deletion java/com/facebook/yoga/YogaUnit.java
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ public enum YogaUnit {
AUTO(3),
MAX_CONTENT(4),
FIT_CONTENT(5),
STRETCH(6);
STRETCH(6),
DYNAMIC(7);

private final int mIntValue;

Expand All @@ -37,6 +38,7 @@ public static YogaUnit fromInt(int value) {
case 4: return MAX_CONTENT;
case 5: return FIT_CONTENT;
case 6: return STRETCH;
case 7: return DYNAMIC;
default: throw new IllegalArgumentException("Unknown enum value: " + value);
}
}
Expand Down
2 changes: 2 additions & 0 deletions javascript/src/generated/YGEnums.ts
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,7 @@ export enum Unit {
MaxContent = 4,
FitContent = 5,
Stretch = 6,
Dynamic = 7,
}

export enum Wrap {
Expand Down Expand Up @@ -238,6 +239,7 @@ const constants = {
UNIT_MAX_CONTENT: Unit.MaxContent,
UNIT_FIT_CONTENT: Unit.FitContent,
UNIT_STRETCH: Unit.Stretch,
UNIT_DYNAMIC: Unit.Dynamic,
WRAP_NO_WRAP: Wrap.NoWrap,
WRAP_WRAP: Wrap.Wrap,
WRAP_WRAP_REVERSE: Wrap.WrapReverse,
Expand Down
86 changes: 86 additions & 0 deletions tests/StyleDynamicValueTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
/*
* 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/node/Node.h>
#include <yoga/style/StyleLength.h>
#include <yoga/style/StyleSizeLength.h>

namespace facebook::yoga {

static YGValue _resolveToHalfReference(
YGNodeConstRef /*node*/,
YGValueDynamicID /*id*/,
YGValueDynamicContext context) {
return YGValue{context.referenceLength * 0.5f, YGUnitPoint};
}

static YGValue _resolveToZero(
YGNodeConstRef /*node*/,
YGValueDynamicID /*id*/,
YGValueDynamicContext /*context*/) {
return YGValue{0, YGUnitPoint};
}

TEST(StyleLength, dynamic_resolve_returns_undefined_without_node) {
const auto resolved =
StyleLength::dynamic(_resolveToHalfReference, 1).resolve(100.0f, nullptr);

ASSERT_TRUE(resolved.isUndefined());
}

TEST(StyleLength, dynamic_resolve_returns_undefined_without_callback) {
const Node node{};
const auto resolved = StyleLength::dynamic(nullptr, 1).resolve(100.0f, &node);

ASSERT_TRUE(resolved.isUndefined());
}

TEST(StyleLength, dynamic_resolve_calls_callback) {
const Node node{};
const auto resolved =
StyleLength::dynamic(_resolveToHalfReference, 1).resolve(80.0f, &node);

ASSERT_TRUE(resolved.isDefined());
ASSERT_FLOAT_EQ(resolved.unwrap(), 40.0f);
}

TEST(StyleSizeLength, dynamic_resolve_calls_callback) {
const Node node{};
const auto resolved = StyleSizeLength::dynamic(_resolveToHalfReference, 1)
.resolve(80.0f, &node);

ASSERT_TRUE(resolved.isDefined());
ASSERT_FLOAT_EQ(resolved.unwrap(), 40.0f);
}

TEST(StyleLength, dynamic_equality) {
const auto a = StyleLength::dynamic(_resolveToHalfReference, 1);
const auto b = StyleLength::dynamic(_resolveToHalfReference, 1);
const auto differentId = StyleLength::dynamic(_resolveToHalfReference, 2);
const auto differentCallback = StyleLength::dynamic(_resolveToZero, 1);
const auto points = StyleLength::points(50);

ASSERT_TRUE(a == b);
ASSERT_FALSE(a == differentId);
ASSERT_FALSE(a == differentCallback);
ASSERT_FALSE(a == points);
ASSERT_TRUE(a.inexactEquals(b));
ASSERT_FALSE(a.inexactEquals(differentId));
ASSERT_FALSE(a.inexactEquals(differentCallback));
}

TEST(StyleLength, dynamic_ygvalue_conversion) {
const auto length = StyleLength::dynamic(_resolveToHalfReference, 1);
const YGValue ygValue = static_cast<YGValue>(length);

ASSERT_EQ(ygValue.unit, YGUnitDynamic);
ASSERT_TRUE(YGFloatIsUndefined(ygValue.value));
}

} // namespace facebook::yoga
11 changes: 6 additions & 5 deletions tests/StyleTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,30 +14,31 @@ TEST(Style, computed_padding_is_floored) {
yoga::Style style;
style.setPadding(Edge::All, StyleLength::points(-1.0f));
auto paddingStart = style.computeInlineStartPadding(
FlexDirection::Row, Direction::LTR, 0.0f /*widthSize*/);
FlexDirection::Row, Direction::LTR, 0.0f /*widthSize*/, nullptr);
ASSERT_EQ(paddingStart, 0.0f);
}

TEST(Style, computed_border_is_floored) {
yoga::Style style;
style.setBorder(Edge::All, StyleLength::points(-1.0f));
auto borderStart =
style.computeInlineStartBorder(FlexDirection::Row, Direction::LTR);
auto borderStart = style.computeInlineStartBorder(
FlexDirection::Row, Direction::LTR, nullptr);
ASSERT_EQ(borderStart, 0.0f);
}

TEST(Style, computed_gap_is_floored) {
yoga::Style style;
style.setGap(Gutter::Column, StyleLength::points(-1.0f));
auto gapBetweenColumns = style.computeGapForAxis(FlexDirection::Row, 0.0);
auto gapBetweenColumns =
style.computeGapForAxis(FlexDirection::Row, 0.0, nullptr);
ASSERT_EQ(gapBetweenColumns, 0.0f);
}

TEST(Style, computed_margin_is_not_floored) {
yoga::Style style;
style.setMargin(Edge::All, StyleLength::points(-1.0f));
auto marginStart = style.computeInlineStartMargin(
FlexDirection::Row, Direction::LTR, 0.0f /*widthSize*/);
FlexDirection::Row, Direction::LTR, 0.0f /*widthSize*/, nullptr);
ASSERT_EQ(marginStart, -1.0f);
}

Expand Down
25 changes: 25 additions & 0 deletions tests/StyleValuePoolTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,11 @@

namespace facebook::yoga {

static YGValue
dynamicValueCallback(YGNodeConstRef, YGValueDynamicID, YGValueDynamicContext) {
return YGValue{0.0f, YGUnitPoint};
}

TEST(StyleValuePool, undefined_at_init) {
StyleValuePool pool;
StyleValueHandle handle;
Expand Down Expand Up @@ -143,4 +148,24 @@ TEST(StyleValuePool, store_keywords) {
EXPECT_EQ(pool.getSize(handleStretch), StyleSizeLength::ofStretch());
}

TEST(StyleValuePool, stores_and_recovers_dynamic) {
StyleValuePool pool;
StyleValueHandle lengthHandle;
StyleValueHandle sizeHandle;

pool.store(lengthHandle, StyleLength::dynamic(dynamicValueCallback, 11));
pool.store(sizeHandle, StyleSizeLength::dynamic(dynamicValueCallback, 12));

const auto restoredLength = pool.getLength(lengthHandle);
const auto restoredSize = pool.getSize(sizeHandle);

EXPECT_TRUE(restoredLength.isDynamic());
EXPECT_EQ(restoredLength.callback(), dynamicValueCallback);
EXPECT_EQ(restoredLength.callbackId(), 11);

EXPECT_TRUE(restoredSize.isDynamic());
EXPECT_EQ(restoredSize.callback(), dynamicValueCallback);
EXPECT_EQ(restoredSize.callbackId(), 12);
}

} // namespace facebook::yoga
Loading
Loading