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 "gtest/gtest.h"
17 
18 #define protected public
19 #define private public
20 
21 #include "core/components_ng/render/shape_painter.h"
22 
23 #include "core/components/common/properties/color.h"
24 #include "core/components/common/properties/paint_state.h"
25 #include "core/components_ng/render/drawing.h"
26 #include "core/components_ng/render/drawing_prop_convertor.h"
27 
28 #undef private
29 #undef protected
30 
31 using namespace testing;
32 using namespace testing::ext;
33 
34 namespace OHOS::Ace {
35 namespace {
36 constexpr double STROKE_OPACITY = 1.0;
37 constexpr double STROKE_MITER_LIMIT = 1.0;
38 constexpr double FILL_OPACITY = 2.0;
39 
40 const Color STROKE_COLOR_RED = Color::RED;
41 const Color FILL_COLOR_BLUE = Color::BLUE;
42 
43 const Dimension STROKE_WIDTH_5 {5.0, DimensionUnit::PX};
44 const Dimension STROKE_WIDTH_0 {0.0, DimensionUnit::PX};
45 const Dimension STROKE_DASH_0 {0.0, DimensionUnit::PX};
46 const Dimension STROKE_DASH_1 {1.0, DimensionUnit::PX};
47 const Dimension STROKE_DASH_2 {2.0, DimensionUnit::PX};
48 const Dimension STROKE_DASH_OFFSET {1.0, DimensionUnit::PX};
49 }
50 
51 class ShapePainterTestNg : public testing::Test {
52 };
53 
54 /**
55  * @tc.name: ShapePainterTestNg001
56  * @tc.desc: Test cast to ShapePainterTestNg
57  * @tc.type: FUNC
58  */
59 HWTEST_F(ShapePainterTestNg, ShapePainterTestNg001, TestSize.Level1)
60 {
61     /**
62      * @tc.steps1: create testPen and shapePaintProperty.
63      */
64     RSPen testPen;
65     NG::ShapePaintProperty shapePaintProperty;
66 
67     /**
68      * @tc.steps2: call SetPen.
69      * @tc.expected: the retrun value is true.
70      */
71     bool retSetPen = NG::ShapePainter::SetPen(testPen, shapePaintProperty);
72     EXPECT_TRUE(retSetPen);
73 
74     /**
75      * @tc.steps3: call SetPen and UpdateStrokeWidth with STROKE_WIDTH_5.
76      * @tc.expected: the retrun value is true.
77      */
78     shapePaintProperty.UpdateStrokeWidth(STROKE_WIDTH_5);
79     retSetPen = NG::ShapePainter::SetPen(testPen, shapePaintProperty);
80     EXPECT_TRUE(retSetPen);
81 
82     /**
83      * @tc.steps4: call SetPen and UpdateStrokeWidth with STROKE_WIDTH_0.
84      * @tc.expected: the retrun value is false.
85      */
86     shapePaintProperty.UpdateStrokeWidth(STROKE_WIDTH_0);
87     retSetPen = NG::ShapePainter::SetPen(testPen, shapePaintProperty);
88     EXPECT_FALSE(retSetPen);
89 }
90 
91 /**
92  * @tc.name: ShapePainterTestNg002
93  * @tc.desc: Test cast to ShapePainterTestNg
94  * @tc.type: FUNC
95  */
96 HWTEST_F(ShapePainterTestNg, ShapePainterTestNg002, TestSize.Level1)
97 {
98     /**
99      * @tc.steps1: create testPen and shapePaintProperty.
100       */
101     RSPen testPen;
102     NG::ShapePaintProperty shapePaintProperty;
103 
104     /**
105      * @tc.steps2: call SetPen and UpdateAntiAlias with true.
106      * @tc.expected: the retrun value is true.
107      */
108     shapePaintProperty.UpdateAntiAlias(true);
109     bool retSetPen = NG::ShapePainter::SetPen(testPen, shapePaintProperty);
110     EXPECT_TRUE(retSetPen);
111 
112     /**
113      * @tc.steps3: call SetPen and UpdateStroke with STROKE_COLOR_RED.
114      * @tc.expected: the retrun value is true.
115      */
116     shapePaintProperty.UpdateStroke(STROKE_COLOR_RED);
117     retSetPen = NG::ShapePainter::SetPen(testPen, shapePaintProperty);
118     EXPECT_TRUE(retSetPen);
119 
120     /**
121      * @tc.steps4: call SetPen and UpdateStrokeOpacity with STROKE_OPACITY.
122      * @tc.expected: the retrun value is true.
123      */
124     shapePaintProperty.UpdateStrokeOpacity(STROKE_OPACITY);
125     retSetPen = NG::ShapePainter::SetPen(testPen, shapePaintProperty);
126     EXPECT_TRUE(retSetPen);
127 
128     /**
129      * @tc.steps5: call SetPen and UpdateStrokeMiterLimit with STROKE_MITER_LIMIT.
130      * @tc.expected: the retrun value is true.
131      */
132     shapePaintProperty.UpdateStrokeMiterLimit(STROKE_MITER_LIMIT);
133     retSetPen = NG::ShapePainter::SetPen(testPen, shapePaintProperty);
134     EXPECT_TRUE(retSetPen);
135 }
136 
137 /**
138  * @tc.name: ShapePainterTestNg003
139  * @tc.desc: Test cast to ShapePainterTestNg
140  * @tc.type: FUNC
141  */
142 HWTEST_F(ShapePainterTestNg, ShapePainterTestNg003, TestSize.Level1)
143 {
144     /**
145      * @tc.steps1: create testPen and shapePaintProperty.
146      */
147     RSPen testPen;
148     NG::ShapePaintProperty shapePaintProperty;
149 
150     /**
151      * @tc.steps2: call SetPen and UpdateStrokeLineCap with CapStyle.
152      * @tc.expected: the retrun value is true.
153      */
154     for (int32_t index = 0; index < 4; index++) {
155         shapePaintProperty.UpdateStrokeLineCap(index);
156         bool retSetPen = NG::ShapePainter::SetPen(testPen, shapePaintProperty);
157         auto strokeLineCapValue = static_cast<RSPen::CapStyle>(index);
158         switch (strokeLineCapValue) {
159             case RSPen::CapStyle::FLAT_CAP:
160             case RSPen::CapStyle::SQUARE_CAP:
161             case RSPen::CapStyle::ROUND_CAP:
162                 EXPECT_TRUE(retSetPen);
163             break;
164             default:
165                 EXPECT_TRUE(retSetPen);
166         }
167     }
168 }
169 
170 /**
171  * @tc.name: ShapePainterTestNg004
172  * @tc.desc: Test cast to ShapePainterTestNg
173  * @tc.type: FUNC
174  */
175 HWTEST_F(ShapePainterTestNg, ShapePainterTestNg004, TestSize.Level1)
176 {
177     /**
178      * @tc.steps1: create testPen and shapePaintProperty.
179      */
180     RSPen testPen;
181     NG::ShapePaintProperty shapePaintProperty;
182 
183     /**
184      * @tc.steps2: call SetPen and UpdateStrokeLineJoin with JoinStyle.
185      * @tc.expected: the retrun value is true.
186      */
187     for (int32_t index = 0; index < 4; index++) {
188         shapePaintProperty.UpdateStrokeLineJoin(index);
189         bool retSetPen = NG::ShapePainter::SetPen(testPen, shapePaintProperty);
190         auto strokeLineJoinValue = static_cast<RSPen::JoinStyle>(index);
191         switch (strokeLineJoinValue) {
192             case RSPen::JoinStyle::BEVEL_JOIN:
193             case RSPen::JoinStyle::MITER_JOIN:
194             case RSPen::JoinStyle::ROUND_JOIN:
195                 EXPECT_TRUE(retSetPen);
196             break;
197             default:
198                 EXPECT_TRUE(retSetPen);
199         }
200     }
201 }
202 
203 /**
204  * @tc.name: ShapePainterTestNg005
205  * @tc.desc: Test cast to ShapePainterTestNg
206  * @tc.type: FUNC
207  */
208 HWTEST_F(ShapePainterTestNg, ShapePainterTestNg005, TestSize.Level1)
209 {
210     /**
211      * @tc.steps1: create testPen and shapePaintProperty.
212      */
213     RSPen testPen;
214     NG::ShapePaintProperty shapePaintProperty;
215 
216     /**
217      * @tc.steps2: push three dash to strokeDash.
218      */
219     std::vector<Ace::Dimension> strokeDash;
220     strokeDash.push_back(STROKE_DASH_0);
221     strokeDash.push_back(STROKE_DASH_1);
222     strokeDash.push_back(STROKE_DASH_2);
223 
224     /**
225      * @tc.steps3: call SetPen and UpdateStrokeMiterLimit with strokeDash.
226      * @tc.expected: the retrun value is true.
227      */
228     shapePaintProperty.UpdateStrokeDashArray(strokeDash);
229     bool retSetPen = NG::ShapePainter::SetPen(testPen, shapePaintProperty);
230     EXPECT_TRUE(retSetPen);
231 
232     /**
233      * @tc.steps4: call SetPen and UpdateStrokeDashOffset with STROKE_DASH_OFFSET.
234      * @tc.expected: the retrun value is true.
235      */
236     shapePaintProperty.UpdateStrokeDashOffset(STROKE_DASH_OFFSET);
237     retSetPen = NG::ShapePainter::SetPen(testPen, shapePaintProperty);
238     EXPECT_TRUE(retSetPen);
239 }
240 
241 /**
242  * @tc.name: ShapePainterTestNg006
243  * @tc.desc: Test cast to ShapePainterTestNg
244  * @tc.type: FUNC
245  */
246 HWTEST_F(ShapePainterTestNg, ShapePainterTestNg006, TestSize.Level1)
247 {
248     /**
249      * @tc.steps1: create brush and  shapePaintProperty.
250      */
251     RSBrush brush;
252     NG::ShapePaintProperty shapePaintProperty;
253 
254     /**
255      * @tc.steps2: call SetBrush.
256      * @tc.expected: shapePaintProperty.HasFill() and shapePaintProperty.HasFillOpacity() are false.
257      */
258     NG::ShapePainter::SetBrush(brush, shapePaintProperty);
259     EXPECT_FALSE(shapePaintProperty.HasFill());
260     EXPECT_FALSE(shapePaintProperty.HasFillOpacity());
261 
262     /**
263      * @tc.steps3: UpdateFill with FILL_COLOR_BLUE and UpdateFillOpacity with FILL_OPACITY.
264      */
265     shapePaintProperty.UpdateFill(FILL_COLOR_BLUE);
266     shapePaintProperty.UpdateFillOpacity(FILL_OPACITY);
267 
268     /**
269      * @tc.steps4: call SetBrush.
270      * @tc.expected: shapePaintProperty.HasFill() and shapePaintProperty.HasFillOpacity() are true.
271      */
272     NG::ShapePainter::SetBrush(brush, shapePaintProperty);
273     EXPECT_TRUE(shapePaintProperty.HasFill());
274     EXPECT_TRUE(shapePaintProperty.HasFillOpacity());
275 }
276 } // namespace OHOS::Ace
277