1 /*
2  * Copyright (c) 2023 iSoftStone Information Technology (Group) 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 <memory>
17 
18 #include "gtest/gtest.h"
19 
20 #include "base/geometry/dimension.h"
21 #include "base/json/json_util.h"
22 #include "core/components/common/layout/constants.h"
23 #include "core/components/common/properties/color.h"
24 #include "core/components_ng/property/border_property.h"
25 
26 using namespace testing;
27 using namespace testing::ext;
28 
29 class BorderPropertyTestNg : public testing::Test {};
30 
31 namespace OHOS::Ace::NG {
32 namespace {
33 const InspectorFilter filter;
34 }
35 
36 /**
37  * @tc.name: BorderPropertyTestNg001
38  * @tc.desc: Test BorderStyleProperty::ToJsonValue function.
39  * @tc.type: FUNC
40  */
41 HWTEST_F(BorderPropertyTestNg, BorderPropertyTestNg001, TestSize.Level1)
42 {
43     BorderStyleProperty borderStyleProperty;
44     borderStyleProperty.multiValued = true;
45 
46     /**
47      * @tc.steps: step1. set multiValued is true, use the default borderStyle and call ToJsonValue
48      * @tc.expected: the return value is false.
49      */
50     auto json = JsonUtil::Create(true);
51     auto borderJson = JsonUtil::Create(true);
52     borderStyleProperty.ToJsonValue(json, borderJson, filter);
53 
54     auto ret = json->GetValue("borderStyle")->GetString("left", "");
55     EXPECT_EQ(ret, "BorderStyle.Solid");
56     ret = json->GetValue("borderStyle")->GetString("top", "");
57     EXPECT_EQ(ret, "BorderStyle.Solid");
58     ret = json->GetValue("borderStyle")->GetString("right", "");
59     EXPECT_EQ(ret, "BorderStyle.Solid");
60     ret = json->GetValue("borderStyle")->GetString("bottom", "");
61     EXPECT_EQ(ret, "BorderStyle.Solid");
62 
63     /**
64      * @tc.steps: step2. set multiValued is true, use the default borderStyle and call ToJsonValue
65      * @tc.expected: the return value is false.
66      */
67     json = JsonUtil::Create(true);
68     borderJson = JsonUtil::Create(true);
69     borderStyleProperty.styleLeft = BorderStyle::DASHED;
70     borderStyleProperty.styleTop = BorderStyle::DOTTED;
71     borderStyleProperty.styleRight = BorderStyle::NONE;
72     borderStyleProperty.styleBottom = BorderStyle::DASHED;
73     borderStyleProperty.ToJsonValue(json, borderJson, filter);
74 
75     ret = json->GetValue("borderStyle")->GetString("left", "");
76     EXPECT_EQ(ret, "BorderStyle.Dashed");
77     ret = json->GetValue("borderStyle")->GetString("top", "");
78     EXPECT_EQ(ret, "BorderStyle.Dotted");
79     ret = json->GetValue("borderStyle")->GetString("right", "");
80     EXPECT_EQ(ret, "BorderStyle.None");
81     ret = json->GetValue("borderStyle")->GetString("bottom", "");
82     EXPECT_EQ(ret, "BorderStyle.Dashed");
83 }
84 
85 /**
86  * @tc.name: BorderPropertyTestNg002
87  * @tc.desc: Test BorderWidthPropertyT<Dimension>::ToString function.
88  * @tc.type: FUNC
89  */
90 HWTEST_F(BorderPropertyTestNg, BorderPropertyTestNg002, TestSize.Level1)
91 {
92     /**
93      * @tc.steps: step1. set SetBorderWidth is 1px and call ToString
94      * @tc.expected: the return value is "leftDimen: [1.00px]rightDimen: [1.00px]topDimen: [1.00px]bottomDimen:
95      * [1.00px]".
96      */
97     BorderWidthPropertyT<Dimension> borderWidthProperty;
98     auto borderWidth = Dimension(1.0f);
99     borderWidthProperty.SetBorderWidth(borderWidth);
100     auto ret = borderWidthProperty.ToString();
101     EXPECT_EQ(ret, "[1.00px,1.00px,1.00px,1.00px]");
102 }
103 
104 /**
105  * @tc.name: BorderPropertyTestNg003
106  * @tc.desc: Test BorderWidthPropertyT<Dimension>::ToJsonValue function.
107  * @tc.type: FUNC
108  */
109 HWTEST_F(BorderPropertyTestNg, BorderPropertyTestNg003, TestSize.Level1)
110 {
111     BorderWidthPropertyT<Dimension> borderWidthProperty;
112     borderWidthProperty.multiValued = true;
113 
114     /**
115      * @tc.steps: step1. set multiValued is true, use the default borderWidth and call ToJsonValue
116      * @tc.expected: the expected value is left: 0.00vp, top: 0.00vp, right: 0.00vp, bottom: 0.00vp.
117      */
118     auto json = JsonUtil::Create(true);
119     auto borderJson = JsonUtil::Create(true);
120     borderWidthProperty.ToJsonValue(json, borderJson, filter);
121 
122     auto ret = json->GetValue("borderWidth")->GetString("left", "");
123     EXPECT_EQ(ret, "0.00vp");
124     ret = json->GetValue("borderWidth")->GetString("top", "");
125     EXPECT_EQ(ret, "0.00vp");
126     ret = json->GetValue("borderWidth")->GetString("right", "");
127     EXPECT_EQ(ret, "0.00vp");
128     ret = json->GetValue("borderWidth")->GetString("bottom", "");
129     EXPECT_EQ(ret, "0.00vp");
130 
131     /**
132      * @tc.steps: step2. set multiValued is true, set the borderStyle 1px and call ToJsonValue
133      * @tc.expected: the expected value is left: 1.00px, top: 1.00px, right: 1.00px, bottom: 1.00px.
134      */
135     json = JsonUtil::Create(true);
136     borderJson = JsonUtil::Create(true);
137     auto borderWidth = Dimension(1.0f);
138     borderWidthProperty.SetBorderWidth(borderWidth);
139     borderWidthProperty.ToJsonValue(json, borderJson, filter);
140 
141     ret = json->GetValue("borderWidth")->GetString("left", "");
142     EXPECT_EQ(ret, "1.00px");
143     ret = json->GetValue("borderWidth")->GetString("top", "");
144     EXPECT_EQ(ret, "1.00px");
145     ret = json->GetValue("borderWidth")->GetString("right", "");
146     EXPECT_EQ(ret, "1.00px");
147     ret = json->GetValue("borderWidth")->GetString("bottom", "");
148     EXPECT_EQ(ret, "1.00px");
149 }
150 
151 /**
152  * @tc.name: BorderPropertyTestNg004
153  * @tc.desc: Test BorderColorProperty::ToJsonValue function.
154  * @tc.type: FUNC
155  */
156 HWTEST_F(BorderPropertyTestNg, BorderPropertyTestNg004, TestSize.Level1)
157 {
158     BorderColorProperty borderColorProperty;
159     borderColorProperty.multiValued = true;
160 
161     /**
162      * @tc.steps: step1. set multiValued is true, use the default borderColor and call ToJsonValue
163      * @tc.expected: the expected value is left: #FF000000, top: #FF000000, right: #FF000000, bottom: #FF000000.
164      */
165     auto json = JsonUtil::Create(true);
166     auto borderJson = JsonUtil::Create(true);
167     borderColorProperty.ToJsonValue(json, borderJson, filter);
168 
169     auto borderColor = json->GetValue("borderColor");
170     auto left = borderColor->GetString("left", "");
171     EXPECT_EQ(left, "#FF000000");
172     auto top = borderColor->GetString("top", "");
173     EXPECT_EQ(top, "#FF000000");
174     auto right = borderColor->GetString("right", "");
175     EXPECT_EQ(right, "#FF000000");
176     auto bottom = borderColor->GetString("bottom", "");
177     EXPECT_EQ(bottom, "#FF000000");
178 
179     /**
180      * @tc.steps: step2. set multiValued is true, set the borderColor blue and call ToJsonValue
181      * @tc.expected: the expected value is left: #FF0000FF, top: #FF0000FF, right: #FF0000FF, bottom: #FF0000FF.
182      */
183     json = JsonUtil::Create(true);
184     borderJson = JsonUtil::Create(true);
185     borderColorProperty.SetColor(Color::BLUE);
186     borderColorProperty.ToJsonValue(json, borderJson, filter);
187 
188     borderColor = json->GetValue("borderColor");
189     left = borderColor->GetString("left", "");
190     EXPECT_EQ(left, "#FF0000FF");
191     top = borderColor->GetString("top", "");
192     EXPECT_EQ(top, "#FF0000FF");
193     right = borderColor->GetString("right", "");
194     EXPECT_EQ(right, "#FF0000FF");
195     bottom = borderColor->GetString("bottom", "");
196     EXPECT_EQ(bottom, "#FF0000FF");
197 }
198 
199 /**
200  * @tc.name: BorderPropertyTestNg005
201  * @tc.desc: Test BorderRadiusPropertyT<Dimension>::ToJsonValue function.
202  * @tc.type: FUNC
203  */
204 HWTEST_F(BorderPropertyTestNg, BorderPropertyTestNg005, TestSize.Level1)
205 {
206     BorderRadiusPropertyT<Dimension> borderRadiusProperty;
207     borderRadiusProperty.multiValued = false;
208 
209     /**
210      * @tc.steps: step1. set multiValued is true, use the default borderRadius and call ToJsonValue
211      * @tc.expected: the expected value is borderRadius: 0.00vp.
212      */
213     auto json = JsonUtil::Create(true);
214     auto borderJson = JsonUtil::Create(true);
215     borderRadiusProperty.ToJsonValue(json, borderJson, filter);
216 
217     auto borderRadius = json->GetString("borderRadius");
218     EXPECT_EQ(borderRadius, "0.00vp");
219 
220     /**
221      * @tc.steps: step2. set multiValued is true, set the borderRadius 1px and call ToJsonValue
222      * @tc.expected: the expected value is borderRadius: 1.00px.
223      */
224     json = JsonUtil::Create(true);
225     borderJson = JsonUtil::Create(true);
226     auto borderRadiusVal = Dimension(1.0f);
227     borderRadiusProperty.SetRadius(borderRadiusVal);
228     borderRadiusProperty.ToJsonValue(json, borderJson, filter);
229 
230     borderRadius = json->GetString("borderRadius");
231     EXPECT_EQ(borderRadius, "1.00px");
232 }
233 
234 /**
235  * @tc.name: BorderPropertyTestNg006
236  * @tc.desc: Test BorderRadiusPropertyT<Dimension>::UpdateWithCheck function.
237  * @tc.type: FUNC
238  */
239 HWTEST_F(BorderPropertyTestNg, BorderPropertyTestNg006, TestSize.Level1)
240 {
241     BorderRadiusPropertyT<Dimension> borderRadiusProperty;
242 
243     /**
244      * @tc.steps: step1. set a borderRadiusProperty topLeft as 1.0px and call UpdateWithCheck
245      * @tc.expected: the return value is true.
246      */
247     BorderRadiusPropertyT<Dimension> newBorderRadiusProperty;
248     newBorderRadiusProperty.radiusTopLeft = Dimension(1.0);
249     auto retFlag = borderRadiusProperty.UpdateWithCheck(newBorderRadiusProperty);
250     EXPECT_TRUE(retFlag);
251 
252     /**
253      * @tc.steps: step2. set a borderRadiusProperty topRight as 1.0px and call UpdateWithCheck
254      * @tc.expected: the return value is true.
255      */
256     borderRadiusProperty = BorderRadiusPropertyT<Dimension>();
257     newBorderRadiusProperty = BorderRadiusPropertyT<Dimension>();
258     newBorderRadiusProperty.radiusTopRight = Dimension(1.0);
259     retFlag = borderRadiusProperty.UpdateWithCheck(newBorderRadiusProperty);
260     EXPECT_TRUE(retFlag);
261 
262     /**
263      * @tc.steps: step3. set a borderRadiusProperty bottomRight as 1.0px and call UpdateWithCheck
264      * @tc.expected: the return value is true.
265      */
266     borderRadiusProperty = BorderRadiusPropertyT<Dimension>();
267     newBorderRadiusProperty = BorderRadiusPropertyT<Dimension>();
268     newBorderRadiusProperty.radiusBottomRight = Dimension(1.0);
269     retFlag = borderRadiusProperty.UpdateWithCheck(newBorderRadiusProperty);
270     EXPECT_TRUE(retFlag);
271 
272     /**
273      * @tc.steps: step4. set a borderRadiusProperty bottomLeft as 1.0px and call UpdateWithCheck
274      * @tc.expected: the return value is true.
275      */
276     borderRadiusProperty = BorderRadiusPropertyT<Dimension>();
277     newBorderRadiusProperty = BorderRadiusPropertyT<Dimension>();
278     newBorderRadiusProperty.radiusBottomLeft = Dimension(1.0);
279     retFlag = borderRadiusProperty.UpdateWithCheck(newBorderRadiusProperty);
280     EXPECT_TRUE(retFlag);
281 
282     /**
283      * @tc.steps: step5. do not set any borderRadius and call UpdateWithCheck
284      * @tc.expected: the return value is false.
285      */
286     borderRadiusProperty = BorderRadiusPropertyT<Dimension>();
287     newBorderRadiusProperty = BorderRadiusPropertyT<Dimension>();
288     retFlag = borderRadiusProperty.UpdateWithCheck(newBorderRadiusProperty);
289     EXPECT_FALSE(retFlag);
290 }
291 
292 /**
293  * @tc.name: BorderPropertyTestNg007
294  * @tc.desc: Test BorderRadiusPropertyT<Dimension>::ToString function.
295  * @tc.type: FUNC
296  */
297 HWTEST_F(BorderPropertyTestNg, BorderPropertyTestNg007, TestSize.Level1)
298 {
299     BorderColorProperty borderColorProperty;
300     string strResult = borderColorProperty.ToString();
301     const string strTemp = "leftColor: [NA]rightColor: [NA]topColor: [NA]bottomColor: [NA]";
302     EXPECT_EQ(strTemp, strResult);
303 
304     Color cTemp = Color::FromARGB(25, 0, 0, 0);
305     borderColorProperty.SetColor(cTemp);
306     EXPECT_TRUE(borderColorProperty.leftColor.has_value());
307     strResult = borderColorProperty.ToString();
308     EXPECT_NE(strTemp, strResult);
309 
310     BorderWidthPropertyT<Dimension> borderWidthProperty;
311     BorderWidthPropertyT<Dimension> borderWidthProperty2;
312     Dimension dimenTemp(2.0, DimensionUnit::PX);
313     borderWidthProperty.SetBorderWidth(dimenTemp);
314     EXPECT_TRUE(borderWidthProperty.leftDimen.has_value());
315     EXPECT_TRUE(borderWidthProperty.topDimen.has_value());
316     EXPECT_TRUE(borderWidthProperty.rightDimen.has_value());
317     EXPECT_TRUE(borderWidthProperty.bottomDimen.has_value());
318     borderWidthProperty2.SetBorderWidth(dimenTemp);
319     EXPECT_EQ(borderWidthProperty, borderWidthProperty2);
320 }
321 } // namespace OHOS::Ace::NG
322