1 /*
2 * Copyright (c) 2025 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 "UdmfTest.h"
17 #include "napi/native_api.h"
18 #include <database/udmf/udmf.h>
19 #include <js_native_api_types.h>
20 #include <napi/native_api.h>
21 #include <multimedia/image_framework/image/pixelmap_native.h>
22 #include <string>
23 #include <database/udmf/udmf_meta.h>
24 #include <database/udmf/udmf_err_code.h>
25
26 int g_invalidParamCode = 20400001;
27 int g_err = 20400000;
28
AddUdmfTest(std::vector<napi_property_descriptor> & descData)29 void AddUdmfTest(std::vector<napi_property_descriptor> &descData)
30 {
31 descData.push_back({"testDestroyInvalidParam", nullptr, TestDestroyInvalidParam, nullptr, nullptr, nullptr,
32 napi_default, nullptr});
33 descData.push_back({"testAddFileUriInvalidParam", nullptr, TestAddFileUriInvalidParam, nullptr, nullptr, nullptr,
34 napi_default, nullptr});
35 descData.push_back(
36 {"testAddFileUriSuccess", nullptr, TestAddFileUriSuccess, nullptr, nullptr, nullptr, napi_default, nullptr});
37 descData.push_back({"testAddPixelMapInvalidParam", nullptr, TestAddPixelMapInvalidParam, nullptr, nullptr, nullptr,
38 napi_default, nullptr});
39 descData.push_back(
40 {"testAddPixelMapSuccess", nullptr, TestAddPixelMapSuccess, nullptr, nullptr, nullptr, napi_default, nullptr});
41 descData.push_back({"testGetGeneralEntryInvalidParam", nullptr, TestGetGeneralEntryInvalidParam, nullptr, nullptr,
42 nullptr, napi_default, nullptr});
43 descData.push_back(
44 {"testGetPlainTextError", nullptr, testGetPlainTextError, nullptr, nullptr, nullptr, napi_default, nullptr});
45 descData.push_back(
46 {"testGetHyperlinkError", nullptr, TestGetHyperlinkError, nullptr, nullptr, nullptr, napi_default, nullptr});
47 descData.push_back(
48 {"testGetHtmlError", nullptr, TestGetHtmlError, nullptr, nullptr, nullptr, napi_default, nullptr});
49 descData.push_back({"testGetAppItemInvalidParam", nullptr, TestGetAppItemInvalidParam, nullptr, nullptr, nullptr,
50 napi_default, nullptr});
51 descData.push_back(
52 {"testGetAppItemError", nullptr, TestGetAppItemError, nullptr, nullptr, nullptr, napi_default, nullptr});
53 descData.push_back({"testSetProviderInvalidParam", nullptr, TestSetProviderInvalidParam, nullptr, nullptr, nullptr,
54 napi_default, nullptr});
55 descData.push_back({"testGetVisibilitySuccess", nullptr, TestGetVisibilitySuccess, nullptr, nullptr, nullptr,
56 napi_default, nullptr});
57 descData.push_back({"testSetVisibilityInvalidParam", nullptr, TestSetVisibilityInvalidParam, nullptr, nullptr,
58 nullptr, napi_default, nullptr});
59 descData.push_back({"testSetVisibilitySuccess", nullptr, TestSetVisibilitySuccess, nullptr, nullptr, nullptr,
60 napi_default, nullptr});
61 descData.push_back({"testGetUnifiedDataError", nullptr, TestGetUnifiedDataError, nullptr, nullptr, nullptr,
62 napi_default, nullptr});
63 descData.push_back({"testGetUnifiedDataByOptionsError", nullptr, TestGetUnifiedDataByOptionsError, nullptr, nullptr,
64 nullptr, napi_default, nullptr});
65 descData.push_back({"testSetUnifiedDataError", nullptr, TestSetUnifiedDataError, nullptr, nullptr, nullptr,
66 napi_default, nullptr});
67 descData.push_back({"testUpdateUnifiedDataError", nullptr, TestUpdateUnifiedDataError, nullptr, nullptr, nullptr,
68 napi_default, nullptr});
69 descData.push_back({"testDeleteUnifiedDataError", nullptr, TestDeleteUnifiedDataError, nullptr, nullptr, nullptr,
70 napi_default, nullptr});
71 descData.push_back({"testUdmfOptionsGetVisibilityProcess", nullptr, TestUdmfOptionsGetVisibilityProcess, nullptr,
72 nullptr, nullptr, napi_default, nullptr});
73
74 descData.push_back({"testUdmfOptionsGetVisibilityAll", nullptr, TestUdmfOptionsGetVisibilityAll, nullptr, nullptr,
75 nullptr, napi_default, nullptr});
76 }
77
78 //数据回调
GetDataCallbackFunc(void * context,const char * type)79 static void* GetDataCallbackFunc(void* context, const char* type)
80 {
81 auto plainText = OH_UdsPlainText_Create();
82 OH_UdsPlainText_SetAbstract(plainText, "doing something");
83 OH_UdsPlainText_SetContent(plainText, "doing something");
84 return plainText;
85 }
86 //资源释放
FinalizeFunc(void * context)87 static void FinalizeFunc(void* context) {};
88
TestDestroyInvalidParam(napi_env env,napi_callback_info info)89 napi_value TestDestroyInvalidParam(napi_env env, napi_callback_info info)
90 {
91 napi_value result;
92 OH_UdmfRecordProvider* provider = nullptr;
93
94 int ret = OH_UdmfRecordProvider_Destroy(provider);
95
96 napi_create_int32(env, ret == Udmf_ErrCode::UDMF_E_INVALID_PARAM ? g_invalidParamCode : 1, &result);
97
98 return result;
99 }
100
TestAddFileUriInvalidParam(napi_env env,napi_callback_info info)101 napi_value TestAddFileUriInvalidParam(napi_env env, napi_callback_info info)
102 {
103 napi_value result;
104 OH_UdmfRecord *record = nullptr;
105 OH_UdsFileUri *fileUri = OH_UdsFileUri_Create();
106 const char *fileUriPath = "./resources/base/media/icon.png";
107
108 OH_UdsFileUri_SetFileUri(fileUri, fileUriPath);
109
110 int ret = OH_UdmfRecord_AddFileUri(record, fileUri);
111
112 OH_UdsFileUri_Destroy(fileUri);
113
114 napi_create_int32(env, ret == Udmf_ErrCode::UDMF_E_INVALID_PARAM ? g_invalidParamCode : 1, &result);
115 return result;
116 }
117
TestAddFileUriSuccess(napi_env env,napi_callback_info info)118 napi_value TestAddFileUriSuccess(napi_env env, napi_callback_info info)
119 {
120 napi_value result;
121 OH_UdmfRecord *record = OH_UdmfRecord_Create();
122 OH_UdsFileUri *fileUri = OH_UdsFileUri_Create();
123 const char *fileUriPath = "./resources/base/media/icon.png";
124
125 OH_UdsFileUri_SetFileUri(fileUri, fileUriPath);
126
127 int ret = OH_UdmfRecord_AddFileUri(record, fileUri);
128
129 OH_UdsFileUri_Destroy(fileUri);
130 OH_UdmfRecord_Destroy(record);
131 napi_create_int32(env, ret == Udmf_ErrCode::UDMF_E_OK ? 0 : 1, &result);
132 return result;
133 }
134
TestAddPixelMapInvalidParam(napi_env env,napi_callback_info info)135 napi_value TestAddPixelMapInvalidParam(napi_env env, napi_callback_info info)
136 {
137 napi_value result;
138 OH_UdsPixelMap *pixelMap = OH_UdsPixelMap_Create();
139 uint8_t data [500];
140 int dataSize = 500;
141 for (int i = 0; i < dataSize; i++) {
142 data[i] = i + 1;
143 }
144 OH_Pixelmap_InitializationOptions *createOpts;
145 OH_PixelmapInitializationOptions_Create(&createOpts);
146 int width = 6;
147 OH_PixelmapInitializationOptions_SetWidth(createOpts, width);
148 int height = 4;
149 OH_PixelmapInitializationOptions_SetHeight(createOpts, height);
150 OH_PixelmapInitializationOptions_SetPixelFormat(createOpts, PIXEL_FORMAT_RGBA_8888);
151 OH_PixelmapInitializationOptions_SetAlphaType(createOpts, PIXELMAP_ALPHA_TYPE_UNKNOWN);
152
153 OH_PixelmapNative *pixelmapNative;
154 OH_PixelmapNative_CreatePixelmap(data, dataSize, createOpts, &pixelmapNative);
155
156 OH_UdsPixelMap_SetPixelMap(pixelMap, pixelmapNative);
157
158 OH_UdmfRecord *udmfRecord = OH_UdmfRecord_Create();
159 int ret = OH_UdmfRecord_AddPixelMap(nullptr, pixelMap);
160 napi_create_int32(env, ret == Udmf_ErrCode::UDMF_E_INVALID_PARAM ? g_invalidParamCode : 1, &result);
161 return result;
162 }
163
TestAddPixelMapSuccess(napi_env env,napi_callback_info info)164 napi_value TestAddPixelMapSuccess(napi_env env, napi_callback_info info)
165 {
166 napi_value result;
167 OH_UdsPixelMap *pixelMap = OH_UdsPixelMap_Create();
168 uint8_t data [500];
169 int dataSize = 500;
170 for (int i = 0; i < dataSize; i++) {
171 data[i] = i + 1;
172 }
173 OH_Pixelmap_InitializationOptions *createOpts;
174 OH_PixelmapInitializationOptions_Create(&createOpts);
175 int width = 6;
176 OH_PixelmapInitializationOptions_SetWidth(createOpts, width);
177 int height = 4;
178 OH_PixelmapInitializationOptions_SetHeight(createOpts, height);
179 OH_PixelmapInitializationOptions_SetPixelFormat(createOpts, PIXEL_FORMAT_RGBA_8888);
180 OH_PixelmapInitializationOptions_SetAlphaType(createOpts, PIXELMAP_ALPHA_TYPE_UNKNOWN);
181
182 OH_PixelmapNative *pixelmapNative;
183 OH_PixelmapNative_CreatePixelmap(data, dataSize, createOpts, &pixelmapNative);
184
185 OH_UdsPixelMap_SetPixelMap(pixelMap, pixelmapNative);
186
187 OH_UdmfRecord *udmfRecord = OH_UdmfRecord_Create();
188 int ret = OH_UdmfRecord_AddPixelMap(udmfRecord, pixelMap);
189 napi_create_int32(env, ret == Udmf_ErrCode::UDMF_E_OK ? 0 : 1, &result);
190 return result;
191 }
192
TestGetGeneralEntryInvalidParam(napi_env env,napi_callback_info info)193 napi_value TestGetGeneralEntryInvalidParam(napi_env env, napi_callback_info info)
194 {
195 napi_value result;
196 OH_UdmfRecord *record = nullptr;
197
198 char typeId[] = "test_type";
199 unsigned char *entry;
200 unsigned int count = 0;
201
202 int ret = OH_UdmfRecord_GetGeneralEntry(record, typeId, &entry, &count);
203
204 napi_create_int32(env, ret == Udmf_ErrCode::UDMF_E_INVALID_PARAM ? g_invalidParamCode : 1, &result);
205 return result;
206 }
207
testGetPlainTextError(napi_env env,napi_callback_info info)208 napi_value testGetPlainTextError(napi_env env, napi_callback_info info)
209 {
210 napi_value result;
211 //新建UDMF记录
212 OH_UdmfRecord *record = OH_UdmfRecord_Create();
213
214 OH_UdsHyperlink *hyperlink1 = OH_UdsHyperlink_Create();
215 char url[] = "";
216 OH_UdsHyperlink_SetUrl(hyperlink1, url);
217 int addRes = OH_UdmfRecord_AddHyperlink(record, hyperlink1);
218
219 OH_UdsPlainText *plainText = OH_UdsPlainText_Create();
220
221 int ret = OH_UdmfRecord_GetPlainText(record, plainText);
222
223 napi_create_int32(env, ret == Udmf_ErrCode::UDMF_ERR ? g_err : 1, &result);
224 OH_UdmfRecord_Destroy(record);
225 OH_UdsHyperlink_Destroy(hyperlink1);
226 OH_UdsPlainText_Destroy(plainText);
227 return result;
228 }
229
TestGetHyperlinkError(napi_env env,napi_callback_info info)230 napi_value TestGetHyperlinkError(napi_env env, napi_callback_info info)
231 {
232 napi_value result;
233 //新建UDMF记录
234 OH_UdmfRecord *record = OH_UdmfRecord_Create();
235
236 OH_UdsPlainText *plainText = OH_UdsPlainText_Create();
237 char content[] = "hello world";
238 OH_UdsPlainText_SetContent(plainText, content);
239 int addRes = OH_UdmfRecord_AddPlainText(record, plainText);
240
241 OH_UdsHyperlink *hyperlink1 = OH_UdsHyperlink_Create();
242
243 int ret = OH_UdmfRecord_GetHyperlink(record, hyperlink1);
244
245 napi_create_int32(env, ret == Udmf_ErrCode::UDMF_ERR ? g_err : 1, &result);
246 OH_UdmfRecord_Destroy(record);
247 OH_UdsHyperlink_Destroy(hyperlink1);
248 OH_UdsPlainText_Destroy(plainText);
249 return result;
250 }
251
TestGetHtmlError(napi_env env,napi_callback_info info)252 napi_value TestGetHtmlError(napi_env env, napi_callback_info info)
253 {
254 napi_value result;
255 //新建UDMF记录
256 OH_UdmfRecord *record = OH_UdmfRecord_Create();
257
258 OH_UdsPlainText *plainText = OH_UdsPlainText_Create();
259 char content[] = "hello world";
260 OH_UdsPlainText_SetContent(plainText, content);
261 int addRes = OH_UdmfRecord_AddPlainText(record, plainText);
262
263 OH_UdsHtml *html1 = OH_UdsHtml_Create();
264
265 int ret = OH_UdmfRecord_GetHtml(record, html1);
266
267 napi_create_int32(env, ret == Udmf_ErrCode::UDMF_ERR ? g_err : 1, &result);
268 OH_UdmfRecord_Destroy(record);
269 OH_UdsHtml_Destroy(html1);
270 OH_UdsPlainText_Destroy(plainText);
271 return result;
272 }
273
TestGetAppItemInvalidParam(napi_env env,napi_callback_info info)274 napi_value TestGetAppItemInvalidParam(napi_env env, napi_callback_info info)
275 {
276 napi_value result;
277 OH_UdmfRecord *record1 = OH_UdmfRecord_Create();
278 OH_UdsAppItem *appItem1 = OH_UdsAppItem_Create();
279 char name[] = "appItem";
280
281 OH_UdsAppItem_SetName(appItem1, name);
282 OH_UdmfRecord_AddAppItem(record1, appItem1);
283
284 OH_UdsAppItem *appItem2 = OH_UdsAppItem_Create();
285
286 int ret = OH_UdmfRecord_GetAppItem(nullptr, appItem2);
287
288 napi_create_int32(env, ret == Udmf_ErrCode::UDMF_E_INVALID_PARAM ? g_invalidParamCode : 1, &result);
289 OH_UdmfRecord_Destroy(record1);
290 OH_UdsAppItem_Destroy(appItem1);
291 OH_UdsAppItem_Destroy(appItem2);
292 return result;
293 }
294
TestGetAppItemError(napi_env env,napi_callback_info info)295 napi_value TestGetAppItemError(napi_env env, napi_callback_info info)
296 {
297 napi_value result;
298 OH_UdmfRecord *record1 = OH_UdmfRecord_Create();
299 OH_UdsPlainText *plainText1 = OH_UdsPlainText_Create();
300 char context[] = "hello";
301
302 OH_UdsPlainText_SetContent(plainText1, context);
303 OH_UdmfRecord_AddPlainText(record1, plainText1);
304
305 OH_UdsAppItem *appItem2 = OH_UdsAppItem_Create();
306
307 int ret = OH_UdmfRecord_GetAppItem(record1, appItem2);
308
309 napi_create_int32(env, ret == Udmf_ErrCode::UDMF_ERR ? g_err : 1, &result);
310 OH_UdmfRecord_Destroy(record1);
311 OH_UdsPlainText_Destroy(plainText1);
312 OH_UdsAppItem_Destroy(appItem2);
313 return result;
314 }
315
TestSetProviderInvalidParam(napi_env env,napi_callback_info info)316 napi_value TestSetProviderInvalidParam(napi_env env, napi_callback_info info)
317 {
318 napi_value result;
319 OH_UdmfRecord *record = OH_UdmfRecord_Create();
320 OH_UdsPlainText *plainText = OH_UdsPlainText_Create();
321 char content[] = "hello world";
322 OH_UdsPlainText_SetContent(plainText, content);
323 OH_UdmfRecord_AddPlainText(record, plainText);
324 OH_UdmfRecordProvider* provider = OH_UdmfRecordProvider_Create();
325
326 int num = 1;
327 void *context = #
328
329 OH_UdmfRecordProvider_SetData(provider, content, GetDataCallbackFunc, FinalizeFunc);
330
331 const char* types[3] = { "general.plain-text", "general.hyperlink", "general.html" };
332 int count = 3;
333 int ret = OH_UdmfRecord_SetProvider(nullptr, types, count, provider);
334
335 napi_create_int32(env, ret == Udmf_ErrCode::UDMF_E_INVALID_PARAM ? g_invalidParamCode : 1, &result);
336 OH_UdmfRecord_Destroy(record);
337 OH_UdsPlainText_Destroy(plainText);
338 OH_UdmfRecordProvider_Destroy(provider);
339 return result;
340 }
341
TestGetVisibilitySuccess(napi_env env,napi_callback_info info)342 napi_value TestGetVisibilitySuccess(napi_env env, napi_callback_info info)
343 {
344 napi_value result;
345
346 OH_UdmfOptions *pThis = OH_UdmfOptions_Create();
347 Udmf_Visibility visibility = UDMF_ALL;
348 OH_UdmfOptions_SetVisibility(pThis, visibility);
349
350 int ret = OH_UdmfOptions_GetVisibility(pThis);
351
352 napi_create_int32(env, ret == Udmf_ErrCode::UDMF_E_OK ? 0 : 1, &result);
353 OH_UdmfOptions_Destroy(pThis);
354 return result;
355 }
356
TestSetVisibilityInvalidParam(napi_env env,napi_callback_info info)357 napi_value TestSetVisibilityInvalidParam(napi_env env, napi_callback_info info)
358 {
359 napi_value result;
360 OH_UdmfOptions *pThis = nullptr;
361 Udmf_Visibility visibility = UDMF_ALL;
362 int ret = OH_UdmfOptions_SetVisibility(pThis, visibility);
363
364 napi_create_int32(env, ret == Udmf_ErrCode::UDMF_E_INVALID_PARAM ? g_invalidParamCode : 1, &result);
365 return result;
366 }
367
TestSetVisibilitySuccess(napi_env env,napi_callback_info info)368 napi_value TestSetVisibilitySuccess(napi_env env, napi_callback_info info)
369 {
370 napi_value result;
371 OH_UdmfOptions *pThis = OH_UdmfOptions_Create();
372 Udmf_Visibility visibility = UDMF_ALL;
373 int ret = OH_UdmfOptions_SetVisibility(pThis, visibility);
374
375 napi_create_int32(env, ret == Udmf_ErrCode::UDMF_E_OK ? 0 : 1, &result);
376 return result;
377 }
378
TestGetUnifiedDataError(napi_env env,napi_callback_info info)379 napi_value TestGetUnifiedDataError(napi_env env, napi_callback_info info)
380 {
381 napi_value result;
382 OH_UdmfData *udmfUnifiedData = OH_UdmfData_Create();
383 OH_UdmfRecord *record = OH_UdmfRecord_Create();
384 OH_UdsPlainText *plainText = OH_UdsPlainText_Create();
385 char content[] = "hello world";
386 OH_UdsPlainText_SetContent(plainText, content);
387 OH_UdmfRecord_AddPlainText(record, plainText);
388 OH_UdmfData_AddRecord(udmfUnifiedData, record);
389 Udmf_Intention intention = UDMF_INTENTION_DRAG;
390 char key[256];
391 unsigned int keyLen = 256;
392 OH_Udmf_SetUnifiedData(intention, udmfUnifiedData, key, keyLen);
393 OH_UdmfData *readUnifiedData = OH_UdmfData_Create();
394 int ret = OH_Udmf_GetUnifiedData(key, intention, readUnifiedData);
395
396 napi_create_int32(env, ret == Udmf_ErrCode::UDMF_ERR ? g_err : 1, &result);
397 OH_UdsPlainText_Destroy(plainText);
398 OH_UdmfRecord_Destroy(record);
399 OH_UdmfData_Destroy(udmfUnifiedData);
400 OH_UdmfData_Destroy(readUnifiedData);
401 return result;
402 }
403
TestGetUnifiedDataByOptionsError(napi_env env,napi_callback_info info)404 napi_value TestGetUnifiedDataByOptionsError(napi_env env, napi_callback_info info)
405 {
406 napi_value result;
407 std::string uri = "https://xxx/xx/xx.jpg";
408 OH_UdmfData *udmfUnifiedData = OH_UdmfData_Create();
409 OH_UdmfRecord *record = OH_UdmfRecord_Create();
410 OH_UdsFileUri *fileUri = OH_UdsFileUri_Create();
411
412 OH_UdsFileUri_SetFileUri(fileUri, uri.c_str());
413 OH_UdsFileUri_SetFileType(fileUri, UDMF_META_IMAGE);
414 OH_UdmfRecord_AddFileUri(record, fileUri);
415 OH_UdmfData_AddRecord(udmfUnifiedData, record);
416
417 OH_UdmfOptions* options = OH_UdmfOptions_Create();
418 Udmf_Intention testIntention = UDMF_INTENTION_PICKER;
419 OH_UdmfOptions_SetIntention(options, testIntention);
420 char key[256];
421 unsigned int keyLen = 256;
422 OH_Udmf_SetUnifiedDataByOptions(options, udmfUnifiedData, key, keyLen);
423
424 OH_UdmfOptions* options1 = OH_UdmfOptions_Create();
425 OH_UdmfOptions_SetIntention(options1, testIntention);
426 OH_UdmfOptions_SetKey(options1, key);
427
428 unsigned dataSize = 1;
429 OH_UdmfData* dataArray1 = nullptr;
430 int ret = OH_Udmf_GetUnifiedDataByOptions(options1, &dataArray1, &dataSize);
431
432 napi_create_int32(env, ret == Udmf_ErrCode::UDMF_ERR ? g_err : 1, &result);
433 OH_UdmfOptions_Destroy(options);
434 OH_UdmfOptions_Destroy(options1);
435 OH_UdsFileUri_Destroy(fileUri);
436 OH_UdmfRecord_Destroy(record);
437 OH_UdmfData_Destroy(udmfUnifiedData);
438 return result;
439 }
440
TestSetUnifiedDataError(napi_env env,napi_callback_info info)441 napi_value TestSetUnifiedDataError(napi_env env, napi_callback_info info)
442 {
443 napi_value result;
444 OH_UdmfData* udmfData = OH_UdmfData_Create();
445 OH_UdmfRecord* record = OH_UdmfRecord_Create();
446 OH_UdsFileUri* fileUri = OH_UdsFileUri_Create();
447
448 const char *restrictedUri = "/proc/sys/kernel/hostname";
449 OH_UdsFileUri_SetFileUri(fileUri, restrictedUri);
450 OH_UdsFileUri_SetFileType(fileUri, UDMF_META_HYPERLINK);
451
452 OH_UdmfRecord_AddFileUri(record, fileUri);
453
454 Udmf_Intention intention = UDMF_INTENTION_DRAG;
455 char key[513];
456
457 int ret = OH_Udmf_SetUnifiedData(intention, udmfData, key, 512);
458
459 napi_create_int32(env, ret == Udmf_ErrCode::UDMF_ERR ? g_err : 1, &result);
460
461 OH_UdmfRecord_Destroy(record);
462 OH_UdmfData_Destroy(udmfData);
463 OH_UdsFileUri_Destroy(fileUri);
464 return result;
465 }
466
TestUpdateUnifiedDataError(napi_env env,napi_callback_info info)467 napi_value TestUpdateUnifiedDataError(napi_env env, napi_callback_info info)
468 {
469 napi_value result;
470 std::string uri = "https://xxx/xx/xx.jpg";
471 OH_UdmfData *udmfUnifiedData = OH_UdmfData_Create();
472 OH_UdmfRecord *record = OH_UdmfRecord_Create();
473 OH_UdsFileUri *fileUri = OH_UdsFileUri_Create();
474
475 OH_UdsFileUri_SetFileUri(fileUri, uri.c_str());
476 OH_UdsFileUri_SetFileType(fileUri, UDMF_META_IMAGE);
477 OH_UdmfRecord_AddFileUri(record, fileUri);
478 OH_UdmfData_AddRecord(udmfUnifiedData, record);
479
480 OH_UdmfOptions* options = OH_UdmfOptions_Create();
481 Udmf_Intention testIntention = UDMF_INTENTION_DATA_HUB;
482 OH_UdmfOptions_SetIntention(options, testIntention);
483
484 char key[UDMF_KEY_BUFFER_LEN];
485 int setRes = OH_Udmf_SetUnifiedDataByOptions(options, udmfUnifiedData, key, UDMF_KEY_BUFFER_LEN);
486
487 std::string uri2 = "https://new/uri/path.jpg";
488 OH_UdmfData *udmfUnifiedData2 = OH_UdmfData_Create();
489 OH_UdmfRecord *record2 = OH_UdmfRecord_Create();
490 OH_UdsFileUri *fileUri2 = OH_UdsFileUri_Create();
491
492 OH_UdsFileUri_SetFileUri(fileUri2, uri2.c_str());
493 OH_UdsFileUri_SetFileType(fileUri2, UDMF_META_IMAGE);
494 OH_UdmfRecord_AddFileUri(record2, fileUri2);
495
496 OH_UdmfOptions* options2 = OH_UdmfOptions_Create();
497 OH_UdmfOptions_SetIntention(options2, testIntention);
498 OH_UdmfOptions_SetKey(options2, key); // 指定要更新的key
499
500 int ret = OH_Udmf_UpdateUnifiedData(options2, udmfUnifiedData2);
501
502 napi_create_int32(env, ret == Udmf_ErrCode::UDMF_ERR ? g_err : 1, &result);
503 OH_UdsFileUri_Destroy(fileUri);
504 OH_UdmfRecord_Destroy(record);
505 OH_UdmfData_Destroy(udmfUnifiedData);
506 OH_UdsFileUri_Destroy(fileUri2);
507 OH_UdmfRecord_Destroy(record2);
508 OH_UdmfData_Destroy(udmfUnifiedData2);
509 OH_UdmfOptions_Destroy(options);
510 OH_UdmfOptions_Destroy(options2);
511 return result;
512 }
513
TestDeleteUnifiedDataError(napi_env env,napi_callback_info info)514 napi_value TestDeleteUnifiedDataError(napi_env env, napi_callback_info info)
515 {
516 napi_value result;
517 std::string uri = "https://xxx/xx/xx.jpg";
518 OH_UdmfData *udmfUnifiedData = OH_UdmfData_Create();
519 OH_UdmfRecord *record = OH_UdmfRecord_Create();
520 OH_UdsFileUri *fileUri = OH_UdsFileUri_Create();
521
522 OH_UdsFileUri_SetFileUri(fileUri, uri.c_str());
523 OH_UdsFileUri_SetFileType(fileUri, UDMF_META_IMAGE);
524 OH_UdmfRecord_AddFileUri(record, fileUri);
525
526 OH_UdmfOptions* options = OH_UdmfOptions_Create();
527 Udmf_Intention testIntention = UDMF_INTENTION_PICKER;
528 OH_UdmfOptions_SetIntention(options, testIntention);
529 char key[UDMF_KEY_BUFFER_LEN];
530 OH_Udmf_SetUnifiedDataByOptions(options, udmfUnifiedData, key, UDMF_KEY_BUFFER_LEN);
531
532 OH_UdmfOptions* options1 = OH_UdmfOptions_Create();
533 OH_UdmfOptions_SetIntention(options1, testIntention);
534 OH_UdmfOptions_SetKey(options1, key);
535
536 unsigned dataSize = 1;
537 OH_UdmfData* dataArray1 = nullptr;
538 OH_Udmf_GetUnifiedDataByOptions(options1, &dataArray1, &dataSize);
539
540 unsigned int dataSize1 = 1;
541 OH_UdmfData* dataArray2 = nullptr;
542 int ret = OH_Udmf_DeleteUnifiedData(options1, &dataArray2, &dataSize1);
543
544 napi_create_int32(env, ret == Udmf_ErrCode::UDMF_ERR ? g_err : 1, &result);
545 OH_UdmfOptions_Destroy(options);
546 OH_UdmfOptions_Destroy(options1);
547 OH_UdsFileUri_Destroy(fileUri);
548 OH_UdmfRecord_Destroy(record);
549 OH_UdmfData_Destroy(udmfUnifiedData);
550 return result;
551 }
552
TestUdmfOptionsGetVisibilityProcess(napi_env env,napi_callback_info info)553 napi_value TestUdmfOptionsGetVisibilityProcess(napi_env env, napi_callback_info info)
554 {
555 OH_UdmfOptions* options = OH_UdmfOptions_Create();
556 OH_UdmfOptions_SetVisibility(options, UDMF_OWN_PROCESS);
557 Udmf_Visibility getVisibility = OH_UdmfOptions_GetVisibility(options);
558 napi_value result;
559 if (getVisibility == UDMF_OWN_PROCESS) {
560 napi_create_int32(env, 0, &result);
561 return result;
562 }
563 napi_create_int32(env, -1, &result);
564 return result;
565 }
566
TestUdmfOptionsGetVisibilityAll(napi_env env,napi_callback_info info)567 napi_value TestUdmfOptionsGetVisibilityAll(napi_env env, napi_callback_info info)
568 {
569 OH_UdmfOptions* options = OH_UdmfOptions_Create();
570 OH_UdmfOptions_SetVisibility(options, UDMF_ALL);
571 Udmf_Visibility getVisibility = OH_UdmfOptions_GetVisibility(options);
572 napi_value result;
573 if (getVisibility == UDMF_ALL) {
574 napi_create_int32(env, 0, &result);
575 return result;
576 }
577 napi_create_int32(env, -1, &result);
578 return result;
579 }