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_error_code.h" 17 #include "drawing_shadow_layer.h" 18 #include "gtest/gtest.h" 19 #include <random> 20 21 using namespace testing; 22 using namespace testing::ext; 23 24 namespace OHOS { 25 namespace Rosen { 26 namespace Drawing { 27 class DrawingNativeShadowLayerTest : public testing::Test { 28 protected: 29 // 在每个测试用例执行前调用 SetUp()30 void SetUp() override 31 { 32 // 设置代码 33 std::cout << "DrawingNativeShadowLayerTest Setup code called before each test case." << std::endl; 34 OH_Drawing_ErrorCodeReset(); 35 std::cout << "DrawingNativeShadowLayerTest errorCodeReset before each test case." << std::endl; 36 } TearDown()37 void TearDown() override 38 { 39 std::cout << "DrawingNativeShadowLayerTest Setup code called after each test case." << std::endl; 40 OH_Drawing_ErrorCodeReset(); 41 std::cout << "DrawingNativeShadowLayerTest errorCodeReset after each test case." << std::endl; 42 } 43 }; 44 45 /* 46 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SHADOW_LAYER_0100 47 * @tc.name: testShadowLayerCreateNormal 48 * @tc.desc: test for testShadowLayerCreateNormal. 49 * @tc.size : SmallTest 50 * @tc.type : Function 51 * @tc.level : Level 0 52 */ 53 HWTEST_F(DrawingNativeShadowLayerTest, testShadowLayerCreateNormal, Function | SmallTest | Level0) { 54 // 1. Call OH_Drawing_ShadowLayerCreate with integer values for blurRadius, x, and y 55 OH_Drawing_ShadowLayer *shadow = OH_Drawing_ShadowLayerCreate(3, -3, 3, 0xFF00FF00); 56 EXPECT_NE(shadow, nullptr); 57 OH_Drawing_ShadowLayerDestroy(shadow); 58 // 2. Call OH_Drawing_ShadowLayerCreate with floating-point values for blurRadius, x, and y 59 OH_Drawing_ShadowLayer *shadow2 = OH_Drawing_ShadowLayerCreate(3.f, -3.f, 3.f, 0xFF00FF00); 60 EXPECT_NE(shadow2, nullptr); 61 OH_Drawing_ShadowLayerDestroy(shadow2); 62 } 63 64 /* 65 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SHADOW_LAYER_0101 66 * @tc.name: testShadowLayerCreateNull 67 * @tc.desc: test for testShadowLayerCreateNull. 68 * @tc.size : SmallTest 69 * @tc.type : Function 70 * @tc.level : Level 3 71 */ 72 HWTEST_F(DrawingNativeShadowLayerTest, testShadowLayerCreateNull, Function | SmallTest | Level3) { 73 // 1. OH_Drawing_ShadowLayerCreate with the first parameter being empty, check the error code using 74 // OH_Drawing_ErrorCodeGet 75 OH_Drawing_ShadowLayer *shadow = OH_Drawing_ShadowLayerCreate(0, -3, 3, 0xFF00FF00); 76 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_PARAMETER_OUT_OF_RANGE); 77 // 2. OH_Drawing_ShadowLayerCreate with the second parameter being empty 78 OH_Drawing_ShadowLayer *shadow2 = OH_Drawing_ShadowLayerCreate(3, 0, 3, 0xFF00FF00); 79 EXPECT_NE(shadow2, nullptr); 80 // 3. OH_Drawing_ShadowLayerCreate with the third parameter being empty 81 OH_Drawing_ShadowLayer *shadow3 = OH_Drawing_ShadowLayerCreate(3, -3, 0, 0xFF00FF00); 82 EXPECT_NE(shadow3, nullptr); 83 // 4. OH_Drawing_ShadowLayerCreate with the fourth parameter being empty 84 OH_Drawing_ShadowLayer *shadow4 = OH_Drawing_ShadowLayerCreate(3, -3, 3, 0); 85 EXPECT_NE(shadow4, nullptr); 86 // 5. Free memory 87 OH_Drawing_ShadowLayerDestroy(shadow); 88 OH_Drawing_ShadowLayerDestroy(shadow2); 89 OH_Drawing_ShadowLayerDestroy(shadow3); 90 OH_Drawing_ShadowLayerDestroy(shadow4); 91 } 92 93 /* 94 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SHADOW_LAYER_0102 95 * @tc.name: testShadowLayerCreateAbnormal 96 * @tc.desc: test for testShadowLayerCreateAbnormal. 97 * @tc.size : SmallTest 98 * @tc.type : Function 99 * @tc.level : Level 3 100 */ 101 HWTEST_F(DrawingNativeShadowLayerTest, testShadowLayerCreateAbnormal, Function | SmallTest | Level3) { 102 // 1. OH_Drawing_ShadowLayerCreate with the first parameter as a negative number, check the error code using 103 // OH_Drawing_ErrorCodeGet 104 OH_Drawing_ShadowLayer *shadow = OH_Drawing_ShadowLayerCreate(-3, 3, 3, 0xFF00FF00); 105 EXPECT_EQ(OH_Drawing_ErrorCodeGet(), OH_DRAWING_ERROR_PARAMETER_OUT_OF_RANGE); 106 // 2. OH_Drawing_ShadowLayerCreate with the second parameter as a negative number 107 OH_Drawing_ShadowLayer *shadow2 = OH_Drawing_ShadowLayerCreate(3, -3, 3, 0xFF00FF00); 108 EXPECT_NE(shadow2, nullptr); 109 // 3. OH_Drawing_ShadowLayerCreate with the third parameter as a negative number 110 OH_Drawing_ShadowLayer *shadow3 = OH_Drawing_ShadowLayerCreate(3, 3, -3, 0xFF00FF00); 111 EXPECT_NE(shadow3, nullptr); 112 // 4. OH_Drawing_ShadowLayerCreate with the fourth parameter as a negative number 113 OH_Drawing_ShadowLayer *shadow4 = OH_Drawing_ShadowLayerCreate(3, 3, 3, -0xFF00FF00); 114 EXPECT_NE(shadow4, nullptr); 115 // 5. Free memory 116 OH_Drawing_ShadowLayerDestroy(shadow); 117 OH_Drawing_ShadowLayerDestroy(shadow2); 118 OH_Drawing_ShadowLayerDestroy(shadow3); 119 OH_Drawing_ShadowLayerDestroy(shadow4); 120 } 121 122 /* 123 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SHADOW_LAYER_0103 124 * @tc.name: testShadowLayerCreateMaximum 125 * @tc.desc: test for testShadowLayerCreateMaximum. 126 * @tc.size : SmallTest 127 * @tc.type : Function 128 * @tc.level : Level 3 129 */ 130 HWTEST_F(DrawingNativeShadowLayerTest, testShadowLayerCreateMaximum, Function | SmallTest | Level3) { 131 // 1. Call OH_Drawing_ShadowLayerCreate with the first parameter as the maximum value 132 OH_Drawing_ShadowLayer *shadow = OH_Drawing_ShadowLayerCreate(FLT_MAX, 3, 3, 0xFF00FF00); 133 EXPECT_NE(shadow, nullptr); 134 // 2. Call OH_Drawing_ShadowLayerCreate with the second parameter as the maximum value 135 OH_Drawing_ShadowLayer *shadow2 = OH_Drawing_ShadowLayerCreate(3, FLT_MAX, 3, 0xFF00FF00); 136 EXPECT_NE(shadow2, nullptr); 137 // 3. Call OH_Drawing_ShadowLayerCreate with the third parameter as the maximum value 138 OH_Drawing_ShadowLayer *shadow3 = OH_Drawing_ShadowLayerCreate(3, 3, FLT_MAX, 0xFF00FF00); 139 EXPECT_NE(shadow3, nullptr); 140 // 4. Call OH_Drawing_ShadowLayerCreate with the fourth parameter as the maximum value 141 OH_Drawing_ShadowLayer *shadow4 = OH_Drawing_ShadowLayerCreate(3, 3, 3, UINT32_MAX); 142 EXPECT_NE(shadow4, nullptr); 143 // 5. Free memory 144 OH_Drawing_ShadowLayerDestroy(shadow); 145 OH_Drawing_ShadowLayerDestroy(shadow2); 146 OH_Drawing_ShadowLayerDestroy(shadow3); 147 OH_Drawing_ShadowLayerDestroy(shadow4); 148 } 149 150 /* 151 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SHADOW_LAYER_0104 152 * @tc.name: testShadowLayerCreateMultipleCalls 153 * @tc.desc: test for testShadowLayerCreateMultipleCalls. 154 * @tc.size : SmallTest 155 * @tc.type : Function 156 * @tc.level : Level 3 157 */ 158 HWTEST_F(DrawingNativeShadowLayerTest, testShadowLayerCreateMultipleCalls, Function | SmallTest | Level3) { 159 // 1. Call OH_Drawing_ShadowLayerCreate 10 times with random values for blurRadius, x, y, and different colors 160 std::random_device rd; 161 std::mt19937 gen(rd()); 162 std::uniform_real_distribution<float> dis(0, 100); 163 for (int i = 0; i < 10; i++) { 164 float blurRadius = dis(gen); 165 float x = dis(gen); 166 float y = dis(gen); 167 uint32_t color = dis(gen); 168 OH_Drawing_ShadowLayer *shadow = OH_Drawing_ShadowLayerCreate(blurRadius, x, y, color); 169 EXPECT_NE(shadow, nullptr); 170 OH_Drawing_ShadowLayerDestroy(shadow); 171 } 172 } 173 174 /* 175 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SHADOW_LAYER_0200 176 * @tc.name: testShadowLayerDestroyNormal 177 * @tc.desc: test for testShadowLayerDestroyNormal. 178 * @tc.size : SmallTest 179 * @tc.type : Function 180 * @tc.level : Level 0 181 */ 182 HWTEST_F(DrawingNativeShadowLayerTest, testShadowLayerDestroyNormal, Function | SmallTest | Level0) { 183 // 1. Call OH_Drawing_ShadowLayerCreate 184 OH_Drawing_ShadowLayer *shadow = OH_Drawing_ShadowLayerCreate(3, 3, 3, 0xFF00FF00); 185 // add assert 186 EXPECT_NE(shadow, nullptr); 187 // 2. Call OH_Drawing_ShadowLayerDestroy 188 OH_Drawing_ShadowLayerDestroy(shadow); 189 } 190 191 /* 192 * @tc.number: SUB_BASIC_GRAPHICS_SPECIAL_API_C_DRAWING_SHADOW_LAYER_0201 193 * @tc.name: testShadowLayerDestroyNull 194 * @tc.desc: test for testShadowLayerDestroyNull. 195 * @tc.size : SmallTest 196 * @tc.type : Function 197 * @tc.level : Level 3 198 */ 199 HWTEST_F(DrawingNativeShadowLayerTest, testShadowLayerDestroyNull, Function | SmallTest | Level3) { 200 // 1. OH_Drawing_ShadowLayerDestroy with null parameter 201 OH_Drawing_ShadowLayerDestroy(nullptr); 202 // add assert 203 EXPECT_TRUE(true); 204 } 205 206 } // namespace Drawing 207 } // namespace Rosen 208 } // namespace OHOS