• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SetProperty001
146  * @tc.desc: Test OH_PictureMetadata_SetProperty with null pointers.
147  * @tc.type: FUNC
148  */
149 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_SetPropertyTest001, TestSize.Level3)
150 {
151     Image_ErrorCode res = OH_PictureMetadata_SetProperty(nullptr, nullptr, nullptr);
152     EXPECT_EQ(res, IMAGE_BAD_PARAMETER);
153 }
154 
155 /**
156  * @tc.name: OH_PictureMetadata_GetSetPropertyTest1
157  * @tc.desc: Tests setting and getting a property in picture metadata for EXIF_METADATA.
158  *           The test checks if the metadata can be created, properties set and retrieved correctly.
159  * @tc.type: FUNC
160  */
161 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_GetSetPropertyTest1, TestSize.Level1)
162 {
163     TestPictureMetadataGetSetProperty(EXIF_METADATA, "ImageWidth");
164 }
165 
166 /**
167  * @tc.name: OH_PictureMetadata_GetSetPropertyTest
168  * @tc.desc: Tests setting and getting a property in picture metadata for FRAGMENT_METADATA.
169  *           The test checks if the metadata can be created, properties set and retrieved correctly.
170  * @tc.type: FUNC
171  */
172 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_GetSetPropertyTest2, TestSize.Level1)
173 {
174     TestPictureMetadataGetSetProperty(FRAGMENT_METADATA, FRAGMENT_METADATA_KEY_WIDTH);
175 }
176 /**
177  * @tc.name: OH_PictureMetadata_ReleaseTest001
178  * @tc.desc: Test OH_PictureMetadata_Release with null a pointer.
179  * @tc.type: FUNC
180  */
181 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_ReleaseTest001, TestSize.Level3)
182 {
183     Image_ErrorCode res = OH_PictureMetadata_Release(nullptr);
184     EXPECT_EQ(res, IMAGE_BAD_PARAMETER);
185 }
186 
187 /**
188  * @tc.name: OH_PictureMetadata_CloneTest001
189  * @tc.desc: test OH_PictureMetadata_Clone with null pointers.
190  * @tc.type: FUNC
191  */
192 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_CloneTest001, TestSize.Level3)
193 {
194     Image_ErrorCode res = OH_PictureMetadata_Clone(nullptr, nullptr);
195     EXPECT_EQ(res, IMAGE_BAD_PARAMETER);
196 }
197 
198 /**
199  * @tc.name: OH_PictureMetadata_CloneTest002
200  * @tc.desc: Tests cloning picture metadata for EXIF_METADATA.
201  *           The test checks if the metadata is cloned successfully.
202  * @tc.type: FUNC
203  */
204 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_CloneTest002, TestSize.Level1)
205 {
206     TestMetaDataClone(EXIF_METADATA);
207 }
208 
209 /**
210  * @tc.name: OH_PictureMetadata_CloneTest003
211  * @tc.desc: Tests cloning picture metadata for FRAGMENT_METADATA.
212  *           The test checks if the metadata is cloned successfully.
213  * @tc.type: FUNC
214  */
215 HWTEST_F(ImageCommonNdkTest, OH_PictureMetadata_CloneTest003, TestSize.Level1)
216 {
217     TestMetaDataClone(FRAGMENT_METADATA);
218 }
219 
220 } // namespace Media
221 } // namespace OHOS