|
| 1 | +/* |
| 2 | + * Copyright (c) Meta Platforms, Inc. and affiliates. |
| 3 | + * |
| 4 | + * This source code is licensed under the MIT license found in the |
| 5 | + * LICENSE file in the root directory of this source tree. |
| 6 | + */ |
| 7 | + |
| 8 | +#include "AnimationTestsBase.h" |
| 9 | + |
| 10 | +#include <react/renderer/animated/drivers/AnimationDriverUtils.h> |
| 11 | +#include <react/renderer/core/ReactRootViewTagGenerator.h> |
| 12 | + |
| 13 | +namespace facebook::react { |
| 14 | + |
| 15 | +// Pins the getManagedProps / hasManagedProps seam re-merged by |
| 16 | +// AnimatedMountingOverrideDelegate to prevent the useNativeDriver mount flash. |
| 17 | +class ManagedPropsMountingOverrideTests : public AnimationTestsBase { |
| 18 | + protected: |
| 19 | + struct OpacityGraph { |
| 20 | + Tag opacityTag; |
| 21 | + Tag styleTag; |
| 22 | + Tag propsTag; |
| 23 | + Tag viewTag; |
| 24 | + }; |
| 25 | + |
| 26 | + // value -> style -> props graph for an animated opacity; view left |
| 27 | + // unconnected. |
| 28 | + OpacityGraph buildOpacityGraph(float initialOpacity) { |
| 29 | + const auto opacityTag = ++rootTag_; |
| 30 | + nodesManager_->createAnimatedNode( |
| 31 | + opacityTag, |
| 32 | + folly::dynamic::object("type", "value")("value", initialOpacity)( |
| 33 | + "offset", 0)); |
| 34 | + |
| 35 | + const auto styleTag = ++rootTag_; |
| 36 | + nodesManager_->createAnimatedNode( |
| 37 | + styleTag, |
| 38 | + folly::dynamic::object("type", "style")( |
| 39 | + "style", folly::dynamic::object("opacity", opacityTag))); |
| 40 | + nodesManager_->connectAnimatedNodes(opacityTag, styleTag); |
| 41 | + |
| 42 | + const auto propsTag = ++rootTag_; |
| 43 | + nodesManager_->createAnimatedNode( |
| 44 | + propsTag, |
| 45 | + folly::dynamic::object("type", "props")( |
| 46 | + "props", folly::dynamic::object("style", styleTag))); |
| 47 | + nodesManager_->connectAnimatedNodes(styleTag, propsTag); |
| 48 | + |
| 49 | + const auto viewTag = ++rootTag_; |
| 50 | + return OpacityGraph{ |
| 51 | + .opacityTag = opacityTag, |
| 52 | + .styleTag = styleTag, |
| 53 | + .propsTag = propsTag, |
| 54 | + .viewTag = viewTag}; |
| 55 | + } |
| 56 | + |
| 57 | + Tag rootTag_{getNextRootViewTag()}; |
| 58 | +}; |
| 59 | + |
| 60 | +TEST_F( |
| 61 | + ManagedPropsMountingOverrideTests, |
| 62 | + getManagedPropsNullForUnconnectedView) { |
| 63 | + initNodesManager(); |
| 64 | + |
| 65 | + const auto unknownTag = ++rootTag_; |
| 66 | + EXPECT_TRUE(nodesManager_->getManagedProps(unknownTag).isNull()); |
| 67 | + EXPECT_FALSE(nodesManager_->hasManagedProps()); |
| 68 | +} |
| 69 | + |
| 70 | +// Managed props mirror the live committed value every frame, never the frozen |
| 71 | +// start value nor the 1.0 default. |
| 72 | +TEST_F( |
| 73 | + ManagedPropsMountingOverrideTests, |
| 74 | + getManagedPropsReflectsLiveValueAcrossFrames) { |
| 75 | + initNodesManager(); |
| 76 | + |
| 77 | + auto g = buildOpacityGraph(/*initialOpacity=*/0.0f); |
| 78 | + nodesManager_->connectAnimatedNodeToView(g.propsTag, g.viewTag); |
| 79 | + |
| 80 | + const auto frames = folly::dynamic::array(0.0f, 0.25f, 0.5f, 0.75f, 1.0f); |
| 81 | + nodesManager_->startAnimatingNode( |
| 82 | + /*animationId=*/1, |
| 83 | + g.opacityTag, |
| 84 | + folly::dynamic::object("type", "frames")("frames", frames)( |
| 85 | + "toValue", 1.0f), |
| 86 | + std::nullopt); |
| 87 | + |
| 88 | + const double startTimeInTick = 1000; |
| 89 | + for (int frame = 0; frame <= 4; ++frame) { |
| 90 | + runAnimationFrame(startTimeInTick + frame * SingleFrameIntervalMs); |
| 91 | + |
| 92 | + auto props = nodesManager_->getManagedProps(g.viewTag); |
| 93 | + ASSERT_FALSE(props.isNull()); |
| 94 | + ASSERT_TRUE(props.isObject()); |
| 95 | + ASSERT_EQ(props.count("opacity"), 1u); |
| 96 | + |
| 97 | + const double live = nodesManager_->getValue(g.opacityTag).value(); |
| 98 | + EXPECT_DOUBLE_EQ(props["opacity"].asDouble(), live); |
| 99 | + |
| 100 | + if (frame == 0) { |
| 101 | + EXPECT_DOUBLE_EQ(live, 0.0); |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + EXPECT_DOUBLE_EQ(nodesManager_->getValue(g.opacityTag).value(), 1.0); |
| 106 | + auto finalProps = nodesManager_->getManagedProps(g.viewTag); |
| 107 | + ASSERT_EQ(finalProps.count("opacity"), 1u); |
| 108 | + EXPECT_DOUBLE_EQ(finalProps["opacity"].asDouble(), 1.0); |
| 109 | +} |
| 110 | + |
| 111 | +// Without the connect-time seed props_ is empty at connect, so the override has |
| 112 | +// nothing to merge: the opacity assertion fails pre-hardening, passes post. |
| 113 | +TEST_F( |
| 114 | + ManagedPropsMountingOverrideTests, |
| 115 | + getManagedPropsLiveImmediatelyOnConnect) { |
| 116 | + initNodesManager(); |
| 117 | + |
| 118 | + auto g = buildOpacityGraph(/*initialOpacity=*/0.8f); |
| 119 | + nodesManager_->connectAnimatedNodeToView(g.propsTag, g.viewTag); |
| 120 | + // No runAnimationFrame: the value must be live the instant the view is |
| 121 | + // managed. |
| 122 | + |
| 123 | + auto props = nodesManager_->getManagedProps(g.viewTag); |
| 124 | + EXPECT_FALSE(props.isNull()); |
| 125 | + ASSERT_TRUE(props.isObject()); |
| 126 | + ASSERT_EQ(props.count("opacity"), 1u); |
| 127 | + EXPECT_DOUBLE_EQ(props["opacity"].asDouble(), static_cast<double>(0.8f)); |
| 128 | +} |
| 129 | + |
| 130 | +// A brand-new view connected to an already mid-flight shared value, with no |
| 131 | +// frame in between: fails pre-hardening, passes post. |
| 132 | +TEST_F( |
| 133 | + ManagedPropsMountingOverrideTests, |
| 134 | + getManagedPropsLiveOnConnectWhileValueMidFlight) { |
| 135 | + initNodesManager(); |
| 136 | + |
| 137 | + auto g = buildOpacityGraph(/*initialOpacity=*/0.0f); |
| 138 | + nodesManager_->connectAnimatedNodeToView(g.propsTag, g.viewTag); |
| 139 | + |
| 140 | + const auto frames = folly::dynamic::array(0.0f, 0.25f, 0.5f, 0.75f, 1.0f); |
| 141 | + nodesManager_->startAnimatingNode( |
| 142 | + /*animationId=*/1, |
| 143 | + g.opacityTag, |
| 144 | + folly::dynamic::object("type", "frames")("frames", frames)( |
| 145 | + "toValue", 1.0f), |
| 146 | + std::nullopt); |
| 147 | + runAnimationFrame(1000); |
| 148 | + runAnimationFrame(1000 + 2 * SingleFrameIntervalMs); |
| 149 | + |
| 150 | + const double live = nodesManager_->getValue(g.opacityTag).value(); |
| 151 | + ASSERT_GT(live, 0.0); |
| 152 | + ASSERT_LT(live, 1.0); |
| 153 | + |
| 154 | + const auto styleTag2 = ++rootTag_; |
| 155 | + nodesManager_->createAnimatedNode( |
| 156 | + styleTag2, |
| 157 | + folly::dynamic::object("type", "style")( |
| 158 | + "style", folly::dynamic::object("opacity", g.opacityTag))); |
| 159 | + nodesManager_->connectAnimatedNodes(g.opacityTag, styleTag2); |
| 160 | + const auto propsTag2 = ++rootTag_; |
| 161 | + nodesManager_->createAnimatedNode( |
| 162 | + propsTag2, |
| 163 | + folly::dynamic::object("type", "props")( |
| 164 | + "props", folly::dynamic::object("style", styleTag2))); |
| 165 | + nodesManager_->connectAnimatedNodes(styleTag2, propsTag2); |
| 166 | + const auto viewTag2 = ++rootTag_; |
| 167 | + nodesManager_->connectAnimatedNodeToView(propsTag2, viewTag2); |
| 168 | + |
| 169 | + auto props = nodesManager_->getManagedProps(viewTag2); |
| 170 | + EXPECT_FALSE(props.isNull()); |
| 171 | + ASSERT_EQ(props.count("opacity"), 1u); |
| 172 | + EXPECT_DOUBLE_EQ(props["opacity"].asDouble(), live); |
| 173 | +} |
| 174 | + |
| 175 | +TEST_F( |
| 176 | + ManagedPropsMountingOverrideTests, |
| 177 | + hasManagedPropsTracksConnectAndDisconnect) { |
| 178 | + initNodesManager(); |
| 179 | + |
| 180 | + auto g = buildOpacityGraph(/*initialOpacity=*/0.5f); |
| 181 | + |
| 182 | + EXPECT_FALSE(nodesManager_->hasManagedProps()); |
| 183 | + EXPECT_TRUE(nodesManager_->getManagedProps(g.viewTag).isNull()); |
| 184 | + |
| 185 | + nodesManager_->connectAnimatedNodeToView(g.propsTag, g.viewTag); |
| 186 | + EXPECT_TRUE(nodesManager_->hasManagedProps()); |
| 187 | + EXPECT_FALSE(nodesManager_->getManagedProps(g.viewTag).isNull()); |
| 188 | + |
| 189 | + nodesManager_->disconnectAnimatedNodeFromView(g.propsTag, g.viewTag); |
| 190 | + EXPECT_FALSE(nodesManager_->hasManagedProps()); |
| 191 | + EXPECT_TRUE(nodesManager_->getManagedProps(g.viewTag).isNull()); |
| 192 | +} |
| 193 | + |
| 194 | +TEST_F(ManagedPropsMountingOverrideTests, getManagedPropsIsolatedPerView) { |
| 195 | + initNodesManager(); |
| 196 | + |
| 197 | + auto g = buildOpacityGraph(/*initialOpacity=*/0.3f); |
| 198 | + nodesManager_->connectAnimatedNodeToView(g.propsTag, g.viewTag); |
| 199 | + |
| 200 | + EXPECT_FALSE(nodesManager_->getManagedProps(g.viewTag).isNull()); |
| 201 | + EXPECT_TRUE(nodesManager_->getManagedProps(g.viewTag + 1000).isNull()); |
| 202 | +} |
| 203 | + |
| 204 | +} // namespace facebook::react |
0 commit comments