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 #include "test/mock/core/rosen/mock_canvas.h"
17
18 #include "base/utils/utils.h"
19 #define protected public
20 #define private public
21 #include "core/components/common/properties/color.h"
22 #include "core/components_ng/pattern/pattern.h"
23 #include "core/components_ng/pattern/shape/line_paint_property.h"
24 #include "core/components_ng/render/drawing_prop_convertor.h"
25 #include "core/components_ng/render/line_painter.h"
26 #include "core/components_ng/render/shape_painter.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 const double START_VALUE = 10.0;
37 const double END_VALUE = 30.0;
38 const Dimension TEST { 0.0, DimensionUnit::PX };
39 const NG::OffsetF OFFSET_TEST { 1, 1 };
40 static constexpr NG::ShapePoint START_POINT = ShapePoint(10.0, 10.0);
41 static constexpr NG::ShapePoint END_POINT = ShapePoint(30.0, 30.0);
42
43 } // namespace
44
45 class LinePainterTestNg : public testing::Test {
46 public:
47 void CallBack(Testing::MockCanvas& rSCanvas);
48 };
49
CallBack(Testing::MockCanvas & rSCanvas)50 void LinePainterTestNg::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: LinePainterTestNg001
60 * @tc.desc: Test cast to LinePainterTestNg
61 * @tc.type: FUNC
62 */
63 HWTEST_F(LinePainterTestNg, LinePainterTestNg001, TestSize.Level1)
64 {
65 Testing::MockCanvas* canvasPtr = new Testing::MockCanvas();
66 Testing::MockCanvas& canvas = *canvasPtr;
67 /**
68 * @tc.steps1: create canvas pen and linePaintProperty object.
69 */
70 RSPen pen;
71 NG::LinePaintProperty linePaintProperty;
72
73 /**
74 * @tc.steps2: call DrawLine and UpdateStartPoint with START_POINT.
75 * @tc.steps2: UpdateEndPoint with END_POINT.
76 * @tc.expected: expect SetPen return true.
77 */
78 linePaintProperty.UpdateStartPoint(START_POINT);
79 linePaintProperty.UpdateEndPoint(END_POINT);
80 CallBack(canvas);
81 NG::LinePainter::DrawLine(canvas, linePaintProperty, OFFSET_TEST);
82 bool result = NG::ShapePainter::SetPen(pen, linePaintProperty);
83 EXPECT_TRUE(result);
84 EXPECT_EQ(linePaintProperty.GetStartPointValue().first.ConvertToPx(), START_VALUE);
85 EXPECT_EQ(linePaintProperty.GetEndPointValue().first.ConvertToPx(), END_VALUE);
86
87 /**
88 * @tc.steps3: call DrawLine and UpdateStrokeWidth with TEST.
89 * @tc.expected: expect SetPen return false, HasStrokeWidth return true.
90 */
91 linePaintProperty.UpdateStrokeWidth(TEST);
92 CallBack(canvas);
93 NG::LinePainter::DrawLine(canvas, linePaintProperty, OFFSET_TEST);
94 bool result_test = NG::ShapePainter::SetPen(pen, linePaintProperty);
95 EXPECT_FALSE(result_test);
96 EXPECT_TRUE(linePaintProperty.HasStrokeWidth());
97 testing::Mock::AllowLeak(canvasPtr);
98 }
99 } // namespace OHOS::Ace
100