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