1 /*
2 * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "gtest/gtest.h"
17
18 #include "frameworks/bridge/js_frontend/engine/quickjs/animation_bridge.h"
19
20 using namespace testing;
21 using namespace testing::ext;
22
23 namespace OHOS::Ace::Framework {
24 namespace {
25
26 const int32_t ANIMATION_ITERATIONS_DEFAULT = 1;
27 const int32_t ANIMATION_ITERATIONS_INFINITY = -1;
28 const double ANIMATION_DURATIONS_DEFAULT = 3000;
29 const double ANIMATION_DELAY_DEFAULT = 1000;
30 const double ANIMATION_FROM_DEFAULT = 100;
31 const double ANIMATION_TO_DEFAULT = 300;
32 const std::string ANIMATION_EASING_DEFAULT = "linear";
33 const std::string ANIMATION_FILL_DEFAULT = "forwards";
34 const std::string ANIMATION_FILL_NONE = "none";
35 const std::string ANIMATION_EASING_ANTICIPATE = "anticipate";
36 const std::string ANIMATION_EASING_CUBIC = "cubic-bezier(0.0, 0.0, 0.0, 1.0)";
37
38 const std::string PARAMS_DEFAULT_TEST = ""
39 "{ "
40 " \"duration\": 3000, "
41 " \"easing\": \"linear\", "
42 " \"delay\": 1000, "
43 " \"fill\": \"forwards\", "
44 " \"iterations\": 1, "
45 " \"begin\": 100, "
46 " \"end\": 300 "
47 "}";
48
49 const std::string ANTICIPATE_CURVE_TEST = ""
50 "{ "
51 " \"duration\": 3000, "
52 " \"easing\": \"anticipate\","
53 " \"delay\": 1000, "
54 " \"fill\": \"forwards\", "
55 " \"iterations\": 1, "
56 " \"begin\": 100, "
57 " \"end\": 300 "
58 "}";
59
60 const std::string CUBIC_CURVE_TEST = ""
61 "{ "
62 " \"duration\": 3000, "
63 " \"easing\": \"cubic-bezier(0.0, 0.0, 0.0, 1.0)\","
64 " \"delay\": 1000, "
65 " \"fill\": \"forwards\", "
66 " \"iterations\": 1, "
67 " \"begin\": 100, "
68 " \"end\": 300 "
69 "}";
70
71 const std::string ITERATIONS_INFINITY_TEST = ""
72 "{ "
73 " \"duration\": 3000, "
74 " \"easing\": \"linear\", "
75 " \"delay\": 1000, "
76 " \"fill\": \"forwards\", "
77 " \"iterations\": \"Infinity\", "
78 " \"begin\": 100, "
79 " \"end\": 300 "
80 "}";
81
82 const std::string ITERATIONS_STRING_TEST = ""
83 "{ "
84 " \"duration\": 3000, "
85 " \"easing\": \"linear\", "
86 " \"delay\": 1000, "
87 " \"fill\": \"forwards\", "
88 " \"iterations\": \"1\", "
89 " \"begin\": 100, "
90 " \"end\": 300 "
91 "}";
92
93 const std::string FILLMODE_STRING_TEST = ""
94 "{ "
95 " \"duration\": 3000, "
96 " \"easing\": \"linear\", "
97 " \"delay\": 1000, "
98 " \"fill\": \"none\", "
99 " \"iterations\": \"1\", "
100 " \"begin\": 100, "
101 " \"end\": 300 "
102 "}";
103
104 } // namespace
105
106 class AnimatorApiTest : public testing::Test, public BaseAnimationBridgeUtils {
107 public:
108 static void SetUpTestCase();
109 static void TearDownTestCase();
110 void SetUp() override;
111 void TearDown() override;
112 };
113
SetUpTestCase()114 void AnimatorApiTest::SetUpTestCase() {}
TearDownTestCase()115 void AnimatorApiTest::TearDownTestCase() {}
SetUp()116 void AnimatorApiTest::SetUp() {}
TearDown()117 void AnimatorApiTest::TearDown() {}
118
119 /**
120 * @tc.name: AnimatorApiTest001
121 * @tc.desc: Animation JS api parse
122 * @tc.type: FUNC
123 */
124 HWTEST_F(AnimatorApiTest, AnimatorApiTest001, TestSize.Level1)
125 {
126 /**
127 * @tc.steps: step1. parse the params of default.
128 */
129 int32_t iterations = 0;
130 std::unordered_map<std::string, double> animationDoubleParams;
131 std::unordered_map<std::string, std::string> animationStringParams;
132 JsParseAnimatorParams(PARAMS_DEFAULT_TEST, iterations, animationDoubleParams, animationStringParams);
133
134 /**
135 * @tc.steps: step2. get the params from the map.
136 */
137 auto easing = animationStringParams.find(DOM_ANIMATION_EASING)->second;
138 auto fill = animationStringParams.find(DOM_ANIMATION_FILL)->second;
139 auto duration = animationDoubleParams.find(DOM_ANIMATION_DURATION_API)->second;
140 auto delay = animationDoubleParams.find(DOM_ANIMATION_DELAY_API)->second;
141 auto from = animationDoubleParams.find(DOM_ANIMATION_BEGIN)->second;
142 auto to = animationDoubleParams.find(DOM_ANIMATION_END)->second;
143
144 /**
145 * @tc.steps: step3. Judge whether the parameter resolution is successful.
146 * @tc.expected: step3. The value of the params are as expected.
147 */
148 EXPECT_EQ(ANIMATION_ITERATIONS_DEFAULT, iterations);
149 EXPECT_EQ(ANIMATION_DURATIONS_DEFAULT, duration);
150 EXPECT_EQ(ANIMATION_DELAY_DEFAULT, delay);
151 EXPECT_EQ(ANIMATION_FROM_DEFAULT, from);
152 EXPECT_EQ(ANIMATION_TO_DEFAULT, to);
153 EXPECT_EQ(ANIMATION_EASING_DEFAULT, easing);
154 EXPECT_EQ(ANIMATION_FILL_DEFAULT, fill);
155 }
156
157 /**
158 * @tc.name: AnimatorApiTest002
159 * @tc.desc: Animation JS api parse
160 * @tc.type: FUNC
161 */
162 HWTEST_F(AnimatorApiTest, AnimatorApiTest002, TestSize.Level1)
163 {
164 /**
165 * @tc.steps: step1. parse the params of curve.
166 */
167 int32_t iterations = 0;
168 std::unordered_map<std::string, double> animationDoubleParams;
169 std::unordered_map<std::string, std::string> animationStringParams;
170 JsParseAnimatorParams(ANTICIPATE_CURVE_TEST, iterations, animationDoubleParams, animationStringParams);
171
172 /**
173 * @tc.steps: step2. get the params from the map.
174 */
175 auto easing = animationStringParams.find(DOM_ANIMATION_EASING)->second;
176 auto fill = animationStringParams.find(DOM_ANIMATION_FILL)->second;
177 auto duration = animationDoubleParams.find(DOM_ANIMATION_DURATION_API)->second;
178 auto delay = animationDoubleParams.find(DOM_ANIMATION_DELAY_API)->second;
179 auto from = animationDoubleParams.find(DOM_ANIMATION_BEGIN)->second;
180 auto to = animationDoubleParams.find(DOM_ANIMATION_END)->second;
181
182 /**
183 * @tc.steps: step3. Judge whether the parameter resolution is successful.
184 * @tc.expected: step3. The value of the params are as expected.
185 */
186 EXPECT_EQ(ANIMATION_ITERATIONS_DEFAULT, iterations);
187 EXPECT_EQ(ANIMATION_DURATIONS_DEFAULT, duration);
188 EXPECT_EQ(ANIMATION_DELAY_DEFAULT, delay);
189 EXPECT_EQ(ANIMATION_FROM_DEFAULT, from);
190 EXPECT_EQ(ANIMATION_TO_DEFAULT, to);
191 EXPECT_EQ(ANIMATION_EASING_ANTICIPATE, easing);
192 EXPECT_EQ(ANIMATION_FILL_DEFAULT, fill);
193 }
194
195 /**
196 * @tc.name: AnimatorApiTest003
197 * @tc.desc: Animation JS api parse
198 * @tc.type: FUNC
199 */
200 HWTEST_F(AnimatorApiTest, AnimatorApiTest003, TestSize.Level1)
201 {
202 /**
203 * @tc.steps: step1. parse the params of curve.
204 */
205 int32_t iterations = 0;
206 std::unordered_map<std::string, double> animationDoubleParams;
207 std::unordered_map<std::string, std::string> animationStringParams;
208 JsParseAnimatorParams(CUBIC_CURVE_TEST, iterations, animationDoubleParams, animationStringParams);
209
210 /**
211 * @tc.steps: step2. get the params from the map.
212 */
213 auto easing = animationStringParams.find(DOM_ANIMATION_EASING)->second;
214 auto fill = animationStringParams.find(DOM_ANIMATION_FILL)->second;
215 auto duration = animationDoubleParams.find(DOM_ANIMATION_DURATION_API)->second;
216 auto delay = animationDoubleParams.find(DOM_ANIMATION_DELAY_API)->second;
217 auto from = animationDoubleParams.find(DOM_ANIMATION_BEGIN)->second;
218 auto to = animationDoubleParams.find(DOM_ANIMATION_END)->second;
219
220 /**
221 * @tc.steps: step3. Judge whether the parameter resolution is successful.
222 * @tc.expected: step3. The value of the params are as expected.
223 */
224 EXPECT_EQ(ANIMATION_ITERATIONS_DEFAULT, iterations);
225 EXPECT_EQ(ANIMATION_DURATIONS_DEFAULT, duration);
226 EXPECT_EQ(ANIMATION_DELAY_DEFAULT, delay);
227 EXPECT_EQ(ANIMATION_FROM_DEFAULT, from);
228 EXPECT_EQ(ANIMATION_TO_DEFAULT, to);
229 EXPECT_EQ(ANIMATION_EASING_CUBIC, easing);
230 EXPECT_EQ(ANIMATION_FILL_DEFAULT, fill);
231 }
232
233 /**
234 * @tc.name: AnimatorApiTest004
235 * @tc.desc: Animation JS api parse
236 * @tc.type: FUNC
237 */
238 HWTEST_F(AnimatorApiTest, AnimatorApiTest004, TestSize.Level1)
239 {
240 /**
241 * @tc.steps: step1. parse the params of iterations.
242 */
243 int32_t iterations = 0;
244 std::unordered_map<std::string, double> animationDoubleParams;
245 std::unordered_map<std::string, std::string> animationStringParams;
246 JsParseAnimatorParams(ITERATIONS_INFINITY_TEST, iterations, animationDoubleParams, animationStringParams);
247
248 /**
249 * @tc.steps: step2. get the params from the map.
250 */
251 auto easing = animationStringParams.find(DOM_ANIMATION_EASING)->second;
252 auto fill = animationStringParams.find(DOM_ANIMATION_FILL)->second;
253 auto duration = animationDoubleParams.find(DOM_ANIMATION_DURATION_API)->second;
254 auto delay = animationDoubleParams.find(DOM_ANIMATION_DELAY_API)->second;
255 auto from = animationDoubleParams.find(DOM_ANIMATION_BEGIN)->second;
256 auto to = animationDoubleParams.find(DOM_ANIMATION_END)->second;
257
258 /**
259 * @tc.steps: step3. Judge whether the parameter resolution is successful.
260 * @tc.expected: step3. The value of the params are as expected.
261 */
262 EXPECT_EQ(ANIMATION_ITERATIONS_INFINITY, iterations);
263 EXPECT_EQ(ANIMATION_DURATIONS_DEFAULT, duration);
264 EXPECT_EQ(ANIMATION_DELAY_DEFAULT, delay);
265 EXPECT_EQ(ANIMATION_FROM_DEFAULT, from);
266 EXPECT_EQ(ANIMATION_TO_DEFAULT, to);
267 EXPECT_EQ(ANIMATION_EASING_DEFAULT, easing);
268 EXPECT_EQ(ANIMATION_FILL_DEFAULT, fill);
269 }
270
271 /**
272 * @tc.name: AnimatorApiTest005
273 * @tc.desc: Animation JS api parse
274 * @tc.type: FUNC
275 */
276 HWTEST_F(AnimatorApiTest, AnimatorApiTest005, TestSize.Level1)
277 {
278 /**
279 * @tc.steps: step1. parse the params of iterations.
280 */
281 int32_t iterations = 0;
282 std::unordered_map<std::string, double> animationDoubleParams;
283 std::unordered_map<std::string, std::string> animationStringParams;
284 JsParseAnimatorParams(ITERATIONS_STRING_TEST, iterations, animationDoubleParams, animationStringParams);
285
286 /**
287 * @tc.steps: step2. get the params from the map.
288 */
289 auto easing = animationStringParams.find(DOM_ANIMATION_EASING)->second;
290 auto fill = animationStringParams.find(DOM_ANIMATION_FILL)->second;
291 auto duration = animationDoubleParams.find(DOM_ANIMATION_DURATION_API)->second;
292 auto delay = animationDoubleParams.find(DOM_ANIMATION_DELAY_API)->second;
293 auto from = animationDoubleParams.find(DOM_ANIMATION_BEGIN)->second;
294 auto to = animationDoubleParams.find(DOM_ANIMATION_END)->second;
295
296 /**
297 * @tc.steps: step3. Judge whether the parameter resolution is successful.
298 * @tc.expected: step3. The value of the params are as expected.
299 */
300 EXPECT_EQ(ANIMATION_ITERATIONS_DEFAULT, iterations);
301 EXPECT_EQ(ANIMATION_DURATIONS_DEFAULT, duration);
302 EXPECT_EQ(ANIMATION_DELAY_DEFAULT, delay);
303 EXPECT_EQ(ANIMATION_FROM_DEFAULT, from);
304 EXPECT_EQ(ANIMATION_TO_DEFAULT, to);
305 EXPECT_EQ(ANIMATION_EASING_DEFAULT, easing);
306 EXPECT_EQ(ANIMATION_FILL_DEFAULT, fill);
307 }
308
309 /**
310 * @tc.name: AnimatorApiTest006
311 * @tc.desc: Animation JS api parse
312 * @tc.type: FUNC
313 */
314 HWTEST_F(AnimatorApiTest, AnimatorApiTest006, TestSize.Level1)
315 {
316 /**
317 * @tc.steps: step1. parse the params of fillmode.
318 */
319 int32_t iterations = 0;
320 std::unordered_map<std::string, double> animationDoubleParams;
321 std::unordered_map<std::string, std::string> animationStringParams;
322 JsParseAnimatorParams(FILLMODE_STRING_TEST, iterations, animationDoubleParams, animationStringParams);
323
324 /**
325 * @tc.steps: step2. get the params from the map.
326 */
327 auto easing = animationStringParams.find(DOM_ANIMATION_EASING)->second;
328 auto fill = animationStringParams.find(DOM_ANIMATION_FILL)->second;
329 auto duration = animationDoubleParams.find(DOM_ANIMATION_DURATION_API)->second;
330 auto delay = animationDoubleParams.find(DOM_ANIMATION_DELAY_API)->second;
331 auto from = animationDoubleParams.find(DOM_ANIMATION_BEGIN)->second;
332 auto to = animationDoubleParams.find(DOM_ANIMATION_END)->second;
333
334 /**
335 * @tc.steps: step3. Judge whether the parameter resolution is successful.
336 * @tc.expected: step3. The value of the params are as expected.
337 */
338 EXPECT_EQ(ANIMATION_ITERATIONS_DEFAULT, iterations);
339 EXPECT_EQ(ANIMATION_DURATIONS_DEFAULT, duration);
340 EXPECT_EQ(ANIMATION_DELAY_DEFAULT, delay);
341 EXPECT_EQ(ANIMATION_FROM_DEFAULT, from);
342 EXPECT_EQ(ANIMATION_TO_DEFAULT, to);
343 EXPECT_EQ(ANIMATION_EASING_DEFAULT, easing);
344 EXPECT_EQ(ANIMATION_FILL_NONE, fill);
345 }
346
347 } // namespace OHOS::Ace::Framework
348