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 "drawing_sampling_options.h" 17 #include "drawing_types.h" 18 #include "gtest/gtest.h" 19 #include "drawing_error_code.h" 20 #include <random> 21 22 using namespace testing; 23 using namespace testing::ext; 24 25 namespace OHOS { 26 namespace Rosen { 27 namespace Drawing { 28 class DrawingNativeSamplingOptionsTest : public testing::Test { 29 protected: 30 // 在每个测试用例执行前调用 SetUp()31 void SetUp() override 32 { 33 // 设置代码 34 std::cout << "DrawingNativeSamplingOptionsTest Setup code called before each test case." << std::endl; 35 OH_Drawing_ErrorCodeReset(); 36 std::cout << "DrawingNativeSamplingOptionsTest errorCodeReset before each test case." << std::endl; 37 } TearDown()38 void TearDown() override 39 { 40 std::cout << "DrawingNativeSamplingOptionsTest Setup code called after each test case." << std::endl; 41 OH_Drawing_ErrorCodeReset(); 42 std::cout << "DrawingNativeSamplingOptionsTest errorCodeReset after each test case." << std::endl; 43 } 44 }; 45 46 /* 47 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SAMPLING_OPTIONS_0100 48 * @tc.name: testSamplingOptionsCreateDestroyDestroyNormal 49 * @tc.desc: test for testSamplingOptionsCreateDestroyDestroyNormal. 50 * @tc.size : SmallTest 51 * @tc.type : Function 52 * @tc.level : Level 0 53 */ 54 HWTEST_F(DrawingNativeSamplingOptionsTest, testSamplingOptionsCreateDestroyDestroyNormal, TestSize.Level0) { 55 OH_Drawing_FilterMode filterModes[] = { 56 FILTER_MODE_NEAREST, 57 FILTER_MODE_LINEAR, 58 }; 59 OH_Drawing_MipmapMode mipmapModes[] = { 60 MIPMAP_MODE_NONE, 61 MIPMAP_MODE_NEAREST, 62 MIPMAP_MODE_LINEAR, 63 }; 64 // 1. Enumerate OH_Drawing_FilterMode and OH_Drawing_MipmapMode values with cross iteration 65 for (OH_Drawing_FilterMode filterMode : filterModes) { 66 for (OH_Drawing_MipmapMode mipmapMode : mipmapModes) { 67 OH_Drawing_SamplingOptions *options = OH_Drawing_SamplingOptionsCreate(filterMode, mipmapMode); 68 // add assert 69 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_Drawing_ErrorCode::OH_DRAWING_SUCCESS); 70 EXPECT_NE(options, nullptr); 71 // 2. Destroy the objects created in step 1 with OH_Drawing_SamplingOptionsDestroy 72 OH_Drawing_SamplingOptionsDestroy(options); 73 } 74 } 75 } 76 77 /* 78 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SAMPLING_OPTIONS_0101 79 * @tc.name: testSamplingOptionsCreateDestroyDestroyNull 80 * @tc.desc: test for testSamplingOptionsCreateDestroyDestroyNull. 81 * @tc.size : SmallTest 82 * @tc.type : Function 83 * @tc.level : Level 3 84 */ 85 HWTEST_F(DrawingNativeSamplingOptionsTest, testSamplingOptionsCreateDestroyDestroyNull, TestSize.Level3) { 86 // 1. OH_Drawing_SamplingOptionsDestroy with empty parameter 87 OH_Drawing_SamplingOptionsDestroy(nullptr); 88 // add assert 89 EXPECT_TRUE(true); 90 } 91 92 /* 93 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SAMPLING_OPTIONS_0102 94 * @tc.name: testSamplingOptionsCreateDestroyDestroyMultipleCalls 95 * @tc.desc: test for testSamplingOptionsCreateDestroyDestroyMultipleCalls. 96 * @tc.size : SmallTest 97 * @tc.type : Function 98 * @tc.level : Level 3 99 */ 100 HWTEST_F(DrawingNativeSamplingOptionsTest, testSamplingOptionsCreateDestroyDestroyMultipleCalls, TestSize.Level3) { 101 // 1. Call OH_Drawing_SamplingOptionsCreate 10 times with random enum values 102 std::random_device rd; 103 std::mt19937 gen(rd()); 104 std::uniform_int_distribution<int> dis(0, 10); 105 OH_Drawing_SamplingOptions *options[10]; 106 for (int i = 0; i < 10; i++) { 107 int random = dis(gen); 108 OH_Drawing_FilterMode filterMode = static_cast<OH_Drawing_FilterMode>(random % 2); 109 OH_Drawing_MipmapMode mipmapMode = static_cast<OH_Drawing_MipmapMode>(random % 3); 110 options[i] = OH_Drawing_SamplingOptionsCreate(filterMode, mipmapMode); 111 EXPECT_NE(options[i], nullptr); 112 } 113 // 2. Call OH_Drawing_SamplingOptionsDestroy 10 times 114 for (int i = 0; i < 10; i++) { 115 OH_Drawing_SamplingOptionsDestroy(options[i]); 116 } 117 // 3. Call OH_Drawing_SamplingOptionsCreate and OH_Drawing_SamplingOptionsDestroy alternately 10 times 118 for (int i = 0; i < 10; i++) { 119 int random = dis(gen); 120 OH_Drawing_FilterMode filterMode = static_cast<OH_Drawing_FilterMode>(random % 2); 121 OH_Drawing_MipmapMode mipmapMode = static_cast<OH_Drawing_MipmapMode>(random % 3); 122 OH_Drawing_SamplingOptions *options = OH_Drawing_SamplingOptionsCreate(filterMode, mipmapMode); 123 EXPECT_NE(options, nullptr); 124 OH_Drawing_SamplingOptionsDestroy(options); 125 } 126 } 127 128 } // namespace Drawing 129 } // namespace Rosen 130 } // namespace OHOS