1 /*
2 * Copyright (c) 2021 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 * @tc.require: AR000FL0UV
124 * @tc.author: jiachunhui
125 */
126 HWTEST_F(AnimatorApiTest, AnimatorApiTest001, TestSize.Level1)
127 {
128 /**
129 * @tc.steps: step1. parse the params of default.
130 */
131 int32_t iterations = 0;
132 std::unordered_map<std::string, double> animationDoubleParams;
133 std::unordered_map<std::string, std::string> animationStringParams;
134 JsParseAnimatorParams(PARAMS_DEFAULT_TEST, iterations, animationDoubleParams, animationStringParams);
135
136 /**
137 * @tc.steps: step2. get the params from the map.
138 */
139 auto easing = animationStringParams.find(DOM_ANIMATION_EASING)->second;
140 auto fill = animationStringParams.find(DOM_ANIMATION_FILL)->second;
141 auto duration = animationDoubleParams.find(DOM_ANIMATION_DURATION_API)->second;
142 auto delay = animationDoubleParams.find(DOM_ANIMATION_DELAY_API)->second;
143 auto from = animationDoubleParams.find(DOM_ANIMATION_BEGIN)->second;
144 auto to = animationDoubleParams.find(DOM_ANIMATION_END)->second;
145
146 /**
147 * @tc.steps: step3. Judge whether the parameter resolution is successful.
148 * @tc.expected: step3. The value of the params are as expected.
149 */
150 EXPECT_EQ(ANIMATION_ITERATIONS_DEFAULT, iterations);
151 EXPECT_EQ(ANIMATION_DURATIONS_DEFAULT, duration);
152 EXPECT_EQ(ANIMATION_DELAY_DEFAULT, delay);
153 EXPECT_EQ(ANIMATION_FROM_DEFAULT, from);
154 EXPECT_EQ(ANIMATION_TO_DEFAULT, to);
155 EXPECT_EQ(ANIMATION_EASING_DEFAULT, easing);
156 EXPECT_EQ(ANIMATION_FILL_DEFAULT, fill);
157 }
158
159 /**
160 * @tc.name: AnimatorApiTest002
161 * @tc.desc: Animation JS api parse
162 * @tc.type: FUNC
163 * @tc.require: AR000FL0UV
164 * @tc.author: jiachunhui
165 */
166 HWTEST_F(AnimatorApiTest, AnimatorApiTest002, TestSize.Level1)
167 {
168 /**
169 * @tc.steps: step1. parse the params of curve.
170 */
171 int32_t iterations = 0;
172 std::unordered_map<std::string, double> animationDoubleParams;
173 std::unordered_map<std::string, std::string> animationStringParams;
174 JsParseAnimatorParams(ANTICIPATE_CURVE_TEST, iterations, animationDoubleParams, animationStringParams);
175
176 /**
177 * @tc.steps: step2. get the params from the map.
178 */
179 auto easing = animationStringParams.find(DOM_ANIMATION_EASING)->second;
180 auto fill = animationStringParams.find(DOM_ANIMATION_FILL)->second;
181 auto duration = animationDoubleParams.find(DOM_ANIMATION_DURATION_API)->second;
182 auto delay = animationDoubleParams.find(DOM_ANIMATION_DELAY_API)->second;
183 auto from = animationDoubleParams.find(DOM_ANIMATION_BEGIN)->second;
184 auto to = animationDoubleParams.find(DOM_ANIMATION_END)->second;
185
186 /**
187 * @tc.steps: step3. Judge whether the parameter resolution is successful.
188 * @tc.expected: step3. The value of the params are as expected.
189 */
190 EXPECT_EQ(ANIMATION_ITERATIONS_DEFAULT, iterations);
191 EXPECT_EQ(ANIMATION_DURATIONS_DEFAULT, duration);
192 EXPECT_EQ(ANIMATION_DELAY_DEFAULT, delay);
193 EXPECT_EQ(ANIMATION_FROM_DEFAULT, from);
194 EXPECT_EQ(ANIMATION_TO_DEFAULT, to);
195 EXPECT_EQ(ANIMATION_EASING_ANTICIPATE, easing);
196 EXPECT_EQ(ANIMATION_FILL_DEFAULT, fill);
197 }
198
199 /**
200 * @tc.name: AnimatorApiTest003
201 * @tc.desc: Animation JS api parse
202 * @tc.type: FUNC
203 * @tc.require: AR000FL0UV
204 * @tc.author: jiachunhui
205 */
206 HWTEST_F(AnimatorApiTest, AnimatorApiTest003, TestSize.Level1)
207 {
208 /**
209 * @tc.steps: step1. parse the params of curve.
210 */
211 int32_t iterations = 0;
212 std::unordered_map<std::string, double> animationDoubleParams;
213 std::unordered_map<std::string, std::string> animationStringParams;
214 JsParseAnimatorParams(CUBIC_CURVE_TEST, iterations, animationDoubleParams, animationStringParams);
215
216 /**
217 * @tc.steps: step2. get the params from the map.
218 */
219 auto easing = animationStringParams.find(DOM_ANIMATION_EASING)->second;
220 auto fill = animationStringParams.find(DOM_ANIMATION_FILL)->second;
221 auto duration = animationDoubleParams.find(DOM_ANIMATION_DURATION_API)->second;
222 auto delay = animationDoubleParams.find(DOM_ANIMATION_DELAY_API)->second;
223 auto from = animationDoubleParams.find(DOM_ANIMATION_BEGIN)->second;
224 auto to = animationDoubleParams.find(DOM_ANIMATION_END)->second;
225
226 /**
227 * @tc.steps: step3. Judge whether the parameter resolution is successful.
228 * @tc.expected: step3. The value of the params are as expected.
229 */
230 EXPECT_EQ(ANIMATION_ITERATIONS_DEFAULT, iterations);
231 EXPECT_EQ(ANIMATION_DURATIONS_DEFAULT, duration);
232 EXPECT_EQ(ANIMATION_DELAY_DEFAULT, delay);
233 EXPECT_EQ(ANIMATION_FROM_DEFAULT, from);
234 EXPECT_EQ(ANIMATION_TO_DEFAULT, to);
235 EXPECT_EQ(ANIMATION_EASING_CUBIC, easing);
236 EXPECT_EQ(ANIMATION_FILL_DEFAULT, fill);
237 }
238
239 /**
240 * @tc.name: AnimatorApiTest004
241 * @tc.desc: Animation JS api parse
242 * @tc.type: FUNC
243 * @tc.require: AR000FL0V0
244 * @tc.author: jiachunhui
245 */
246 HWTEST_F(AnimatorApiTest, AnimatorApiTest004, TestSize.Level1)
247 {
248 /**
249 * @tc.steps: step1. parse the params of iterations.
250 */
251 int32_t iterations = 0;
252 std::unordered_map<std::string, double> animationDoubleParams;
253 std::unordered_map<std::string, std::string> animationStringParams;
254 JsParseAnimatorParams(ITERATIONS_INFINITY_TEST, iterations, animationDoubleParams, animationStringParams);
255
256 /**
257 * @tc.steps: step2. get the params from the map.
258 */
259 auto easing = animationStringParams.find(DOM_ANIMATION_EASING)->second;
260 auto fill = animationStringParams.find(DOM_ANIMATION_FILL)->second;
261 auto duration = animationDoubleParams.find(DOM_ANIMATION_DURATION_API)->second;
262 auto delay = animationDoubleParams.find(DOM_ANIMATION_DELAY_API)->second;
263 auto from = animationDoubleParams.find(DOM_ANIMATION_BEGIN)->second;
264 auto to = animationDoubleParams.find(DOM_ANIMATION_END)->second;
265
266 /**
267 * @tc.steps: step3. Judge whether the parameter resolution is successful.
268 * @tc.expected: step3. The value of the params are as expected.
269 */
270 EXPECT_EQ(ANIMATION_ITERATIONS_INFINITY, iterations);
271 EXPECT_EQ(ANIMATION_DURATIONS_DEFAULT, duration);
272 EXPECT_EQ(ANIMATION_DELAY_DEFAULT, delay);
273 EXPECT_EQ(ANIMATION_FROM_DEFAULT, from);
274 EXPECT_EQ(ANIMATION_TO_DEFAULT, to);
275 EXPECT_EQ(ANIMATION_EASING_DEFAULT, easing);
276 EXPECT_EQ(ANIMATION_FILL_DEFAULT, fill);
277 }
278
279 /**
280 * @tc.name: AnimatorApiTest005
281 * @tc.desc: Animation JS api parse
282 * @tc.type: FUNC
283 * @tc.require: AR000FL0V1
284 * @tc.author: jiachunhui
285 */
286 HWTEST_F(AnimatorApiTest, AnimatorApiTest005, TestSize.Level1)
287 {
288 /**
289 * @tc.steps: step1. parse the params of iterations.
290 */
291 int32_t iterations = 0;
292 std::unordered_map<std::string, double> animationDoubleParams;
293 std::unordered_map<std::string, std::string> animationStringParams;
294 JsParseAnimatorParams(ITERATIONS_STRING_TEST, iterations, animationDoubleParams, animationStringParams);
295
296 /**
297 * @tc.steps: step2. get the params from the map.
298 */
299 auto easing = animationStringParams.find(DOM_ANIMATION_EASING)->second;
300 auto fill = animationStringParams.find(DOM_ANIMATION_FILL)->second;
301 auto duration = animationDoubleParams.find(DOM_ANIMATION_DURATION_API)->second;
302 auto delay = animationDoubleParams.find(DOM_ANIMATION_DELAY_API)->second;
303 auto from = animationDoubleParams.find(DOM_ANIMATION_BEGIN)->second;
304 auto to = animationDoubleParams.find(DOM_ANIMATION_END)->second;
305
306 /**
307 * @tc.steps: step3. Judge whether the parameter resolution is successful.
308 * @tc.expected: step3. The value of the params are as expected.
309 */
310 EXPECT_EQ(ANIMATION_ITERATIONS_DEFAULT, iterations);
311 EXPECT_EQ(ANIMATION_DURATIONS_DEFAULT, duration);
312 EXPECT_EQ(ANIMATION_DELAY_DEFAULT, delay);
313 EXPECT_EQ(ANIMATION_FROM_DEFAULT, from);
314 EXPECT_EQ(ANIMATION_TO_DEFAULT, to);
315 EXPECT_EQ(ANIMATION_EASING_DEFAULT, easing);
316 EXPECT_EQ(ANIMATION_FILL_DEFAULT, fill);
317 }
318
319 /**
320 * @tc.name: AnimatorApiTest006
321 * @tc.desc: Animation JS api parse
322 * @tc.type: FUNC
323 * @tc.require: AR000FL0V2
324 * @tc.author: jiachunhui
325 */
326 HWTEST_F(AnimatorApiTest, AnimatorApiTest006, TestSize.Level1)
327 {
328 /**
329 * @tc.steps: step1. parse the params of fillmode.
330 */
331 int32_t iterations = 0;
332 std::unordered_map<std::string, double> animationDoubleParams;
333 std::unordered_map<std::string, std::string> animationStringParams;
334 JsParseAnimatorParams(FILLMODE_STRING_TEST, iterations, animationDoubleParams, animationStringParams);
335
336 /**
337 * @tc.steps: step2. get the params from the map.
338 */
339 auto easing = animationStringParams.find(DOM_ANIMATION_EASING)->second;
340 auto fill = animationStringParams.find(DOM_ANIMATION_FILL)->second;
341 auto duration = animationDoubleParams.find(DOM_ANIMATION_DURATION_API)->second;
342 auto delay = animationDoubleParams.find(DOM_ANIMATION_DELAY_API)->second;
343 auto from = animationDoubleParams.find(DOM_ANIMATION_BEGIN)->second;
344 auto to = animationDoubleParams.find(DOM_ANIMATION_END)->second;
345
346 /**
347 * @tc.steps: step3. Judge whether the parameter resolution is successful.
348 * @tc.expected: step3. The value of the params are as expected.
349 */
350 EXPECT_EQ(ANIMATION_ITERATIONS_DEFAULT, iterations);
351 EXPECT_EQ(ANIMATION_DURATIONS_DEFAULT, duration);
352 EXPECT_EQ(ANIMATION_DELAY_DEFAULT, delay);
353 EXPECT_EQ(ANIMATION_FROM_DEFAULT, from);
354 EXPECT_EQ(ANIMATION_TO_DEFAULT, to);
355 EXPECT_EQ(ANIMATION_EASING_DEFAULT, easing);
356 EXPECT_EQ(ANIMATION_FILL_NONE, fill);
357 }
358
359 } // namespace OHOS::Ace::Framework
360