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