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 "pasteboard_client.h"
32 #include "os_account_manager.h"
33
34 using namespace testing::ext;
35 using namespace OHOS::Security::AccessToken;
36 using namespace OHOS::MiscServices;
37
38 namespace OHOS::Test {
39 class PasteboardCapiTest : public testing::Test {
40 public:
41 static void SetUpTestCase(void);
42 static void TearDownTestCase(void);
43 void SetUp();
44 void TearDown();
45 static void CallbackFunc(void* context, Pasteboard_NotifyType type);
46 static void RemoveCallbackSideEffects();
47 static void ContextFinalizeFunc(void* context);
48 static int callbackValue;
49 static void AllocTestTokenId();
50 static void DeleteTestTokenId();
51 static void SetTestTokenId();
52 static void RestoreSelfTokenId();
53 static void* GetDataCallback(void* context, const char* type);
54 static constexpr int INIT_VALUE = 0;
55 static constexpr int UPDATE_VALUE = 1;
56 static uint64_t selfTokenId_;
57 static AccessTokenID testTokenId_;
58 static constexpr char PLAINTEXT_CONTENT[] = "PLAINTEXT_CONTENT";
59 static constexpr char HYPERLINK_URL[] = "file://data/image.png";
60 static constexpr char HTML_URL[] = "file://data/image.png";
61 static constexpr char HTML_TEXT[] = "<P>html text</P>";
62 };
63 uint64_t PasteboardCapiTest::selfTokenId_ = 0;
64 AccessTokenID PasteboardCapiTest::testTokenId_ = 0;
65 int PasteboardCapiTest::callbackValue = 0;
66
SetUpTestCase(void)67 void PasteboardCapiTest::SetUpTestCase(void)
68 {
69 callbackValue = INIT_VALUE;
70 selfTokenId_ = GetSelfTokenID();
71 AllocTestTokenId();
72 }
73
TearDownTestCase(void)74 void PasteboardCapiTest::TearDownTestCase(void)
75 {
76 RemoveCallbackSideEffects();
77 DeleteTestTokenId();
78 }
79
SetUp(void)80 void PasteboardCapiTest::SetUp(void)
81 {
82 }
83
TearDown(void)84 void PasteboardCapiTest::TearDown(void)
85 {
86 }
87
AllocTestTokenId()88 void PasteboardCapiTest::AllocTestTokenId()
89 {
90 std::vector<int32_t> ids;
91 auto ret = AccountSA::OsAccountManager::QueryActiveOsAccountIds(ids);
92 if (ret != ERR_OK || ids.empty()) {
93 PASTEBOARD_HILOGE(PASTEBOARD_MODULE_SERVICE, "query active user failed errCode = %{public}d", ret);
94 return;
95 }
96 HapInfoParams infoParams = {
97 .userID = ids[0],
98 .bundleName = "ohos.privacy_test.pasteboard",
99 .instIndex = 0,
100 .appIDDesc = "privacy_test.pasteboard"
101 };
102 PermissionStateFull testState = {
103 .permissionName = "ohos.permission.DUMP",
104 .isGeneral = true,
105 .resDeviceID = { "local" },
106 .grantStatus = { PermissionState::PERMISSION_GRANTED },
107 .grantFlags = { 1 }
108 };
109 HapPolicyParams policyParams = {
110 .apl = APL_NORMAL,
111 .domain = "test.domain.pasteboard",
112 .permList = {},
113 .permStateList = { testState }
114 };
115
116 AccessTokenKit::AllocHapToken(infoParams, policyParams);
117 testTokenId_ = Security::AccessToken::AccessTokenKit::GetHapTokenID(
118 infoParams.userID, infoParams.bundleName, infoParams.instIndex);
119 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "userID = %{public}d, testTokenId = 0x%{public}x.", infoParams.userID,
120 testTokenId_);
121 }
122
DeleteTestTokenId()123 void PasteboardCapiTest::DeleteTestTokenId()
124 {
125 AccessTokenKit::DeleteToken(testTokenId_);
126 }
127
128
SetTestTokenId()129 void PasteboardCapiTest::SetTestTokenId()
130 {
131 auto ret = SetSelfTokenID(testTokenId_);
132 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "testTokenId = 0x%{public}x, ret = %{public}d!", testTokenId_, ret);
133 }
134
RestoreSelfTokenId()135 void PasteboardCapiTest::RestoreSelfTokenId()
136 {
137 auto ret = SetSelfTokenID(selfTokenId_);
138 PASTEBOARD_HILOGD(PASTEBOARD_MODULE_SERVICE, "ret = %{public}d!", ret);
139 }
140
CallbackFunc(void * context,Pasteboard_NotifyType type)141 void PasteboardCapiTest::CallbackFunc(void* context, Pasteboard_NotifyType type)
142 {
143 callbackValue = UPDATE_VALUE;
144 }
145
RemoveCallbackSideEffects()146 void PasteboardCapiTest::RemoveCallbackSideEffects()
147 {
148 callbackValue = INIT_VALUE;
149 }
150
ContextFinalizeFunc(void * context)151 void PasteboardCapiTest::ContextFinalizeFunc(void* context) {}
152
GetDataCallback(void * context,const char * type)153 void* PasteboardCapiTest::GetDataCallback(void* context, const char* type)
154 {
155 if (std::string(type) == "general.plain-text") {
156 OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
157 OH_UdsPlainText_SetContent(plainText, PLAINTEXT_CONTENT);
158 return plainText;
159 } else if (std::string(type) == "general.hyperlink") {
160 OH_UdsHyperlink* link = OH_UdsHyperlink_Create();
161 OH_UdsHyperlink_SetUrl(link, HYPERLINK_URL);
162 return link;
163 } else if (std::string(type) == "general.html") {
164 OH_UdsHtml* html = OH_UdsHtml_Create();
165 OH_UdsHtml_SetContent(html, HTML_URL);
166 return html;
167 }
168 return nullptr;
169 }
170
171 /**
172 * @tc.name: OH_PasteboardSubscriber_Create001
173 * @tc.desc: OH_PasteboardObserver_Create test
174 * @tc.type: FUNC
175 * @tc.require: AROOOH5R5G
176 */
177 HWTEST_F(PasteboardCapiTest, OH_PasteboardSubscriber_Create001, TestSize.Level1)
178 {
179 OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
180 EXPECT_NE(observer, nullptr);
181
182 OH_PasteboardObserver_Destroy(observer);
183 OH_PasteboardObserver_Destroy(nullptr);
184 }
185
186 /**
187 * @tc.name: OH_PasteboardObserver_SetData001
188 * @tc.desc: OH_PasteboardObserver_SetData test valid
189 * @tc.type: FUNC
190 * @tc.require: AROOOH5R5G
191 */
192 HWTEST_F(PasteboardCapiTest, OH_PasteboardObserver_SetData001, TestSize.Level1)
193 {
194 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
195 void* context = static_cast<void*>(pasteboard);
196 OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
197
198 int setRes1 = OH_PasteboardObserver_SetData(observer, context, CallbackFunc, ContextFinalizeFunc);
199 EXPECT_EQ(setRes1, ERR_OK);
200
201 OH_PasteboardObserver_Destroy(observer);
202 OH_Pasteboard_Destroy(pasteboard);
203 }
204
205 /**
206 * @tc.name: OH_PasteboardObserver_SetData002
207 * @tc.desc: OH_PasteboardObserver_SetData test invalid
208 * @tc.type: FUNC
209 * @tc.require: AROOOH5R5G
210 */
211 HWTEST_F(PasteboardCapiTest, OH_PasteboardObserver_SetData002, TestSize.Level1)
212 {
213 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
214 void* context = static_cast<void*>(pasteboard);
215 OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
216
217 int setRes1 = OH_PasteboardObserver_SetData(nullptr, context, CallbackFunc, ContextFinalizeFunc);
218 EXPECT_EQ(setRes1, ERR_INVALID_PARAMETER);
219
220 int setRes2 = OH_PasteboardObserver_SetData(observer, context, nullptr, ContextFinalizeFunc);
221 EXPECT_EQ(setRes2, ERR_INVALID_PARAMETER);
222
223 int setRes3 = OH_PasteboardObserver_SetData(observer, context, CallbackFunc, nullptr);
224 EXPECT_EQ(setRes3, ERR_INVALID_PARAMETER);
225
226 OH_PasteboardObserver_Destroy(observer);
227 OH_Pasteboard_Destroy(pasteboard);
228 }
229
230 /**
231 * @tc.name: OH_Pasteboard_Create001
232 * @tc.desc: OH_Pasteboard_Create test valid
233 * @tc.type: FUNC
234 * @tc.require: AROOOH5R5G
235 */
236 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_Create001, TestSize.Level1)
237 {
238 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
239 EXPECT_NE(pasteboard, nullptr);
240
241 OH_Pasteboard_Destroy(pasteboard);
242 }
243
244 /**
245 * @tc.name: OH_Pasteboard_Subscribe001
246 * @tc.desc: OH_Pasteboard_Subscribe test valid
247 * @tc.type: FUNC
248 * @tc.require: AROOOH5R5G
249 */
250 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_Subscribe001, TestSize.Level1)
251 {
252 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
253 OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
254 OH_PasteboardObserver_SetData(observer, nullptr, CallbackFunc, ContextFinalizeFunc);
255
256 int res = OH_Pasteboard_Subscribe(pasteboard, NOTIFY_LOCAL_DATA_CHANGE, observer);
257 EXPECT_EQ(res, ERR_OK);
258
259 int resRepeat = OH_Pasteboard_Subscribe(pasteboard, NOTIFY_LOCAL_DATA_CHANGE, observer);
260 EXPECT_EQ(resRepeat, ERR_OK);
261
262 OH_Pasteboard_Destroy(pasteboard);
263 OH_PasteboardObserver_Destroy(observer);
264 }
265
266 /**
267 * @tc.name: OH_Pasteboard_Subscribe002
268 * @tc.desc: OH_Pasteboard_Subscribe test invalid
269 * @tc.type: FUNC
270 * @tc.require: AROOOH5R5G
271 */
272 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_Subscribe002, TestSize.Level1)
273 {
274 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
275 OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
276 OH_PasteboardObserver_SetData(observer, nullptr, CallbackFunc, ContextFinalizeFunc);
277
278 int res1 = OH_Pasteboard_Subscribe(nullptr, NOTIFY_LOCAL_DATA_CHANGE, observer);
279 EXPECT_EQ(res1, ERR_INVALID_PARAMETER);
280
281 int res2 = OH_Pasteboard_Subscribe(pasteboard, 10, observer);
282 EXPECT_EQ(res2, ERR_INVALID_PARAMETER);
283
284 int res3 = OH_Pasteboard_Subscribe(pasteboard, -1, observer);
285 EXPECT_EQ(res3, ERR_INVALID_PARAMETER);
286
287 int res4 = OH_Pasteboard_Subscribe(pasteboard, NOTIFY_LOCAL_DATA_CHANGE, nullptr);
288 EXPECT_EQ(res4, ERR_INVALID_PARAMETER);
289
290 OH_Pasteboard_Destroy(pasteboard);
291 OH_PasteboardObserver_Destroy(observer);
292 }
293
294 /**
295 * @tc.name: OH_Pasteboard_Unsubcribe001
296 * @tc.desc: OH_Pasteboard_Unsubcribe test valid
297 * @tc.type: FUNC
298 * @tc.require: AROOOH5R5G
299 */
300 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_Unsubcribe001, TestSize.Level1)
301 {
302 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
303 OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
304 OH_PasteboardObserver_SetData(observer, nullptr, CallbackFunc, ContextFinalizeFunc);
305
306 OH_Pasteboard_Subscribe(pasteboard, NOTIFY_LOCAL_DATA_CHANGE, observer);
307
308 int res = OH_Pasteboard_Unsubscribe(pasteboard, NOTIFY_LOCAL_DATA_CHANGE, observer);
309 EXPECT_EQ(res, ERR_OK);
310
311 OH_Pasteboard_Destroy(pasteboard);
312 OH_PasteboardObserver_Destroy(observer);
313 }
314
315 /**
316 * @tc.name: OH_Pasteboard_Unsubscribe002
317 * @tc.desc: OH_Pasteboard_Unsubscribe test invalid
318 * @tc.type: FUNC
319 * @tc.require: AROOOH5R5G
320 */
321 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_Unsubcribe002, TestSize.Level1)
322 {
323 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
324 OH_PasteboardObserver* observer = OH_PasteboardObserver_Create();
325 OH_PasteboardObserver_SetData(observer, nullptr, CallbackFunc, ContextFinalizeFunc);
326
327 int res1 = OH_Pasteboard_Unsubscribe(nullptr, NOTIFY_LOCAL_DATA_CHANGE, observer);
328 EXPECT_EQ(res1, ERR_INVALID_PARAMETER);
329
330 int res2 = OH_Pasteboard_Unsubscribe(pasteboard, 10, observer);
331 EXPECT_EQ(res2, ERR_INVALID_PARAMETER);
332
333 int res3 = OH_Pasteboard_Unsubscribe(pasteboard, -1, observer);
334 EXPECT_EQ(res3, ERR_INVALID_PARAMETER);
335
336 int res4 = OH_Pasteboard_Unsubscribe(pasteboard, NOTIFY_LOCAL_DATA_CHANGE, nullptr);
337 EXPECT_EQ(res4, ERR_INVALID_PARAMETER);
338
339 OH_Pasteboard_Destroy(pasteboard);
340 OH_PasteboardObserver_Destroy(observer);
341 }
342
343 /**
344 * @tc.name: OH_Pasteboard_IsRemoteData001
345 * @tc.desc: OH_Pasteboard_IsRemoteData test valid
346 * @tc.type: FUNC
347 * @tc.require: AROOOH5R5G
348 */
349 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_IsRemoteData001, TestSize.Level1)
350 {
351 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
352
353 bool res = OH_Pasteboard_IsRemoteData(pasteboard);
354 EXPECT_FALSE(res);
355
356 OH_Pasteboard_Destroy(pasteboard);
357 }
358
359 /**
360 * @tc.name: OH_Pasteboard_GetDataSrouce001
361 * @tc.desc: OH_Pasteboard_GetDataSrouce test valid
362 * @tc.type: FUNC
363 * @tc.require: AROOOH5R5G
364 */
365 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetDataSrouce001, TestSize.Level1)
366 {
367 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
368 OH_UdmfData* setData = OH_UdmfData_Create();
369 OH_UdmfRecord* record = OH_UdmfRecord_Create();
370 OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
371 char content[] = "hello world";
372 OH_UdsPlainText_SetContent(plainText, content);
373 OH_UdmfRecord_AddPlainText(record, plainText);
374 OH_UdmfData_AddRecord(setData, record);
375
376 OH_Pasteboard_SetData(pasteboard, setData);
377
378 int len = 100;
379 char source[100];
380 int res = OH_Pasteboard_GetDataSource(pasteboard, source, len);
381 EXPECT_EQ(res, ERR_OK);
382
383 OH_Pasteboard_Destroy(pasteboard);
384 }
385
386 /**
387 * @tc.name: OH_Pasteboard_GetMimeTypes001
388 * @tc.desc: OH_Pasteboard_GetMimeTypes test empty data
389 * @tc.type: FUNC
390 * @tc.require: AR20241012964265
391 */
392 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetMimeTypes001, TestSize.Level1)
393 {
394 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
395 ASSERT_TRUE(pasteboard);
396 OH_Pasteboard_ClearData(pasteboard);
397 unsigned int count = 1000;
398 char** res = OH_Pasteboard_GetMimeTypes(pasteboard, &count);
399 EXPECT_EQ(0, count);
400 EXPECT_TRUE(res == nullptr);
401 }
402
403 /**
404 * @tc.name: OH_Pasteboard_GetMimeTypes002
405 * @tc.desc: OH_Pasteboard_GetMimeTypes test plainText
406 * @tc.type: FUNC
407 * @tc.require: AR20241012964265
408 */
409 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetMimeTypes002, TestSize.Level1)
410 {
411 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
412 ASSERT_TRUE(pasteboard);
413 OH_UdmfData* setData = OH_UdmfData_Create();
414 ASSERT_TRUE(setData);
415 OH_UdmfRecord* record = OH_UdmfRecord_Create();
416 ASSERT_TRUE(record);
417 OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
418 ASSERT_TRUE(plainText);
419 char content[] = "hello world";
420 OH_UdsPlainText_SetContent(plainText, content);
421 OH_UdmfRecord_AddPlainText(record, plainText);
422 OH_UdmfData_AddRecord(setData, record);
423 OH_Pasteboard_SetData(pasteboard, setData);
424
425 unsigned int count = 1000;
426 char** res = OH_Pasteboard_GetMimeTypes(pasteboard, &count);
427 EXPECT_EQ(1, count);
428 EXPECT_TRUE(res != nullptr);
429 EXPECT_STREQ(MIMETYPE_TEXT_PLAIN, res[0]);
430
431 OH_Pasteboard_Destroy(pasteboard);
432 }
433
434 /**
435 * @tc.name: OH_Pasteboard_GetMimeTypes003
436 * @tc.desc: OH_Pasteboard_GetMimeTypes test multi Mime types
437 * @tc.type: FUNC
438 * @tc.require: AR20241012964265
439 */
440 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetMimeTypes003, TestSize.Level1)
441 {
442 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
443 ASSERT_TRUE(pasteboard);
444 OH_UdmfData* setData = OH_UdmfData_Create();
445 ASSERT_TRUE(setData);
446 OH_UdmfRecord* record = OH_UdmfRecord_Create();
447 ASSERT_TRUE(record);
448 OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
449 ASSERT_TRUE(plainText);
450 char content[] = "hello world";
451 OH_UdsPlainText_SetContent(plainText, content);
452 OH_UdmfRecord_AddPlainText(record, plainText);
453 OH_UdmfData_AddRecord(setData, record);
454
455 OH_UdmfRecord* record2 = OH_UdmfRecord_Create();
456 ASSERT_TRUE(record2);
457 OH_UdsHtml* htmlText = OH_UdsHtml_Create();
458 ASSERT_TRUE(htmlText);
459 char html[] = "<div class='disabled'>hello</div>";
460 OH_UdsHtml_SetContent(htmlText, html);
461 OH_UdmfRecord_AddHtml(record2, htmlText);
462 OH_UdmfData_AddRecord(setData, record2);
463 OH_Pasteboard_SetData(pasteboard, setData);
464
465 unsigned int count = 1000;
466 char** res = OH_Pasteboard_GetMimeTypes(pasteboard, &count);
467 EXPECT_EQ(2, count);
468 EXPECT_TRUE(res != nullptr);
469 EXPECT_TRUE((strcmp(MIMETYPE_TEXT_PLAIN, res[0]) == 0 && strcmp(MIMETYPE_TEXT_HTML, res[1]) == 0) ||
470 (strcmp(MIMETYPE_TEXT_PLAIN, res[1]) == 0 && strcmp(MIMETYPE_TEXT_HTML, res[0]) == 0));
471
472 OH_Pasteboard_Destroy(pasteboard);
473 }
474
475 /**
476 * @tc.name: OH_Pasteboard_HasType001
477 * @tc.desc: OH_Pasteboard_HasType test valid
478 * @tc.type: FUNC
479 * @tc.require: AROOOH5R5G
480 */
481 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_HasType001, TestSize.Level1)
482 {
483 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
484 OH_UdmfData* setData = OH_UdmfData_Create();
485 OH_UdmfRecord* record = OH_UdmfRecord_Create();
486 OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
487 char content[] = "hello world";
488 OH_UdsPlainText_SetContent(plainText, content);
489 OH_UdmfRecord_AddPlainText(record, plainText);
490 OH_UdmfData_AddRecord(setData, record);
491
492 OH_Pasteboard_SetData(pasteboard, setData);
493 char type[] = "general.plain-text";
494 bool res = OH_Pasteboard_HasType(pasteboard, type);
495 EXPECT_FALSE(res);
496
497 OH_Pasteboard_Destroy(pasteboard);
498 }
499
500 /**
501 * @tc.name: OH_Pasteboard_HasData001
502 * @tc.desc: OH_Pasteboard_HasData test valid
503 * @tc.type: FUNC
504 * @tc.require: AROOOH5R5G
505 */
506 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_HasData001, TestSize.Level1)
507 {
508 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
509 OH_Pasteboard_ClearData(pasteboard);
510 bool res = OH_Pasteboard_HasData(pasteboard);
511 EXPECT_FALSE(res);
512
513 OH_UdmfData* setData = OH_UdmfData_Create();
514 OH_UdmfRecord* record = OH_UdmfRecord_Create();
515 OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
516 char content[] = "hello world";
517 OH_UdsPlainText_SetContent(plainText, content);
518 OH_UdmfRecord_AddPlainText(record, plainText);
519 OH_UdmfData_AddRecord(setData, record);
520
521 OH_Pasteboard_SetData(pasteboard, setData);
522
523 res = OH_Pasteboard_HasData(pasteboard);
524 EXPECT_TRUE(res);
525
526 OH_Pasteboard_Destroy(pasteboard);
527 }
528
529 /**
530 * @tc.name: OH_Pasteboard_ClearData001
531 * @tc.desc: OH_Pasteboard_ClearData test valid
532 * @tc.type: FUNC
533 * @tc.require: AROOOH5R5G
534 */
535 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_ClearData001, TestSize.Level1)
536 {
537 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
538 bool res = OH_Pasteboard_ClearData(pasteboard);
539 EXPECT_EQ(res, ERR_OK);
540
541 OH_Pasteboard_Destroy(pasteboard);
542 }
543
544 /**
545 * @tc.name: OH_Pasteboard_SetData001
546 * @tc.desc: OH_Pasteboard_SetData test valid
547 * @tc.type: FUNC
548 * @tc.require: AROOOH5R5G
549 */
550 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_SetData001, TestSize.Level1)
551 {
552 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
553 OH_UdmfData *setData = OH_UdmfData_Create();
554 OH_UdmfRecord *record = OH_UdmfRecord_Create();
555 OH_UdsPlainText *plainText = OH_UdsPlainText_Create();
556 char content[] = "hello world";
557 OH_UdsPlainText_SetContent(plainText, content);
558 OH_UdmfRecord_AddPlainText(record, plainText);
559 OH_UdmfData_AddRecord(setData, record);
560
561 bool res = OH_Pasteboard_SetData(pasteboard, setData);
562 EXPECT_EQ(res, ERR_OK);
563
564 OH_Pasteboard_Destroy(pasteboard);
565 OH_UdsPlainText_Destroy(plainText);
566 OH_UdmfRecord_Destroy(record);
567 OH_UdmfData_Destroy(setData);
568 }
569
570 /**
571 * @tc.name: OH_Pasteboard_GetData001
572 * @tc.desc: OH_Pasteboard_GetData test valid
573 * @tc.type: FUNC
574 * @tc.require: AROOOH5R5G
575 */
576 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetData001, TestSize.Level1)
577 {
578 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
579 OH_UdmfData* setData = OH_UdmfData_Create();
580 OH_UdmfRecord* record = OH_UdmfRecord_Create();
581 OH_UdsPlainText* plainText = OH_UdsPlainText_Create();
582 char content[] = "hello world";
583 OH_UdsPlainText_SetContent(plainText, content);
584 OH_UdmfRecord_AddPlainText(record, plainText);
585 OH_UdmfData_AddRecord(setData, record);
586
587 int res = OH_Pasteboard_SetData(pasteboard, setData);
588 EXPECT_EQ(res, ERR_OK);
589
590 int status = -1;
591 OH_UdmfData* getData = OH_Pasteboard_GetData(pasteboard, &status);
592 EXPECT_EQ(status, ERR_OK);
593 EXPECT_NE(getData, nullptr);
594
595 unsigned int count = 0;
596 OH_UdmfRecord **getRecords = OH_UdmfData_GetRecords(getData, &count);
597 EXPECT_EQ(count, 1);
598 OH_UdsPlainText *getPlainText = OH_UdsPlainText_Create();
599 OH_UdmfRecord_GetPlainText(getRecords[0], getPlainText);
600 const char *getContent = OH_UdsPlainText_GetContent(getPlainText);
601 EXPECT_EQ(strcmp(getContent, content), 0);
602
603 OH_Pasteboard_Destroy(pasteboard);
604 OH_UdsPlainText_Destroy(plainText);
605 OH_UdsPlainText_Destroy(getPlainText);
606 OH_UdmfRecord_Destroy(record);
607 OH_UdmfData_Destroy(setData);
608 OH_UdmfData_Destroy(getData);
609 }
610
611 /**
612 * @tc.name: OH_Pasteboard_GetData002
613 * @tc.desc: OH_Pasteboard_GetData test valid
614 * @tc.type: FUNC
615 * @tc.require: AROOOH5R5G
616 */
617 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetData002, TestSize.Level1)
618 {
619 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
620 OH_UdmfData* setData = OH_UdmfData_Create();
621 OH_UdmfRecord* record = OH_UdmfRecord_Create();
622 OH_UdmfRecordProvider* provider = OH_UdmfRecordProvider_Create();
623 EXPECT_NE(provider, nullptr);
624 OH_UdmfRecordProvider_SetData(provider, static_cast<void *>(record), GetDataCallback, ContextFinalizeFunc);
625 OH_UdmfData_AddRecord(setData, record);
626
627 const char* types[3] = { "general.plain-text", "general.hyperlink", "general.html" };
628 OH_UdmfRecord_SetProvider(record, types, 3, provider);
629 int res = OH_Pasteboard_SetData(pasteboard, setData);
630 EXPECT_EQ(res, ERR_OK);
631
632 int status = -1;
633 OH_UdmfData* getData = OH_Pasteboard_GetData(pasteboard, &status);
634 EXPECT_EQ(status, ERR_OK);
635 EXPECT_NE(getData, nullptr);
636
637 unsigned int count = 0;
638 OH_UdmfRecord **getRecords = OH_UdmfData_GetRecords(getData, &count);
639 EXPECT_EQ(count, 1);
640 OH_UdsPlainText *getPlainText = OH_UdsPlainText_Create();
641 OH_UdmfRecord_GetPlainText(getRecords[0], getPlainText);
642 const char *getContent = OH_UdsPlainText_GetContent(getPlainText);
643 EXPECT_EQ(strcmp(getContent, PLAINTEXT_CONTENT), 0);
644
645 OH_UdsHyperlink *getHyperLink = OH_UdsHyperlink_Create();
646 OH_UdmfRecord_GetHyperlink(getRecords[0], getHyperLink);
647 const char *getUrl = OH_UdsHyperlink_GetUrl(getHyperLink);
648 EXPECT_EQ(strcmp(getUrl, HYPERLINK_URL), 0);
649 OH_Pasteboard_Destroy(pasteboard);
650 OH_UdsPlainText_Destroy(getPlainText);
651 OH_UdsHyperlink_Destroy(getHyperLink);
652 OH_UdmfRecord_Destroy(record);
653 OH_UdmfData_Destroy(setData);
654 OH_UdmfData_Destroy(getData);
655 }
656
657 /**
658 * @tc.name: OH_Pasteboard_GetData003
659 * @tc.desc: OH_Pasteboard_GetData test valid
660 * @tc.type: FUNC
661 * @tc.require: AROOOH5R5G
662 */
663 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetData003, TestSize.Level1)
664 {
665 char typeId[] = "ApplicationDefined-myType";
666 unsigned char entry[] = "CreateGeneralRecord1";
667 unsigned int count = sizeof(entry);
668 OH_UdmfRecord *record = OH_UdmfRecord_Create();
669 int addRes1 = OH_UdmfRecord_AddGeneralEntry(record, typeId, entry, count);
670 EXPECT_EQ(addRes1, ERR_OK);
671
672 OH_UdmfData* setData = OH_UdmfData_Create();
673 OH_UdmfData_AddRecord(setData, record);
674 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
675 int res = OH_Pasteboard_SetData(pasteboard, setData);
676 EXPECT_EQ(res, ERR_OK);
677
678 int status = -1;
679 OH_UdmfData* getData = OH_Pasteboard_GetData(pasteboard, &status);
680 EXPECT_EQ(status, ERR_OK);
681 EXPECT_NE(getData, nullptr);
682
683 unsigned int getrecordCount = 0;
684 OH_UdmfRecord **getRecords = OH_UdmfData_GetRecords(getData, &getrecordCount);
685 EXPECT_EQ(getrecordCount, 1);
686
687 unsigned int getCount = 0;
688 unsigned char *getEntry;
689 int getRes = OH_UdmfRecord_GetGeneralEntry(getRecords[0], typeId, &getEntry, &getCount);
690 EXPECT_EQ(getRes, ERR_OK);
691 EXPECT_EQ(getCount, count);
692 EXPECT_EQ(memcmp(entry, getEntry, getCount), 0);
693
694 OH_Pasteboard_Destroy(pasteboard);
695 OH_UdmfRecord_Destroy(record);
696 OH_UdmfData_Destroy(setData);
697 OH_UdmfData_Destroy(getData);
698 }
699
700 /**
701 * @tc.name: OH_Pasteboard_GetData004
702 * @tc.desc: OH_Pasteboard_GetData test data type
703 * @tc.type: FUNC
704 */
705 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetData004, TestSize.Level1)
706 {
707 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "OH_Pasteboard_GetData004 start");
708 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
709 OH_UdmfData* setData = OH_UdmfData_Create();
710 OH_UdmfRecord* record = OH_UdmfRecord_Create();
711
712 OH_UdmfRecordProvider* provider = OH_UdmfRecordProvider_Create();
713 EXPECT_NE(provider, nullptr);
714 OH_UdmfRecordProvider_SetData(provider, static_cast<void *>(record), GetDataCallback, ContextFinalizeFunc);
715
716 const char* types[3] = { "general.plain-text", "general.hyperlink", "general.html" };
717 OH_UdmfRecord_SetProvider(record, types, 3, provider);
718 OH_UdmfData_AddRecord(setData, record);
719
720 int res = OH_Pasteboard_SetData(pasteboard, setData);
721 EXPECT_EQ(res, ERR_OK);
722
723 PasteData pasteData;
724 auto ret = PasteboardClient::GetInstance()->GetPasteData(pasteData);
725 ASSERT_TRUE(ret == static_cast<int32_t>(PasteboardError::E_OK));
726
727 auto record1 = pasteData.GetRecordAt(0);
728 auto mimeType = record1->GetMimeType();
729 EXPECT_EQ(mimeType, MIMETYPE_TEXT_PLAIN);
730
731 OH_Pasteboard_Destroy(pasteboard);
732 OH_UdmfRecord_Destroy(record);
733 OH_UdmfData_Destroy(setData);
734 OH_UdmfRecordProvider_Destroy(provider);
735 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "OH_Pasteboard_GetData004 end");
736 }
737
738 /**
739 * @tc.name: OH_Pasteboard_GetData005
740 * @tc.desc: OH_Pasteboard_GetData test data type
741 * @tc.type: FUNC
742 */
743 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetData005, TestSize.Level1)
744 {
745 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "OH_Pasteboard_GetData005 start");
746 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
747 OH_UdmfData* setData = OH_UdmfData_Create();
748 OH_UdmfRecord* record = OH_UdmfRecord_Create();
749
750 OH_UdmfRecordProvider* provider = OH_UdmfRecordProvider_Create();
751 EXPECT_NE(provider, nullptr);
752 OH_UdmfRecordProvider_SetData(provider, static_cast<void *>(record), GetDataCallback, ContextFinalizeFunc);
753 const char* types[3] = { "general.plain-text", "general.hyperlink", "general.html" };
754 OH_UdmfRecord_SetProvider(record, types, 3, provider);
755
756 OH_UdsHyperlink *link = OH_UdsHyperlink_Create();
757 OH_UdsHyperlink_SetUrl(link, HYPERLINK_URL);
758 OH_UdmfRecord_AddHyperlink(record, link);
759
760 OH_UdmfData_AddRecord(setData, record);
761 auto res = OH_Pasteboard_SetData(pasteboard, setData);
762 EXPECT_EQ(res, ERR_OK);
763
764 PasteData pasteData;
765 auto ret = PasteboardClient::GetInstance()->GetPasteData(pasteData);
766 ASSERT_TRUE(ret == static_cast<int32_t>(PasteboardError::E_OK));
767
768 auto record1 = pasteData.GetRecordAt(0);
769 auto mimeType = record1->GetMimeType();
770 EXPECT_EQ(mimeType, MIMETYPE_TEXT_PLAIN);
771
772 auto text = record1->GetPlainText();
773 EXPECT_EQ(strcmp(text->c_str(), PLAINTEXT_CONTENT), 0);
774
775 OH_Pasteboard_Destroy(pasteboard);
776 OH_UdmfRecord_Destroy(record);
777 OH_UdmfData_Destroy(setData);
778 OH_UdsHyperlink_Destroy(link);
779 OH_UdmfRecordProvider_Destroy(provider);
780 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "OH_Pasteboard_GetData005 end");
781 }
782
783 /**
784 * @tc.name: OH_Pasteboard_GetData006
785 * @tc.desc: OH_Pasteboard_GetData test data type
786 * @tc.type: FUNC
787 */
788 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetData006, TestSize.Level1)
789 {
790 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "OH_Pasteboard_GetData006 start");
791 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
792 OH_UdmfData* setData = OH_UdmfData_Create();
793 OH_UdmfRecord* record = OH_UdmfRecord_Create();
794
795 OH_UdsHtml *html = OH_UdsHtml_Create();
796 OH_UdsHtml_SetContent(html, HTML_TEXT);
797 OH_UdmfRecord_AddHtml(record, html);
798
799 OH_UdmfRecordProvider* provider = OH_UdmfRecordProvider_Create();
800 EXPECT_NE(provider, nullptr);
801 OH_UdmfRecordProvider_SetData(provider, static_cast<void *>(record), GetDataCallback, ContextFinalizeFunc);
802 const char* types[3] = { "general.plain-text", "general.hyperlink", "general.html" };
803 OH_UdmfRecord_SetProvider(record, types, 3, provider);
804
805 OH_UdmfData_AddRecord(setData, record);
806 bool res = OH_Pasteboard_SetData(pasteboard, setData);
807 EXPECT_EQ(res, ERR_OK);
808
809 PasteData pasteData;
810 auto ret = PasteboardClient::GetInstance()->GetPasteData(pasteData);
811 ASSERT_TRUE(ret == static_cast<int32_t>(PasteboardError::E_OK));
812
813 auto record1 = pasteData.GetRecordAt(0);
814 auto mimeType = record1->GetMimeType();
815 EXPECT_EQ(mimeType, MIMETYPE_TEXT_HTML);
816
817 auto html1 = record1->GetHtmlText();
818 EXPECT_EQ(strcmp(html1->c_str(), HTML_TEXT), 0);
819
820 OH_Pasteboard_Destroy(pasteboard);
821 OH_UdmfRecord_Destroy(record);
822 OH_UdmfData_Destroy(setData);
823 OH_UdmfRecordProvider_Destroy(provider);
824 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "OH_Pasteboard_GetData006 end");
825 }
826
827 /**
828 * @tc.name: OH_Pasteboard_GetData007
829 * @tc.desc: OH_Pasteboard_GetData test data type
830 * @tc.type: FUNC
831 */
832 HWTEST_F(PasteboardCapiTest, OH_Pasteboard_GetData007, TestSize.Level1)
833 {
834 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "OH_Pasteboard_GetData007 start");
835 std::string plainText = "helloWorld";
836 auto newData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
837 auto ret = PasteboardClient::GetInstance()->SetPasteData(*newData);
838 ASSERT_TRUE(ret == static_cast<int32_t>(PasteboardError::E_OK));
839
840 OH_Pasteboard* pasteboard = OH_Pasteboard_Create();
841 int status = -1;
842 OH_UdmfData* getData = OH_Pasteboard_GetData(pasteboard, &status);
843 EXPECT_EQ(status, ERR_OK);
844 EXPECT_NE(getData, nullptr);
845
846 unsigned int getrecordCount = 0;
847 OH_UdmfRecord **getRecords = OH_UdmfData_GetRecords(getData, &getrecordCount);
848 EXPECT_EQ(getrecordCount, 1);
849 OH_UdsPlainText *getPlainText = OH_UdsPlainText_Create();
850 OH_UdmfRecord_GetPlainText(getRecords[0], getPlainText);
851 const char *getContent = OH_UdsPlainText_GetContent(getPlainText);
852 EXPECT_STREQ(getContent, plainText.c_str());
853
854 OH_Pasteboard_Destroy(pasteboard);
855 PASTEBOARD_HILOGI(PASTEBOARD_MODULE_SERVICE, "OH_Pasteboard_GetData007 end");
856 }
857 }