|
7 | 7 |
|
8 | 8 | #include "AnimationTestsBase.h" |
9 | 9 |
|
| 10 | +#include <react/renderer/animated/drivers/AnimationDriverUtils.h> |
10 | 11 | #include <react/renderer/components/scrollview/ScrollEvent.h> |
11 | 12 | #include <react/renderer/core/ReactRootViewTagGenerator.h> |
12 | 13 |
|
13 | 14 | namespace facebook::react { |
14 | 15 |
|
15 | | -class EventAnimationDriverTests : public AnimationTestsBase {}; |
| 16 | +namespace { |
| 17 | + |
| 18 | +// Runs event-driven updates at a timestamp the test sets, like a platform |
| 19 | +// whose current time is later than the vsync time of the next frame. |
| 20 | +class FakeAnimationBackend : public UIManagerAnimationBackend { |
| 21 | + public: |
| 22 | + AnimationTimestamp pushTimestamp{0}; |
| 23 | + |
| 24 | + void onAnimationFrame(AnimationTimestamp /*timestamp*/) override {} |
| 25 | + CallbackId start(const Callback& /*callback*/) override { |
| 26 | + return 0; |
| 27 | + } |
| 28 | + void stop(CallbackId /*callbackId*/) override {} |
| 29 | + void clearRegistry(SurfaceId /*surfaceId*/) override {} |
| 30 | + void clearRegistryOnSurfaceStop(SurfaceId /*surfaceId*/) override {} |
| 31 | + void trigger() override {} |
| 32 | + void pushAnimationMutations(const Callback& callback) override { |
| 33 | + callback(pushTimestamp); |
| 34 | + } |
| 35 | + void registerJSInvoker(std::shared_ptr<CallInvoker> /*jsInvoker*/) override {} |
| 36 | +}; |
| 37 | + |
| 38 | +} // namespace |
| 39 | + |
| 40 | +class EventAnimationDriverTests : public AnimationTestsBase { |
| 41 | + protected: |
| 42 | + static constexpr double kStartTime = 1000; |
| 43 | + |
| 44 | + std::shared_ptr<FakeAnimationBackend> backend_ = |
| 45 | + std::make_shared<FakeAnimationBackend>(); |
| 46 | + Tag tag_{getNextRootViewTag()}; |
| 47 | + Tag viewTag_{++tag_}; |
| 48 | + Tag scrollValueTag_{++tag_}; |
| 49 | + Tag animatedValueTag_{++tag_}; |
| 50 | + |
| 51 | + // A shared-backend manager with a scroll event mapped to scrollValueTag_ and |
| 52 | + // the given animation running on animatedValueTag_. |
| 53 | + void startSharedBackendAnimation(const folly::dynamic& animationConfig) { |
| 54 | + // Marks this thread as the render thread. |
| 55 | + initNodesManager(); |
| 56 | + nodesManager_ = std::make_shared<NativeAnimatedNodesManager>(backend_); |
| 57 | + |
| 58 | + const folly::dynamic valueNodeConfig = |
| 59 | + folly::dynamic::object("type", "value")("value", 0)("offset", 0); |
| 60 | + nodesManager_->createAnimatedNode(scrollValueTag_, valueNodeConfig); |
| 61 | + nodesManager_->addAnimatedEventToView( |
| 62 | + viewTag_, |
| 63 | + "scroll", |
| 64 | + folly::dynamic::object("animatedValueTag", scrollValueTag_)( |
| 65 | + "nativeEventPath", folly::dynamic::array("contentOffset", "y"))); |
| 66 | + nodesManager_->createAnimatedNode(animatedValueTag_, valueNodeConfig); |
| 67 | + nodesManager_->startAnimatingNode( |
| 68 | + 1, animatedValueTag_, animationConfig, std::nullopt); |
| 69 | + } |
| 70 | + |
| 71 | + void runBackendFrame(double timestamp) { |
| 72 | + nodesManager_->pullAnimationMutations(AnimationTimestamp{timestamp}); |
| 73 | + } |
| 74 | + |
| 75 | + void dispatchScrollAt(double timestamp, double offsetY) { |
| 76 | + backend_->pushTimestamp = AnimationTimestamp{timestamp}; |
| 77 | + auto scrollEvent = std::make_shared<ScrollEvent>(); |
| 78 | + scrollEvent->contentOffset = {.x = 0, .y = static_cast<Float>(offsetY)}; |
| 79 | + (*nodesManager_->getEventEmitterListener())( |
| 80 | + viewTag_, "scroll", *scrollEvent); |
| 81 | + } |
| 82 | + |
| 83 | + double animatedValue() { |
| 84 | + return nodesManager_->getValue(animatedValueTag_).value(); |
| 85 | + } |
| 86 | +}; |
16 | 87 |
|
17 | 88 | TEST_F(EventAnimationDriverTests, subscribeToViewEvent) { |
18 | 89 | initNodesManager(); |
@@ -67,4 +138,41 @@ TEST_F(EventAnimationDriverTests, subscribeToViewEvent) { |
67 | 138 | EXPECT_EQ(nodesManager_->getValue(animatedValueTag2), 11); |
68 | 139 | } |
69 | 140 |
|
| 141 | +TEST_F( |
| 142 | + EventAnimationDriverTests, |
| 143 | + sharedBackendEventDoesNotStepTimingAnimationAheadOfFrames) { |
| 144 | + startSharedBackendAnimation( |
| 145 | + folly::dynamic::object("type", "frames")( |
| 146 | + "frames", folly::dynamic::array(0.0, 0.25, 0.5, 0.75, 1.0))( |
| 147 | + "toValue", 100)); |
| 148 | + |
| 149 | + runBackendFrame(kStartTime); |
| 150 | + runBackendFrame(kStartTime + SingleFrameIntervalMs); |
| 151 | + const auto valueBeforeEvent = animatedValue(); |
| 152 | + EXPECT_GT(valueBeforeEvent, 0); |
| 153 | + |
| 154 | + // The event is handled later than the next frame's vsync time. |
| 155 | + dispatchScrollAt(kStartTime + SingleFrameIntervalMs * 3, 4); |
| 156 | + EXPECT_EQ(nodesManager_->getValue(scrollValueTag_), 4); |
| 157 | + EXPECT_EQ(animatedValue(), valueBeforeEvent); |
| 158 | + |
| 159 | + runBackendFrame(kStartTime + SingleFrameIntervalMs * 2); |
| 160 | + EXPECT_GT(animatedValue(), valueBeforeEvent); |
| 161 | +} |
| 162 | + |
| 163 | +TEST_F(EventAnimationDriverTests, sharedBackendEventDoesNotEndDecayAnimation) { |
| 164 | + startSharedBackendAnimation( |
| 165 | + folly::dynamic::object("type", "decay")("velocity", 1.0)( |
| 166 | + "deceleration", 0.998)("iterations", 1)); |
| 167 | + |
| 168 | + runBackendFrame(kStartTime); |
| 169 | + runBackendFrame(kStartTime + SingleFrameIntervalMs); |
| 170 | + dispatchScrollAt(kStartTime + SingleFrameIntervalMs * 2, 4); |
| 171 | + runBackendFrame(kStartTime + SingleFrameIntervalMs * 2); |
| 172 | + const auto valueAfterEvent = animatedValue(); |
| 173 | + |
| 174 | + runBackendFrame(kStartTime + SingleFrameIntervalMs * 3); |
| 175 | + EXPECT_GT(animatedValue(), valueAfterEvent); |
| 176 | +} |
| 177 | + |
70 | 178 | } // namespace facebook::react |
0 commit comments