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 Testing::MockCanvas canvas;
44 } // namespace
45
46 class LinePainterTestNg : public testing::Test {
47 public:
48 void CallBack(Testing::MockCanvas& rSCanvas);
49 };
50
CallBack(Testing::MockCanvas & rSCanvas)51 void LinePainterTestNg::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: LinePainterTestNg001
61 * @tc.desc: Test cast to LinePainterTestNg
62 * @tc.type: FUNC
63 */
64 HWTEST_F(LinePainterTestNg, LinePainterTestNg001, TestSize.Level1)
65 {
66 /**
67 * @tc.steps1: create canvas pen and linePaintProperty object.
68 */
69 RSPen pen;
70 NG::LinePaintProperty linePaintProperty;
71
72 /**
73 * @tc.steps2: call DrawLine and UpdateStartPoint with START_POINT.
74 * @tc.steps2: UpdateEndPoint with END_POINT.
75 * @tc.expected: expect SetPen return true.
76 */
77 linePaintProperty.UpdateStartPoint(START_POINT);
78 linePaintProperty.UpdateEndPoint(END_POINT);
79 CallBack(canvas);
80 NG::LinePainter::DrawLine(canvas, linePaintProperty, OFFSET_TEST);
81 bool result = NG::ShapePainter::SetPen(pen, linePaintProperty);
82 EXPECT_TRUE(result);
83 EXPECT_EQ(linePaintProperty.GetStartPointValue().first.ConvertToPx(), START_VALUE);
84 EXPECT_EQ(linePaintProperty.GetEndPointValue().first.ConvertToPx(), END_VALUE);
85
86 /**
87 * @tc.steps3: call DrawLine and UpdateStrokeWidth with TEST.
88 * @tc.expected: expect SetPen return false, HasStrokeWidth return true.
89 */
90 linePaintProperty.UpdateStrokeWidth(TEST);
91 CallBack(canvas);
92 NG::LinePainter::DrawLine(canvas, linePaintProperty, OFFSET_TEST);
93 bool result_test = NG::ShapePainter::SetPen(pen, linePaintProperty);
94 EXPECT_FALSE(result_test);
95 EXPECT_TRUE(linePaintProperty.HasStrokeWidth());
96 }
97 } // namespace OHOS::Ace
98