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