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 #include "gtest/gtest.h"
16 
17 #include "base/utils/utils.h"
18 #define protected public
19 #define private public
20 #include "test/mock/core/rosen/mock_canvas.h"
21 
22 #include "core/components_ng/pattern/shape/polygon_paint_property.h"
23 #include "core/components_ng/pattern/shape/shape_paint_property.h"
24 #include "core/components_ng/render/drawing.h"
25 #include "core/components_ng/render/drawing_prop_convertor.h"
26 #include "core/components_ng/render/polygon_painter.h"
27 #include "core/components_ng/render/shape_painter.h"
28 
29 #undef private
30 #undef protected
31 
32 using namespace testing;
33 using namespace testing::ext;
34 
35 namespace OHOS::Ace {
36 namespace {
37 const Dimension TEST { 0.0, DimensionUnit::PX };
38 const std::pair<Dimension, Dimension> START_POINT = { 10.0_vp, 10.0_vp };
39 const std::pair<Dimension, Dimension> END_POINT = { 30.0_vp, 30.0_vp };
40 
41 std::vector<ShapePoint> shape_Point;
42 
43 Testing::MockCanvas canvas;
44 } // namespace
45 
46 class PolygonPainterTestNg : public testing::Test {
47 public:
48     void CallBack(Testing::MockCanvas& rSCanvas);
49 };
50 
CallBack(Testing::MockCanvas & rSCanvas)51 void PolygonPainterTestNg::CallBack(Testing::MockCanvas& rSCanvas)
52 {
53     EXPECT_CALL(rSCanvas, AttachBrush(_)).WillOnce(ReturnRef(rSCanvas));
54     EXPECT_CALL(rSCanvas, DetachBrush()).WillOnce(ReturnRef(rSCanvas));
55     EXPECT_CALL(rSCanvas, AttachPen(_)).WillOnce(ReturnRef(rSCanvas));
56     EXPECT_CALL(rSCanvas, DetachPen()).WillOnce(ReturnRef(rSCanvas));
57 }
58 
59 /**
60  * @tc.name: PolygonPainterTestNg001
61  * @tc.desc: Test cast to PolygonPainterTestNg
62  * @tc.type: FUNC
63  */
64 HWTEST_F(PolygonPainterTestNg, PolygonPainterTestNg001, TestSize.Level1)
65 {
66     /**
67      * @tc.steps1: create canvas and polygonPaintProperty object.
68      */
69     CallBack(canvas);
70     NG::PolygonPaintProperty polygonPaintProperty;
71 
72     /**
73      * @tc.steps2: call DrawPolygon.
74      * @tc.expected: expect HasPoints return false.
75      */
76     NG::PolygonPainter::DrawPolygon(canvas, polygonPaintProperty, true);
77     EXPECT_FALSE(polygonPaintProperty.HasPoints());
78 
79     /**
80      * @tc.steps3: call DrawPolygon and UpdatePoints with shape_Point.
81      * @tc.expected: expect HasPoints return true.
82      */
83     polygonPaintProperty.UpdatePoints(shape_Point);
84     CallBack(canvas);
85     NG::PolygonPainter::DrawPolygon(canvas, polygonPaintProperty, true);
86     EXPECT_TRUE(polygonPaintProperty.HasPoints());
87     EXPECT_TRUE(polygonPaintProperty.GetPointsValue().empty());
88 }
89 
90 /**
91  * @tc.name: PolygonPainterTestNg002
92  * @tc.desc: Test cast to PolygonPainterTestNg
93  * @tc.type: FUNC
94  */
95 HWTEST_F(PolygonPainterTestNg, PolygonPainterTestNg002, TestSize.Level1)
96 {
97     /**
98      * @tc.steps1: create canvas and polygonPaintProperty object.
99      */
100     CallBack(canvas);
101     NG::PolygonPaintProperty polygonPaintProperty;
102 
103     /**
104      * @tc.steps2: Add START_POINT and END_POINT element to the shape_Point.
105      * @tc.steps2: UpdatePoints with shape_Point and UpdateStrokeWidth with TEST.
106      */
107     shape_Point.push_back(START_POINT);
108     shape_Point.push_back(END_POINT);
109     polygonPaintProperty.UpdatePoints(shape_Point);
110     polygonPaintProperty.UpdateStrokeWidth(TEST);
111 
112     /**
113      * @tc.steps3: call DrawPolygon.
114      * @tc.expected: expect HasStrokeWidth return true.
115      */
116     NG::PolygonPainter::DrawPolygon(canvas, polygonPaintProperty, true);
117     EXPECT_TRUE(polygonPaintProperty.HasStrokeWidth());
118     EXPECT_FALSE(polygonPaintProperty.GetPointsValue().empty());
119 }
120 } // namespace OHOS::Ace
121