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 "gmock/gmock.h"
16 #include "gtest/gtest.h"
17 
18 #include "base/utils/utils.h"
19 #define protected public
20 #define private public
21 #include "test/mock/core/rosen/mock_canvas.h"
22 
23 #include "core/components_ng/pattern/shape/shape_paint_property.h"
24 #include "core/components_ng/render/circle_painter.h"
25 #include "core/components_ng/render/drawing.h"
26 #include "core/components_ng/render/drawing_prop_convertor.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 Dimension test { 0, DimensionUnit::PX };
38 
39 float radius = 1;
40 
41 Testing::MockCanvas testingCanvas;
42 Testing::TestingPen pen;
43 
44 const NG::OffsetF& DRAWOFFSET { 1, 1 };
45 
46 NG::ShapePaintProperty shapePaintProperty;
47 } // namespace
48 
49 class CirclePainterTestNg : public testing::Test {
50 public:
51     void CallBack(Testing::MockCanvas& rSCanvas);
52 };
53 
CallBack(Testing::MockCanvas & rSCanvas)54 void CirclePainterTestNg::CallBack(Testing::MockCanvas& rSCanvas)
55 {
56     EXPECT_CALL(rSCanvas, AttachBrush(_)).WillOnce(ReturnRef(rSCanvas));
57     EXPECT_CALL(rSCanvas, DetachBrush()).WillOnce(ReturnRef(rSCanvas));
58     EXPECT_CALL(rSCanvas, AttachPen(_)).WillOnce(ReturnRef(rSCanvas));
59     EXPECT_CALL(rSCanvas, DetachPen()).WillOnce(ReturnRef(rSCanvas));
60 }
61 
62 /**
63  * @tc.name: CirclePainterTestNg001
64  * @tc.desc: Test cast to CirclePainterTestNg
65  * @tc.type: FUNC
66  */
67 HWTEST_F(CirclePainterTestNg, CirclePainterTestNg_DrawCircle001, TestSize.Level1)
68 {
69     /**
70      * @tc.steps1: callback DrawCircle.push shapePaintProperty is null.
71      * @tc.expected: expect SetPen return true.
72      */
73     CallBack(testingCanvas);
74     NG::CirclePainter::DrawCircle(testingCanvas, radius, DRAWOFFSET, shapePaintProperty);
75     bool result = NG::ShapePainter::SetPen(pen, shapePaintProperty);
76     EXPECT_TRUE(result);
77 
78     /**
79      * @tc.steps2: callback DrawCircle.push shapePaintProperty is not null.
80      * @tc.expected: expect SetPen return false.
81      */
82     shapePaintProperty.UpdateStrokeWidth(test);
83 
84     CallBack(testingCanvas);
85     NG::CirclePainter::DrawCircle(testingCanvas, radius, DRAWOFFSET, shapePaintProperty);
86     bool result1 = NG::ShapePainter::SetPen(pen, shapePaintProperty);
87     EXPECT_FALSE(result1);
88 }
89 } // namespace OHOS::Ace
90