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 {};
drawCircle(OH_Drawing_Canvas *canvas, int position)51 void drawCircle(OH_Drawing_Canvas *canvas, int position)
52 {
53 int x = 10;
54 int radius = 200;
55 OH_Drawing_Point *point = OH_Drawing_PointCreate(x * position + radius, x + radius);
56 OH_Drawing_CanvasDrawCircle(canvas, point, radius);
57 OH_Drawing_PointDestroy(point);
58 }
threadFunctionTest1()59 OH_Drawing_RecordCmd *threadFunctionTest1()
60 {
61 int32_t width = 2;
62 int32_t height = 5;
63 OH_Drawing_RecordCmd *recordCmd = nullptr;
64 OH_Drawing_Canvas *canvas = OH_Drawing_CanvasCreate();
65 OH_Drawing_RecordCmdUtils *recordCmdUtils = OH_Drawing_RecordCmdUtilsCreate();
66 OH_Drawing_RecordCmdUtilsBeginRecording(recordCmdUtils, width, height, &canvas);
67 float penWidth = 1.0f; // pen width 1
68 // 创建一个画笔Pen对象,Pen对象用于形状的边框线绘制
69 OH_Drawing_Pen *cPen = OH_Drawing_PenCreate();
70 OH_Drawing_PenSetAntiAlias(cPen, true);
71 OH_Drawing_PenSetColor(cPen, OH_Drawing_ColorSetArgb(0xFF, 0xFF, 0x00, 0x00));
72 OH_Drawing_PenSetWidth(cPen, penWidth);
73 OH_Drawing_PenSetJoin(cPen, LINE_ROUND_JOIN);
74 // 将Pen画笔设置到canvas中
75 OH_Drawing_CanvasAttachPen(canvas, cPen);
76 // 创建一个画刷Brush对象,Brush对象用于形状的填充
77 OH_Drawing_Brush *cBrush = OH_Drawing_BrushCreate();
78 OH_Drawing_BrushSetColor(cBrush, OH_Drawing_ColorSetArgb(0xFF, 0x00, 0xFF, 0x00));
79 // 将Brush画刷设置到canvas中
80 OH_Drawing_CanvasAttachBrush(canvas, cBrush);
81 drawCircle(canvas, width);
82 OH_Drawing_RecordCmdUtilsFinishRecording(recordCmdUtils, &recordCmd);
83 OH_Drawing_RecordCmdUtilsDestroy(recordCmdUtils);
84 OH_Drawing_BrushDestroy(cBrush);
85 OH_Drawing_PenDestroy(cPen);
86 cBrush = nullptr;
87 cPen = nullptr;
88 return recordCmd;
89 }
90
91
92 /*
93 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_RECORDER_0400
94 * @tc.name: testRecordCmdDestroyNormal
95 * @tc.desc: test for testRecordCmdDestroyNormal.
96 * @tc.size : SmallTest
97 * @tc.type : Function
98 * @tc.level : Level 0
99 */
HWTEST_F(DrawingRecordCmdDestroyTest, testRecordCmdDestroyNormal, TestSize.Level0)100 HWTEST_F(DrawingRecordCmdDestroyTest, testRecordCmdDestroyNormal, TestSize.Level0) {
101 // 1. The OH-Drawing-RecordCmdDestroy parameter is not empty
102 OH_Drawing_ErrorCode drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
103 OH_Drawing_RecordCmd *picture = nullptr;
104 std::thread thread([&picture, this]() { picture = threadFunctionTest1(); });
105 thread.join();
106 drawingErrorCode = OH_Drawing_RecordCmdDestroy (picture);
107 EXPECT_EQ(drawingErrorCode, OH_DRAWING_SUCCESS);
108 }
109
110 /*
111 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_RECORDER_0400
112 * @tc.name: testRecordCmdDestroyNULL
113 * @tc.desc: test for testRecordCmdDestroyNULL.
114 * @tc.size : SmallTest
115 * @tc.type : Function
116 * @tc.level : Level 0
117 */
HWTEST_F(DrawingRecordCmdDestroyTest, testRecordCmdDestroyNULL, TestSize.Level0)118 HWTEST_F(DrawingRecordCmdDestroyTest, testRecordCmdDestroyNULL, TestSize.Level0) {
119 // 1. The OH-Drawing-RecordCmdDestroy parameter is not empty
120 OH_Drawing_ErrorCode drawingErrorCode = OH_DRAWING_ERROR_INVALID_PARAMETER;
121 OH_Drawing_RecordCmd *picture = nullptr;
122 drawingErrorCode = OH_Drawing_RecordCmdDestroy (picture);
123 EXPECT_EQ(drawingErrorCode, OH_DRAWING_ERROR_INVALID_PARAMETER);
124 }
125
126 } // namespace Drawing
127 } // namespace Rosen
128 } // namespace OHOS
129