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 #define LOG_TAG "PasteboardCapiTest"
17
18 #include <gtest/gtest.h>
19 #include <unistd.h>
20
21 #include "token_setproc.h"
22 #include "accesstoken_kit.h"
23 #include "nativetoken_kit.h"
24
25 #include "oh_pasteboard.h"
26 #include "oh_pasteboard_err_code.h"
27 #include "oh_pasteboard_observer_impl.h"
28 #include "udmf.h"
29 #include "uds.h"
30 #include "pasteboard_hilog.h"
31 #include "os_account_manager.h"
32
33 using namespace testing::ext;
34 using namespace OHOS::Security::AccessToken;
35 using namespace OHOS::MiscServices;
36
37 namespace OHOS::Test {
38 class PasteboardCapiTest : public testing::Test {
39 public:
40 static void SetUpTestCase(void);
41 static void TearDownTestCase(void);
42 void SetUp();
43 void TearDown();
44 static void CallbackFunc(void* context, Pasteboard_NotifyType type);
45 static void RemoveCallbackSideEffects();
46 static void ContextFinalizeFunc(void* context);
47 static int callbackValue;
48 static void AllocTestTokenId();
49 static void DeleteTestTokenId();
50 static void SetTestTokenId();
51 static void RestoreSelfTokenId();
52 static void* GetDataCallback(void* context, const char* type);
53 static constexpr int INIT_VALUE = 0;
54 static constexpr int UPDATE_VALUE = 1;
55 static uint64_t selfTokenId_;
56 static AccessTokenID testTokenId_;
57 static constexpr char PLAINTEXT_CONTENT[] = "PLAINTEXT_CONTENT";
58 static constexpr char HYPERLINK_URL[] = "file://data/image.png";
59 };
60 uint64_t PasteboardCapiTest::selfTokenId_ = 0;
61 AccessTokenID PasteboardCapiTest::testTokenId_ = 0;
62 int PasteboardCapiTest::callbackValue = 0;
63
SetUpTestCase(void)64 void PasteboardCapiTest::SetUpTestCase(void)
65 {
66 callbackValue = INIT_VALUE;
67 selfTokenId_ = GetSelfTokenID();
68 AllocTestTokenId();
69 }
70
TearDownTestCase(void)71 void PasteboardCapiTest::TearDownTestCase(void)
72 {
73 RemoveCallbackSideEffects();
74 DeleteTestTokenId();
75 }
76
SetUp(void)77 void PasteboardCapiTest::SetUp(void)
78 {
79 }
80
TearDown(void)81 void PasteboardCapiTest::TearDown(void)
82 {
83 }
84
AllocTestTokenId()85 void PasteboardCapiTest::AllocTestTokenId()
86 {
87 std::vector<int32_t> ids;
88 auto ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
89 if (ret != ERR_OK || ids.empty()) {
90 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "query active user failed errCode = %{public}d", ret);
91 return;
92 }
93 HapInfoParams infoParams = {
94 .userID = ids[0],
95 .bundleName = "ohos.privacy_test.pasteboard",
96 .instIndex = 0,
97 .appIDDesc = "privacy_test.pasteboard"
98 };
99 PermissionStateFull testState = {
100 .permissionName = "ohos.permission.DUMP",
101 .isGeneral = true,
102 .resDeviceID = { "local" },
103 .grantStatus = { PermissionState::PERMISSION_GRANTED },
104 .grantFlags = { 1 }
105 };
106 HapPolicyParams policyParams = {
107 .apl = APL_NORMAL,
108 .domain = "test.domain.pasteboard",
109 .permList = {},
110 .permStateList = { testState }
111 };
112
113 AccessTokenKit::AllocHapToken(infoParams, policyParams);
114 testTokenId_ = Security::AccessToken::AccessTokenKit::GetHapTokenID(
115 infoParams.userID, infoParams.bundleName, infoParams.instIndex);
116 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "userID = %{public}d, testTokenId = 0x%{public}x.", infoParams.userID,
117 testTokenId_);
118 }
119
DeleteTestTokenId()120 void PasteboardCapiTest::DeleteTestTokenId()
121 {
122 AccessTokenKit::DeleteToken(testTokenId_);
123 }
124
125
SetTestTokenId()126 void PasteboardCapiTest::SetTestTokenId()
127 {
128 auto ret = SetSelfTokenID(testTokenId_);
129 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "testTokenId = 0x%{public}x, ret = %{public}d!", testTokenId_, ret);
130 }
131
RestoreSelfTokenId()132 void PasteboardCapiTest::RestoreSelfTokenId()
133 {
134 auto ret = SetSelfTokenID(selfTokenId_);
135 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "ret = %{public}d!", ret);
136 }
137
CallbackFunc(void* context, Pasteboard_NotifyType type)138 void PasteboardCapiTest::CallbackFunc(void* context, Pasteboard_NotifyType type)
139 {
140 callbackValue = UPDATE_VALUE;
141 }
142
RemoveCallbackSideEffects()143 void PasteboardCapiTest::RemoveCallbackSideEffects()
144 {
145 callbackValue = INIT_VALUE;
146 }
147
ContextFinalizeFunc(void* context)148 void PasteboardCapiTest::ContextFinalizeFunc(void* context) {}
149
GetDataCallback(void* context, const char* type)150 void* PasteboardCapiTest::GetDataCallback(void* context, const char* type)
151 {
152 if (std::string(type) == "general.plain-text") {
153 OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
154 OH_UdsPlainText_SetContent(plainText, PLAINTEXT_CONTENT);
155 return plainText;
156 } else if (std::string(type) == "general.hyperlink") {
157 OH_UdsHyperlink* link = OH_UdsHyperlink_Create();
158 OH_UdsHyperlink_SetUrl(link, HYPERLINK_URL);
159 return link;
160 }
161 return nullptr;
162 }
163
164 /**
165 * @tc.name: OH_PasteboardSubscriber_Create001
166 * @tc.desc: OH_PasteboardObserver_Create test
167 * @tc.type: FUNC
168 * @tc.require: AROOOH5R5G
169 */
HWTEST_F(PasteboardCapiTest, OH_PasteboardSubscriber_Create001, TestSize.Level1)170 HWTEST_F(PasteboardCapiTest, OH_PasteboardSubscriber_Create001, TestSize.Level1)
171 {
172 OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
173 EXPECT_NE(observer, nullptr);
174
175 OH_PasteboardObserver_Destroy(observer);
176 OH_PasteboardObserver_Destroy(nullptr);
177 }
178
179 /**
180 * @tc.name: OH_PasteboardObserver_SetData001
181 * @tc.desc: OH_PasteboardObserver_SetData test valid
182 * @tc.type: FUNC
183 * @tc.require: AROOOH5R5G
184 */
HWTEST_F(PasteboardCapiTest, OH_PasteboardObserver_SetData001, TestSize.Level1)185 HWTEST_F(PasteboardCapiTest, OH_PasteboardObserver_SetData001, TestSize.Level1)
186 {
187 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
188 void* context = static_cast<void*>(pasteboard);
189 OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
190
191 int setRes1 = OH_PasteboardObserver_SetData(observer, context, CallbackFunc, ContextFinalizeFunc);
192 EXPECT_EQ(setRes1, ERR_OK);
193
194 OH_PasteboardObserver_Destroy(observer);
195 OH_Pasteboard_Destroy(pasteboard);
196 }
197
198 /**
199 * @tc.name: OH_PasteboardObserver_SetData002
200 * @tc.desc: OH_PasteboardObserver_SetData test invalid
201 * @tc.type: FUNC
202 * @tc.require: AROOOH5R5G
203 */
HWTEST_F(PasteboardCapiTest, OH_PasteboardObserver_SetData002, TestSize.Level1)204 HWTEST_F(PasteboardCapiTest, OH_PasteboardObserver_SetData002, TestSize.Level1)
205 {
206 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
207 void* context = static_cast<void*>(pasteboard);
208 OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
209
210 int setRes1 = OH_PasteboardObserver_SetData(nullptr, context, CallbackFunc, ContextFinalizeFunc);
211 EXPECT_EQ(setRes1, ERR_INVALID_PARAMETER);
212
213 int setRes2 = OH_PasteboardObserver_SetData(observer, context, nullptr, ContextFinalizeFunc);
214 EXPECT_EQ(setRes2, ERR_INVALID_PARAMETER);
215
216 int setRes3 = OH_PasteboardObserver_SetData(observer, context, CallbackFunc, nullptr);
217 EXPECT_EQ(setRes3, ERR_INVALID_PARAMETER);
218
219 OH_PasteboardObserver_Destroy(observer);
220 OH_Pasteboard_Destroy(pasteboard);
221 }
222
223 /**
224 * @tc.name: OH_Pasteboard_Create001
225 * @tc.desc: OH_Pasteboard_Create test valid
226 * @tc.type: FUNC
227 * @tc.require: AROOOH5R5G
228 */
HWTEST_F(PasteboardCapiTest, OH_Pasteboard_Create001, TestSize.Level1)229 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_Create001, TestSize.Level1)
230 {
231 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
232 EXPECT_NE(pasteboard, nullptr);
233
234 OH_Pasteboard_Destroy(pasteboard);
235 }
236
237 /**
238 * @tc.name: OH_Pasteboard_Subscribe001
239 * @tc.desc: OH_Pasteboard_Subscribe test valid
240 * @tc.type: FUNC
241 * @tc.require: AROOOH5R5G
242 */
HWTEST_F(PasteboardCapiTest, OH_Pasteboard_Subscribe001, TestSize.Level1)243 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_Subscribe001, TestSize.Level1)
244 {
245 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
246 OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
247 OH_PasteboardObserver_SetData(observer, nullptr, CallbackFunc, ContextFinalizeFunc);
248
249 int res = OH_Pasteboard_Subscribe(pasteboard, NOTIFY_LOCAL_DATA_CHANGE, observer);
250 EXPECT_EQ(res, ERR_OK);
251
252 int resRepeat = OH_Pasteboard_Subscribe(pasteboard, NOTIFY_LOCAL_DATA_CHANGE, observer);
253 EXPECT_EQ(resRepeat, ERR_OK);
254
255 OH_Pasteboard_Destroy(pasteboard);
256 OH_PasteboardObserver_Destroy(observer);
257 }
258
259 /**
260 * @tc.name: OH_Pasteboard_Subscribe002
261 * @tc.desc: OH_Pasteboard_Subscribe test invalid
262 * @tc.type: FUNC
263 * @tc.require: AROOOH5R5G
264 */
HWTEST_F(PasteboardCapiTest, OH_Pasteboard_Subscribe002, TestSize.Level1)265 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_Subscribe002, TestSize.Level1)
266 {
267 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
268 OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
269 OH_PasteboardObserver_SetData(observer, nullptr, CallbackFunc, ContextFinalizeFunc);
270
271 int res1 = OH_Pasteboard_Subscribe(nullptr, NOTIFY_LOCAL_DATA_CHANGE, observer);
272 EXPECT_EQ(res1, ERR_INVALID_PARAMETER);
273
274 int res2 = OH_Pasteboard_Subscribe(pasteboard, 10, observer);
275 EXPECT_EQ(res2, ERR_INVALID_PARAMETER);
276
277 int res3 = OH_Pasteboard_Subscribe(pasteboard, -1, observer);
278 EXPECT_EQ(res3, ERR_INVALID_PARAMETER);
279
280 int res4 = OH_Pasteboard_Subscribe(pasteboard, NOTIFY_LOCAL_DATA_CHANGE, nullptr);
281 EXPECT_EQ(res4, ERR_INVALID_PARAMETER);
282
283 OH_Pasteboard_Destroy(pasteboard);
284 OH_PasteboardObserver_Destroy(observer);
285 }
286
287 /**
288 * @tc.name: OH_Pasteboard_Unsubcribe001
289 * @tc.desc: OH_Pasteboard_Unsubcribe test valid
290 * @tc.type: FUNC
291 * @tc.require: AROOOH5R5G
292 */
HWTEST_F(PasteboardCapiTest, OH_Pasteboard_Unsubcribe001, TestSize.Level1)293 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_Unsubcribe001, TestSize.Level1)
294 {
295 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
296 OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
297 OH_PasteboardObserver_SetData(observer, nullptr, CallbackFunc, ContextFinalizeFunc);
298
299 OH_Pasteboard_Subscribe(pasteboard, NOTIFY_LOCAL_DATA_CHANGE, observer);
300
301 int res = OH_Pasteboard_Unsubscribe(pasteboard, NOTIFY_LOCAL_DATA_CHANGE, observer);
302 EXPECT_EQ(res, ERR_OK);
303
304 OH_Pasteboard_Destroy(pasteboard);
305 OH_PasteboardObserver_Destroy(observer);
306 }
307
308 /**
309 * @tc.name: OH_Pasteboard_Unsubscribe002
310 * @tc.desc: OH_Pasteboard_Unsubscribe test invalid
311 * @tc.type: FUNC
312 * @tc.require: AROOOH5R5G
313 */
HWTEST_F(PasteboardCapiTest, OH_Pasteboard_Unsubcribe002, TestSize.Level1)314 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_Unsubcribe002, TestSize.Level1)
315 {
316 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
317 OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
318 OH_PasteboardObserver_SetData(observer, nullptr, CallbackFunc, ContextFinalizeFunc);
319
320 int res1 = OH_Pasteboard_Unsubscribe(nullptr, NOTIFY_LOCAL_DATA_CHANGE, observer);
321 EXPECT_EQ(res1, ERR_INVALID_PARAMETER);
322
323 int res2 = OH_Pasteboard_Unsubscribe(pasteboard, 10, observer);
324 EXPECT_EQ(res2, ERR_INVALID_PARAMETER);
325
326 int res3 = OH_Pasteboard_Unsubscribe(pasteboard, -1, observer);
327 EXPECT_EQ(res3, ERR_INVALID_PARAMETER);
328
329 int res4 = OH_Pasteboard_Unsubscribe(pasteboard, NOTIFY_LOCAL_DATA_CHANGE, nullptr);
330 EXPECT_EQ(res4, ERR_INVALID_PARAMETER);
331
332 OH_Pasteboard_Destroy(pasteboard);
333 OH_PasteboardObserver_Destroy(observer);
334 }
335
336 /**
337 * @tc.name: OH_Pasteboard_IsRemoteData001
338 * @tc.desc: OH_Pasteboard_IsRemoteData test valid
339 * @tc.type: FUNC
340 * @tc.require: AROOOH5R5G
341 */
HWTEST_F(PasteboardCapiTest, OH_Pasteboard_IsRemoteData001, TestSize.Level1)342 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_IsRemoteData001, TestSize.Level1)
343 {
344 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
345
346 bool res = OH_Pasteboard_IsRemoteData(pasteboard);
347 EXPECT_FALSE(res);
348
349 OH_Pasteboard_Destroy(pasteboard);
350 }
351
352 /**
353 * @tc.name: OH_Pasteboard_GetDataSrouce001
354 * @tc.desc: OH_Pasteboard_GetDataSrouce test valid
355 * @tc.type: FUNC
356 * @tc.require: AROOOH5R5G
357 */
HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetDataSrouce001, TestSize.Level1)358 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetDataSrouce001, TestSize.Level1)
359 {
360 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
361 OH_UdmfData* setData = OH_UdmfData_Create();
362 OH_UdmfRecord* record = OH_UdmfRecord_Create();
363 OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
364 char content[] = "hello world";
365 OH_UdsPlainText_SetContent(plainText, content);
366 OH_UdmfRecord_AddPlainText(record, plainText);
367 OH_UdmfData_AddRecord(setData, record);
368
369 OH_Pasteboard_SetData(pasteboard, setData);
370
371 int len = 100;
372 char source[100];
373 int res = OH_Pasteboard_GetDataSource(pasteboard, source, len);
374 EXPECT_EQ(res, ERR_OK);
375
376 OH_Pasteboard_Destroy(pasteboard);
377 }
378
379 /**
380 * @tc.name: OH_Pasteboard_HasType001
381 * @tc.desc: OH_Pasteboard_HasType test valid
382 * @tc.type: FUNC
383 * @tc.require: AROOOH5R5G
384 */
HWTEST_F(PasteboardCapiTest, OH_Pasteboard_HasType001, TestSize.Level1)385 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_HasType001, TestSize.Level1)
386 {
387 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
388 OH_UdmfData* setData = OH_UdmfData_Create();
389 OH_UdmfRecord* record = OH_UdmfRecord_Create();
390 OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
391 char content[] = "hello world";
392 OH_UdsPlainText_SetContent(plainText, content);
393 OH_UdmfRecord_AddPlainText(record, plainText);
394 OH_UdmfData_AddRecord(setData, record);
395
396 OH_Pasteboard_SetData(pasteboard, setData);
397 char type[] = "general.plain-text";
398 bool res = OH_Pasteboard_HasType(pasteboard, type);
399 EXPECT_FALSE(res);
400
401 OH_Pasteboard_Destroy(pasteboard);
402 }
403
404 /**
405 * @tc.name: OH_Pasteboard_HasData001
406 * @tc.desc: OH_Pasteboard_HasData test valid
407 * @tc.type: FUNC
408 * @tc.require: AROOOH5R5G
409 */
HWTEST_F(PasteboardCapiTest, OH_Pasteboard_HasData001, TestSize.Level1)410 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_HasData001, TestSize.Level1)
411 {
412 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
413 OH_Pasteboard_ClearData(pasteboard);
414 bool res = OH_Pasteboard_HasData(pasteboard);
415 EXPECT_FALSE(res);
416
417 OH_UdmfData* setData = OH_UdmfData_Create();
418 OH_UdmfRecord* record = OH_UdmfRecord_Create();
419 OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
420 char content[] = "hello world";
421 OH_UdsPlainText_SetContent(plainText, content);
422 OH_UdmfRecord_AddPlainText(record, plainText);
423 OH_UdmfData_AddRecord(setData, record);
424
425 OH_Pasteboard_SetData(pasteboard, setData);
426
427 res = OH_Pasteboard_HasData(pasteboard);
428 EXPECT_TRUE(res);
429
430 OH_Pasteboard_Destroy(pasteboard);
431 }
432
433 /**
434 * @tc.name: OH_Pasteboard_ClearData001
435 * @tc.desc: OH_Pasteboard_ClearData test valid
436 * @tc.type: FUNC
437 * @tc.require: AROOOH5R5G
438 */
HWTEST_F(PasteboardCapiTest, OH_Pasteboard_ClearData001, TestSize.Level1)439 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_ClearData001, TestSize.Level1)
440 {
441 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
442 bool res = OH_Pasteboard_ClearData(pasteboard);
443 EXPECT_EQ(res, ERR_OK);
444
445 OH_Pasteboard_Destroy(pasteboard);
446 }
447
448 /**
449 * @tc.name: OH_Pasteboard_SetData001
450 * @tc.desc: OH_Pasteboard_SetData test valid
451 * @tc.type: FUNC
452 * @tc.require: AROOOH5R5G
453 */
HWTEST_F(PasteboardCapiTest, OH_Pasteboard_SetData001, TestSize.Level1)454 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_SetData001, TestSize.Level1)
455 {
456 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
457 OH_UdmfData *setData = OH_UdmfData_Create();
458 OH_UdmfRecord *record = OH_UdmfRecord_Create();
459 OH_UdsPlainText *plainText = OH_UdsPlainText_Create();
460 char content[] = "hello world";
461 OH_UdsPlainText_SetContent(plainText, content);
462 OH_UdmfRecord_AddPlainText(record, plainText);
463 OH_UdmfData_AddRecord(setData, record);
464
465 bool res = OH_Pasteboard_SetData(pasteboard, setData);
466 EXPECT_EQ(res, ERR_OK);
467
468 OH_Pasteboard_Destroy(pasteboard);
469 OH_UdsPlainText_Destroy(plainText);
470 OH_UdmfRecord_Destroy(record);
471 OH_UdmfData_Destroy(setData);
472 }
473
474 /**
475 * @tc.name: OH_Pasteboard_GetData001
476 * @tc.desc: OH_Pasteboard_GetData test valid
477 * @tc.type: FUNC
478 * @tc.require: AROOOH5R5G
479 */
HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetData001, TestSize.Level1)480 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetData001, TestSize.Level1)
481 {
482 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
483 OH_UdmfData* setData = OH_UdmfData_Create();
484 OH_UdmfRecord* record = OH_UdmfRecord_Create();
485 OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
486 char content[] = "hello world";
487 OH_UdsPlainText_SetContent(plainText, content);
488 OH_UdmfRecord_AddPlainText(record, plainText);
489 OH_UdmfData_AddRecord(setData, record);
490
491 int res = OH_Pasteboard_SetData(pasteboard, setData);
492 EXPECT_EQ(res, ERR_OK);
493
494 int status = -1;
495 OH_UdmfData* getData = OH_Pasteboard_GetData(pasteboard, &status);
496 EXPECT_EQ(status, ERR_OK);
497 EXPECT_NE(getData, nullptr);
498
499 unsigned int count = 0;
500 OH_UdmfRecord **getRecords = OH_UdmfData_GetRecords(getData, &count);
501 EXPECT_EQ(count, 1);
502 OH_UdsPlainText *getPlainText = OH_UdsPlainText_Create();
503 OH_UdmfRecord_GetPlainText(getRecords[0], getPlainText);
504 const char *getContent = OH_UdsPlainText_GetContent(getPlainText);
505 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "content is %{public}s", getContent);
506 EXPECT_EQ(strcmp(getContent, content), 0);
507
508 OH_Pasteboard_Destroy(pasteboard);
509 OH_UdsPlainText_Destroy(plainText);
510 OH_UdsPlainText_Destroy(getPlainText);
511 OH_UdmfRecord_Destroy(record);
512 OH_UdmfData_Destroy(setData);
513 OH_UdmfData_Destroy(getData);
514 }
515
516 /**
517 * @tc.name: OH_Pasteboard_GetData002
518 * @tc.desc: OH_Pasteboard_GetData test valid
519 * @tc.type: FUNC
520 * @tc.require: AROOOH5R5G
521 */
HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetData002, TestSize.Level1)522 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetData002, TestSize.Level1)
523 {
524 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
525 OH_UdmfData* setData = OH_UdmfData_Create();
526 OH_UdmfRecord* record = OH_UdmfRecord_Create();
527 OH_UdmfRecordProvider* provider = OH_UdmfRecordProvider_Create();
528 EXPECT_NE(provider, nullptr);
529 OH_UdmfRecordProvider_SetData(provider, static_cast<void *>(record), GetDataCallback, ContextFinalizeFunc);
530 OH_UdmfData_AddRecord(setData, record);
531
532 const char* types[3] = { "general.plain-text", "general.hyperlink", "general.html" };
533 OH_UdmfRecord_SetProvider(record, types, 3, provider);
534 int res = OH_Pasteboard_SetData(pasteboard, setData);
535 EXPECT_EQ(res, ERR_OK);
536
537 int status = -1;
538 OH_UdmfData* getData = OH_Pasteboard_GetData(pasteboard, &status);
539 EXPECT_EQ(status, ERR_OK);
540 EXPECT_NE(getData, nullptr);
541
542 unsigned int count = 0;
543 OH_UdmfRecord **getRecords = OH_UdmfData_GetRecords(getData, &count);
544 EXPECT_EQ(count, 1);
545 OH_UdsPlainText *getPlainText = OH_UdsPlainText_Create();
546 OH_UdmfRecord_GetPlainText(getRecords[0], getPlainText);
547 const char *getContent = OH_UdsPlainText_GetContent(getPlainText);
548 EXPECT_EQ(strcmp(getContent, PLAINTEXT_CONTENT), 0);
549
550 OH_UdsHyperlink *getHyperLink = OH_UdsHyperlink_Create();
551 OH_UdmfRecord_GetHyperlink(getRecords[0], getHyperLink);
552 const char *getUrl = OH_UdsHyperlink_GetUrl(getHyperLink);
553 EXPECT_EQ(strcmp(getUrl, HYPERLINK_URL), 0);
554 OH_Pasteboard_Destroy(pasteboard);
555 OH_UdsPlainText_Destroy(getPlainText);
556 OH_UdsHyperlink_Destroy(getHyperLink);
557 OH_UdmfRecord_Destroy(record);
558 OH_UdmfData_Destroy(setData);
559 OH_UdmfData_Destroy(getData);
560 }
561
562 /**
563 * @tc.name: OH_Pasteboard_GetData003
564 * @tc.desc: OH_Pasteboard_GetData test valid
565 * @tc.type: FUNC
566 * @tc.require: AROOOH5R5G
567 */
HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetData003, TestSize.Level1)568 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetData003, TestSize.Level1)
569 {
570 char typeId[] = "ApplicationDefined-myType";
571 unsigned char entry[] = "CreateGeneralRecord1";
572 unsigned int count = sizeof(entry);
573 OH_UdmfRecord *record = OH_UdmfRecord_Create();
574 int addRes1 = OH_UdmfRecord_AddGeneralEntry(record, typeId, entry, count);
575 EXPECT_EQ(addRes1, ERR_OK);
576
577 OH_UdmfData* setData = OH_UdmfData_Create();
578 OH_UdmfData_AddRecord(setData, record);
579 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
580 int res = OH_Pasteboard_SetData(pasteboard, setData);
581 EXPECT_EQ(res, ERR_OK);
582
583 int status = -1;
584 OH_UdmfData* getData = OH_Pasteboard_GetData(pasteboard, &status);
585 EXPECT_EQ(status, ERR_OK);
586 EXPECT_NE(getData, nullptr);
587
588 unsigned int getrecordCount = 0;
589 OH_UdmfRecord **getRecords = OH_UdmfData_GetRecords(getData, &getrecordCount);
590 EXPECT_EQ(getrecordCount, 1);
591
592 unsigned int getCount = 0;
593 unsigned char *getEntry;
594 int getRes = OH_UdmfRecord_GetGeneralEntry(getRecords[0], typeId, &getEntry, &getCount);
595 EXPECT_EQ(getRes, ERR_OK);
596 EXPECT_EQ(getCount, count);
597 EXPECT_EQ(memcmp(entry, getEntry, getCount), 0);
598
599 OH_Pasteboard_Destroy(pasteboard);
600 OH_UdmfRecord_Destroy(record);
601 OH_UdmfData_Destroy(setData);
602 OH_UdmfData_Destroy(getData);
603 }
604 }