1 /*
2 * Copyright (c) 2022-2023 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 "copy_uri_handler.h"
17 #include "common/block_object.h"
18 #include "clip/clip_plugin.h"
19 #include "clip_factory.h"
20 #include "int_wrapper.h"
21 #include "paste_uri_handler.h"
22 #include "pasteboard_client.h"
23 #include "remote_file_share.h"
24 #include <gmock/gmock.h>
25 #include <gtest/gtest.h>
26
27 namespace OHOS::MiscServices {
28 using namespace testing::ext;
29 using namespace testing;
30 using namespace OHOS::AAFwk;
31 using namespace OHOS::Media;
32 constexpr const int32_t INVALID_FD = -1;
33 constexpr const char *FILE_URI = "/data/test/resource/pasteboardTest.txt";
34 class PasteDataTest : public testing::Test {
35 public:
36 static void SetUpTestCase(void);
37 static void TearDownTestCase(void);
38 void SetUp();
39 void TearDown();
40 };
41
SetUpTestCase(void)42 void PasteDataTest::SetUpTestCase(void)
43 {
44 }
45
TearDownTestCase(void)46 void PasteDataTest::TearDownTestCase(void)
47 {
48 }
49
SetUp(void)50 void PasteDataTest::SetUp(void)
51 {
52 }
53
TearDown(void)54 void PasteDataTest::TearDown(void)
55 {
56 }
57
ClipFactory()58 ClipFactory::ClipFactory()
59 {
60 ClipPlugin::RegCreator("distributed_clip", this);
61 }
62
63 class UriHandlerMock : public UriHandler {
64 public:
65 UriHandlerMock() = default;
66 virtual ~UriHandlerMock() = default;
67
68 MOCK_METHOD1(ToUri, std::string(int32_t fd));
69 MOCK_METHOD2(ToFd, int32_t(const std::string &uri, bool isClient));
70 MOCK_CONST_METHOD1(IsFile, bool(const std::string &uri));
71 };
72
73 /**
74 * @tc.name: ReplaceShareUri001
75 * @tc.desc: replace user id in share path
76 * @tc.type: FUNC
77 * @tc.require:AR000H5I1D
78 * @tc.author: baoyayong
79 */
80 HWTEST_F(PasteDataTest, ReplaceShareUri001, TestSize.Level0)
81 {
82 PasteData data;
83 PasteDataRecord::Builder builder(MIMETYPE_TEXT_URI);
84 std::string uriStr = "/data/storage/100/haps/caches/xxx.txt";
85 auto uri = std::make_shared<OHOS::Uri>(uriStr);
86 builder.SetUri(uri);
87 auto record = builder.Build();
88
89 // mock
90 UriHandlerMock mock;
91 std::string mockUri = "/mnt/hmdfs/100/account/merge_view/services/psteboard_service/.share/xxx.txt";
92 EXPECT_CALL(mock, ToUri(_)).WillRepeatedly(Return(mockUri));
93 EXPECT_CALL(mock, ToFd(_, _)).WillRepeatedly(Return(2));
94 EXPECT_CALL(mock, IsFile(_)).WillRepeatedly(Return(true));
95
96 data.AddRecord(record);
97 MessageParcel parcel;
98 data.WriteUriFd(parcel, mock);
99 bool result = data.ReadUriFd(parcel, mock);
100 EXPECT_TRUE(result);
101 EXPECT_EQ(mockUri, data.GetPrimaryUri()->ToString());
102 data.ReplaceShareUri(200);
103 std::string mockUri2 = "/mnt/hmdfs/200/account/merge_view/services/psteboard_service/.share/xxx.txt";
104 EXPECT_EQ(mockUri2, data.GetPrimaryUri()->ToString());
105 }
106
107 /**
108 * @tc.name: uriConvertTest001
109 * @tc.desc: uri convert(in same app)
110 * @tc.type: FUNC
111 * @tc.require:AR000H5I1D
112 * @tc.author: chenyu
113 */
114 HWTEST_F(PasteDataTest, uriConvertTest001, TestSize.Level0)
115 {
116 PasteData data;
117 PasteDataRecord::Builder builder(MIMETYPE_TEXT_URI);
118 std::string uriStr = FILE_URI;
119 auto uri = std::make_shared<OHOS::Uri>(uriStr);
120 builder.SetUri(uri);
121 auto record = builder.Build();
122 data.AddRecord(record);
123
124 MessageParcel parcel;
125 CopyUriHandler copyHandler;
126 data.WriteUriFd(parcel, copyHandler);
127 bool result = data.ReadUriFd(parcel, copyHandler);
128 EXPECT_TRUE(result);
129 auto distributedUri = data.GetPrimaryUri()->ToString();
130 EXPECT_FALSE(uriStr == distributedUri);
131
132 MessageParcel parcel1;
133 PasteUriHandler pasteHandler;
134 int32_t fd = 5;
135 pasteHandler.ToUri(fd);
136
137 data.SetLocalPasteFlag(true);
138 data.WriteUriFd(parcel1, pasteHandler);
139 result = data.ReadUriFd(parcel1, pasteHandler);
140 EXPECT_TRUE(result);
141 ASSERT_TRUE(data.GetPrimaryUri() != nullptr);
142 auto convertedUri = data.GetPrimaryUri()->ToString();
143 EXPECT_EQ(distributedUri, convertedUri);
144 EXPECT_FALSE(uriStr == convertedUri);
145 }
146
147 /**
148 * @tc.name: uriConvertTest002
149 * @tc.desc: uri convert(in same app)
150 * @tc.type: FUNC
151 */
152 HWTEST_F(PasteDataTest, uriConvertTest002, TestSize.Level0)
153 {
154 PasteUriHandler pasteHandler;
155 int32_t fd = -100;
156 std::string convertUri = pasteHandler.ToUri(fd);
157 EXPECT_TRUE(convertUri == "");
158 }
159
160 /**
161 * @tc.name: uriConvertTest003
162 * @tc.desc: uri convert(in same app)
163 * @tc.type: FUNC
164 */
165 HWTEST_F(PasteDataTest, uriConvertTest003, TestSize.Level0)
166 {
167 CopyUriHandler copyHandler;
168 int32_t fd = -100;
169 std::string convertUri = copyHandler.ToUri(fd);
170 EXPECT_TRUE(convertUri == "");
171 }
172
173 /**
174 * @tc.name: AddRecord001
175 * @tc.desc: PasteDataRecord AddRecord
176 * @tc.type: FUNC
177 */
178 HWTEST_F(PasteDataTest, AddRecord001, TestSize.Level0)
179 {
180 PasteData data;
181 PasteDataRecord::Builder builder(MIMETYPE_TEXT_URI);
182 std::string uriStr = FILE_URI;
183 auto uri = std::make_shared<OHOS::Uri>(uriStr);
184 builder.SetUri(uri);
185 auto record = builder.Build();
186 EXPECT_TRUE(record != nullptr);
187 data.AddRecord(nullptr);
188 auto count = data.GetRecordCount();
189 EXPECT_TRUE(count == 0);
190 }
191
192 /**
193 * @tc.name: AddRecord002
194 * @tc.desc: PasteDataRecord AddRecord
195 * @tc.type: FUNC
196 */
197 HWTEST_F(PasteDataTest, AddRecord002, TestSize.Level0)
198 {
199 PasteData data;
200 PasteDataRecord::Builder builder(MIMETYPE_TEXT_URI);
201 std::string uriStr = FILE_URI;
202 auto uri = std::make_shared<OHOS::Uri>(uriStr);
203 builder.SetUri(uri);
204 auto record = builder.Build();
205 data.AddRecord(*record);
206 auto count = data.GetRecordCount();
207 EXPECT_TRUE(count == 1);
208 }
209
210 /**
211 * @tc.name: Marshalling001
212 * @tc.desc: PasteData Marshalling
213 * @tc.type: FUNC
214 */
215 HWTEST_F(PasteDataTest, Marshalling001, TestSize.Level0)
216 {
217 PasteData data1;
218 PasteDataRecord::Builder builder(MIMETYPE_TEXT_URI);
219 std::string uriStr = FILE_URI;
220 auto uri = std::make_shared<OHOS::Uri>(uriStr);
221 builder.SetUri(uri);
222 auto record = builder.Build();
223 data1.AddRecord(*record);
224 auto count = data1.GetRecordCount();
225 EXPECT_TRUE(count == 1);
226 Parcel parcel;
227 data1.Marshalling(parcel);
228
229 auto data2 = PasteData::Unmarshalling(parcel);
230 auto count2 = data2->GetRecordCount();
231 EXPECT_TRUE(count == count2);
232 std::shared_ptr<OHOS::Uri> uri2 = data2->GetPrimaryUri();
233 std::string uriStr2 = uri2->ToString();
234 EXPECT_TRUE(uriStr == uriStr2);
235 }
236
237 /**
238 * @tc.name: GetRealPathFailed001
239 * @tc.desc: GetRealPath Failed(realpath(inOriPath.c_str(), realPath) == nullptr)
240 * @tc.type: FUNC
241 * @tc.require: issuesI5Y6PO
242 * @tc.author: chenyu
243 */
244 HWTEST_F(PasteDataTest, GetRealPathFailed001, TestSize.Level0)
245 {
246 std::string uriStr = "/data/storage/100/haps/caches/xxx.txt";
247 PasteUriHandler pasteHandler;
248 auto ret = pasteHandler.ToFd(uriStr, true);
249 EXPECT_EQ(ret, INVALID_FD);
250 }
251
252 /**
253 * @tc.name: GetRealPathFailed002
254 * @tc.desc: GetRealPath Failed(inOriPath.size() > PATH_MAX)
255 * @tc.type: FUNC
256 * @tc.require: issuesI5Y6PO
257 * @tc.author: chenyu
258 */
259 HWTEST_F(PasteDataTest, GetRealPathFailed002, TestSize.Level0)
260 {
261 std::string uriStr(PATH_MAX + 2, '*');
262 PasteUriHandler pasteHandler;
263 auto ret = pasteHandler.ToFd(uriStr, true);
264 EXPECT_EQ(ret, INVALID_FD);
265 }
266
267 /**
268 * @tc.name: MaxLength001
269 * @tc.desc: PasteDataRecord: maxLength NewHtmlRecord
270 * @tc.type: FUNC
271 * @tc.require:
272 * @tc.author:
273 */
274 HWTEST_F(PasteDataTest, MaxLength001, TestSize.Level0)
275 {
276 int maxLength = 20 * 1024 * 1024;
277 std::string res = "hello";
278 std::string temp = "world";
279 for (int i = 0; i < maxLength; i++)
280 {
281 res += temp;
282 }
283 std::string htmlText = "<div class='disabled'>" + res + "</div>";
284 auto record = PasteboardClient::GetInstance()->CreateHtmlTextRecord(htmlText);
285 ASSERT_TRUE(record == nullptr);
286 }
287
288 /**
289 * @tc.name: MaxLength002
290 * @tc.desc: PasteDataRecord: maxLength NewPlaintTextRecord
291 * @tc.type: FUNC
292 * @tc.require:
293 * @tc.author:
294 */
295 HWTEST_F(PasteDataTest, MaxLength002, TestSize.Level0)
296 {
297 int maxLength = 20 * 1024 * 1024;
298 std::string plainText = "hello";
299 std::string temp = "world";
300 for (int i = 0; i < maxLength; i++)
301 {
302 plainText += temp;
303 }
304 auto record = PasteboardClient::GetInstance()->CreatePlainTextRecord(plainText);
305 ASSERT_TRUE(record == nullptr);
306 }
307
308 /**
309 * @tc.name: ConvertToText001
310 * @tc.desc: PasteDataRecord: ConvertToText htmlText
311 * @tc.type: FUNC
312 * @tc.require: AR000HEECD
313 * @tc.author: chenyu
314 */
315 HWTEST_F(PasteDataTest, ConvertToText001, TestSize.Level0)
316 {
317 std::string htmlText = "<div class='disabled item tip user-programs'>";
318 auto record = PasteboardClient::GetInstance()->CreateHtmlTextRecord(htmlText);
319 ASSERT_TRUE(record != nullptr);
320 auto text = record->ConvertToText();
321 EXPECT_EQ(text, htmlText);
322 }
323
324 /**
325 * @tc.name: ConvertToText002
326 * @tc.desc: PasteDataRecord: ConvertToText plainText
327 * @tc.type: FUNC
328 * @tc.require:
329 * @tc.author:
330 */
331 HWTEST_F(PasteDataTest, ConvertToText002, TestSize.Level0)
332 {
333 std::string plainText = "paste record test";
334 auto record = PasteboardClient::GetInstance()->CreatePlainTextRecord(plainText);
335 ASSERT_TRUE(record != nullptr);
336 auto text = record->ConvertToText();
337 EXPECT_EQ(text, plainText);
338 }
339
340 /**
341 * @tc.name: ConvertToText003
342 * @tc.desc: PasteDataRecord: ConvertToText uri
343 * @tc.type: FUNC
344 * @tc.require:
345 * @tc.author:
346 */
347 HWTEST_F(PasteDataTest, ConvertToText003, TestSize.Level0)
348 {
349 OHOS::Uri uri("uri");
350 auto record = PasteboardClient::GetInstance()->CreateUriRecord(uri);
351 ASSERT_TRUE(record != nullptr);
352 auto text = record->ConvertToText();
353 EXPECT_EQ(text, uri.ToString());
354 }
355
356 /**
357 * @tc.name: ConvertToText004
358 * @tc.desc: PasteDataRecord: ConvertToText uri
359 * @tc.type: FUNC
360 * @tc.require:
361 * @tc.author:
362 */
363 HWTEST_F(PasteDataTest, ConvertToText004, TestSize.Level0)
364 {
365 uint32_t color[100] = { 3, 7, 9, 9, 7, 6 };
366 InitializationOptions opts = { { 5, 7 }, PixelFormat::ARGB_8888 };
367 std::unique_ptr<PixelMap> pixelMap = PixelMap::Create(color, 100, opts);
368 std::shared_ptr<PixelMap> pixelMapIn = move(pixelMap);
369 auto record = PasteboardClient::GetInstance()->CreatePixelMapRecord(pixelMapIn);
370 ASSERT_TRUE(record != nullptr);
371 auto text = record->ConvertToText();
372 EXPECT_EQ(text, "");
373 }
374
375 /**
376 * @tc.name: GetPasteDataMsg001
377 * @tc.desc: PasteData: GetPrimaryMimeType is nullptr and so on
378 * @tc.type: FUNC
379 * @tc.require:
380 * @tc.author:
381 */
382 HWTEST_F(PasteDataTest, GetPasteDataMsg001, TestSize.Level0)
383 {
384 std::string plainText1 = "helloWorld";
385 auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText1);
386 ASSERT_TRUE(pasteData != nullptr);
387 auto newPrimaryPixelMap = pasteData->GetPrimaryPixelMap();
388 ASSERT_TRUE(newPrimaryPixelMap == nullptr);
389 auto newPrimaryMimeType = pasteData->GetPrimaryMimeType();
390 ASSERT_TRUE(newPrimaryMimeType != nullptr);
391 auto newPasteData = std::make_shared<PasteData>();
392 auto newPrimaryMimeType2 = newPasteData->GetPrimaryMimeType();
393 ASSERT_TRUE(newPrimaryMimeType2 == nullptr);
394 std::string plainText2 = "plain text";
395 auto record = PasteboardClient::GetInstance()->CreatePlainTextRecord(plainText2);
396 ASSERT_TRUE(record != nullptr);
397 ASSERT_FALSE(pasteData->ReplaceRecordAt(1000, record));
398 }
399
400 /**
401 * @tc.name: GetPasteDataMsg002
402 * @tc.desc: PasteData: GetPrimaryWant is nullptr and so on
403 * @tc.type: FUNC
404 * @tc.require:
405 * @tc.author:
406 */
407 HWTEST_F(PasteDataTest, GetPasteDataMsg002, TestSize.Level0)
408 {
409 uint32_t color[100] = { 3, 7, 9, 9, 7, 6 };
410 InitializationOptions opts = { { 5, 7 }, PixelFormat::ARGB_8888 };
411 std::unique_ptr<PixelMap> pixelMap = PixelMap::Create(color, sizeof(color) / sizeof(color[0]), opts);
412 std::shared_ptr<PixelMap> pixelMapIn = move(pixelMap);
413 auto newPasteData = PasteboardClient::GetInstance()->CreatePixelMapData(pixelMapIn);
414 ASSERT_TRUE(newPasteData != nullptr);
415 auto pixMap = newPasteData->GetPrimaryPixelMap();
416 ASSERT_TRUE(pixMap != nullptr);
417 auto primaryWant = newPasteData->GetPrimaryWant();
418 ASSERT_TRUE(primaryWant == nullptr);
419 auto primaryText = newPasteData->GetPrimaryText();
420 ASSERT_TRUE(primaryText == nullptr);
421 auto primaryUri = newPasteData->GetPrimaryUri();
422 ASSERT_TRUE(primaryUri == nullptr);
423 auto record = newPasteData->GetRecordAt(1);
424 ASSERT_TRUE(record == nullptr);
425 auto res1 = newPasteData->RemoveRecordAt(1);
426 ASSERT_FALSE(res1);
427 std::string mimeType = "text/plain";
428 ASSERT_FALSE(newPasteData->HasMimeType(mimeType));
429 }
430
431 /**
432 * @tc.name: ShareOptionToString001
433 * @tc.desc: PasteData: ShareOptionToString
434 * @tc.type: FUNC
435 * @tc.require:
436 * @tc.author:
437 */
438 HWTEST_F(PasteDataTest, ShareOptionToString001, TestSize.Level0)
439 {
440 std::string shareOption1;
441 PasteData::ShareOptionToString(ShareOption::InApp, shareOption1);
442 ASSERT_TRUE(shareOption1 == "InAPP");
443 std::string shareOption2;
444 PasteData::ShareOptionToString(ShareOption::LocalDevice, shareOption2);
445 ASSERT_TRUE(shareOption2 == "LocalDevice");
446 std::string shareOption3;
447 PasteData::ShareOptionToString(ShareOption::CrossDevice, shareOption3);
448 ASSERT_TRUE(shareOption3 == "CrossDevice");
449 }
450
451 /**
452 * @tc.name: SetInvalid001
453 * @tc.desc: PasteData: SetInvalid001
454 * @tc.type: FUNC
455 * @tc.require:
456 * @tc.author:
457 */
458 HWTEST_F(PasteDataTest, SetInvalid001, TestSize.Level0)
459 {
460 bool result = true;
461 std::shared_ptr<PasteData> pasteData = std::make_shared<PasteData>();
462 pasteData->SetInvalid();
463 result = pasteData->IsValid();
464 ASSERT_FALSE(result);
465 }
466
467 /**
468 * @tc.name: SetLocalOnly001
469 * @tc.desc: PasteData: SetLocalOnly
470 * @tc.type: FUNC
471 * @tc.require:
472 * @tc.author:
473 */
474 HWTEST_F(PasteDataTest, SetLocalOnly001, TestSize.Level0)
475 {
476 bool result = false;
477 std::shared_ptr<PasteData> pasteData = std::make_shared<PasteData>();
478 pasteData->SetLocalOnly(true);
479 result = pasteData->GetLocalOnly();
480 ASSERT_TRUE(result);
481 }
482
483 /**
484 * @tc.name: SetAddition001
485 * @tc.desc: PasteData: SetAddition
486 * @tc.type: FUNC
487 * @tc.require:
488 * @tc.author:
489 */
490 HWTEST_F(PasteDataTest, SetAddition001, TestSize.Level0)
491 {
492 std::string plainText = "plain text";
493 auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
494 size_t fileSize = 0;
495 pasteData->SetAddition(PasteData::REMOTE_FILE_SIZE, AAFwk::Integer::Box(fileSize));
496 AAFwk::WantParams additions;
497 pasteData->SetAdditions(additions);
498 ASSERT_TRUE(pasteData != nullptr);
499 }
500
501 /**
502 * @tc.name: SetRemote001
503 * @tc.desc: PasteData: SetRemote
504 * @tc.type: FUNC
505 * @tc.require:
506 * @tc.author:
507 */
508 HWTEST_F(PasteDataTest, SetRemote001, TestSize.Level0)
509 {
510 bool isRemote = false;
511 std::string plainText = "plain text";
512 auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
513 isRemote = true;
514 pasteData->SetRemote(isRemote);
515 bool result = pasteData->IsRemote();
516 ASSERT_TRUE(result);
517 }
518
519 /**
520 * @tc.name: SetOrginAuthority001
521 * @tc.desc: PasteData: SetOrginAuthority
522 * @tc.type: FUNC
523 * @tc.require:
524 * @tc.author:
525 */
526 HWTEST_F(PasteDataTest, SetOrginAuthority001, TestSize.Level0)
527 {
528 std::string plainText = "plain text";
529 std::string bundleName = "com.example.myapplication";
530 auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
531 pasteData->SetBundleName(bundleName);
532 pasteData->SetOrginAuthority(bundleName);
533 std::string getBundleName = pasteData->GetBundleName();
534 std::string getOrginAuthority = pasteData->GetOrginAuthority();
535 ASSERT_TRUE(getBundleName == bundleName);
536 ASSERT_TRUE(getOrginAuthority == bundleName);
537 std::string time = "2023-08-09";
538 pasteData->SetTime(time);
539 std::string getTime = pasteData->GetTime();
540 ASSERT_TRUE(getTime == time);
541 }
542
543 /**
544 * @tc.name: GetConvertUri001
545 * @tc.desc: PasteDataRecord: GetConvertUri
546 * @tc.type: FUNC
547 * @tc.require:
548 * @tc.author:
549 */
550 HWTEST_F(PasteDataTest, GetConvertUri001, TestSize.Level0)
551 {
552 std::vector<uint8_t> arrayBuffer(46);
553 arrayBuffer = { 2, 7, 6, 8, 9 };
554 std::string mimeType = "image/jpg";
555 auto pasteDataRecord = PasteboardClient::GetInstance()->CreateKvRecord(mimeType, arrayBuffer);
556 ASSERT_TRUE(pasteDataRecord != nullptr);
557 std::string convertUri_ = "/mnt/hmdfs/";
558 pasteDataRecord->SetConvertUri(convertUri_);
559 std::string result = pasteDataRecord->GetConvertUri();
560 ASSERT_TRUE(result == convertUri_);
561 std::string newUriStr = "/mnt/hmdfs/test";
562 pasteDataRecord->SetUri(std::make_shared<OHOS::Uri>(newUriStr));
563 std::shared_ptr<Uri> uri = pasteDataRecord->GetUri();
564 ASSERT_TRUE(uri != nullptr);
565 std::shared_ptr<Uri> getOriginUri = pasteDataRecord->GetOrginUri();
566 ASSERT_TRUE(getOriginUri != nullptr);
567 }
568
569 /**
570 * @tc.name: GetConvertUri002
571 * @tc.desc: PasteDataRecord: GetConvertUri
572 * @tc.type: FUNC
573 * @tc.require:
574 * @tc.author:
575 */
576 HWTEST_F(PasteDataTest, GetConvertUri002, TestSize.Level0)
577 {
578 std::vector<uint8_t> arrayBuffer(46);
579 arrayBuffer = { 2, 7, 6, 8, 9 };
580 std::string mimeType = "image/jpg";
581 auto pasteDataRecord = PasteboardClient::GetInstance()->CreateKvRecord(mimeType, arrayBuffer);
582 ASSERT_TRUE(pasteDataRecord != nullptr);
583 std::string convertUri_ = "";
584 pasteDataRecord->SetConvertUri(convertUri_);
585 std::string result = pasteDataRecord->GetConvertUri();
586 ASSERT_TRUE(result == convertUri_);
587 pasteDataRecord->ReplaceShareUri(200);
588 std::string result2 = pasteDataRecord->GetConvertUri();
589 ASSERT_TRUE(result2 == convertUri_);
590 }
591
592 /**
593 * @tc.name: HasGrantUriPermission001
594 * @tc.desc: PasteDataRecord: HasGrantUriPermission
595 * @tc.type: FUNC
596 * @tc.require:
597 * @tc.author:
598 */
599 HWTEST_F(PasteDataTest, HasGrantUriPermission001, TestSize.Level0)
600 {
601 std::vector<uint8_t> arrayBuffer(46);
602 arrayBuffer = { 1, 2, 6, 8, 9 };
603 std::string mimeType = "image/jpg";
604 auto pasteDataRecord = PasteboardClient::GetInstance()->CreateKvRecord(mimeType, arrayBuffer);
605 ASSERT_TRUE(pasteDataRecord != nullptr);
606 pasteDataRecord->SetGrantUriPermission(true);
607 auto hasGrantUriPermission_ = pasteDataRecord->HasGrantUriPermission();
608 ASSERT_TRUE(hasGrantUriPermission_);
609 }
610
611 /**
612 * @tc.name: LoadSystemAbilityFail001
613 * @tc.desc: PasteDataRecord: LoadSystemAbilityFail
614 * @tc.type: FUNC
615 * @tc.require:
616 * @tc.author:
617 */
618 HWTEST_F(PasteDataTest, LoadSystemAbilityFail001, TestSize.Level0)
619 {
620 std::vector<uint8_t> arrayBuffer(46);
621 std::string mimeType = "image/jpg";
622 arrayBuffer = { 1, 2, 3, 4, 6 };
623 PasteboardClient::GetInstance()->LoadSystemAbilityFail();
624 auto pasteDataRecord = PasteboardClient::GetInstance()->CreateKvRecord(mimeType, arrayBuffer);
625 ASSERT_TRUE(pasteDataRecord != nullptr);
626 }
627
628 /**
629 * @tc.name: LoadSystemAbilitySuccess001
630 * @tc.desc: PasteDataRecord: LoadSystemAbilitySuccess
631 * @tc.type: FUNC
632 * @tc.require:
633 * @tc.author:
634 */
635 HWTEST_F(PasteDataTest, LoadSystemAbilitySuccess001, TestSize.Level0)
636 {
637 std::vector<uint8_t> arrayBuffer(46);
638 std::string mimeType = "image/jpg";
639 arrayBuffer = { 1, 2, 3, 4, 6 };
640 sptr<IRemoteObject> remoteObject = nullptr;
641 PasteboardClient::GetInstance()->LoadSystemAbilitySuccess(remoteObject);
642 auto pasteDataRecord = PasteboardClient::GetInstance()->CreateKvRecord(mimeType, arrayBuffer);
643 ASSERT_TRUE(pasteDataRecord != nullptr);
644 }
645
646 /**
647 * @tc.name: SetInterval001
648 * @tc.desc: BlockObject: SetInterval
649 * @tc.type: FUNC
650 * @tc.require:
651 * @tc.author:
652 */
653 HWTEST_F(PasteDataTest, SetInterval001, TestSize.Level0)
654 {
655 uint32_t POPUP_INTERVAL = 1000;
656 auto block = std::make_shared<BlockObject<std::shared_ptr<PasteData>>>(POPUP_INTERVAL);
657 std::shared_ptr<PasteData> pasteData = std::make_shared<PasteData>();
658 block->SetValue(pasteData);
659 block->SetInterval(POPUP_INTERVAL);
660 auto value = block->GetValue();
661 EXPECT_TRUE(value != nullptr);
662 }
663
664 /**
665 * @tc.name: ClipPlugin001
666 * @tc.desc: API_EXPORT: ClipPlugin
667 * @tc.type: FUNC
668 * @tc.require:
669 * @tc.author:
670 */
671 HWTEST_F(PasteDataTest, ClipPlugin001, TestSize.Level0)
672 {
673 std::string PLUGIN_NAME_VAL = "distributed_clip";
__anonacf5d0360102(ClipPlugin *plugin) 674 auto release = [&PLUGIN_NAME_VAL, this](ClipPlugin *plugin) {
675 ClipPlugin::DestroyPlugin(PLUGIN_NAME_VAL, plugin);
676 };
677 auto clipPlugin_ = std::shared_ptr<ClipPlugin>(ClipPlugin::CreatePlugin(PLUGIN_NAME_VAL), release);
678 ClipPlugin::Factory *factory = nullptr;
679 auto result = ClipPlugin::RegCreator(PLUGIN_NAME_VAL, factory);
680 EXPECT_TRUE(result);
681 auto userId = 10000;
682 auto events1 = clipPlugin_->GetTopEvents(1, userId);
683 EXPECT_TRUE(events1.size() == 0);
684 auto events2 = clipPlugin_->GetTopEvents(1);
685 EXPECT_TRUE(events2.size() == 0);
686 clipPlugin_->Clear();
687 clipPlugin_->Clear(userId);
688 }
689
690 /**
691 * @tc.name: ClipPlugin002
692 * @tc.desc: API_EXPORT: ClipPlugin
693 * @tc.type: FUNC
694 * @tc.require:
695 * @tc.author:
696 */
697 HWTEST_F(PasteDataTest, ClipPlugin002, TestSize.Level0)
698 {
699 std::string PLUGIN_NAME_VAL = "distributed_clip";
__anonacf5d0360202(ClipPlugin *plugin) 700 auto release = [&PLUGIN_NAME_VAL, this](ClipPlugin *plugin) {
701 ClipPlugin::DestroyPlugin(PLUGIN_NAME_VAL, plugin);
702 };
703 auto clipPlugin_ = std::shared_ptr<ClipPlugin>(ClipPlugin::CreatePlugin(PLUGIN_NAME_VAL), release);
704 ClipPlugin::Factory *factory = new ClipFactory();
705 auto result = ClipPlugin::RegCreator(PLUGIN_NAME_VAL, factory);
706 EXPECT_FALSE(result);
707 auto userId = 3701;
708 auto events1 = clipPlugin_->GetTopEvents(1, userId);
709 EXPECT_TRUE(events1.size() == 0);
710 clipPlugin_->Clear(userId);
711 }
712
713 /**
714 * @tc.name: ClipPlugin003
715 * @tc.desc: API_EXPORT: ClipPlugin
716 * @tc.type: FUNC
717 * @tc.require:
718 * @tc.author:
719 */
720 HWTEST_F(PasteDataTest, ClipPlugin003, TestSize.Level0)
721 {
722 ClipPlugin::GlobalEvent event1;
723 event1.seqId = 0;
724 event1.deviceId = "test_device_id";
725 event1.user = 0;
726 ClipPlugin::GlobalEvent event2;
727 event2.seqId = 0;
728 event2.deviceId = "test_device_id";
729 event2.user = 1;
730 EXPECT_TRUE(event1 == event2);
731 }
732
733 /**
734 * @tc.name: ClipPlugin004
735 * @tc.desc: API_EXPORT: ClipPlugin
736 * @tc.type: FUNC
737 * @tc.require:
738 * @tc.author:
739 */
740 HWTEST_F(PasteDataTest, ClipPlugin004, TestSize.Level0)
741 {
742 ClipPlugin::GlobalEvent event1;
743 event1.seqId = 0;
744 event1.deviceId = "test_device_id";
745 event1.user = 0;
746 ClipPlugin::GlobalEvent event2;
747 event2.seqId = 0;
748 event2.deviceId = "test_device_id1";
749 event2.user = 1;
750 EXPECT_FALSE(event1 == event2);
751 }
752
753 /**
754 * @tc.name: ClipPlugin005
755 * @tc.desc: API_EXPORT: ClipPlugin
756 * @tc.type: FUNC
757 * @tc.require:
758 * @tc.author:
759 */
760 HWTEST_F(PasteDataTest, ClipPlugin005, TestSize.Level0)
761 {
762 ClipPlugin::GlobalEvent event1;
763 event1.seqId = 0;
764 event1.deviceId = "test_device_id";
765 event1.user = 0;
766 ClipPlugin::GlobalEvent event2;
767 event2.seqId = 1;
768 event2.deviceId = "test_device_id";
769 event2.user = 1;
770 EXPECT_FALSE(event1 == event2);
771 }
772
773 /**
774 * @tc.name: PasteDataOperator001
775 * @tc.desc: PasteData: operator
776 * @tc.type: FUNC
777 * @tc.require:
778 * @tc.author:
779 */
780 HWTEST_F(PasteDataTest, PasteDataOperator001, TestSize.Level0)
781 {
782 PasteData data1;
783 PasteDataRecord::Builder builder1(MIMETYPE_TEXT_URI);
784 std::string uriStr1 = FILE_URI;
785 auto uri1 = std::make_shared<OHOS::Uri>(uriStr1);
786 builder1.SetUri(uri1);
787 auto record1 = builder1.Build();
788 data1.AddRecord(record1);
789 std::string bundleName1 = "com.example.myapplication";
790 data1.SetOrginAuthority(bundleName1);
791 PasteData data2;
792 PasteDataRecord::Builder builder2(MIMETYPE_TEXT_URI);
793 std::string uriStr2 = FILE_URI;
794 auto uri2 = std::make_shared<OHOS::Uri>(uriStr2);
795 builder2.SetUri(uri2);
796 auto record2 = builder2.Build();
797 data2.AddRecord(record2);
798 std::string bundleName2 = "com.example.myapplication";
799 data2.SetOrginAuthority(bundleName2);
800 ASSERT_TRUE(data1.GetBundleName() == data2.GetBundleName());
801 }
802
803 /**
804 * @tc.name: GetShareOption001
805 * @tc.desc: GetShareOption call
806 * @tc.desc:
807 * @tc.type: FUNC
808 * @tc.require:
809 * @tc.author:
810 */
811 HWTEST_F(PasteDataTest, GetShareOption001, TestSize.Level0)
812 {
813 std::string plainText = "plain text";
814 auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
815 ASSERT_TRUE(pasteData != nullptr);
816 ShareOption option = InApp;
817 pasteData->SetShareOption(option);
818 auto result = pasteData->GetShareOption();
819 ASSERT_TRUE(result == InApp);
820 }
821
822 /**
823 * @tc.name: AddKvRecord001
824 * @tc.desc: AddKvRecord call
825 * @tc.desc:
826 * @tc.type: FUNC
827 * @tc.require:
828 * @tc.author:
829 */
830 HWTEST_F(PasteDataTest, AddKvRecord001, TestSize.Level0)
831 {
832 PasteData data;
833 std::vector<uint8_t> arrayBuffer(46);
834 arrayBuffer = { 2, 7, 6, 8, 9 };
835 std::string mimeType = "image/jpg";
836 data.AddKvRecord(mimeType, arrayBuffer);
837 ASSERT_TRUE(data.GetRecordCount() > 0);
838 }
839
840 /**
841 * @tc.name: GetProperty001
842 * @tc.desc: GetProperty call
843 * @tc.desc:
844 * @tc.type: FUNC
845 * @tc.require:
846 * @tc.author:
847 */
848 HWTEST_F(PasteDataTest, GetProperty001, TestSize.Level0)
849 {
850 std::string plainText = "plain text";
851 auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
852 ASSERT_TRUE(pasteData != nullptr);
853 PasteDataProperty property = pasteData->GetProperty();
854 ASSERT_TRUE(property.tokenId == 0);
855 }
856
857 /**
858 * @tc.name: SetProperty001
859 * @tc.desc:
860 * @tc.type: FUNC
861 * @tc.require:
862 * @tc.author:
863 */
864 HWTEST_F(PasteDataTest, SetProperty001, TestSize.Level0)
865 {
866 std::string plainText = "plain text";
867 auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
868 ASSERT_TRUE(pasteData != nullptr);
869 PasteDataProperty property;
870 property.tokenId = 1;
871 pasteData->SetProperty(property);
872 PasteDataProperty pasteDataProperty = pasteData->GetProperty();
873 ASSERT_TRUE(pasteDataProperty.tokenId == 1);
874 }
875
876 /**
877 * @tc.name: SetShareOption001
878 * @tc.desc: SetShareOption call
879 * @tc.desc:
880 * @tc.type: FUNC
881 * @tc.require:
882 * @tc.author:
883 */
884 HWTEST_F(PasteDataTest, SetShareOption001, TestSize.Level0)
885 {
886 std::string plainText = "plain text";
887 auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
888 ASSERT_TRUE(pasteData != nullptr);
889 ShareOption option = LocalDevice;
890 pasteData->SetShareOption(option);
891 auto result = pasteData->GetShareOption();
892 ASSERT_TRUE(result == LocalDevice);
893 }
894
895 /**
896 * @tc.name: SetTokenId001
897 * @tc.desc: SetTokenId call
898 * @tc.desc:
899 * @tc.type: FUNC
900 * @tc.require:
901 * @tc.author:
902 */
903 HWTEST_F(PasteDataTest, SetTokenId001, TestSize.Level0)
904 {
905 std::string plainText = "plain text";
906 auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
907 ASSERT_TRUE(pasteData != nullptr);
908 uint32_t tokenId = 1;
909 pasteData->SetTokenId(tokenId);
910 auto result = pasteData->GetTokenId();
911 ASSERT_TRUE(result == 1);
912 }
913
914 /**
915 * @tc.name: IsDraggedData001
916 * @tc.desc: IsDraggedData call
917 * @tc.desc:
918 * @tc.type: FUNC
919 * @tc.require:
920 * @tc.author:
921 */
922 HWTEST_F(PasteDataTest, IsDraggedData001, TestSize.Level0)
923 {
924 std::string plainText = "plain text";
925 auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
926 ASSERT_TRUE(pasteData != nullptr);
927 bool isDraggedData = false;
928 pasteData->SetDraggedDataFlag(isDraggedData);
929 auto result = pasteData->IsDraggedData();
930 ASSERT_FALSE(result);
931 }
932
933 /**
934 * @tc.name: SetDraggedDataFlag001
935 * @tc.desc: SetDraggedDataFlag call
936 * @tc.desc:
937 * @tc.type: FUNC
938 * @tc.require:
939 * @tc.author:
940 */
941 HWTEST_F(PasteDataTest, SetDraggedDataFlag001, TestSize.Level0)
942 {
943 std::string plainText = "plain text";
944 auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
945 ASSERT_TRUE(pasteData != nullptr);
946 bool isDraggedData = true;
947 pasteData->SetDraggedDataFlag(isDraggedData);
948 auto result = pasteData->IsDraggedData();
949 ASSERT_TRUE(result);
950 }
951
952 /**
953 * @tc.name: SetScreenStatus
954 * @tc.desc:
955 * @tc.type: FUNC
956 * @tc.require:
957 * @tc.author:
958 */
959 HWTEST_F(PasteDataTest, SetScreenStatus, TestSize.Level0)
960 {
961 std::string plainText = "plain text";
962 auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
963 ScreenEvent event = ScreenEvent::ScreenLocked;
964 pasteData->SetScreenStatus(event);
965 ScreenEvent ret = pasteData->GetScreenStatus();
966 ASSERT_TRUE(ret == ScreenEvent::ScreenLocked);
967 }
968
969 /**
970 * @tc.name: GetMimeTypes
971 * @tc.desc: PasteData GetMimeTypes
972 * @tc.type: FUNC
973 * @tc.require:
974 * @tc.author:
975 */
976 HWTEST_F(PasteDataTest, GetMimeTypes, TestSize.Level0)
977 {
978 PasteData data;
979 PasteDataRecord::Builder builder(MIMETYPE_TEXT_URI);
980 std::string uriStr = FILE_URI;
981 auto uri = std::make_shared<OHOS::Uri>(uriStr);
982 auto record = builder.SetUri(uri).Build();
983 data.AddRecord(*record);
984
985 PasteDataRecord::Builder builder1(MIMETYPE_TEXT_PLAIN);
986 std::string plainText = "plain text";
987 auto text = std::make_shared<std::string>(plainText);
988 auto record1 = builder1.SetPlainText(text).Build();
989 data.AddRecord(*record1);
990
991 auto mimeType = data.GetMimeTypes();
992 EXPECT_TRUE((strcmp(MIMETYPE_TEXT_PLAIN, mimeType.at(0).c_str()) == 0 &&
993 strcmp(MIMETYPE_TEXT_URI, mimeType.at(1).c_str()) == 0) ||
994 (strcmp(MIMETYPE_TEXT_PLAIN, mimeType.at(1).c_str()) == 0 &&
995 strcmp(MIMETYPE_TEXT_URI, mimeType.at(0).c_str()) == 0));
996 }
997 } // namespace OHOS::MiscServices
998