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::TestingPen pen;
42
43 const NG::OffsetF& DRAWOFFSET { 1, 1 };
44
45 NG::ShapePaintProperty shapePaintProperty;
46 } // namespace
47
48 class CirclePainterTestNg : public testing::Test {
49 public:
50 void CallBack(Testing::MockCanvas& rSCanvas);
51 };
52
CallBack(Testing::MockCanvas & rSCanvas)53 void CirclePainterTestNg::CallBack(Testing::MockCanvas& rSCanvas)
54 {
55 EXPECT_CALL(rSCanvas, AttachBrush(_)).WillOnce(ReturnRef(rSCanvas));
56 EXPECT_CALL(rSCanvas, DetachBrush()).WillOnce(ReturnRef(rSCanvas));
57 EXPECT_CALL(rSCanvas, AttachPen(_)).WillOnce(ReturnRef(rSCanvas));
58 EXPECT_CALL(rSCanvas, DetachPen()).WillOnce(ReturnRef(rSCanvas));
59 }
60
61 /**
62 * @tc.name: CirclePainterTestNg001
63 * @tc.desc: Test cast to CirclePainterTestNg
64 * @tc.type: FUNC
65 */
66 HWTEST_F(CirclePainterTestNg, CirclePainterTestNg_DrawCircle001, TestSize.Level1)
67 {
68 Testing::MockCanvas* testingCanvasPtr = new Testing::MockCanvas();
69 Testing::MockCanvas& testingCanvas = *testingCanvasPtr;
70 /**
71 * @tc.steps1: callback DrawCircle.push shapePaintProperty is null.
72 * @tc.expected: expect SetPen return true.
73 */
74 CallBack(testingCanvas);
75 NG::CirclePainter::DrawCircle(testingCanvas, radius, DRAWOFFSET, shapePaintProperty);
76 bool result = NG::ShapePainter::SetPen(pen, shapePaintProperty);
77 EXPECT_TRUE(result);
78
79 /**
80 * @tc.steps2: callback DrawCircle.push shapePaintProperty is not null.
81 * @tc.expected: expect SetPen return false.
82 */
83 shapePaintProperty.UpdateStrokeWidth(test);
84
85 CallBack(testingCanvas);
86 NG::CirclePainter::DrawCircle(testingCanvas, radius, DRAWOFFSET, shapePaintProperty);
87 bool result1 = NG::ShapePainter::SetPen(pen, shapePaintProperty);
88 EXPECT_FALSE(result1);
89 Mock::AllowLeak(testingCanvasPtr);
90 }
91 } // namespace OHOS::Ace
92