1 /*
2 * Copyright (c) 2024 Shenzhen Kaihong Digital Industry Development 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
16 #include "gtest/gtest.h"
17 #include "drawing_bitmap.h"
18 #include "drawing_brush.h"
19 #include "drawing_canvas.h"
20 #include "drawing_color.h"
21 #include "drawing_color_filter.h"
22 #include "drawing_filter.h"
23 #include "drawing_font.h"
24 #include "drawing_image.h"
25 #include "drawing_mask_filter.h"
26 #include "drawing_matrix.h"
27 #include "drawing_memory_stream.h"
28 #include "drawing_path.h"
29 #include "drawing_pen.h"
30 #include "drawing_point.h"
31 #include "drawing_rect.h"
32 #include "drawing_region.h"
33 #include "drawing_round_rect.h"
34 #include "drawing_sampling_options.h"
35 #include "drawing_shader_effect.h"
36 #include "drawing_text_blob.h"
37 #include "drawing_typeface.h"
38 #include "drawing_record_cmd.h"
39 #include "utils/scalar.h"
40 #include <random>
41 #include <thread>
42 #include <iomanip>
43 #include <sstream>
44 using namespace testing;
45 using namespace testing::ext;
46
47 namespace OHOS {
48 namespace Rosen {
49 namespace Drawing {
50 class DrawingRecordCmdDestroyTest : public testing::Test {
51 protected:
52 // 在每个测试用例执行前调用
SetUp()53 void SetUp() override
54 {
55 // 设置代码
56 std::cout << "DrawingRecordCmdDestroyTest Setup code called before each test case." << std::endl;
57 OH_Drawing_ErrorCodeReset();
58 std::cout << "DrawingRecordCmdDestroyTest errorCodeReset before each test case." << std::endl;
59 }
TearDown()60 void TearDown() override
61 {
62 std::cout << "DrawingRecordCmdDestroyTest Setup code called after each test case." << std::endl;
63 OH_Drawing_ErrorCodeReset();
64 std::cout << "DrawingRecordCmdDestroyTest errorCodeReset after each test case." << std::endl;
65 }
66 };
drawCircle(OH_Drawing_Canvas * canvas,int position)67 void drawCircle(OH_Drawing_Canvas *canvas, int position)
68 {
69 int x = 10;
70 int radius = 200;
71 OH_Drawing_Point *point = OH_Drawing_PointCreate(x * position + radius, x + radius);
72 OH_Drawing_CanvasDrawCircle(canvas, point, radius);
73 OH_Drawing_PointDestroy(point);
74 }
threadFunctionTest1()75 OH_Drawing_RecordCmd *threadFunctionTest1()
76 {
77 int32_t width = 2;
78 int32_t height = 5;
79 OH_Drawing_RecordCmd *recordCmd = nullptr;
80 OH_Drawing_Canvas *canvas = OH_Drawing_CanvasCreate();
81 OH_Drawing_RecordCmdUtils *recordCmdUtils = OH_Drawing_RecordCmdUtilsCreate();
82 OH_Drawing_RecordCmdUtilsBeginRecording(recordCmdUtils, width, height, &canvas);
83 float penWidth = 1.0f; // pen width 1
84 // 创建一个画笔Pen对象,Pen对象用于形状的边框线绘制
85 OH_Drawing_Pen *cPen = OH_Drawing_PenCreate();
86 OH_Drawing_PenSetAntiAlias(cPen, true);
87 OH_Drawing_PenSetColor(cPen, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0x00, 0x00));
88 OH_Drawing_PenSetWidth(cPen, penWidth);
89 OH_Drawing_PenSetJoin(cPen, LINE_ROUND_JOIN);
90 // 将Pen画笔设置到canvas中
91 OH_Drawing_CanvasAttachPen(canvas, cPen);
92 // 创建一个画刷Brush对象,Brush对象用于形状的填充
93 OH_Drawing_Brush *cBrush = OH_Drawing_BrushCreate();
94 OH_Drawing_BrushSetColor(cBrush, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0xFF, 0x00));
95 // 将Brush画刷设置到canvas中
96 OH_Drawing_CanvasAttachBrush(canvas, cBrush);
97 drawCircle(canvas, width);
98 OH_Drawing_RecordCmdUtilsFinishRecording(recordCmdUtils, &recordCmd);
99 OH_Drawing_RecordCmdUtilsDestroy(recordCmdUtils);
100 OH_Drawing_BrushDestroy(cBrush);
101 OH_Drawing_PenDestroy(cPen);
102 cBrush = nullptr;
103 cPen = nullptr;
104 return recordCmd;
105 }
106
107
108 /*
109 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_RECORDER_0400
110 * @tc.name: testRecordCmdDestroyNormal
111 * @tc.desc: test for testRecordCmdDestroyNormal.
112 * @tc.size : SmallTest
113 * @tc.type : Function
114 * @tc.level : Level 0
115 */
116 HWTEST_F(DrawingRecordCmdDestroyTest, testRecordCmdDestroyNormal, TestSize.Level0) {
117 // 1. The OH-Drawing-RecordCmdDestroy parameter is not empty
118 OH_Drawing_ErrorCode drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
119 OH_Drawing_RecordCmd *picture = nullptr;
__anona4648fb20102() 120 std::thread thread([&picture, this]() { picture = threadFunctionTest1(); });
121 thread.join();
122 drawingErrorCode = OH_Drawing_RecordCmdDestroy (picture);
123 EXPECT_EQ(drawingErrorCode, OH_DRAWING_SUCCESS);
124 }
125
126 /*
127 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_RECORDER_0400
128 * @tc.name: testRecordCmdDestroyNULL
129 * @tc.desc: test for testRecordCmdDestroyNULL.
130 * @tc.size : SmallTest
131 * @tc.type : Function
132 * @tc.level : Level 0
133 */
134 HWTEST_F(DrawingRecordCmdDestroyTest, testRecordCmdDestroyNULL, TestSize.Level0) {
135 // 1. The OH-Drawing-RecordCmdDestroy parameter is not empty
136 OH_Drawing_ErrorCode drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
137 OH_Drawing_RecordCmd *picture = nullptr;
138 drawingErrorCode = OH_Drawing_RecordCmdDestroy (picture);
139 EXPECT_EQ(drawingErrorCode, OH_DRAWING_ERROR_INVALID_PARAMETER);
140 }
141
142 } // namespace Drawing
143 } // namespace Rosen
144 } // namespace OHOS
145