-
Notifications
You must be signed in to change notification settings - Fork 25.2k
Expand file tree
/
Copy pathAnimationDriverTests.cpp
More file actions
168 lines (132 loc) · 5.89 KB
/
Copy pathAnimationDriverTests.cpp
File metadata and controls
168 lines (132 loc) · 5.89 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
/*
* 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 "AnimationTestsBase.h"
#include <react/renderer/animated/drivers/AnimationDriverUtils.h>
#include <react/renderer/core/ReactRootViewTagGenerator.h>
namespace facebook::react {
class AnimationDriverTests : public AnimationTestsBase {
protected:
double round(double value) noexcept {
// Round to 2 decimal places
return std::ceil(value * 100) / 100;
}
};
TEST_F(AnimationDriverTests, framesAnimation) {
initNodesManager();
auto rootTag = getNextRootViewTag();
auto valueNodeTag = ++rootTag;
nodesManager_->createAnimatedNode(
valueNodeTag,
folly::dynamic::object("type", "value")("value", 0)("offset", 0));
const auto animationId = 1;
const auto frames = folly::dynamic::array(0.0f, 0.1f, 0.4f, 0.9f, 1.0f);
const auto toValue = 100;
nodesManager_->startAnimatingNode(
animationId,
valueNodeTag,
folly::dynamic::object("type", "frames")("frames", frames)(
"toValue", toValue),
std::nullopt);
const double startTimeInTick = 12345;
runAnimationFrame(startTimeInTick);
EXPECT_EQ(round(nodesManager_->getValue(valueNodeTag).value()), 0);
runAnimationFrame(startTimeInTick + SingleFrameIntervalMs * 2.5);
EXPECT_EQ(round(nodesManager_->getValue(valueNodeTag).value()), 65);
runAnimationFrame(startTimeInTick + SingleFrameIntervalMs * 3);
EXPECT_EQ(round(nodesManager_->getValue(valueNodeTag).value()), 90);
runAnimationFrame(startTimeInTick + SingleFrameIntervalMs * 4);
EXPECT_EQ(round(nodesManager_->getValue(valueNodeTag).value()), toValue);
runAnimationFrame(startTimeInTick + SingleFrameIntervalMs * 10);
EXPECT_EQ(round(nodesManager_->getValue(valueNodeTag).value()), toValue);
}
TEST_F(AnimationDriverTests, framesAnimationReconfigurationClearsFrames) {
// This test verifies that when an animation is reconfigured via updateConfig,
// the frames array is cleared before adding new frames. Without clearing,
// frames would accumulate and cause incorrect animation behavior.
initNodesManager();
auto rootTag = getNextRootViewTag();
auto valueNodeTag = ++rootTag;
nodesManager_->createAnimatedNode(
valueNodeTag,
folly::dynamic::object("type", "value")("value", 0)("offset", 0));
const auto animationId = 1;
// First animation: 5 frames from 0 to 100
const auto frames1 = folly::dynamic::array(0.0f, 0.25f, 0.5f, 0.75f, 1.0f);
const auto toValue1 = 100;
nodesManager_->startAnimatingNode(
animationId,
valueNodeTag,
folly::dynamic::object("type", "frames")("frames", frames1)(
"toValue", toValue1),
std::nullopt);
const double startTimeInTick = 12345;
// Run first frame
runAnimationFrame(startTimeInTick);
EXPECT_EQ(round(nodesManager_->getValue(valueNodeTag).value()), 0);
// Reconfigure the same animation (same animationId) with new frames
// This triggers updateConfig on the existing FrameAnimationDriver
const auto frames2 = folly::dynamic::array(0.0f, 0.5f, 1.0f);
const auto toValue2 = 200;
nodesManager_->startAnimatingNode(
animationId,
valueNodeTag,
folly::dynamic::object("type", "frames")("frames", frames2)(
"toValue", toValue2),
std::nullopt);
// Reset animation timing
const double newStartTimeInTick = 20000;
// Run animation at halfway point (1 frame into 3-frame animation)
runAnimationFrame(newStartTimeInTick);
runAnimationFrame(newStartTimeInTick + SingleFrameIntervalMs * 1);
// At frame 1 of 3 frames (50% progress), value should be approximately:
// startValue (0) + 0.5 * (toValue2 - startValue) = 0 + 0.5 * 200 = 100
// If frames accumulated (5 + 3 = 8 frames), we'd be at wrong position
// Use ceil rounding so 100.00x becomes 100.01
EXPECT_EQ(round(nodesManager_->getValue(valueNodeTag).value()), 100.01);
// Complete the animation
runAnimationFrame(newStartTimeInTick + SingleFrameIntervalMs * 2);
EXPECT_EQ(round(nodesManager_->getValue(valueNodeTag).value()), toValue2);
}
TEST_F(AnimationDriverTests, framesAnimationDeferredStart) {
// Deferred start outputs frame 0 on the first update and re-anchors
// startFrameTimeMs_ so the second update also sees timeDelta=0.
// Without the defer the second frame would already be at value 25.
initNodesManager();
auto rootTag = getNextRootViewTag();
auto valueNodeTag = ++rootTag;
nodesManager_->createAnimatedNode(
valueNodeTag,
folly::dynamic::object("type", "value")("value", 0)("offset", 0));
const auto animationId = 1;
const auto frames = folly::dynamic::array(0.0f, 0.25f, 0.5f, 0.75f, 1.0f);
const auto toValue = 100;
nodesManager_->startAnimatingNode(
animationId,
valueNodeTag,
folly::dynamic::object("type", "frames")("frames", frames)(
"toValue", toValue)("deferredStart", true),
std::nullopt);
const double t = 12345;
// Frame 1: both with and without deferredStart, timeDelta=0 → value=0
runAnimationFrame(t);
EXPECT_EQ(nodesManager_->getValue(valueNodeTag).value(), 0);
// Frame 2: WITHOUT deferredStart timeDelta=SI → value≈25.
// WITH deferredStart the deferred start re-anchored startFrameTimeMs_, so
// timeDelta=0 → value=0. This assertion fails without deferredStart.
runAnimationFrame(t + SingleFrameIntervalMs);
EXPECT_EQ(nodesManager_->getValue(valueNodeTag).value(), 0);
// Frame 3: now timeDelta=SI from the re-anchored start
runAnimationFrame(t + SingleFrameIntervalMs * 2);
EXPECT_NEAR(nodesManager_->getValue(valueNodeTag).value(), 25, 0.01);
// Frame 4
runAnimationFrame(t + SingleFrameIntervalMs * 3);
EXPECT_NEAR(nodesManager_->getValue(valueNodeTag).value(), 50, 0.01);
// Complete
runAnimationFrame(t + SingleFrameIntervalMs * 5);
EXPECT_EQ(nodesManager_->getValue(valueNodeTag).value(), toValue);
}
} // namespace facebook::react