Skip to content
Closed
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 packages/react-native/Package.swift
Original file line number Diff line number Diff line change
Expand Up @@ -459,6 +459,7 @@ let reactFabric = RNTarget(
path: "ReactCommon/react/renderer",
excludedPaths: [
"animated/tests",
"animationbackend/tests",
"animations/tests",
"attributedstring/tests",
"core/tests",
Expand Down
1 change: 1 addition & 0 deletions packages/react-native/ReactCommon/React-Fabric.podspec
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,7 @@ Pod::Spec.new do |s|

s.subspec "animationbackend" do |ss|
ss.source_files = podspec_sources("react/renderer/animationbackend/**/*.{m,mm,cpp,h}", "react/renderer/animationbackend/**/*.{h}")
ss.exclude_files = "react/renderer/animationbackend/tests"
ss.header_dir = "react/renderer/animationbackend"
end

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -32,23 +32,27 @@ void AnimatedPropsRegistry::update(
.first;
}
auto& snapshot = it->second;
auto& viewProps = snapshot->props;

if (animatedProps.rawProps) {
const auto& newRawProps = *animatedProps.rawProps;
auto& currentRawProps = snapshot->rawProps;

if (currentRawProps) {
auto newRawPropsDynamic = newRawProps.toDynamic();
currentRawProps->merge_patch(newRawPropsDynamic);
if (const auto* dynamic = newRawProps.getDynamic()) {
currentRawProps->merge_patch(*dynamic);
} else {
currentRawProps->merge_patch(newRawProps.toDynamic());
}
} else {
currentRawProps =
std::make_unique<folly::dynamic>(newRawProps.toDynamic());
}
}
if (!animatedProps.props.empty() && !snapshot->props) {
snapshot->props = std::make_unique<BaseViewProps>();
}
for (const auto& animatedProp : animatedProps.props) {
snapshot->propNames.insert(animatedProp->propName);
cloneProp(viewProps, *animatedProp);
cloneProp(*snapshot->props, *animatedProp);
}
}
}
Expand Down Expand Up @@ -83,9 +87,14 @@ AnimatedPropsRegistry::getMap(SurfaceId surfaceId) {
currentSnapshot->rawProps = std::move(propsSnapshot->rawProps);
}
}
for (auto& propName : propsSnapshot->propNames) {
currentSnapshot->propNames.insert(propName);
updateProp(propName, currentSnapshot->props, *propsSnapshot);
if (!currentSnapshot->props) {
currentSnapshot->props = std::move(propsSnapshot->props);
currentSnapshot->propNames = std::move(propsSnapshot->propNames);
} else {
for (auto& propName : propsSnapshot->propNames) {
currentSnapshot->propNames.insert(propName);
updateProp(propName, *currentSnapshot->props, *propsSnapshot);
}
}
}
}
Expand Down

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
@@ -0,0 +1,226 @@
/*
* 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 <hermes/hermes.h>
#include <react/renderer/animationbackend/AnimatedPropsBuilder.h>
#include <react/renderer/animationbackend/AnimatedPropsRegistry.h>
#include <react/renderer/components/view/ViewComponentDescriptor.h>
#include <react/renderer/element/ComponentBuilder.h>
#include <react/renderer/element/Element.h>
#include <react/renderer/element/testUtils.h>
#include <react/utils/ContextContainer.h>

namespace facebook::react {

class AnimatedPropsRegistryTest : public ::testing::Test {
protected:
ComponentBuilder builder =
simpleComponentBuilder(std::make_shared<ContextContainer>());
std::shared_ptr<ViewShadowNode> node =
builder.build(Element<ViewShadowNode>().surfaceId(1).tag(10));
AnimatedPropsRegistry registry;

void SetUp() override {
registry.initializeSurface(1);
}

void update(AnimatedProps props) {
std::vector<AnimationMutations> batches(1);
batches[0].batch.push_back(
AnimationMutation{
.tag = 10,
.family = node->getFamilyShared(),
.props = std::move(props)});
registry.update(batches);
}

void updateRaw(folly::dynamic props) {
update({{}, std::make_unique<RawProps>(std::move(props))});
}

void expectRawProps(folly::dynamic expected) {
EXPECT_EQ(*snapshot().rawProps, expected);
}

PropsSnapshot& snapshot() {
return *registry.getMap(1).second.at(10);
}
};

TEST_F(AnimatedPropsRegistryTest, rawUpdatesDoNotAllocateTypedProps) {
for (int frame = 0; frame < 10; frame++) {
updateRaw(folly::dynamic::object("opacity", frame / 10.0));
auto& props = snapshot();
EXPECT_EQ(props.props, nullptr);
EXPECT_TRUE(props.propNames.empty());
EXPECT_EQ((*props.rawProps)["opacity"], frame / 10.0);
}
}

TEST_F(AnimatedPropsRegistryTest, updatePreservesInputForSynchronousMount) {
folly::dynamic rawProps = folly::dynamic::object("opacity", 0.25)(
"transform",
folly::dynamic::array(folly::dynamic::object("translateX", 10)));
std::vector<AnimationMutations> batches(1);
batches[0].batch.push_back(
AnimationMutation{
.tag = 10,
.family = node->getFamilyShared(),
.props = AnimatedProps{{}, std::make_unique<RawProps>(rawProps)}});
registry.update(batches);
registry.update(batches);
EXPECT_EQ(batches[0].batch[0].props.rawProps->toDynamic(), rawProps);
EXPECT_EQ(*snapshot().rawProps, rawProps);
}

TEST_F(AnimatedPropsRegistryTest, mergesPendingUpdatesRecursively) {
updateRaw(
folly::dynamic::object("opacity", 0.25)(
"shadowOffset", folly::dynamic::object("width", 2)("height", 3)));
updateRaw(
folly::dynamic::object(
"shadowOffset", folly::dynamic::object("width", 7)));
expectRawProps(
folly::dynamic::object("opacity", 0.25)(
"shadowOffset", folly::dynamic::object("width", 7)("height", 3)));
}

TEST_F(AnimatedPropsRegistryTest, mergesCommittedUpdatesRecursively) {
updateRaw(
folly::dynamic::object("opacity", 0.25)(
"shadowOffset", folly::dynamic::object("width", 2)("height", 3)));
snapshot();
updateRaw(
folly::dynamic::object(
"shadowOffset", folly::dynamic::object("width", 7)));
expectRawProps(
folly::dynamic::object("opacity", 0.25)(
"shadowOffset", folly::dynamic::object("width", 7)("height", 3)));
}

TEST_F(AnimatedPropsRegistryTest, nullDeletesPendingProps) {
updateRaw(
folly::dynamic::object("opacity", 0.25)(
"shadowOffset", folly::dynamic::object("width", 2)("height", 3)));
updateRaw(
folly::dynamic::object("opacity", nullptr)(
"shadowOffset", folly::dynamic::object("width", nullptr)));
expectRawProps(
folly::dynamic::object(
"shadowOffset", folly::dynamic::object("height", 3)));
}

TEST_F(AnimatedPropsRegistryTest, nullDeletesCommittedProps) {
updateRaw(
folly::dynamic::object("opacity", 0.25)(
"shadowOffset", folly::dynamic::object("width", 2)("height", 3)));
snapshot();
updateRaw(
folly::dynamic::object("opacity", nullptr)(
"shadowOffset", folly::dynamic::object("width", nullptr)));
expectRawProps(
folly::dynamic::object(
"shadowOffset", folly::dynamic::object("height", 3)));
}

TEST_F(AnimatedPropsRegistryTest, arraysReplaceInsteadOfMerging) {
updateRaw(
folly::dynamic::object(
"transform",
folly::dynamic::array(
folly::dynamic::object("translateX", 2),
folly::dynamic::object("scale", 3))));
snapshot();
auto transform =
folly::dynamic::array(folly::dynamic::object("rotate", "1rad"));
updateRaw(folly::dynamic::object("transform", transform));
EXPECT_EQ((*snapshot().rawProps)["transform"], transform);
}

TEST_F(AnimatedPropsRegistryTest, objectPatchReplacesScalarAndRemovesNulls) {
updateRaw(folly::dynamic::object("value", 5));
snapshot();
updateRaw(
folly::dynamic::object(
"value", folly::dynamic::object("width", 2)("height", nullptr)));
expectRawProps(
folly::dynamic::object("value", folly::dynamic::object("width", 2)));
updateRaw(folly::dynamic::object("value", 7));
expectRawProps(folly::dynamic::object("value", 7));
}

TEST_F(AnimatedPropsRegistryTest, typedPropsCanFollowRawProps) {
updateRaw(folly::dynamic::object("opacity", 0.25));
snapshot();
AnimatedPropsBuilder builder;
builder.setOpacity(0.75);
update(builder.get());
auto& props = snapshot();
ASSERT_NE(props.props, nullptr);
EXPECT_FLOAT_EQ(props.props->opacity, 0.75);
EXPECT_TRUE(props.propNames.contains(OPACITY));
EXPECT_EQ((*props.rawProps)["opacity"], 0.25);
BaseViewProps viewProps;
updateProp(OPACITY, viewProps, props);
EXPECT_FLOAT_EQ(viewProps.opacity, 0.75);
}

TEST_F(AnimatedPropsRegistryTest, rawPropsCanFollowTypedProps) {
AnimatedPropsBuilder builder;
builder.setOpacity(0.75);
update(builder.get());
snapshot();
updateRaw(folly::dynamic::object("opacity", 0.25));
auto& props = snapshot();
ASSERT_NE(props.props, nullptr);
EXPECT_FLOAT_EQ(props.props->opacity, 0.75);
EXPECT_TRUE(props.propNames.contains(OPACITY));
EXPECT_EQ((*props.rawProps)["opacity"], 0.25);
}

TEST_F(AnimatedPropsRegistryTest, mergesTypedUpdatesAcrossCommits) {
AnimatedPropsBuilder builder;
builder.setOpacity(0.75);
builder.setShadowRadius(2);
update(builder.get());
snapshot();
builder.setOpacity(0.5);
update(builder.get());
auto& props = snapshot();
ASSERT_NE(props.props, nullptr);
EXPECT_FLOAT_EQ(props.props->opacity, 0.5);
EXPECT_FLOAT_EQ(props.props->shadowRadius, 2);
EXPECT_EQ(props.propNames.size(), 2);
}

TEST_F(AnimatedPropsRegistryTest, jsiRawPropsAreConverted) {
auto runtime = hermes::makeHermesRuntime();
{
auto object = jsi::Object(*runtime);
object.setProperty(*runtime, "opacity", 0.25);
update(
{{},
std::make_unique<RawProps>(*runtime, jsi::Value(*runtime, object))});
object.setProperty(*runtime, "opacity", 0.5);
update(
{{},
std::make_unique<RawProps>(*runtime, jsi::Value(*runtime, object))});
}
runtime.reset();
expectRawProps(folly::dynamic::object("opacity", 0.5));
EXPECT_EQ(snapshot().props, nullptr);
}

TEST_F(AnimatedPropsRegistryTest, stoppedSurfacesIgnoreUpdates) {
registry.clearOnSurfaceStop(1);
updateRaw(folly::dynamic::object("opacity", 0.5));
registry.initializeSurface(1);
EXPECT_TRUE(registry.getMap(1).second.empty());
}

} // namespace facebook::react
2 changes: 1 addition & 1 deletion scripts/cxx-api/api-snapshots/ReactAndroidDebugCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -7818,7 +7818,7 @@ struct facebook::react::PropsParserContext {
}

struct facebook::react::PropsSnapshot {
public facebook::react::BaseViewProps props;
public std::unique_ptr<facebook::react::BaseViewProps> props;
public std::unique_ptr<folly::dynamic> rawProps;
public std::unordered_set<facebook::react::PropName> propNames;
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/cxx-api/api-snapshots/ReactAndroidNewarchCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -7578,7 +7578,7 @@ struct facebook::react::PropsParserContext {
}

struct facebook::react::PropsSnapshot {
public facebook::react::BaseViewProps props;
public std::unique_ptr<facebook::react::BaseViewProps> props;
public std::unique_ptr<folly::dynamic> rawProps;
public std::unordered_set<facebook::react::PropName> propNames;
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/cxx-api/api-snapshots/ReactAndroidReleaseCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -7809,7 +7809,7 @@ struct facebook::react::PropsParserContext {
}

struct facebook::react::PropsSnapshot {
public facebook::react::BaseViewProps props;
public std::unique_ptr<facebook::react::BaseViewProps> props;
public std::unique_ptr<folly::dynamic> rawProps;
public std::unordered_set<facebook::react::PropName> propNames;
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/cxx-api/api-snapshots/ReactAppleDebugCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -9805,7 +9805,7 @@ struct facebook::react::PropsParserContext {
}

struct facebook::react::PropsSnapshot {
public facebook::react::BaseViewProps props;
public std::unique_ptr<facebook::react::BaseViewProps> props;
public std::unique_ptr<folly::dynamic> rawProps;
public std::unordered_set<facebook::react::PropName> propNames;
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/cxx-api/api-snapshots/ReactAppleNewarchCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -9621,7 +9621,7 @@ struct facebook::react::PropsParserContext {
}

struct facebook::react::PropsSnapshot {
public facebook::react::BaseViewProps props;
public std::unique_ptr<facebook::react::BaseViewProps> props;
public std::unique_ptr<folly::dynamic> rawProps;
public std::unordered_set<facebook::react::PropName> propNames;
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/cxx-api/api-snapshots/ReactAppleReleaseCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -9796,7 +9796,7 @@ struct facebook::react::PropsParserContext {
}

struct facebook::react::PropsSnapshot {
public facebook::react::BaseViewProps props;
public std::unique_ptr<facebook::react::BaseViewProps> props;
public std::unique_ptr<folly::dynamic> rawProps;
public std::unordered_set<facebook::react::PropName> propNames;
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/cxx-api/api-snapshots/ReactCommonDebugCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -5957,7 +5957,7 @@ struct facebook::react::PropsParserContext {
}

struct facebook::react::PropsSnapshot {
public facebook::react::BaseViewProps props;
public std::unique_ptr<facebook::react::BaseViewProps> props;
public std::unique_ptr<folly::dynamic> rawProps;
public std::unordered_set<facebook::react::PropName> propNames;
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/cxx-api/api-snapshots/ReactCommonNewarchCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -5781,7 +5781,7 @@ struct facebook::react::PropsParserContext {
}

struct facebook::react::PropsSnapshot {
public facebook::react::BaseViewProps props;
public std::unique_ptr<facebook::react::BaseViewProps> props;
public std::unique_ptr<folly::dynamic> rawProps;
public std::unordered_set<facebook::react::PropName> propNames;
}
Expand Down
2 changes: 1 addition & 1 deletion scripts/cxx-api/api-snapshots/ReactCommonReleaseCxx.api
Original file line number Diff line number Diff line change
Expand Up @@ -5948,7 +5948,7 @@ struct facebook::react::PropsParserContext {
}

struct facebook::react::PropsSnapshot {
public facebook::react::BaseViewProps props;
public std::unique_ptr<facebook::react::BaseViewProps> props;
public std::unique_ptr<folly::dynamic> rawProps;
public std::unordered_set<facebook::react::PropName> propNames;
}
Expand Down
Loading