1 /*
2 * Copyright (C) 2024 Huawei Device 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 "exif_metadata.h"
17 #include "fragment_metadata.h"
18 #include "image_common.h"
19 #include "image_common_impl.h"
20 #include <cstring>
21 #include <gtest/gtest.h>
22 using namespace testing::ext;
23 namespace OHOS {
24 namespace Media {
25 class ImageCommonNdkTest : public testing::Test {
26 public:
ImageCommonNdkTest()27 ImageCommonNdkTest() {}
~ImageCommonNdkTest()28 ~ImageCommonNdkTest() {}
29 };
30 static constexpr Image_MetadataType INVALID_METADATA = static_cast<Image_MetadataType>(100);
31
TestMetaDataClone(Image_MetadataType metadataType)32 static void TestMetaDataClone(Image_MetadataType metadataType)
33 {
34 OH_PictureMetadata *oldMetadata = nullptr;
35 Image_ErrorCode res = OH_PictureMetadata_Create(metadataType, &oldMetadata);
36 EXPECT_EQ(res, IMAGE_SUCCESS);
37 OH_PictureMetadata *newMetadata = nullptr;
38 res = OH_PictureMetadata_Clone(oldMetadata, &newMetadata);
39 EXPECT_EQ(res, IMAGE_SUCCESS);
40 res = OH_PictureMetadata_Release(oldMetadata);
41 EXPECT_EQ(res, IMAGE_SUCCESS);
42 res = OH_PictureMetadata_Release(newMetadata);
43 EXPECT_EQ(res, IMAGE_SUCCESS);
44 }
45
TestPictureMetadataGetSetProperty(Image_MetadataType metadataType,const std::string & keyString)46 static void TestPictureMetadataGetSetProperty(Image_MetadataType metadataType, const std::string &keyString)
47 {
48 OH_PictureMetadata *metadataPtr = nullptr;
49 Image_ErrorCode res = OH_PictureMetadata_Create(metadataType, &metadataPtr);
50 ASSERT_NE(metadataPtr, nullptr);
51
52 Image_String key;
53 key.data = strdup(keyString.c_str());
54 key.size = strlen(key.data);
55 Image_String srcValue;
56 char bufferValue[] = "666";
57 srcValue.data = bufferValue;
58 srcValue.size = strlen(bufferValue);
59 res = OH_PictureMetadata_SetProperty(metadataPtr, &key, &srcValue);
60 EXPECT_EQ(res, IMAGE_SUCCESS);
61
62 Image_String dstValue;
63 res = OH_PictureMetadata_GetProperty(metadataPtr, &key, &dstValue);
64 EXPECT_EQ(res, IMAGE_SUCCESS);
65 ASSERT_NE(dstValue.data, nullptr);
66 EXPECT_EQ(srcValue.size, dstValue.size);
67 EXPECT_EQ(strncmp(srcValue.data, dstValue.data, srcValue.size), 0);
68
69 res = OH_PictureMetadata_Release(metadataPtr);
70 EXPECT_EQ(res, IMAGE_SUCCESS);
71 free(key.data);
72 delete[] dstValue.data;
73 }
74
75 /**
76 * @tc.name: OH_PictureMetadata_CreateTest001
77 * @tc.desc: Tests the creation and release of picture metadata for EXIF_METADATA.
78 * The test checks if the metadata is created and released successfully.
79 * @tc.type: FUNC
80 */
81 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_CreateTest001, TestSize.Level3)
82 {
83 Image_MetadataType metadataType = EXIF_METADATA;
84 OH_PictureMetadata *metadataPtr = nullptr;
85 Image_ErrorCode res = OH_PictureMetadata_Create(metadataType, &metadataPtr);
86 EXPECT_EQ(res, IMAGE_SUCCESS);
87 OH_PictureMetadata_Release(metadataPtr);
88 EXPECT_EQ(res, IMAGE_SUCCESS);
89 }
90
91 /**
92 * @tc.name: OH_PictureMetadata_CreateTest001
93 * @tc.desc: Tests the creation and release of picture metadata for FRAGMENT_METADATA.
94 * The test checks if the metadata is created and released successfully.
95 * @tc.type: FUNC
96 */
97 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_CreateTest002, TestSize.Level1)
98 {
99 Image_MetadataType metadataType = FRAGMENT_METADATA;
100 OH_PictureMetadata *metadataPtr = nullptr;
101 Image_ErrorCode res = OH_PictureMetadata_Create(metadataType, &metadataPtr);
102 EXPECT_EQ(res, IMAGE_SUCCESS);
103 OH_PictureMetadata_Release(metadataPtr);
104 EXPECT_EQ(res, IMAGE_SUCCESS);
105 }
106
107 /**
108 * @tc.name: OH_PictureMetadata_CreateTest003
109 * @tc.desc: Tests creating picture metadata with a null pointer for the metadata pointer.
110 * The test checks if the function returns the correct error code when a null pointer is provided.
111 * @tc.type: FUNC
112 */
113 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_CreateTest003, TestSize.Level3)
114 {
115 Image_MetadataType metadataType = EXIF_METADATA;
116 Image_ErrorCode res = OH_PictureMetadata_Create(metadataType, nullptr);
117 EXPECT_EQ(res, IMAGE_BAD_PARAMETER);
118 }
119
120 /**
121 * @tc.name: OH_PictureMetadata_CreateTest004
122 * @tc.desc: Tests creating picture metadata with an invalid metadata type.
123 * The test checks if the function returns the correct error code when an invalid type is provided.
124 * @tc.type: FUNC
125 */
126 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_CreateTest004, TestSize.Level3)
127 {
128 OH_PictureMetadata *metadataPtr = nullptr;
129 Image_ErrorCode res = OH_PictureMetadata_Create(INVALID_METADATA, &metadataPtr);
130 EXPECT_EQ(res, IMAGE_BAD_PARAMETER);
131 }
132
133 /**
134 * @tc.name: OH_PictureMetadata_GetProperty001
135 * @tc.desc: Test OH_PictureMetadata_GetProperty with null pointers.
136 * @tc.type: FUNC
137 */
138 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_GetPropertyTest001, TestSize.Level3)
139 {
140 Image_ErrorCode res = OH_PictureMetadata_GetProperty(nullptr, nullptr, nullptr);
141 EXPECT_EQ(res, IMAGE_BAD_PARAMETER);
142 }
143
144 /**
145 * @tc.name: OH_PictureMetadata_GetPropertyWithNull
146 * @tc.desc: test OH_PictureMetadata_GetPropertyWithNull with null pointer
147 * @tc.type: FUNC
148 */
149 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_GetPropertyWithNullTest001, TestSize.Level3)
150 {
151 GTEST_LOG_(INFO) << "ImagSourceNdk2Test: OH_PictureMetadata_GetPropertyWithNull start";
152 OH_PictureMetadata *metadataPtr = nullptr;
153 Image_String* key = nullptr;
154 Image_String* value = nullptr;
155 Image_ErrorCode ret = OH_PictureMetadata_GetPropertyWithNull(metadataPtr, key, value);
156 ASSERT_NE(ret, IMAGE_SUCCESS);
157 GTEST_LOG_(INFO) << "ImagSourceNdk2Test: OH_PictureMetadata_GetPropertyWithNull end";
158 }
159
160 /**
161 * @tc.name: OH_PictureMetadata_GetPropertyWithNullTest002
162 * @tc.desc: test OH_PictureMetadata_GetPropertyWithNull with right value
163 * @tc.type: FUNC
164 */
165 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_GetPropertyWithNullTest002, TestSize.Level3)
166 {
167 GTEST_LOG_(INFO) << "ImagSourceNdk2Test: OH_PictureMetadata_GetPropertyWithNullTest002 start";
168 Image_MetadataType metadataType = EXIF_METADATA;
169 const std::string keyString = "ImageWidth";
170
171 OH_PictureMetadata *metadataPtr = nullptr;
172 Image_ErrorCode res = OH_PictureMetadata_Create(metadataType, &metadataPtr);
173 ASSERT_NE(metadataPtr, nullptr);
174
175 Image_String key;
176 key.data = strdup(keyString.c_str());
177 key.size = strlen(key.data);
178 Image_String srcValue;
179 char bufferValue[] = "666";
180 srcValue.data = bufferValue;
181 srcValue.size = strlen(bufferValue);
182 res = OH_PictureMetadata_SetProperty(metadataPtr, &key, &srcValue);
183 EXPECT_EQ(res, IMAGE_SUCCESS);
184
185 Image_String dstValue;
186 res = OH_PictureMetadata_GetPropertyWithNull(metadataPtr, &key, &dstValue);
187 EXPECT_EQ(res, IMAGE_SUCCESS);
188 ASSERT_NE(dstValue.data, nullptr);
189 EXPECT_EQ(srcValue.size, dstValue.size);
190 EXPECT_EQ(strncmp(srcValue.data, dstValue.data, srcValue.size), 0);
191
192 res = OH_PictureMetadata_Release(metadataPtr);
193 EXPECT_EQ(res, IMAGE_SUCCESS);
194 free(key.data);
195 if (dstValue.data != nullptr) {
196 delete[] dstValue.data;
197 dstValue.data = nullptr;
198 }
199
200 GTEST_LOG_(INFO) << "ImagSourceNdk2Test: OH_PictureMetadata_GetPropertyWithNullTest002 end";
201 }
202
203 /**
204 * @tc.name: OH_PictureMetadata_SetProperty001
205 * @tc.desc: Test OH_PictureMetadata_SetProperty with null pointers.
206 * @tc.type: FUNC
207 */
208 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_SetPropertyTest001, TestSize.Level3)
209 {
210 Image_ErrorCode res = OH_PictureMetadata_SetProperty(nullptr, nullptr, nullptr);
211 EXPECT_EQ(res, IMAGE_BAD_PARAMETER);
212 }
213
214 /**
215 * @tc.name: OH_PictureMetadata_GetSetPropertyTest1
216 * @tc.desc: Tests setting and getting a property in picture metadata for EXIF_METADATA.
217 * The test checks if the metadata can be created, properties set and retrieved correctly.
218 * @tc.type: FUNC
219 */
220 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_GetSetPropertyTest1, TestSize.Level1)
221 {
222 TestPictureMetadataGetSetProperty(EXIF_METADATA, "ImageWidth");
223 }
224
225 /**
226 * @tc.name: OH_PictureMetadata_GetSetPropertyTest
227 * @tc.desc: Tests setting and getting a property in picture metadata for FRAGMENT_METADATA.
228 * The test checks if the metadata can be created, properties set and retrieved correctly.
229 * @tc.type: FUNC
230 */
231 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_GetSetPropertyTest2, TestSize.Level1)
232 {
233 TestPictureMetadataGetSetProperty(FRAGMENT_METADATA, FRAGMENT_METADATA_KEY_WIDTH);
234 }
235 /**
236 * @tc.name: OH_PictureMetadata_ReleaseTest001
237 * @tc.desc: Test OH_PictureMetadata_Release with null a pointer.
238 * @tc.type: FUNC
239 */
240 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_ReleaseTest001, TestSize.Level3)
241 {
242 Image_ErrorCode res = OH_PictureMetadata_Release(nullptr);
243 EXPECT_EQ(res, IMAGE_BAD_PARAMETER);
244 }
245
246 /**
247 * @tc.name: OH_PictureMetadata_CloneTest001
248 * @tc.desc: test OH_PictureMetadata_Clone with null pointers.
249 * @tc.type: FUNC
250 */
251 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_CloneTest001, TestSize.Level3)
252 {
253 Image_ErrorCode res = OH_PictureMetadata_Clone(nullptr, nullptr);
254 EXPECT_EQ(res, IMAGE_BAD_PARAMETER);
255 }
256
257 /**
258 * @tc.name: OH_PictureMetadata_CloneTest002
259 * @tc.desc: Tests cloning picture metadata for EXIF_METADATA.
260 * The test checks if the metadata is cloned successfully.
261 * @tc.type: FUNC
262 */
263 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_CloneTest002, TestSize.Level1)
264 {
265 TestMetaDataClone(EXIF_METADATA);
266 }
267
268 /**
269 * @tc.name: OH_PictureMetadata_CloneTest003
270 * @tc.desc: Tests cloning picture metadata for FRAGMENT_METADATA.
271 * The test checks if the metadata is cloned successfully.
272 * @tc.type: FUNC
273 */
274 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_CloneTest003, TestSize.Level1)
275 {
276 TestMetaDataClone(FRAGMENT_METADATA);
277 }
278
279 } // namespace Media
280 } // namespace OHOS