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
18 #include "drawing_bitmap.h"
19 #include "drawing_color.h"
20 #include "drawing_color_filter.h"
21 #include "drawing_error_code.h"
22 #include "drawing_image.h"
23 #include "drawing_image_filter.h"
24 #include "drawing_mask_filter.h"
25 #include "drawing_memory_stream.h"
26
27 using namespace testing;
28 using namespace testing::ext;
29
30 namespace OHOS {
31 namespace Rosen {
32 namespace Drawing {
33 class DrawingNativeMemoryStreamTest : public testing::Test {};
34
35 /*
36 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MEMORY_STREAM_0100
37 * @tc.name: testMemoryStreamCreateNormal
38 * @tc.desc: Test for creating memory stream with normal parameters.
39 * @tc.size : SmallTest
40 * @tc.type : Function
41 * @tc.level : Level 0
42 */
HWTEST_F(DrawingNativeMemoryStreamTest, testMemoryStreamCreateNormal, TestSize.Level0)43 HWTEST_F(DrawingNativeMemoryStreamTest, testMemoryStreamCreateNormal, TestSize.Level0) {
44 // 1. Call OH_Drawing_MemoryStreamCreate with copyData set to true
45 char data[10] = {0};
46 OH_Drawing_MemoryStream *stream = OH_Drawing_MemoryStreamCreate(data, 10, true);
47 OH_Drawing_MemoryStreamDestroy(stream);
48 // 2. Call OH_Drawing_MemoryStreamCreate with copyData set to false
49 stream = OH_Drawing_MemoryStreamCreate(data, 10, false);
50 OH_Drawing_MemoryStreamDestroy(stream);
51 }
52
53 /*
54 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MEMORY_STREAM_0101
55 * @tc.name: testMemoryStreamCreateNull
56 * @tc.desc: Test for creating memory stream with NULL or invalid parameters.
57 * @tc.size : SmallTest
58 * @tc.type : Function
59 * @tc.level : Level 3
60 */
HWTEST_F(DrawingNativeMemoryStreamTest, testMemoryStreamCreateNull, TestSize.Level3)61 HWTEST_F(DrawingNativeMemoryStreamTest, testMemoryStreamCreateNull, TestSize.Level3) {
62 char data[10] = {0};
63 // 1. OH_Drawing_MemoryStreamCreate with the first parameter set to nullptr, check the error code using
64 // OH_Drawing_ErrorCodeGet
65 OH_Drawing_MemoryStream *stream = OH_Drawing_MemoryStreamCreate(nullptr, 10, true);
66 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_INVALID_PARAMETER);
67 // 2. OH_Drawing_MemoryStreamCreate with the second parameter set to 0, check the error code using
68 // OH_Drawing_ErrorCodeGet
69 stream = OH_Drawing_MemoryStreamCreate(data, 0, true);
70 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_INVALID_PARAMETER);
71 // 3. Free memory
72 OH_Drawing_MemoryStreamDestroy(stream);
73 }
74
75 /*
76 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MEMORY_STREAM_0102
77 * @tc.name: testMemoryStreamCreateAbnormal
78 * @tc.desc: Test for creating memory stream with abnormal parameters (negative values).
79 * @tc.size : SmallTest
80 * @tc.type : Function
81 * @tc.level : Level 3
82 */
HWTEST_F(DrawingNativeMemoryStreamTest, testMemoryStreamCreateAbnormal, TestSize.Level3)83 HWTEST_F(DrawingNativeMemoryStreamTest, testMemoryStreamCreateAbnormal, TestSize.Level3) {
84 // 1. OH_Drawing_MemoryStreamCreate with a negative value as the second parameter
85 OH_Drawing_MemoryStream *stream = OH_Drawing_MemoryStreamCreate(nullptr, -10, true);
86 // 2. Free memory
87 OH_Drawing_MemoryStreamDestroy(stream);
88 }
89
90 /*
91 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MEMORY_STREAM_0103
92 * @tc.name: testMemoryStreamCreateMultipleCalls
93 * @tc.desc: Test for creating memory stream with multiple calls using different data segments.
94 * @tc.size : SmallTest
95 * @tc.type : Function
96 * @tc.level : Level 3
97 */
HWTEST_F(DrawingNativeMemoryStreamTest, testMemoryStreamCreateMultipleCalls, TestSize.Level3)98 HWTEST_F(DrawingNativeMemoryStreamTest, testMemoryStreamCreateMultipleCalls, TestSize.Level3) {
99 // 1. Call OH_Drawing_MemoryStreamCreate 10 times, passing different data segments
100 for (int i = 0; i < 10; i++) {
101 char data[10] = {i};
102 OH_Drawing_MemoryStream *stream = OH_Drawing_MemoryStreamCreate(data, 10, true);
103 OH_Drawing_MemoryStreamDestroy(stream);
104 }
105 }
106
107 } // namespace Drawing
108 } // namespace Rosen
109 } // namespace OHOS