1 /*
2  * Copyright (c) 2024 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 #define private public
19 #define protected public
20 #include "frameworks/bridge/common/utils/transform_convertor.h"
21 #include "frameworks/bridge/common/utils/utils.h"
22 
23 #undef private
24 #undef protected
25 
26 using namespace testing;
27 using namespace testing::ext;
28 
29 namespace OHOS::Ace::Framework {
30 class UtilsTest : public testing::Test {
31 public:
SetUpTestSuite()32     static void SetUpTestSuite() {}
TearDownTestSuite()33     static void TearDownTestSuite() {}
34 };
35 
36 /**
37  * @tc.name: UtilsTest001
38  * @tc.desc: StepsCurveCreator
39  * @tc.type: FUNC
40  */
41 HWTEST_F(UtilsTest, UtilsTest001, TestSize.Level1)
42 {
43     /**
44      * @tc.steps: step1. Create a string container.
45      */
46     std::string curve = "";
47 
48     /**
49      * @tc.steps: step2. Testing illegal values.
50      * @tc.expected: return Curves::EASE_IN_OUT.
51      */
52     EXPECT_EQ(CreateCurve(curve), Curves::EASE_IN_OUT);
53     curve = "123)";
54     EXPECT_EQ(CreateCurve(curve, false), nullptr);
55     curve = "123)";
56     EXPECT_EQ(CreateCurve(curve), Curves::EASE_IN_OUT);
57     curve = "()";
58     EXPECT_EQ(CreateCurve(curve), Curves::EASE_IN_OUT);
59     curve = "steps()";
60     EXPECT_EQ(CreateCurve(curve), Curves::EASE_IN_OUT);
61     curve = "(test)";
62     EXPECT_EQ(CreateCurve(curve), Curves::EASE_IN_OUT);
63     curve = "steps(1,2,3,4,5)";
64     EXPECT_EQ(CreateCurve(curve), Curves::EASE_IN_OUT);
65     curve = "steps(0,aaaa)";
66     EXPECT_EQ(CreateCurve(curve), Curves::EASE_IN_OUT);
67     curve = "abc(1,aaaa)";
68     EXPECT_EQ(CreateCurve(curve), Curves::EASE_IN_OUT);
69     curve = "steps(1,aaaa)";
70     EXPECT_EQ(CreateCurve(curve), Curves::EASE_IN_OUT);
71     curve = "steps(1)";
72     EXPECT_NE(CreateCurve(curve), nullptr);
73     /**
74      * @tc.steps: step3. Normal testing
75      * @tc.expected: return Curve.
76      */
77     curve = "steps(1,start)";
78     EXPECT_NE(CreateCurve(curve), nullptr);
79     curve = "steps(1,end)";
80     EXPECT_NE(CreateCurve(curve), nullptr);
81     curve = "ease";
82     EXPECT_NE(CreateCurve(curve), nullptr);
83 }
84 
85 /**
86  * @tc.name: UtilsTest002
87  * @tc.desc: CubicCurveCreator、 SpringCurveCreator and InterpolatingSpringCreator
88  * @tc.type: FUNC
89  */
90 HWTEST_F(UtilsTest, UtilsTest002, TestSize.Level1)
91 {
92     /**
93      * @tc.steps: step1. Create a string container.
94      */
95 
96     std::string curve = "";
97     /**
98      * @tc.steps: step2. Testing illegal values.
99      * @tc.expected: return Curves::EASE_IN_OUT.
100      */
101 
102     curve = "cubic-bezier(1,2,3,4,5)";
103     EXPECT_EQ(CreateCurve(curve), Curves::EASE_IN_OUT);
104     curve = "spring(1,2,3,4,5)";
105     EXPECT_EQ(CreateCurve(curve), Curves::EASE_IN_OUT);
106     curve = "interpolating-spring(1,2,3,4,5)";
107     EXPECT_EQ(CreateCurve(curve), Curves::EASE_IN_OUT);
108     curve = "spring-motion(1,2,3,4,5)";
109     EXPECT_EQ(CreateCurve(curve), Curves::EASE_IN_OUT);
110     curve = "responsive-spring-motion(1,2,3,4,5)";
111     EXPECT_EQ(CreateCurve(curve), Curves::EASE_IN_OUT);
112     /**
113      * @tc.steps: step3. Normal testing
114      * @tc.expected: return Curve.
115      */
116 
117     curve = "cubic-bezier(10,10,10,10)";
118     EXPECT_NE(CreateCurve(curve), nullptr);
119     curve = "spring(10,10,10,10)";
120     EXPECT_NE(CreateCurve(curve), nullptr);
121     curve = "interpolating-spring(10,10,10,10)";
122     EXPECT_NE(CreateCurve(curve), nullptr);
123     curve = "spring-motion(10,10)";
124     EXPECT_NE(CreateCurve(curve), nullptr);
125     curve = "responsive-spring-motion(10,10)";
126     EXPECT_NE(CreateCurve(curve), nullptr);
127 }
128 
129 /**
130  * @tc.name: UtilsTest003
131  * @tc.desc: ParseBackgroundImagePosition
132  * @tc.type: FUNC
133  */
134 HWTEST_F(UtilsTest, UtilsTest003, TestSize.Level1)
135 {
136     /**
137      * @tc.steps: step1. Create a string container.
138      */
139 
140     std::string value = "";
141     BackgroundImagePosition backgroundImagePosition;
142     /**
143      * @tc.steps: step2. Testing illegal values.
144      * @tc.expected: return true or false.
145      */
146     ParseImageObjectPosition(value);
147     EXPECT_TRUE(ParseBackgroundImagePosition(value, backgroundImagePosition));
148     value = "10";
149     ParseImageObjectPosition(value);
150     EXPECT_TRUE(ParseBackgroundImagePosition(value, backgroundImagePosition));
151     value = "top";
152     ParseImageObjectPosition(value);
153     EXPECT_TRUE(ParseBackgroundImagePosition(value, backgroundImagePosition));
154     value = "10 10";
155     ParseImageObjectPosition(value);
156     EXPECT_FALSE(ParseBackgroundImagePosition(value, backgroundImagePosition));
157     value = "top 10";
158     ParseImageObjectPosition(value);
159     EXPECT_FALSE(ParseBackgroundImagePosition(value, backgroundImagePosition));
160     value = "10 left";
161     ParseImageObjectPosition(value);
162     EXPECT_FALSE(ParseBackgroundImagePosition(value, backgroundImagePosition));
163     value = "top left";
164     ParseImageObjectPosition(value);
165     EXPECT_TRUE(ParseBackgroundImagePosition(value, backgroundImagePosition));
166     value = "left top";
167     ParseImageObjectPosition(value);
168     value = "center center";
169     ParseImageObjectPosition(value);
170     EXPECT_TRUE(ParseBackgroundImagePosition(value, backgroundImagePosition));
171     /**
172      * @tc.steps: step3. Normal testing
173      * @tc.expected: return true or false.
174      */
175     value = "10% 10%";
176     ParseImageObjectPosition(value);
177     EXPECT_TRUE(ParseBackgroundImagePosition(value, backgroundImagePosition));
178     value = "10px 10px";
179     ParseImageObjectPosition(value);
180     EXPECT_TRUE(ParseBackgroundImagePosition(value, backgroundImagePosition));
181 }
182 
183 /**
184  * @tc.name: UtilsTest004
185  * @tc.desc: ParseRadialGradientSize
186  * @tc.type: FUNC
187  */
188 HWTEST_F(UtilsTest, UtilsTest004, TestSize.Level1)
189 {
190     /**
191      * @tc.steps: step1. Create a string container.
192      */
193     std::string value = "";
194     /**
195      * @tc.steps: step2. Testing illegal values.
196      * @tc.expected: return std::nullopt.
197      */
198     EXPECT_EQ(ParseRadialGradientSize(value), std::nullopt);
199     value = "10 10";
200     EXPECT_EQ(ParseRadialGradientSize(value), std::nullopt);
201     /**
202      * @tc.steps: step3. Normal testing
203      * @tc.expected: return RadialSizeType.
204      */
205     value = "closest-corner";
206     EXPECT_EQ(ParseRadialGradientSize(value), RadialSizeType::CLOSEST_CORNER);
207 }
208 
209 /**
210  * @tc.name: UtilsTest005
211  * @tc.desc: ParseRadialGradientSize
212  * @tc.type: FUNC
213  */
214 HWTEST_F(UtilsTest, UtilsTest005, TestSize.Level1)
215 {
216     /**
217      * @tc.steps: step1. Create a string container.
218      */
219     std::string value = "";
220     /**
221      * @tc.steps: step2. Testing illegal values.
222      * @tc.expected: return nullptr.
223      */
224     EXPECT_EQ(CreateClipPath(value), nullptr);
225     value = "test";
226     EXPECT_EQ(CreateClipPath(value), nullptr);
227     /**
228      * @tc.steps: step3. Normal testing
229      * @tc.expected: return ClipPath.
230      */
231     value = "border-box circle(";
232     EXPECT_NE(CreateClipPath(value), nullptr);
233 }
234 
235 /**
236  * @tc.name: UtilsTest006
237  * @tc.desc: ParseRadialGradientSize
238  * @tc.type: FUNC
239  */
240 HWTEST_F(UtilsTest, UtilsTest006, TestSize.Level1)
241 {
242     /**
243      * @tc.steps: step1. Create a string container.
244      */
245     std::string value = "";
246     BackgroundImageSize bgImgSize;
247     /**
248      * @tc.steps: step2. Testing illegal values.
249      * @tc.expected: return true.
250      */
251     EXPECT_TRUE(ParseBackgroundImageSize(value, bgImgSize));
252     value = "10 10";
253     EXPECT_TRUE(ParseBackgroundImageSize(value, bgImgSize));
254     value = "10% 10%";
255     EXPECT_TRUE(ParseBackgroundImageSize(value, bgImgSize));
256     value = "10px 10px";
257     EXPECT_TRUE(ParseBackgroundImageSize(value, bgImgSize));
258     /**
259      * @tc.steps: step3. Normal testing
260      * @tc.expected: return true or false.
261      */
262     value = "contain";
263     EXPECT_TRUE(ParseBackgroundImageSize(value, bgImgSize));
264     value = "10";
265     EXPECT_TRUE(ParseBackgroundImageSize(value, bgImgSize));
266     value = "10%";
267     EXPECT_TRUE(ParseBackgroundImageSize(value, bgImgSize));
268     value = "10px";
269     EXPECT_TRUE(ParseBackgroundImageSize(value, bgImgSize));
270 }
271 
272 /**
273  * @tc.name: UtilsTest007
274  * @tc.desc: ParseTransitionType
275  * @tc.type: FUNC
276  */
277 HWTEST_F(UtilsTest, UtilsTest007, TestSize.Level1)
278 {
279     /**
280      * @tc.steps: step1. Create a string container.
281      */
282     std::string value = "";
283 
284     /**
285      * @tc.steps: step2. Testing illegal values.
286      * @tc.expected: return TransitionType::ALL.
287      */
288 
289     EXPECT_EQ(ParseTransitionType(value), TransitionType::ALL);
290     /**
291      * @tc.steps: step3. Normal testing
292      * @tc.expected: return TransitionType.
293      */
294 
295     value = "All";
296     EXPECT_EQ(ParseTransitionType(value), TransitionType::ALL);
297     value = "Insert";
298     EXPECT_EQ(ParseTransitionType(value), TransitionType::APPEARING);
299     value = "Delete";
300     EXPECT_EQ(ParseTransitionType(value), TransitionType::DISAPPEARING);
301 }
302 
303 /**
304  * @tc.name: UtilsTest008
305  * @tc.desc: ParseTransitionType
306  * @tc.type: FUNC
307  */
308 HWTEST_F(UtilsTest, UtilsTest008, TestSize.Level1)
309 {
310     /**
311      * @tc.steps: step1. Create a string container.
312      */
313     std::string value = "";
314 
315     /**
316      * @tc.steps: step2. Testing illegal values.
317      * @tc.expected: return nullptr.
318      */
319     std::function<float(float)> fun;
320 
321     EXPECT_EQ(CreateCurve(fun), Curves::EASE_IN_OUT);
322     EXPECT_NE(CreateCurveExceptSpring(value, fun), nullptr);
323     /**
324      * @tc.steps: step3. Normal testing
325      * @tc.expected: return Curve.
326      */
327 
328     value = "ease";
329     EXPECT_NE(CreateCurveExceptSpring(value, fun), nullptr);
330     value = "cubic-bezier";
331     EXPECT_NE(CreateCurveExceptSpring(value, fun), nullptr);
__anon2b212a520102(float num) 332     fun = [](float num) { return num; };
333     EXPECT_NE(CreateCurveExceptSpring(value, fun), nullptr);
334     EXPECT_NE(CreateCurve(fun), nullptr);
335 }
336 
337 /**
338  * @tc.name: UtilsTest009
339  * @tc.desc: CreateCurveExceptSpring
340  * @tc.type: FUNC
341  */
342 HWTEST_F(UtilsTest, UtilsTest009, TestSize.Level1)
343 {
344     /**
345      * @tc.steps: step1. construct params
346      */
347     std::string value = "aaa(int a,int b)";
348     std::function<float(float)> fun = nullptr;
349     EXPECT_EQ(CreateCurveExceptSpring(value, fun) == Curves::EASE_IN_OUT, true);
350 }
351 
352 /**
353  * @tc.name: transformConvertorTest001
354  * @tc.desc: test func Convert
355  * @tc.type: FUNC
356  */
357 HWTEST_F(UtilsTest, transformConvertorTest001, TestSize.Level1)
358 {
359     /**
360      * @tc.steps: step1. construct params for Convert
361      */
362     TransformConvertor transformConvertor;
363     std::string key = "aaa";
364     std::string value = "bbb";
365     double time = 0.0;
366     transformConvertor.Convert(key, value, time);
367     key = "translate";
368     transformConvertor.Convert(key, value, time);
369 
370     /**
371      * @tc.steps: step2. construct params for AddAnimationToTweenOption
372      */
373     TweenOption tweenOption;
374     transformConvertor.AddAnimationToTweenOption(tweenOption);
375     EXPECT_EQ(tweenOption.GetTransformAnimations().size(), 1);
376     transformConvertor.InsertIdentityKeyframe(time);
377     EXPECT_EQ(transformConvertor.noneKeyframeTimes_.size() == 1, true);
378     transformConvertor.AddAnimationToTweenOption(tweenOption);
379     EXPECT_EQ(tweenOption.GetTransformAnimations().size(), 2);
380     AnimationType type = AnimationType::TRANSLATE;
381     RefPtr<Keyframe<TransformOperation>> keyframe;
382     transformConvertor.AddKeyframe(type, keyframe);
383 
384     /**
385      * @tc.steps: step3. construct params for ApplyCurve
386      */
387     RefPtr<Curve> curve;
388     transformConvertor.ApplyCurve(curve);
389     EXPECT_EQ(transformConvertor.operationList_.size(), 1);
390 }
391 } // namespace OHOS::Ace::Framework
392