• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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: GetRealPathFailed001
212 * @tc.desc: GetRealPath Failed(realpath(inOriPath.c_str(), realPath) == nullptr)
213 * @tc.type: FUNC
214 * @tc.require: issuesI5Y6PO
215 * @tc.author: chenyu
216 */
217 HWTEST_F(PasteDataTest, GetRealPathFailed001, TestSize.Level0)
218 {
219     std::string uriStr = "/data/storage/100/haps/caches/xxx.txt";
220     PasteUriHandler pasteHandler;
221     auto ret = pasteHandler.ToFd(uriStr, true);
222     EXPECT_EQ(ret, INVALID_FD);
223 }
224 
225 /**
226 * @tc.name: GetRealPathFailed002
227 * @tc.desc: GetRealPath Failed(inOriPath.size() > PATH_MAX)
228 * @tc.type: FUNC
229 * @tc.require: issuesI5Y6PO
230 * @tc.author: chenyu
231 */
232 HWTEST_F(PasteDataTest, GetRealPathFailed002, TestSize.Level0)
233 {
234     std::string uriStr(PATH_MAX + 2, '*');
235     PasteUriHandler pasteHandler;
236     auto ret = pasteHandler.ToFd(uriStr, true);
237     EXPECT_EQ(ret, INVALID_FD);
238 }
239 
240 /**
241 * @tc.name: MaxLength001
242 * @tc.desc: PasteDataRecord: maxLength NewHtmlRecord
243 * @tc.type: FUNC
244 * @tc.require:
245 * @tc.author:
246 */
247 HWTEST_F(PasteDataTest, MaxLength001, TestSize.Level0)
248 {
249     int maxLength = 20 * 1024 * 1024;
250     std::string res = "hello";
251     std::string temp = "world";
252     for (int i = 0; i < maxLength; i++)
253     {
254         res += temp;
255     }
256     std::string htmlText = "<div class='disabled'>" + res + "</div>";
257     auto record = PasteboardClient::GetInstance()->CreateHtmlTextRecord(htmlText);
258     ASSERT_TRUE(record == nullptr);
259 }
260 
261 /**
262 * @tc.name: MaxLength002
263 * @tc.desc: PasteDataRecord: maxLength NewPlaintTextRecord
264 * @tc.type: FUNC
265 * @tc.require:
266 * @tc.author:
267 */
268 HWTEST_F(PasteDataTest, MaxLength002, TestSize.Level0)
269 {
270     int maxLength = 20 * 1024 * 1024;
271     std::string plainText = "hello";
272     std::string temp = "world";
273     for (int i = 0; i < maxLength; i++)
274     {
275         plainText += temp;
276     }
277     auto record = PasteboardClient::GetInstance()->CreatePlainTextRecord(plainText);
278     ASSERT_TRUE(record == nullptr);
279 }
280 
281 /**
282 * @tc.name: ConvertToText001
283 * @tc.desc: PasteDataRecord: ConvertToText htmlText
284 * @tc.type: FUNC
285 * @tc.require: AR000HEECD
286 * @tc.author: chenyu
287 */
288 HWTEST_F(PasteDataTest, ConvertToText001, TestSize.Level0)
289 {
290     std::string htmlText = "<div class='disabled item tip user-programs'>";
291     auto record = PasteboardClient::GetInstance()->CreateHtmlTextRecord(htmlText);
292     ASSERT_TRUE(record != nullptr);
293     auto text = record->ConvertToText();
294     EXPECT_EQ(text, htmlText);
295 }
296 
297 /**
298 * @tc.name: ConvertToText002
299 * @tc.desc: PasteDataRecord: ConvertToText plainText
300 * @tc.type: FUNC
301 * @tc.require:
302 * @tc.author:
303 */
304 HWTEST_F(PasteDataTest, ConvertToText002, TestSize.Level0)
305 {
306     std::string plainText = "paste record test";
307     auto record = PasteboardClient::GetInstance()->CreatePlainTextRecord(plainText);
308     ASSERT_TRUE(record != nullptr);
309     auto text = record->ConvertToText();
310     EXPECT_EQ(text, plainText);
311 }
312 
313 /**
314 * @tc.name: ConvertToText003
315 * @tc.desc: PasteDataRecord: ConvertToText uri
316 * @tc.type: FUNC
317 * @tc.require:
318 * @tc.author:
319 */
320 HWTEST_F(PasteDataTest, ConvertToText003, TestSize.Level0)
321 {
322     OHOS::Uri uri("uri");
323     auto record = PasteboardClient::GetInstance()->CreateUriRecord(uri);
324     ASSERT_TRUE(record != nullptr);
325     auto text = record->ConvertToText();
326     EXPECT_EQ(text, uri.ToString());
327 }
328 
329 /**
330 * @tc.name: ConvertToText004
331 * @tc.desc: PasteDataRecord: ConvertToText uri
332 * @tc.type: FUNC
333 * @tc.require:
334 * @tc.author:
335 */
336 HWTEST_F(PasteDataTest, ConvertToText004, TestSize.Level0)
337 {
338     uint32_t color[100] = { 3, 7, 9, 9, 7, 6 };
339     InitializationOptions opts = { { 5, 7 }, PixelFormat::ARGB_8888 };
340     std::unique_ptr<PixelMap> pixelMap = PixelMap::Create(color, 100, opts);
341     std::shared_ptr<PixelMap> pixelMapIn = move(pixelMap);
342     auto record = PasteboardClient::GetInstance()->CreatePixelMapRecord(pixelMapIn);
343     ASSERT_TRUE(record != nullptr);
344     auto text = record->ConvertToText();
345     EXPECT_EQ(text, "");
346 }
347 
348 /**
349 * @tc.name: GetPasteDataMsg001
350 * @tc.desc: PasteData: GetPrimaryMimeType is nullptr and so on
351 * @tc.type: FUNC
352 * @tc.require:
353 * @tc.author:
354 */
355 HWTEST_F(PasteDataTest, GetPasteDataMsg001, TestSize.Level0)
356 {
357     std::string plainText1 = "helloWorld";
358     auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText1);
359     ASSERT_TRUE(pasteData != nullptr);
360     auto newPrimaryPixelMap = pasteData->GetPrimaryPixelMap();
361     ASSERT_TRUE(newPrimaryPixelMap == nullptr);
362     auto newPrimaryMimeType = pasteData->GetPrimaryMimeType();
363     ASSERT_TRUE(newPrimaryMimeType != nullptr);
364     auto newPasteData = std::make_shared<PasteData>();
365     auto newPrimaryMimeType2 = newPasteData->GetPrimaryMimeType();
366     ASSERT_TRUE(newPrimaryMimeType2 == nullptr);
367     std::string plainText2 = "plain text";
368     auto record = PasteboardClient::GetInstance()->CreatePlainTextRecord(plainText2);
369     ASSERT_TRUE(record != nullptr);
370     ASSERT_FALSE(pasteData->ReplaceRecordAt(1000, record));
371 }
372 
373 /**
374 * @tc.name: GetPasteDataMsg002
375 * @tc.desc: PasteData: GetPrimaryWant is nullptr and so on
376 * @tc.type: FUNC
377 * @tc.require:
378 * @tc.author:
379 */
380 HWTEST_F(PasteDataTest, GetPasteDataMsg002, TestSize.Level0)
381 {
382     uint32_t color[100] = { 3, 7, 9, 9, 7, 6 };
383     InitializationOptions opts = { { 5, 7 }, PixelFormat::ARGB_8888 };
384     std::unique_ptr<PixelMap> pixelMap = PixelMap::Create(color, sizeof(color) / sizeof(color[0]), opts);
385     std::shared_ptr<PixelMap> pixelMapIn = move(pixelMap);
386     auto newPasteData = PasteboardClient::GetInstance()->CreatePixelMapData(pixelMapIn);
387     ASSERT_TRUE(newPasteData != nullptr);
388     auto pixMap = newPasteData->GetPrimaryPixelMap();
389     ASSERT_TRUE(pixMap != nullptr);
390     auto primaryWant = newPasteData->GetPrimaryWant();
391     ASSERT_TRUE(primaryWant == nullptr);
392     auto primaryText = newPasteData->GetPrimaryText();
393     ASSERT_TRUE(primaryText == nullptr);
394     auto primaryUri = newPasteData->GetPrimaryUri();
395     ASSERT_TRUE(primaryUri == nullptr);
396     auto record = newPasteData->GetRecordAt(1);
397     ASSERT_TRUE(record == nullptr);
398     auto res1 =  newPasteData->RemoveRecordAt(1);
399     ASSERT_FALSE(res1);
400     std::string mimeType = "text/plain";
401     ASSERT_FALSE(newPasteData->HasMimeType(mimeType));
402 }
403 
404 /**
405 * @tc.name: ShareOptionToString001
406 * @tc.desc: PasteData: ShareOptionToString
407 * @tc.type: FUNC
408 * @tc.require:
409 * @tc.author:
410 */
411 HWTEST_F(PasteDataTest, ShareOptionToString001, TestSize.Level0)
412 {
413     std::string shareOption1;
414     PasteData::ShareOptionToString(ShareOption::InApp, shareOption1);
415     ASSERT_TRUE(shareOption1 == "InAPP");
416     std::string shareOption2;
417     PasteData::ShareOptionToString(ShareOption::LocalDevice, shareOption2);
418     ASSERT_TRUE(shareOption2 == "LocalDevice");
419     std::string shareOption3;
420     PasteData::ShareOptionToString(ShareOption::CrossDevice, shareOption3);
421     ASSERT_TRUE(shareOption3 == "CrossDevice");
422 }
423 
424 /**
425 * @tc.name: SetInvalid001
426 * @tc.desc: PasteData: SetInvalid001
427 * @tc.type: FUNC
428 * @tc.require:
429 * @tc.author:
430 */
431 HWTEST_F(PasteDataTest, SetInvalid001, TestSize.Level0)
432 {
433     bool result = true;
434     std::shared_ptr<PasteData> pasteData = std::make_shared<PasteData>();
435     pasteData->SetInvalid();
436     result = pasteData->IsValid();
437     ASSERT_FALSE(result);
438 }
439 
440 /**
441 * @tc.name: SetLocalOnly001
442 * @tc.desc: PasteData: SetLocalOnly
443 * @tc.type: FUNC
444 * @tc.require:
445 * @tc.author:
446 */
447 HWTEST_F(PasteDataTest, SetLocalOnly001, TestSize.Level0)
448 {
449     bool result = false;
450     std::shared_ptr<PasteData> pasteData = std::make_shared<PasteData>();
451     pasteData->SetLocalOnly(true);
452     result = pasteData->GetLocalOnly();
453     ASSERT_TRUE(result);
454 }
455 
456 /**
457 * @tc.name: SetAddition001
458 * @tc.desc: PasteData: SetAddition
459 * @tc.type: FUNC
460 * @tc.require:
461 * @tc.author:
462 */
463 HWTEST_F(PasteDataTest, SetAddition001, TestSize.Level0)
464 {
465     std::string plainText = "plain text";
466     auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
467     size_t fileSize = 0;
468     pasteData->SetAddition(PasteData::REMOTE_FILE_SIZE, AAFwk::Integer::Box(fileSize));
469     AAFwk::WantParams additions;
470     pasteData->SetAdditions(additions);
471     ASSERT_TRUE(pasteData != nullptr);
472 }
473 
474 /**
475 * @tc.name: SetRemote001
476 * @tc.desc: PasteData: SetRemote
477 * @tc.type: FUNC
478 * @tc.require:
479 * @tc.author:
480 */
481 HWTEST_F(PasteDataTest, SetRemote001, TestSize.Level0)
482 {
483     bool isRemote = false;
484     std::string plainText = "plain text";
485     auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
486     isRemote = true;
487     pasteData->SetRemote(isRemote);
488     bool result = pasteData->IsRemote();
489     ASSERT_TRUE(result);
490 }
491 
492 /**
493 * @tc.name: SetOrginAuthority001
494 * @tc.desc: PasteData: SetOrginAuthority
495 * @tc.type: FUNC
496 * @tc.require:
497 * @tc.author:
498 */
499 HWTEST_F(PasteDataTest, SetOrginAuthority001, TestSize.Level0)
500 {
501     std::string plainText = "plain text";
502     std::string bundleName = "com.example.myapplication";
503     auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
504     pasteData->SetBundleName(bundleName);
505     pasteData->SetOrginAuthority(bundleName);
506     std::string getBundleName = pasteData->GetBundleName();
507     std::string getOrginAuthority = pasteData->GetOrginAuthority();
508     ASSERT_TRUE(getBundleName == bundleName);
509     ASSERT_TRUE(getOrginAuthority == bundleName);
510     std::string time = "2023-08-09";
511     pasteData->SetTime(time);
512     std::string getTime = pasteData->GetTime();
513     ASSERT_TRUE(getTime == time);
514 }
515 
516 /**
517 * @tc.name: GetConvertUri001
518 * @tc.desc: PasteDataRecord: GetConvertUri
519 * @tc.type: FUNC
520 * @tc.require:
521 * @tc.author:
522 */
523 HWTEST_F(PasteDataTest, GetConvertUri001, TestSize.Level0)
524 {
525     std::vector<uint8_t> arrayBuffer(46);
526     arrayBuffer = { 2, 7, 6, 8, 9 };
527     std::string mimeType = "image/jpg";
528     auto pasteDataRecord = PasteboardClient::GetInstance()->CreateKvRecord(mimeType, arrayBuffer);
529     ASSERT_TRUE(pasteDataRecord != nullptr);
530     std::string convertUri_ = "/mnt/hmdfs/";
531     pasteDataRecord->SetConvertUri(convertUri_);
532     std::string result = pasteDataRecord->GetConvertUri();
533     ASSERT_TRUE(result == convertUri_);
534     std::string newUriStr = "/mnt/hmdfs/test";
535     pasteDataRecord->SetUri(std::make_shared<OHOS::Uri>(newUriStr));
536     std::shared_ptr<Uri> uri = pasteDataRecord->GetUri();
537     ASSERT_TRUE(uri != nullptr);
538     std::shared_ptr<Uri> getOriginUri = pasteDataRecord->GetOrginUri();
539     ASSERT_TRUE(getOriginUri != nullptr);
540 }
541 
542 /**
543 * @tc.name: GetConvertUri002
544 * @tc.desc: PasteDataRecord: GetConvertUri
545 * @tc.type: FUNC
546 * @tc.require:
547 * @tc.author:
548 */
549 HWTEST_F(PasteDataTest, GetConvertUri002, TestSize.Level0)
550 {
551     std::vector<uint8_t> arrayBuffer(46);
552     arrayBuffer = { 2, 7, 6, 8, 9 };
553     std::string mimeType = "image/jpg";
554     auto pasteDataRecord = PasteboardClient::GetInstance()->CreateKvRecord(mimeType, arrayBuffer);
555     ASSERT_TRUE(pasteDataRecord != nullptr);
556     std::string convertUri_ = "";
557     pasteDataRecord->SetConvertUri(convertUri_);
558     std::string result = pasteDataRecord->GetConvertUri();
559     ASSERT_TRUE(result == convertUri_);
560     pasteDataRecord->ReplaceShareUri(200);
561     std::string result2 = pasteDataRecord->GetConvertUri();
562     ASSERT_TRUE(result2 == convertUri_);
563 }
564 
565 /**
566 * @tc.name: HasGrantUriPermission001
567 * @tc.desc: PasteDataRecord: HasGrantUriPermission
568 * @tc.type: FUNC
569 * @tc.require:
570 * @tc.author:
571 */
572 HWTEST_F(PasteDataTest, HasGrantUriPermission001, TestSize.Level0)
573 {
574     std::vector<uint8_t> arrayBuffer(46);
575     arrayBuffer = { 1, 2, 6, 8, 9 };
576     std::string mimeType = "image/jpg";
577     auto pasteDataRecord = PasteboardClient::GetInstance()->CreateKvRecord(mimeType, arrayBuffer);
578     ASSERT_TRUE(pasteDataRecord != nullptr);
579     pasteDataRecord->SetGrantUriPermission(true);
580     auto hasGrantUriPermission_ = pasteDataRecord->HasGrantUriPermission();
581     ASSERT_TRUE(hasGrantUriPermission_);
582 }
583 
584 /**
585 * @tc.name: LoadSystemAbilityFail001
586 * @tc.desc: PasteDataRecord: LoadSystemAbilityFail
587 * @tc.type: FUNC
588 * @tc.require:
589 * @tc.author:
590 */
591 HWTEST_F(PasteDataTest, LoadSystemAbilityFail001, TestSize.Level0)
592 {
593     std::vector<uint8_t> arrayBuffer(46);
594     std::string mimeType = "image/jpg";
595     arrayBuffer = { 1, 2, 3, 4, 6 };
596     PasteboardClient::GetInstance()->LoadSystemAbilityFail();
597     auto pasteDataRecord = PasteboardClient::GetInstance()->CreateKvRecord(mimeType, arrayBuffer);
598     ASSERT_TRUE(pasteDataRecord != nullptr);
599 }
600 
601 /**
602 * @tc.name: LoadSystemAbilitySuccess001
603 * @tc.desc: PasteDataRecord: LoadSystemAbilitySuccess
604 * @tc.type: FUNC
605 * @tc.require:
606 * @tc.author:
607 */
608 HWTEST_F(PasteDataTest, LoadSystemAbilitySuccess001, TestSize.Level0)
609 {
610     std::vector<uint8_t> arrayBuffer(46);
611     std::string mimeType = "image/jpg";
612     arrayBuffer = { 1, 2, 3, 4, 6 };
613     sptr<IRemoteObject> remoteObject = nullptr;
614     PasteboardClient::GetInstance()->LoadSystemAbilitySuccess(remoteObject);
615     auto pasteDataRecord = PasteboardClient::GetInstance()->CreateKvRecord(mimeType, arrayBuffer);
616     ASSERT_TRUE(pasteDataRecord != nullptr);
617 }
618 
619 /**
620 * @tc.name: SetInterval001
621 * @tc.desc: BlockObject: SetInterval
622 * @tc.type: FUNC
623 * @tc.require:
624 * @tc.author:
625 */
626 HWTEST_F(PasteDataTest, SetInterval001, TestSize.Level0)
627 {
628     uint32_t POPUP_INTERVAL = 1;
629     auto block = std::make_shared<BlockObject<std::shared_ptr<PasteData>>>(POPUP_INTERVAL);
630     std::shared_ptr<PasteData> pasteData = std::make_shared<PasteData>();
631     block->SetValue(pasteData);
632     block->SetInterval(POPUP_INTERVAL);
633     auto value = block->GetValue();
634     EXPECT_TRUE(value != nullptr);
635 }
636 
637 /**
638 * @tc.name: ClipPlugin001
639 * @tc.desc: API_EXPORT: ClipPlugin
640 * @tc.type: FUNC
641 * @tc.require:
642 * @tc.author:
643 */
644 HWTEST_F(PasteDataTest, ClipPlugin001, TestSize.Level0)
645 {
646     std::string PLUGIN_NAME_VAL = "distributed_clip";
__anon50b2085f0102(ClipPlugin *plugin) 647     auto release = [&PLUGIN_NAME_VAL, this](ClipPlugin *plugin) {
648         ClipPlugin::DestroyPlugin(PLUGIN_NAME_VAL, plugin);
649     };
650     auto clipPlugin_ = std::shared_ptr<ClipPlugin>(ClipPlugin::CreatePlugin(PLUGIN_NAME_VAL), release);
651     ClipPlugin::Factory *factory = nullptr;
652     auto result = ClipPlugin::RegCreator(PLUGIN_NAME_VAL, factory);
653     EXPECT_TRUE(result);
654     auto userId = 10000;
655     auto events1 = clipPlugin_->GetTopEvents(1, userId);
656     EXPECT_TRUE(events1.size() == 0);
657     auto events2 = clipPlugin_->GetTopEvents(1);
658     EXPECT_TRUE(events2.size() == 0);
659     clipPlugin_->Clear();
660     clipPlugin_->Clear(userId);
661 }
662 
663 /**
664 * @tc.name: ClipPlugin002
665 * @tc.desc: API_EXPORT: ClipPlugin
666 * @tc.type: FUNC
667 * @tc.require:
668 * @tc.author:
669 */
670 HWTEST_F(PasteDataTest, ClipPlugin002, TestSize.Level0)
671 {
672     std::string PLUGIN_NAME_VAL = "distributed_clip";
__anon50b2085f0202(ClipPlugin *plugin) 673     auto release = [&PLUGIN_NAME_VAL, this](ClipPlugin *plugin) {
674         ClipPlugin::DestroyPlugin(PLUGIN_NAME_VAL, plugin);
675     };
676     auto clipPlugin_ = std::shared_ptr<ClipPlugin>(ClipPlugin::CreatePlugin(PLUGIN_NAME_VAL), release);
677     ClipPlugin::Factory *factory = new ClipFactory();
678     auto result = ClipPlugin::RegCreator(PLUGIN_NAME_VAL, factory);
679     EXPECT_FALSE(result);
680     auto userId = 3701;
681     auto events1 = clipPlugin_->GetTopEvents(1, userId);
682     EXPECT_TRUE(events1.size() == 0);
683     clipPlugin_->Clear(userId);
684 }
685 
686 /**
687 * @tc.name: ClipPlugin003
688 * @tc.desc: API_EXPORT: ClipPlugin
689 * @tc.type: FUNC
690 * @tc.require:
691 * @tc.author:
692 */
693 HWTEST_F(PasteDataTest, ClipPlugin003, TestSize.Level0)
694 {
695     ClipPlugin::GlobalEvent event1;
696     event1.seqId = 0;
697     event1.deviceId = "test_device_id";
698     event1.user = 0;
699     ClipPlugin::GlobalEvent event2;
700     event2.seqId = 0;
701     event2.deviceId = "test_device_id";
702     event2.user = 1;
703     EXPECT_TRUE(event1 == event2);
704 }
705 
706 /**
707 * @tc.name: ClipPlugin004
708 * @tc.desc: API_EXPORT: ClipPlugin
709 * @tc.type: FUNC
710 * @tc.require:
711 * @tc.author:
712 */
713 HWTEST_F(PasteDataTest, ClipPlugin004, TestSize.Level0)
714 {
715     ClipPlugin::GlobalEvent event1;
716     event1.seqId = 0;
717     event1.deviceId = "test_device_id";
718     event1.user = 0;
719     ClipPlugin::GlobalEvent event2;
720     event2.seqId = 0;
721     event2.deviceId = "test_device_id1";
722     event2.user = 1;
723     EXPECT_FALSE(event1 == event2);
724 }
725 
726 /**
727 * @tc.name: ClipPlugin005
728 * @tc.desc: API_EXPORT: ClipPlugin
729 * @tc.type: FUNC
730 * @tc.require:
731 * @tc.author:
732 */
733 HWTEST_F(PasteDataTest, ClipPlugin005, TestSize.Level0)
734 {
735     ClipPlugin::GlobalEvent event1;
736     event1.seqId = 0;
737     event1.deviceId = "test_device_id";
738     event1.user = 0;
739     ClipPlugin::GlobalEvent event2;
740     event2.seqId = 1;
741     event2.deviceId = "test_device_id";
742     event2.user = 1;
743     EXPECT_FALSE(event1 == event2);
744 }
745 
746 /**
747 * @tc.name: PasteDataOperator001
748 * @tc.desc: PasteData: operator
749 * @tc.type: FUNC
750 * @tc.require:
751 * @tc.author:
752 */
753 HWTEST_F(PasteDataTest, PasteDataOperator001, TestSize.Level0)
754 {
755     PasteData data1;
756     PasteDataRecord::Builder builder1(MIMETYPE_TEXT_URI);
757     std::string uriStr1 = FILE_URI;
758     auto uri1 = std::make_shared<OHOS::Uri>(uriStr1);
759     builder1.SetUri(uri1);
760     auto record1 = builder1.Build();
761     data1.AddRecord(record1);
762     std::string bundleName1 = "com.example.myapplication";
763     data1.SetOrginAuthority(bundleName1);
764 
765     PasteData data2;
766     PasteDataRecord::Builder builder2(MIMETYPE_TEXT_URI);
767     std::string uriStr2 = FILE_URI;
768     auto uri2 = std::make_shared<OHOS::Uri>(uriStr2);
769     builder2.SetUri(uri2);
770     auto record2 = builder2.Build();
771     data2.AddRecord(record2);
772     std::string bundleName2 = "com.example.myapplication";
773     data2.SetOrginAuthority(bundleName2);
774 
775     ASSERT_TRUE(data1.GetBundleName() == data2.GetBundleName());
776 }
777 
778 /**
779 * @tc.name: GetShareOption001
780 * @tc.desc:
781 * @tc.type: FUNC
782 * @tc.require:
783 * @tc.author:
784 */
785 HWTEST_F(PasteDataTest, GetShareOption001, TestSize.Level0)
786 {
787     std::string plainText = "plain text";
788     auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
789     ASSERT_TRUE(pasteData != nullptr);
790     ShareOption option = InApp;
791     pasteData->SetShareOption(option);
792     auto result = pasteData->GetShareOption();
793     ASSERT_TRUE(result == InApp);
794 }
795 
796 /**
797 * @tc.name: AddKvRecord001
798 * @tc.desc:
799 * @tc.type: FUNC
800 * @tc.require:
801 * @tc.author:
802 */
803 HWTEST_F(PasteDataTest, AddKvRecord001, TestSize.Level0)
804 {
805     PasteData data;
806     std::vector<uint8_t> arrayBuffer(46);
807     arrayBuffer = { 2, 7, 6, 8, 9 };
808     std::string mimeType = "image/jpg";
809     data.AddKvRecord(mimeType, arrayBuffer);
810     ASSERT_TRUE(data.GetRecordCount() > 0);
811 }
812 
813 /**
814 * @tc.name: GetProperty001
815 * @tc.desc:
816 * @tc.type: FUNC
817 * @tc.require:
818 * @tc.author:
819 */
820 HWTEST_F(PasteDataTest, GetProperty001, TestSize.Level0)
821 {
822     std::string plainText = "plain text";
823     auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
824     ASSERT_TRUE(pasteData != nullptr);
825     PasteDataProperty property = pasteData->GetProperty();
826     ASSERT_TRUE(property.tokenId == 0);
827 }
828 
829 /**
830 * @tc.name: SetProperty001
831 * @tc.desc:
832 * @tc.type: FUNC
833 * @tc.require:
834 * @tc.author:
835 */
836 HWTEST_F(PasteDataTest, SetProperty001, TestSize.Level0)
837 {
838     std::string plainText = "plain text";
839     auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
840     ASSERT_TRUE(pasteData != nullptr);
841     PasteDataProperty property;
842     property.tokenId = 1;
843     pasteData->SetProperty(property);
844     PasteDataProperty pasteDataProperty = pasteData->GetProperty();
845     ASSERT_TRUE(pasteDataProperty.tokenId == 1);
846 }
847 
848 /**
849 * @tc.name: SetShareOption001
850 * @tc.desc:
851 * @tc.type: FUNC
852 * @tc.require:
853 * @tc.author:
854 */
855 HWTEST_F(PasteDataTest, SetShareOption001, TestSize.Level0)
856 {
857     std::string plainText = "plain text";
858     auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
859     ASSERT_TRUE(pasteData != nullptr);
860     ShareOption option = LocalDevice;
861     pasteData->SetShareOption(option);
862     auto result = pasteData->GetShareOption();
863     ASSERT_TRUE(result == LocalDevice);
864 }
865 
866 /**
867 * @tc.name: SetTokenId001
868 * @tc.desc:
869 * @tc.type: FUNC
870 * @tc.require:
871 * @tc.author:
872 */
873 HWTEST_F(PasteDataTest, SetTokenId001, TestSize.Level0)
874 {
875     std::string plainText = "plain text";
876     auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
877     ASSERT_TRUE(pasteData != nullptr);
878     uint32_t tokenId = 1;
879     pasteData->SetTokenId(tokenId);
880     auto result = pasteData->GetTokenId();
881     ASSERT_TRUE(result == 1);
882 }
883 
884 /**
885 * @tc.name: IsDraggedData001
886 * @tc.desc:
887 * @tc.type: FUNC
888 * @tc.require:
889 * @tc.author:
890 */
891 HWTEST_F(PasteDataTest, IsDraggedData001, TestSize.Level0)
892 {
893     std::string plainText = "plain text";
894     auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
895     ASSERT_TRUE(pasteData != nullptr);
896     bool isDraggedData = false;
897     pasteData->SetDraggedDataFlag(isDraggedData);
898     auto result = pasteData->IsDraggedData();
899     ASSERT_FALSE(result);
900 }
901 
902 /**
903 * @tc.name: SetDraggedDataFlag001
904 * @tc.desc:
905 * @tc.type: FUNC
906 * @tc.require:
907 * @tc.author:
908 */
909 HWTEST_F(PasteDataTest, SetDraggedDataFlag001, TestSize.Level0)
910 {
911     std::string plainText = "plain text";
912     auto pasteData = PasteboardClient::GetInstance()->CreatePlainTextData(plainText);
913     ASSERT_TRUE(pasteData != nullptr);
914     bool isDraggedData = true;
915     pasteData->SetDraggedDataFlag(isDraggedData);
916     auto result = pasteData->IsDraggedData();
917     ASSERT_TRUE(result);
918 }
919 } // namespace OHOS::MiscServices
920