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 protected: 35 // 在每个测试用例执行前调用 SetUp()36 void SetUp() override 37 { 38 // 设置代码 39 std::cout << "DrawingNativeMemoryStreamTest Setup code called before each test case." << std::endl; 40 OH_Drawing_ErrorCodeReset(); 41 std::cout << "DrawingNativeMemoryStreamTest errorCodeReset before each test case." << std::endl; 42 } TearDown()43 void TearDown() override 44 { 45 std::cout << "DrawingNativeMemoryStreamTest Setup code called after each test case." << std::endl; 46 OH_Drawing_ErrorCodeReset(); 47 std::cout << "DrawingNativeMemoryStreamTest errorCodeReset after each test case." << std::endl; 48 } 49 }; 50 51 /* 52 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MEMORY_STREAM_0100 53 * @tc.name: testMemoryStreamCreateNormal 54 * @tc.desc: Test for creating memory stream with normal parameters. 55 * @tc.size : SmallTest 56 * @tc.type : Function 57 * @tc.level : Level 0 58 */ 59 HWTEST_F(DrawingNativeMemoryStreamTest, testMemoryStreamCreateNormal, TestSize.Level0) { 60 // 1. Call OH_Drawing_MemoryStreamCreate with copyData set to true 61 char data[10] = {0}; 62 OH_Drawing_MemoryStream *stream = OH_Drawing_MemoryStreamCreate(data, 10, true); 63 // add assert 64 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_SUCCESS); 65 // add assert 66 EXPECT_NE(stream, nullptr); 67 OH_Drawing_MemoryStreamDestroy(stream); 68 // 2. Call OH_Drawing_MemoryStreamCreate with copyData set to false 69 stream = OH_Drawing_MemoryStreamCreate(data, 10, false); 70 // add assert 71 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_SUCCESS); 72 // add assert 73 EXPECT_NE(stream, nullptr); 74 OH_Drawing_MemoryStreamDestroy(stream); 75 } 76 77 /* 78 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MEMORY_STREAM_0101 79 * @tc.name: testMemoryStreamCreateNull 80 * @tc.desc: Test for creating memory stream with NULL or invalid parameters. 81 * @tc.size : SmallTest 82 * @tc.type : Function 83 * @tc.level : Level 3 84 */ 85 HWTEST_F(DrawingNativeMemoryStreamTest, testMemoryStreamCreateNull, TestSize.Level3) { 86 char data[10] = {0}; 87 // 1. OH_Drawing_MemoryStreamCreate with the first parameter set to nullptr, check the error code using 88 // OH_Drawing_ErrorCodeGet 89 OH_Drawing_MemoryStream *stream = OH_Drawing_MemoryStreamCreate(nullptr, 10, true); 90 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_INVALID_PARAMETER); 91 OH_Drawing_ErrorCodeReset(); 92 // 2. OH_Drawing_MemoryStreamCreate with the second parameter set to 0, check the error code using 93 // OH_Drawing_ErrorCodeGet 94 stream = OH_Drawing_MemoryStreamCreate(data, 0, true); 95 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_INVALID_PARAMETER); 96 // 3. Free memory 97 OH_Drawing_MemoryStreamDestroy(stream); 98 } 99 100 /* 101 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MEMORY_STREAM_0102 102 * @tc.name: testMemoryStreamCreateAbnormal 103 * @tc.desc: Test for creating memory stream with abnormal parameters (negative values). 104 * @tc.size : SmallTest 105 * @tc.type : Function 106 * @tc.level : Level 3 107 */ 108 HWTEST_F(DrawingNativeMemoryStreamTest, testMemoryStreamCreateAbnormal, TestSize.Level3) { 109 // 1. OH_Drawing_MemoryStreamCreate with a negative value as the second parameter 110 OH_Drawing_MemoryStream *stream = OH_Drawing_MemoryStreamCreate(nullptr, -10, true); 111 // add assert ---待验证 112 EXPECT_EQ(stream, nullptr); 113 // add assert 114 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_ERROR_INVALID_PARAMETER); 115 // 2. Free memory 116 OH_Drawing_MemoryStreamDestroy(stream); 117 } 118 119 /* 120 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_MEMORY_STREAM_0103 121 * @tc.name: testMemoryStreamCreateMultipleCalls 122 * @tc.desc: Test for creating memory stream with multiple calls using different data segments. 123 * @tc.size : SmallTest 124 * @tc.type : Function 125 * @tc.level : Level 3 126 */ 127 HWTEST_F(DrawingNativeMemoryStreamTest, testMemoryStreamCreateMultipleCalls, TestSize.Level3) { 128 // 1. Call OH_Drawing_MemoryStreamCreate 10 times, passing different data segments 129 for (int i = 0; i < 10; i++) { 130 char data[10] = {i}; 131 OH_Drawing_MemoryStream *stream = OH_Drawing_MemoryStreamCreate(data, 10, true); 132 // add assert 133 EXPECT_NE(stream, nullptr); 134 OH_Drawing_MemoryStreamDestroy(stream); 135 } 136 } 137 138 } // namespace Drawing 139 } // namespace Rosen 140 } // namespace OHOS