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 <random>
20
21using namespace testing;
22using namespace testing::ext;
23
24namespace OHOS {
25namespace Rosen {
26namespace Drawing {
27class DrawingNativeSamplingOptionsTest : public testing::Test {};
28
29/*
30 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SAMPLING_OPTIONS_0100
31 * @tc.name: testSamplingOptionsCreateDestroyDestroyNormal
32 * @tc.desc: test for testSamplingOptionsCreateDestroyDestroyNormal.
33 * @tc.size  : SmallTest
34 * @tc.type  : Function
35 * @tc.level : Level 0
36 */
37HWTEST_F(DrawingNativeSamplingOptionsTest, testSamplingOptionsCreateDestroyDestroyNormal, TestSize.Level0) {
38    OH_Drawing_FilterMode filterModes[] = {
39        FILTER_MODE_NEAREST,
40        FILTER_MODE_LINEAR,
41    };
42    OH_Drawing_MipmapMode mipmapModes[] = {
43        MIPMAP_MODE_NONE,
44        MIPMAP_MODE_NEAREST,
45        MIPMAP_MODE_LINEAR,
46    };
47    // 1. Enumerate OH_Drawing_FilterMode and OH_Drawing_MipmapMode values with cross iteration
48    for (OH_Drawing_FilterMode filterMode : filterModes) {
49        for (OH_Drawing_MipmapMode mipmapMode : mipmapModes) {
50            OH_Drawing_SamplingOptions *options = OH_Drawing_SamplingOptionsCreate(filterMode, mipmapMode);
51            EXPECT_NE(options, nullptr);
52            // 2. Destroy the objects created in step 1 with OH_Drawing_SamplingOptionsDestroy
53            OH_Drawing_SamplingOptionsDestroy(options);
54        }
55    }
56}
57
58/*
59 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SAMPLING_OPTIONS_0101
60 * @tc.name: testSamplingOptionsCreateDestroyDestroyNull
61 * @tc.desc: test for testSamplingOptionsCreateDestroyDestroyNull.
62 * @tc.size  : SmallTest
63 * @tc.type  : Function
64 * @tc.level : Level 3
65 */
66HWTEST_F(DrawingNativeSamplingOptionsTest, testSamplingOptionsCreateDestroyDestroyNull, TestSize.Level3) {
67    // 1. OH_Drawing_SamplingOptionsDestroy with empty parameter
68    OH_Drawing_SamplingOptionsDestroy(nullptr);
69}
70
71/*
72 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SAMPLING_OPTIONS_0102
73 * @tc.name: testSamplingOptionsCreateDestroyDestroyMultipleCalls
74 * @tc.desc: test for testSamplingOptionsCreateDestroyDestroyMultipleCalls.
75 * @tc.size  : SmallTest
76 * @tc.type  : Function
77 * @tc.level : Level 3
78 */
79HWTEST_F(DrawingNativeSamplingOptionsTest, testSamplingOptionsCreateDestroyDestroyMultipleCalls, TestSize.Level3) {
80    // 1. Call OH_Drawing_SamplingOptionsCreate 10 times with random enum values
81    std::random_device rd;
82    std::mt19937 gen(rd());
83    std::uniform_int_distribution<int> dis(0, 10);
84    OH_Drawing_SamplingOptions *options[10];
85    for (int i = 0; i < 10; i++) {
86        int random = dis(gen);
87        OH_Drawing_FilterMode filterMode = static_cast<OH_Drawing_FilterMode>(random % 2);
88        OH_Drawing_MipmapMode mipmapMode = static_cast<OH_Drawing_MipmapMode>(random % 3);
89        options[i] = OH_Drawing_SamplingOptionsCreate(filterMode, mipmapMode);
90        EXPECT_NE(options[i], nullptr);
91    }
92    // 2. Call OH_Drawing_SamplingOptionsDestroy 10 times
93    for (int i = 0; i < 10; i++) {
94        OH_Drawing_SamplingOptionsDestroy(options[i]);
95    }
96    // 3. Call OH_Drawing_SamplingOptionsCreate and OH_Drawing_SamplingOptionsDestroy alternately 10 times
97    for (int i = 0; i < 10; i++) {
98        int random = dis(gen);
99        OH_Drawing_FilterMode filterMode = static_cast<OH_Drawing_FilterMode>(random % 2);
100        OH_Drawing_MipmapMode mipmapMode = static_cast<OH_Drawing_MipmapMode>(random % 3);
101        OH_Drawing_SamplingOptions *options = OH_Drawing_SamplingOptionsCreate(filterMode, mipmapMode);
102        EXPECT_NE(options, nullptr);
103        OH_Drawing_SamplingOptionsDestroy(options);
104    }
105}
106
107} // namespace Drawing
108} // namespace Rosen
109} // namespace OHOS