mirror of
https://github.com/hanzoai/yoga.git
synced 2026-10-30 19:34:29 +00:00
Summary: Pull Request resolved: https://github.com/react/yoga/pull/2004 Reviewed By: cortinico Differential Revision: D114693533 fbshipit-source-id: 8124510aa0df308d9254e4bbb027c7fe226c0742
163 lines
5.5 KiB
C++
163 lines
5.5 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/event/event.h>
|
|
|
|
#include <array>
|
|
#include <string_view>
|
|
#include <unordered_set>
|
|
|
|
namespace facebook::yoga::test {
|
|
|
|
// Every enum value declared in LayoutPassReason (excluding the COUNT sentinel).
|
|
// Kept in sync with yoga/event/event.h; a missing entry causes the "all known
|
|
// reasons" test to catch the omission on the next run.
|
|
constexpr std::array<LayoutPassReason, 9> kAllKnownReasons = {
|
|
LayoutPassReason::kInitial,
|
|
LayoutPassReason::kAbsLayout,
|
|
LayoutPassReason::kStretch,
|
|
LayoutPassReason::kMultilineStretch,
|
|
LayoutPassReason::kFlexLayout,
|
|
LayoutPassReason::kMeasureChild,
|
|
LayoutPassReason::kAbsMeasureChild,
|
|
LayoutPassReason::kFlexMeasure,
|
|
LayoutPassReason::kGridLayout,
|
|
};
|
|
|
|
TEST(
|
|
LayoutPassReasonToStringTest,
|
|
everyKnownReasonMapsToDistinctNonUnknownLabel) {
|
|
std::unordered_set<std::string_view> labels;
|
|
for (auto reason : kAllKnownReasons) {
|
|
const char* label = LayoutPassReasonToString(reason);
|
|
ASSERT_NE(label, nullptr)
|
|
<< "LayoutPassReasonToString returned nullptr for enum value "
|
|
<< static_cast<int>(reason);
|
|
EXPECT_STRNE(label, "")
|
|
<< "Empty label for enum value " << static_cast<int>(reason);
|
|
EXPECT_STRNE(label, "unknown")
|
|
<< "Enum value " << static_cast<int>(reason)
|
|
<< " unexpectedly fell through to the default case";
|
|
labels.emplace(label);
|
|
}
|
|
EXPECT_EQ(labels.size(), kAllKnownReasons.size())
|
|
<< "At least two LayoutPassReason values share the same label";
|
|
}
|
|
|
|
TEST(LayoutPassReasonToStringTest, fallsBackToUnknownForUnrecognizedValues) {
|
|
// Out-of-range integer cast: exercises the switch's default branch, which
|
|
// guards against silent regressions when new enum values are added without
|
|
// updating the switch.
|
|
EXPECT_STREQ(
|
|
LayoutPassReasonToString(static_cast<LayoutPassReason>(9999)), "unknown");
|
|
|
|
// COUNT is the sentinel used to size arrays and is not a real reason, so
|
|
// it must also fall through to the default case.
|
|
EXPECT_STREQ(LayoutPassReasonToString(LayoutPassReason::COUNT), "unknown");
|
|
}
|
|
|
|
class EventPublisherTest : public ::testing::Test {
|
|
protected:
|
|
// Event maintains a process-global subscriber list; clear it around each
|
|
// test to keep the tests independent regardless of ordering.
|
|
void SetUp() override {
|
|
Event::reset();
|
|
}
|
|
void TearDown() override {
|
|
Event::reset();
|
|
}
|
|
};
|
|
|
|
TEST_F(
|
|
EventPublisherTest,
|
|
resetDetachesPreviouslyRegisteredSubscribersFromFutureEvents) {
|
|
int callCount = 0;
|
|
Event::subscribe(
|
|
[&](YGNodeConstRef, Event::Type, Event::Data) { ++callCount; });
|
|
|
|
// Sanity-check that the subscriber is actually wired up before reset.
|
|
Event::publish<Event::LayoutPassStart>(nullptr);
|
|
ASSERT_EQ(callCount, 1);
|
|
|
|
Event::reset();
|
|
Event::publish<Event::LayoutPassStart>(nullptr);
|
|
Event::publish<Event::LayoutPassEnd>(
|
|
nullptr, Event::TypedData<Event::LayoutPassEnd>{nullptr});
|
|
|
|
EXPECT_EQ(callCount, 1)
|
|
<< "Subscriber should not have been invoked after Event::reset()";
|
|
}
|
|
|
|
TEST_F(EventPublisherTest, publishForwardsToEverySubscriberWithNodeAndType) {
|
|
YGNodeRef sentinelNode = YGNodeNew();
|
|
|
|
int subscriberACallCount = 0;
|
|
int subscriberBCallCount = 0;
|
|
YGNodeConstRef subscriberAReceivedNode = nullptr;
|
|
Event::Type subscriberAReceivedType = Event::NodeAllocation;
|
|
|
|
Event::subscribe([&](YGNodeConstRef node, Event::Type type, Event::Data) {
|
|
++subscriberACallCount;
|
|
subscriberAReceivedNode = node;
|
|
subscriberAReceivedType = type;
|
|
});
|
|
Event::subscribe([&](YGNodeConstRef, Event::Type, Event::Data) {
|
|
++subscriberBCallCount;
|
|
});
|
|
|
|
Event::publish<Event::LayoutPassStart>(sentinelNode);
|
|
|
|
EXPECT_EQ(subscriberACallCount, 1);
|
|
EXPECT_EQ(subscriberBCallCount, 1);
|
|
EXPECT_EQ(subscriberAReceivedNode, sentinelNode);
|
|
EXPECT_EQ(subscriberAReceivedType, Event::LayoutPassStart);
|
|
|
|
YGNodeFree(sentinelNode);
|
|
}
|
|
|
|
TEST_F(EventPublisherTest, publishForwardsTypedEventDataPayloadUnmodified) {
|
|
int measureCallbackEndCount = 0;
|
|
YGMeasureMode capturedWidthMode = YGMeasureModeUndefined;
|
|
float capturedMeasuredWidth = 0.0f;
|
|
float capturedMeasuredHeight = 0.0f;
|
|
LayoutPassReason capturedReason = LayoutPassReason::kInitial;
|
|
|
|
Event::subscribe([&](YGNodeConstRef, Event::Type type, Event::Data data) {
|
|
if (type != Event::MeasureCallbackEnd) {
|
|
return;
|
|
}
|
|
const auto& payload = data.get<Event::MeasureCallbackEnd>();
|
|
capturedWidthMode = payload.widthMeasureMode;
|
|
capturedMeasuredWidth = payload.measuredWidth;
|
|
capturedMeasuredHeight = payload.measuredHeight;
|
|
capturedReason = payload.reason;
|
|
++measureCallbackEndCount;
|
|
});
|
|
|
|
Event::publish<Event::MeasureCallbackEnd>(
|
|
nullptr,
|
|
Event::TypedData<Event::MeasureCallbackEnd>{
|
|
.width = 100.0f,
|
|
.widthMeasureMode = YGMeasureModeAtMost,
|
|
.height = 200.0f,
|
|
.heightMeasureMode = YGMeasureModeExactly,
|
|
.measuredWidth = 42.5f,
|
|
.measuredHeight = 84.25f,
|
|
.reason = LayoutPassReason::kFlexMeasure,
|
|
});
|
|
|
|
ASSERT_EQ(measureCallbackEndCount, 1);
|
|
EXPECT_EQ(capturedWidthMode, YGMeasureModeAtMost);
|
|
EXPECT_FLOAT_EQ(capturedMeasuredWidth, 42.5f);
|
|
EXPECT_FLOAT_EQ(capturedMeasuredHeight, 84.25f);
|
|
EXPECT_EQ(capturedReason, LayoutPassReason::kFlexMeasure);
|
|
}
|
|
|
|
} // namespace facebook::yoga::test
|