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
Original file line number Diff line number Diff line change
Expand Up @@ -158,13 +158,13 @@ YogaLayoutableShadowNode::YogaLayoutableShadowNode(
}

void YogaLayoutableShadowNode::completeClone(
const ShadowNode& /*sourceShadowNode*/,
const ShadowNode& sourceShadowNode,
const ShadowNodeFragment& fragment) {
if (getTraits().check(ShadowNodeTraits::Trait::MeasurableYogaNode) &&
// New children means we must always dirty to visit. Otherwise, ask the
// Node if the new revision invalidates measurement.
(fragment.children ||
shouldNewRevisionDirtyMeasurement(*this, fragment))) {
shouldNewRevisionDirtyMeasurement(sourceShadowNode, fragment))) {
yogaNode_.setDirty(true);
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -828,6 +828,82 @@ using FontScaledTextShadowNode =
using FontScaledInputShadowNode =
FontScaledShadowNode<FontScaledInputComponentName, false>;

// NOLINTNEXTLINE(modernize-avoid-c-arrays)
static const char SourceComparingComponentName[] = "SourceComparing";

/*
* A measurable leaf that keeps its measurement when cloned with the props of
* the revision it is cloned from.
*/
class SourceComparingShadowNode final
: public ConcreteViewShadowNode<SourceComparingComponentName> {
public:
using Base = ConcreteViewShadowNode<SourceComparingComponentName>;
using Base::Base;

static ShadowNodeTraits BaseTraits() {
auto traits = Base::BaseTraits();
traits.set(ShadowNodeTraits::Trait::LeafYogaNode);
traits.set(ShadowNodeTraits::Trait::MeasurableYogaNode);
return traits;
}

protected:
bool shouldNewRevisionDirtyMeasurement(
const ShadowNode& sourceShadowNode,
const ShadowNodeFragment& /*fragment*/) const override {
return sourceShadowNode.getProps() != getProps();
}
};

TEST(ViewTest, completeCloneAsksWhetherToDirtyAgainstTheSourceRevision) {
ComponentDescriptorProviderRegistry providerRegistry{};
auto registry = providerRegistry.createComponentDescriptorRegistry(
ComponentDescriptorParameters{
.eventDispatcher = EventDispatcher::Shared{},
.contextContainer = nullptr,
.flavor = nullptr});
providerRegistry.add(
concreteComponentDescriptorProvider<RootComponentDescriptor>());
providerRegistry.add(
concreteComponentDescriptorProvider<
ConcreteComponentDescriptor<SourceComparingShadowNode>>());
auto builder = ComponentBuilder{registry};

auto rootShadowNode = std::shared_ptr<RootShadowNode>{};
auto leafShadowNode = std::shared_ptr<SourceComparingShadowNode>{};

// clang-format off
auto element =
Element<RootShadowNode>()
.reference(rootShadowNode)
.tag(1)
.children({
Element<SourceComparingShadowNode>()
.reference(leafShadowNode)
.tag(2)
});
// clang-format on

builder.build(element);
EXPECT_TRUE(rootShadowNode->layoutIfNeeded());
ASSERT_TRUE(leafShadowNode->getIsLayoutClean());

auto cloneWithSourceProps = leafShadowNode->clone({});
EXPECT_TRUE(
static_cast<const YogaLayoutableShadowNode&>(*cloneWithSourceProps)
.getIsLayoutClean());

// New non-layout props leave the Yoga style as it was, so only the
// comparison with the source revision can dirty the clone.
auto viewProps = std::make_shared<ViewProps>();
viewProps->opacity = 0.5;
auto cloneWithNewProps = leafShadowNode->clone({.props = viewProps});
EXPECT_FALSE(
static_cast<const YogaLayoutableShadowNode&>(*cloneWithNewProps)
.getIsLayoutClean());
}

// `ModalHostViewShadowNode` needs JNI to create its initial state on Android.
#ifndef ANDROID
class FontScaleLayoutTest : public ::testing::Test {
Expand Down
Loading