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 } // namespace
44
45 class PolygonPainterTestNg : public testing::Test {
46 public:
47 void CallBack(Testing::MockCanvas& rSCanvas);
48 };
49
CallBack(Testing::MockCanvas & rSCanvas)50 void PolygonPainterTestNg::CallBack(Testing::MockCanvas& rSCanvas)
51 {
52 EXPECT_CALL(rSCanvas, AttachBrush(_)).WillOnce(ReturnRef(rSCanvas));
53 EXPECT_CALL(rSCanvas, DetachBrush()).WillOnce(ReturnRef(rSCanvas));
54 EXPECT_CALL(rSCanvas, AttachPen(_)).WillOnce(ReturnRef(rSCanvas));
55 EXPECT_CALL(rSCanvas, DetachPen()).WillOnce(ReturnRef(rSCanvas));
56 }
57
58 /**
59 * @tc.name: PolygonPainterTestNg001
60 * @tc.desc: Test cast to PolygonPainterTestNg
61 * @tc.type: FUNC
62 */
63 HWTEST_F(PolygonPainterTestNg, PolygonPainterTestNg001, TestSize.Level1)
64 {
65 Testing::MockCanvas* canvasPtr = new Testing::MockCanvas();
66 Testing::MockCanvas& canvas = *canvasPtr;
67 /**
68 * @tc.steps1: create canvas and polygonPaintProperty object.
69 */
70 CallBack(canvas);
71 NG::PolygonPaintProperty polygonPaintProperty;
72
73 /**
74 * @tc.steps2: call DrawPolygon.
75 * @tc.expected: expect HasPoints return false.
76 */
77 NG::PolygonPainter::DrawPolygon(canvas, polygonPaintProperty, true);
78 EXPECT_FALSE(polygonPaintProperty.HasPoints());
79
80 /**
81 * @tc.steps3: call DrawPolygon and UpdatePoints with shape_Point.
82 * @tc.expected: expect HasPoints return true.
83 */
84 polygonPaintProperty.UpdatePoints(shape_Point);
85 CallBack(canvas);
86 NG::PolygonPainter::DrawPolygon(canvas, polygonPaintProperty, true);
87 EXPECT_TRUE(polygonPaintProperty.HasPoints());
88 EXPECT_TRUE(polygonPaintProperty.GetPointsValue().empty());
89 testing::Mock::AllowLeak(canvasPtr);
90 }
91
92 /**
93 * @tc.name: PolygonPainterTestNg002
94 * @tc.desc: Test cast to PolygonPainterTestNg
95 * @tc.type: FUNC
96 */
97 HWTEST_F(PolygonPainterTestNg, PolygonPainterTestNg002, TestSize.Level1)
98 {
99 Testing::MockCanvas* canvasPtr = new Testing::MockCanvas();
100 Testing::MockCanvas& canvas = *canvasPtr;
101 /**
102 * @tc.steps1: create canvas and polygonPaintProperty object.
103 */
104 CallBack(canvas);
105 NG::PolygonPaintProperty polygonPaintProperty;
106
107 /**
108 * @tc.steps2: Add START_POINT and END_POINT element to the shape_Point.
109 * @tc.steps2: UpdatePoints with shape_Point and UpdateStrokeWidth with TEST.
110 */
111 shape_Point.push_back(START_POINT);
112 shape_Point.push_back(END_POINT);
113 polygonPaintProperty.UpdatePoints(shape_Point);
114 polygonPaintProperty.UpdateStrokeWidth(TEST);
115
116 /**
117 * @tc.steps3: call DrawPolygon.
118 * @tc.expected: expect HasStrokeWidth return true.
119 */
120 NG::PolygonPainter::DrawPolygon(canvas, polygonPaintProperty, true);
121 EXPECT_TRUE(polygonPaintProperty.HasStrokeWidth());
122 EXPECT_FALSE(polygonPaintProperty.GetPointsValue().empty());
123 testing::Mock::AllowLeak(canvasPtr);
124 }
125 } // namespace OHOS::Ace
126