Files
yoga/tests/YGConfigTest.cpp
Aditya Sharatandmeta-codesync[bot] c8d2ef22b1 Spec-correct CSS Flexbox §4.5 auto-min-size opt-in on YGConfig (re-land) (#1968)
Summary:
X-link: https://github.com/facebook/react-native/pull/57057

Pull Request resolved: https://github.com/facebook/yoga/pull/1968

Re-land of D105720159, originally reverted (revert: 0e642b96c7e2) due to a latent NPE in `ReactProgressBarViewManager.kt` and Instagram-internal `ReactSwitchManager.kt` — both fixed in the prior diffs in this stack. Crash and broken e2e tests reported in T273821098 / P2359486686.

This re-land additionally fixes a correctness gap in the original D105720159 design: it claimed "Default configs carry the bit (preserves today's behaviour); existing trees see no change" — but `LayoutConformance::Strict` in `YogaLayoutableShadowNode::resolveErrata` returned `YGErrataNone`, which clears every errata bit *including* the new `MinSizeUndefinedInsteadOfAuto` (bit 8). Since `react-strict-dom` sets `experimental_layoutConformance="strict"` on every native View, this implicitly opts every RSD-using surface into the new auto-min probe. That's the exact path Airwave's tests took to hit the original NPE.

Change: `LayoutConformance::Strict` now returns `YGErrataMinSizeUndefinedInsteadOfAuto` instead of `YGErrataNone`. Strict mode keeps every other CSS conformance behavior intact; only the new auto-min feature stays opt-in via explicit `YGConfig` setup. This matches the design pattern Litho uses in the child diff (`ComponentsConfiguration.useAutoMinSize` flag, gated independently of any strict-mode plumbing).

Original summary follows:

Pull Request resolved: https://github.com/facebook/yoga/pull/1966

X-link: https://github.com/facebook/react-native/pull/57015

Implements CSS Flexbox §4.5 automatic minimum sizing in Yoga. When opted in on a `YGConfig`, every flex item whose main-axis `min-{width,height}` is undefined receives a content-derived floor — `min(min-content, specified-size, max-size)` plus any aspect-ratio-transferred lower bound — so it cannot shrink below the size CSS browsers would honor.

How to opt in: clear the new `YGErrataMinSizeUndefinedInsteadOfAuto` errata bit on the config. Default configs carry the bit (preserves today's behaviour); existing trees see no change. The bit is added to `YGErrataClassic` so consumers using that constant continue to get the same default. **`LayoutConformance::Strict` also keeps the bit set** — the new feature is strictly opt-in, not inferred from strict-mode CSS conformance.

See D105720159 for full details on precedence rules, container recursion semantics, and per-item opt-outs.

Changelog:
[General][Added] - Add CSS Flexbox §4.5 automatic minimum sizing. Opt in by clearing the new `YGErrataMinSizeUndefinedInsteadOfAuto` errata bit on `YGConfig`.

Reviewed By: javache, aashay-gaikwad

Differential Revision: D107183851

fbshipit-source-id: 451b86347e24da5504649ba575fb27ae0d2f9853
2026-06-03 08:12:26 -07:00

238 lines
7.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>
#include <yoga/config/Config.h>
#include <yoga/node/Node.h>
#include <functional>
#include <memory>
#include <stdexcept>
using namespace facebook;
struct ConfigCloningTest : public ::testing::Test {
std::unique_ptr<yoga::Config, std::function<void(yoga::Config*)>> config;
void SetUp() override;
void TearDown() override;
static yoga::Node clonedNode;
static YGNodeRef cloneNode(
YGNodeConstRef /*unused*/,
YGNodeConstRef /*unused*/,
size_t /*unused*/) {
return &clonedNode;
}
static YGNodeRef doNotClone(
YGNodeConstRef /*unused*/,
YGNodeConstRef /*unused*/,
size_t /*unused*/) {
return nullptr;
}
};
TEST_F(ConfigCloningTest, uses_values_provided_by_cloning_callback) {
config->setCloneNodeCallback(cloneNode);
yoga::Node node{};
yoga::Node owner{};
auto clone = config->cloneNode(&node, &owner, 0);
ASSERT_EQ(clone, &clonedNode);
}
TEST_F(
ConfigCloningTest,
falls_back_to_regular_cloning_if_callback_returns_null) {
config->setCloneNodeCallback(doNotClone);
yoga::Node node{};
yoga::Node owner{};
auto clone = config->cloneNode(&node, &owner, 0);
ASSERT_NE(clone, nullptr);
YGNodeFree(clone);
}
void ConfigCloningTest::SetUp() {
config = {static_cast<yoga::Config*>(YGConfigNew()), YGConfigFree};
}
void ConfigCloningTest::TearDown() {
config.reset();
}
yoga::Node ConfigCloningTest::clonedNode = {};
TEST(YogaTest, config_point_scale_factor_negative_throws) {
YGConfigRef config = YGConfigNew();
// Zero is explicitly allowed per the API contract
YGConfigSetPointScaleFactor(config, 0.0f);
ASSERT_EQ(YGConfigGetPointScaleFactor(config), 0.0f);
// Negative values should trigger a fatal assertion (throws std::logic_error)
ASSERT_THROW(YGConfigSetPointScaleFactor(config, -1.0f), std::logic_error);
// Verify the value was not changed after the failed set
ASSERT_EQ(YGConfigGetPointScaleFactor(config), 0.0f);
YGConfigFree(config);
}
TEST(YogaTest, config_set_logger_null_resets_to_default_logger) {
YGConfigRef config = YGConfigNew();
// Track whether our custom logger was called
bool customLoggerCalled = false;
YGConfigSetContext(config, &customLoggerCalled);
YGLogger customLogger = [](YGConfigConstRef cfg,
YGNodeConstRef /*node*/,
YGLogLevel /*level*/,
const char* /*format*/,
va_list /*args*/) -> int {
bool* called = static_cast<bool*>(YGConfigGetContext(cfg));
*called = true;
return 0;
};
// Set custom logger and verify it's invoked via layout warning
YGConfigSetLogger(config, customLogger);
YGNodeRef node = YGNodeNewWithConfig(config);
YGNodeCalculateLayout(node, YGUndefined, YGUndefined, YGDirectionLTR);
// Trigger a log by setting a negative point scale factor (which logs before
// throwing)
customLoggerCalled = false;
ASSERT_THROW(YGConfigSetPointScaleFactor(config, -1.0f), std::logic_error);
ASSERT_TRUE(customLoggerCalled);
// Reset logger to null — should fall back to default logger
YGConfigSetLogger(config, nullptr);
customLoggerCalled = false;
ASSERT_THROW(YGConfigSetPointScaleFactor(config, -2.0f), std::logic_error);
// Custom logger should NOT have been called since we reset to default
ASSERT_FALSE(customLoggerCalled);
YGNodeFree(node);
YGConfigFree(config);
}
TEST(YogaTest, config_version_increments_only_on_actual_changes) {
auto* config = static_cast<yoga::Config*>(YGConfigNew());
uint32_t initialVersion = config->getVersion();
// Changing point scale factor should increment version
config->setPointScaleFactor(2.0f);
ASSERT_EQ(config->getVersion(), initialVersion + 1);
// Setting the same value again should NOT increment version
config->setPointScaleFactor(2.0f);
ASSERT_EQ(config->getVersion(), initialVersion + 1);
// Changing errata should increment version
config->setErrata(yoga::Errata::StretchFlexBasis);
ASSERT_EQ(config->getVersion(), initialVersion + 2);
// Setting the same errata again should NOT increment version
config->setErrata(yoga::Errata::StretchFlexBasis);
ASSERT_EQ(config->getVersion(), initialVersion + 2);
// Enabling an experimental feature should increment version
config->setExperimentalFeatureEnabled(
yoga::ExperimentalFeature::WebFlexBasis, true);
ASSERT_EQ(config->getVersion(), initialVersion + 3);
// Enabling the same feature again should NOT increment version
config->setExperimentalFeatureEnabled(
yoga::ExperimentalFeature::WebFlexBasis, true);
ASSERT_EQ(config->getVersion(), initialVersion + 3);
YGConfigFree(config);
}
TEST(YogaTest, config_update_invalidates_layout_detects_each_property) {
auto* config1 = static_cast<yoga::Config*>(YGConfigNew());
auto* config2 = static_cast<yoga::Config*>(YGConfigNew());
// Two identical configs should not invalidate layout
ASSERT_FALSE(yoga::configUpdateInvalidatesLayout(*config1, *config2));
// Changing errata on one config should invalidate
config2->setErrata(yoga::Errata::StretchFlexBasis);
ASSERT_TRUE(yoga::configUpdateInvalidatesLayout(*config1, *config2));
// Make them match again
config1->setErrata(yoga::Errata::StretchFlexBasis);
ASSERT_FALSE(yoga::configUpdateInvalidatesLayout(*config1, *config2));
// Changing point scale factor should invalidate
config2->setPointScaleFactor(3.0f);
ASSERT_TRUE(yoga::configUpdateInvalidatesLayout(*config1, *config2));
// Make them match again
config1->setPointScaleFactor(3.0f);
ASSERT_FALSE(yoga::configUpdateInvalidatesLayout(*config1, *config2));
// Changing useWebDefaults should invalidate
config2->setUseWebDefaults(true);
ASSERT_TRUE(yoga::configUpdateInvalidatesLayout(*config1, *config2));
// Make them match again
config1->setUseWebDefaults(true);
ASSERT_FALSE(yoga::configUpdateInvalidatesLayout(*config1, *config2));
// Changing experimental features should invalidate
config2->setExperimentalFeatureEnabled(
yoga::ExperimentalFeature::WebFlexBasis, true);
ASSERT_TRUE(yoga::configUpdateInvalidatesLayout(*config1, *config2));
YGConfigFree(config1);
YGConfigFree(config2);
}
TEST(YogaTest, config_errata_bitmask_add_remove_operations) {
auto* config = static_cast<yoga::Config*>(YGConfigNew());
// Default config carries MinSizeUndefinedInsteadOfAuto (legacy preservation
// for CSS §4.5 auto-min). Clear it for this test so we can assert exact
// equality at the end.
config->removeErrata(yoga::Errata::MinSizeUndefinedInsteadOfAuto);
// Initially no errata
ASSERT_FALSE(config->hasErrata(yoga::Errata::StretchFlexBasis));
ASSERT_FALSE(config->hasErrata(
yoga::Errata::AbsolutePositionWithoutInsetsExcludesPadding));
// Add one errata flag
config->addErrata(yoga::Errata::StretchFlexBasis);
ASSERT_TRUE(config->hasErrata(yoga::Errata::StretchFlexBasis));
ASSERT_FALSE(config->hasErrata(
yoga::Errata::AbsolutePositionWithoutInsetsExcludesPadding));
// Add another errata flag — first should still be set
config->addErrata(yoga::Errata::AbsolutePositionWithoutInsetsExcludesPadding);
ASSERT_TRUE(config->hasErrata(yoga::Errata::StretchFlexBasis));
ASSERT_TRUE(config->hasErrata(
yoga::Errata::AbsolutePositionWithoutInsetsExcludesPadding));
// Remove only the first flag — second should remain
config->removeErrata(yoga::Errata::StretchFlexBasis);
ASSERT_FALSE(config->hasErrata(yoga::Errata::StretchFlexBasis));
ASSERT_TRUE(config->hasErrata(
yoga::Errata::AbsolutePositionWithoutInsetsExcludesPadding));
// Remove the second flag
config->removeErrata(
yoga::Errata::AbsolutePositionWithoutInsetsExcludesPadding);
ASSERT_EQ(config->getErrata(), yoga::Errata::None);
YGConfigFree(config);
}