1 /*
2 * Copyright (c) 2024 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include <gtest/gtest.h>
17
18 #include "application_defined_record.h"
19 #include "audio.h"
20 #include "folder.h"
21 #include "html.h"
22 #include "image.h"
23 #include "link.h"
24 #include "pasteboard_client.h"
25 #include "plain_text.h"
26 #include "system_defined_appitem.h"
27 #include "system_defined_form.h"
28 #include "system_defined_pixelmap.h"
29 #include "video.h"
30 namespace OHOS::MiscServices {
31 using namespace testing::ext;
32 using namespace testing;
33 using namespace OHOS::Media;
34 class PasteboardUnifiedDataTest : public testing::Test {
35 public:
36 static void SetUpTestCase(void);
37 static void TearDownTestCase(void);
38 void SetUp();
39 void TearDown();
40 UDMF::UnifiedData InitTextData();
41 UDMF::UnifiedData InitPlainData();
42 UDMF::UnifiedData InitHtmlData();
43 UDMF::UnifiedData InitWantData();
44 UDMF::UnifiedData InitLinkData();
45 UDMF::UnifiedData InitFileData();
46 UDMF::UnifiedData InitImageData();
47 UDMF::UnifiedData InitVideoData();
48 UDMF::UnifiedData InitAudioData();
49 UDMF::UnifiedData InitFolderData();
50 UDMF::UnifiedData InitSystemRecordData();
51 UDMF::UnifiedData InitSystemAppItemData();
52 UDMF::UnifiedData InitSysteFormData();
53 UDMF::UnifiedData InitSystemPixelMapData();
54 UDMF::UnifiedData InitAppDefinedData();
55
56 protected:
57 Details details_;
58 std::vector<uint8_t> rawData_;
59 std::string text_;
60 std::string extraText_;
61 std::string uri_;
62 };
63
SetUpTestCase(void)64 void PasteboardUnifiedDataTest::SetUpTestCase(void) {}
65
TearDownTestCase(void)66 void PasteboardUnifiedDataTest::TearDownTestCase(void) {}
67
SetUp(void)68 void PasteboardUnifiedDataTest::SetUp(void)
69 {
70 rawData_ = { 1, 2, 3, 4, 5, 6, 7, 8 };
71 details_.insert({ "keyString", "string_test" });
72 details_.insert({ "keyInt32", 1 });
73 details_.insert({ "keyBool", true });
74 details_.insert({ "KeyU8Array", rawData_ });
75 details_.insert({ "KeyDouble", 1.234 });
76 }
77
TearDown(void)78 void PasteboardUnifiedDataTest::TearDown(void) {}
79
InitTextData()80 UDMF::UnifiedData PasteboardUnifiedDataTest::InitTextData()
81 {
82 UDMF::UnifiedData data;
83 std::shared_ptr<UDMF::Text> textRecord = std::make_shared<UDMF::Text>();
84 textRecord->SetDetails(details_);
85 data.AddRecord(textRecord);
86 return data;
87 }
88
InitPlainData()89 UDMF::UnifiedData PasteboardUnifiedDataTest::InitPlainData()
90 {
91 text_ = "helloWorld_plainText";
92 extraText_ = "helloWorld_plainabstract";
93 UDMF::UnifiedData data;
94 std::shared_ptr<UDMF::PlainText> plainTextRecord = std::make_shared<UDMF::PlainText>(text_, extraText_);
95 plainTextRecord->SetDetails(details_);
96 data.AddRecord(plainTextRecord);
97 return data;
98 }
99
InitHtmlData()100 UDMF::UnifiedData PasteboardUnifiedDataTest::InitHtmlData()
101 {
102 text_ = "<div class='disable'>helloWorld</div>";
103 extraText_ = "helloWorld_plainabstract";
104 UDMF::UnifiedData data;
105 std::shared_ptr<UDMF::Html> htmlRecord = std::make_shared<UDMF::Html>(text_, extraText_);
106 htmlRecord->SetDetails(details_);
107 data.AddRecord(htmlRecord);
108 return data;
109 }
InitWantData()110 UDMF::UnifiedData PasteboardUnifiedDataTest::InitWantData()
111 {
112 using namespace OHOS::AAFwk;
113 std::shared_ptr<Want> want = std::make_shared<Want>();
114 std::string idKey = "id";
115 int32_t idValue = 123;
116 std::string deviceKey = "deviceId_key";
117 want->SetParam(idKey, idValue);
118 std::shared_ptr<UDMF::UnifiedRecord> wantRecord =
119 std::make_shared<UDMF::UnifiedRecord>(UDMF::OPENHARMONY_WANT, want);
120 UDMF::UnifiedData data;
121 data.AddRecord(wantRecord);
122 return data;
123 }
124
InitLinkData()125 UDMF::UnifiedData PasteboardUnifiedDataTest::InitLinkData()
126 {
127 text_ = "https://www.test.com";
128 extraText_ = "https://www.test.com/content";
129 UDMF::UnifiedData data;
130 std::shared_ptr<UDMF::Link> linkRecord = std::make_shared<UDMF::Link>(text_, extraText_);
131 linkRecord->SetDetails(details_);
132 data.AddRecord(linkRecord);
133 return data;
134 }
InitFileData()135 UDMF::UnifiedData PasteboardUnifiedDataTest::InitFileData()
136 {
137 UDMF::UnifiedData data;
138 auto typeStr = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::FILE);
139 uri_ = "file:/uri";
140 std::shared_ptr<UDMF::File> fileRecord = std::make_shared<UDMF::File>(uri_);
141 fileRecord->SetDetails(details_);
142 data.AddRecord(fileRecord);
143 return data;
144 }
InitImageData()145 UDMF::UnifiedData PasteboardUnifiedDataTest::InitImageData()
146 {
147 UDMF::UnifiedData data;
148 auto typeStr = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::IMAGE);
149 uri_ = "file:/image";
150 std::shared_ptr<UDMF::Image> imageRecord = std::make_shared<UDMF::Image>(uri_);
151 imageRecord->SetDetails(details_);
152 data.AddRecord(imageRecord);
153 return data;
154 }
InitVideoData()155 UDMF::UnifiedData PasteboardUnifiedDataTest::InitVideoData()
156 {
157 UDMF::UnifiedData data;
158 auto typeStr = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::VIDEO);
159 uri_ = "file:/Video";
160 std::shared_ptr<UDMF::Video> videoRecord = std::make_shared<UDMF::Video>(uri_);
161 videoRecord->SetDetails(details_);
162 data.AddRecord(videoRecord);
163
164 return data;
165 }
InitAudioData()166 UDMF::UnifiedData PasteboardUnifiedDataTest::InitAudioData()
167 {
168 UDMF::UnifiedData data;
169 auto typeStr = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::AUDIO);
170 uri_ = "file:/Audio";
171 std::shared_ptr<UDMF::Audio> audioRecord = std::make_shared<UDMF::Audio>(uri_);
172 audioRecord->SetDetails(details_);
173 data.AddRecord(audioRecord);
174 return data;
175 }
InitFolderData()176 UDMF::UnifiedData PasteboardUnifiedDataTest::InitFolderData()
177 {
178 UDMF::UnifiedData data;
179 auto typeStr = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::FOLDER);
180 uri_ = "file:/Folder";
181 std::shared_ptr<UDMF::Folder> folderRecord = std::make_shared<UDMF::Folder>(uri_);
182 folderRecord->SetDetails(details_);
183 data.AddRecord(folderRecord);
184 return data;
185 }
InitSystemRecordData()186 UDMF::UnifiedData PasteboardUnifiedDataTest::InitSystemRecordData()
187 {
188 UDMF::UnifiedData data;
189 std::shared_ptr<UDMF::SystemDefinedRecord> systemRecord = std::make_shared<UDMF::SystemDefinedRecord>();
190 systemRecord->SetDetails(details_);
191 data.AddRecord(systemRecord);
192 return data;
193 }
InitSystemAppItemData()194 UDMF::UnifiedData PasteboardUnifiedDataTest::InitSystemAppItemData()
195 {
196 UDMF::UnifiedData data;
197 std::shared_ptr<UDMF::SystemDefinedAppItem> systemDefinedAppItem1 = std::make_shared<UDMF::SystemDefinedAppItem>();
198 std::string appId = "appId";
199 std::string appIconId = "appIconId";
200 std::string appName = "appName";
201 std::string appLabelId = "appLabelId";
202 std::string bundleName = "bundleName";
203 std::string abilityName = "abilityName";
204 systemDefinedAppItem1->SetDetails(details_);
205 systemDefinedAppItem1->SetAppId(appId);
206 systemDefinedAppItem1->SetAppName(appName);
207 systemDefinedAppItem1->SetAppIconId(appIconId);
208 systemDefinedAppItem1->SetAppLabelId(appLabelId);
209 systemDefinedAppItem1->SetBundleName(bundleName);
210 systemDefinedAppItem1->SetAbilityName(abilityName);
211 systemDefinedAppItem1->SetType(UDMF::SYSTEM_DEFINED_APP_ITEM);
212 data.AddRecord(systemDefinedAppItem1);
213 return data;
214 }
InitSysteFormData()215 UDMF::UnifiedData PasteboardUnifiedDataTest::InitSysteFormData()
216 {
217 UDMF::UnifiedData data;
218 std::shared_ptr<UDMF::SystemDefinedForm> form = std::make_shared<UDMF::SystemDefinedForm>();
219 int32_t formId = 1;
220 std::string formName = "formName";
221 std::string module = "module";
222 std::string bundleName = "bundleName";
223 std::string abilityName = "abilityName";
224 form->SetDetails(details_);
225 form->SetFormId(formId);
226 form->SetFormName(formName);
227 form->SetAbilityName(abilityName);
228 form->SetBundleName(bundleName);
229 form->SetModule(module);
230 form->SetType(UDMF::SYSTEM_DEFINED_FORM);
231 data.AddRecord(form);
232 return data;
233 }
InitSystemPixelMapData()234 UDMF::UnifiedData PasteboardUnifiedDataTest::InitSystemPixelMapData()
235 {
236 uint32_t color[100] = { 3, 7, 9, 9, 7, 6 };
237 InitializationOptions opts = { { 5, 7 }, PixelFormat::ARGB_8888, PixelFormat::ARGB_8888 };
238 std::unique_ptr<PixelMap> pixelMap = PixelMap::Create(color, sizeof(color) / sizeof(color[0]), opts);
239 std::shared_ptr<PixelMap> pixelMapIn = move(pixelMap);
240 std::shared_ptr<UDMF::UnifiedRecord> pixelMapRecord =
241 std::make_shared<UDMF::SystemDefinedPixelMap>(UDMF::SYSTEM_DEFINED_PIXEL_MAP, pixelMapIn);
242 UDMF::UnifiedData data;
243 data.AddRecord(pixelMapRecord);
244 return data;
245 }
246
InitAppDefinedData()247 UDMF::UnifiedData PasteboardUnifiedDataTest::InitAppDefinedData()
248 {
249 UDMF::UnifiedData data;
250 std::shared_ptr<UDMF::ApplicationDefinedRecord> appRecord = std::make_shared<UDMF::ApplicationDefinedRecord>();
251 std::map<std::string, std::vector<uint8_t>> customData;
252 customData[UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::APPLICATION_DEFINED_RECORD)] = rawData_;
253 appRecord->SetApplicationDefinedType(UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::APPLICATION_DEFINED_RECORD));
254 appRecord->SetRawData(rawData_);
255 data.AddRecord(appRecord);
256 return data;
257 }
258
259 /**
260 * @tc.name: SetText001
261 * @tc.desc: Get the source of the data.
262 * @tc.type: FUNC
263 * @tc.require:
264 * @tc.author:
265 */
266 HWTEST_F(PasteboardUnifiedDataTest, SetText001, TestSize.Level0)
267 {
268 auto data = InitTextData();
269 PasteboardClient::GetInstance()->SetUnifiedData(data);
270 UDMF::UnifiedData newData;
271 PasteboardClient::GetInstance()->GetUnifiedData(newData);
272 ASSERT_EQ(1, newData.GetRecords().size());
273 auto newRecord = newData.GetRecordAt(0);
274 auto newType = newRecord->GetType();
275 ASSERT_EQ(newType, UDMF::TEXT);
276 auto newPlainRecord = static_cast<UDMF::Text*>(newRecord.get());
277 auto newDetails = newPlainRecord->GetDetails();
278 ASSERT_EQ(newDetails, details_);
279
280 PasteData pasteData;
281 PasteboardClient::GetInstance()->GetPasteData(pasteData);
282 ASSERT_EQ(1, pasteData.GetRecordCount());
283 auto record = pasteData.GetRecordAt(0);
284 auto type = record->GetMimeType();
285 ASSERT_EQ(type, UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::TEXT));
286 auto udType = record->GetUDType();
287 ASSERT_EQ(udType, UDMF::UDType::TEXT);
288 auto details1 = record->GetDetails();
289 ASSERT_EQ(*details1, details_);
290 }
291
292 /**
293 * @tc.name: SetPlainText001
294 * @tc.desc: Get the source of the data.
295 * @tc.type: FUNC
296 * @tc.require:
297 * @tc.author:
298 */
299 HWTEST_F(PasteboardUnifiedDataTest, SetPlainText001, TestSize.Level0)
300 {
301 auto data = InitPlainData();
302 PasteboardClient::GetInstance()->SetUnifiedData(data);
303 UDMF::UnifiedData newData;
304 PasteboardClient::GetInstance()->GetUnifiedData(newData);
305 ASSERT_EQ(1, newData.GetRecords().size());
306 auto newRecord = newData.GetRecordAt(0);
307 auto newType = newRecord->GetType();
308 ASSERT_EQ(newType, UDMF::PLAIN_TEXT);
309 auto newPlainRecord = static_cast<UDMF::PlainText*>(newRecord.get());
310 auto newPlainText = newPlainRecord->GetContent();
311 auto newAbstract = newPlainRecord->GetAbstract();
312 auto newDetails = newPlainRecord->GetDetails();
313 ASSERT_EQ(newPlainText, text_);
314 ASSERT_EQ(newAbstract, extraText_);
315 ASSERT_EQ(newDetails, details_);
316
317 PasteData pasteData;
318 PasteboardClient::GetInstance()->GetPasteData(pasteData);
319 ASSERT_EQ(1, pasteData.GetRecordCount());
320 auto record = pasteData.GetRecordAt(0);
321 auto type = record->GetMimeType();
322 ASSERT_EQ(type, MIMETYPE_TEXT_PLAIN);
323 auto udType = record->GetUDType();
324 ASSERT_EQ(udType, UDMF::UDType::PLAIN_TEXT);
325 auto udmfValue = record->GetUDMFValue();
326 ASSERT_NE(udmfValue, nullptr);
327 auto plainText = std::make_shared<UDMF::PlainText>(UDMF::PLAIN_TEXT, *udmfValue);
328 ASSERT_EQ(plainText->GetContent(), text_);
329 ASSERT_EQ(plainText->GetAbstract(), extraText_);
330 }
331
332 /**
333 * @tc.name: SetLink001
334 * @tc.desc: Get the source of the data.
335 * @tc.type: FUNC
336 * @tc.require:
337 * @tc.author:
338 */
339 HWTEST_F(PasteboardUnifiedDataTest, SetLink001, TestSize.Level0)
340 {
341 auto data = InitLinkData();
342 PasteboardClient::GetInstance()->SetUnifiedData(data);
343 UDMF::UnifiedData newData;
344 PasteboardClient::GetInstance()->GetUnifiedData(newData);
345 ASSERT_EQ(1, newData.GetRecords().size());
346 auto newRecord = newData.GetRecordAt(0);
347 auto newType = newRecord->GetType();
348 ASSERT_EQ(newType, UDMF::HYPERLINK);
349 auto newPlainRecord = static_cast<UDMF::Link*>(newRecord.get());
350 auto newUrl = newPlainRecord->GetUrl();
351 auto newDescription = newPlainRecord->GetDescription();
352 auto newDetails = newPlainRecord->GetDetails();
353 ASSERT_EQ(newUrl, text_);
354 ASSERT_EQ(newDescription, extraText_);
355 ASSERT_EQ(newDetails, details_);
356
357 PasteData pasteData;
358 PasteboardClient::GetInstance()->GetPasteData(pasteData);
359 ASSERT_EQ(1, pasteData.GetRecordCount());
360 auto record = pasteData.GetRecordAt(0);
361 auto type = record->GetMimeType();
362 ASSERT_EQ(type, MIMETYPE_TEXT_PLAIN);
363 auto udType = record->GetUDType();
364 ASSERT_EQ(udType, UDMF::UDType::HYPERLINK);
365 auto udmfValue = record->GetUDMFValue();
366 ASSERT_NE(udmfValue, nullptr);
367 auto link = std::make_shared<UDMF::Link>(UDMF::HYPERLINK, *udmfValue);
368 ASSERT_EQ(link->GetUrl(), text_);
369 ASSERT_EQ(link->GetDescription(), extraText_);
370 }
371
372 /**
373 * @tc.name: SetHtml001
374 * @tc.desc: Get the source of the data.
375 * @tc.type: FUNC
376 * @tc.require:
377 * @tc.author:
378 */
379 HWTEST_F(PasteboardUnifiedDataTest, SetHtml001, TestSize.Level0)
380 {
381 auto data = InitHtmlData();
382 PasteboardClient::GetInstance()->SetUnifiedData(data);
383 UDMF::UnifiedData newData;
384 PasteboardClient::GetInstance()->GetUnifiedData(newData);
385 ASSERT_EQ(1, newData.GetRecords().size());
386 auto newRecord = newData.GetRecordAt(0);
387 auto newType = newRecord->GetType();
388 ASSERT_EQ(newType, UDMF::HTML);
389 auto newPlainRecord = static_cast<UDMF::Html*>(newRecord.get());
390 auto newPlainText = newPlainRecord->GetHtmlContent();
391 auto newAbstract = newPlainRecord->GetPlainContent();
392 auto newDetails = newPlainRecord->GetDetails();
393 ASSERT_EQ(newPlainText, text_);
394 ASSERT_EQ(newAbstract, extraText_);
395 ASSERT_EQ(newDetails, details_);
396
397 PasteData pasteData;
398 PasteboardClient::GetInstance()->GetPasteData(pasteData);
399 ASSERT_EQ(1, pasteData.GetRecordCount());
400 auto record = pasteData.GetRecordAt(0);
401 auto type = record->GetMimeType();
402 ASSERT_EQ(type, MIMETYPE_TEXT_HTML);
403 auto udType = record->GetUDType();
404 ASSERT_EQ(udType, UDMF::UDType::HTML);
405 auto udmfValue = record->GetUDMFValue();
406 ASSERT_NE(udmfValue, nullptr);
407 auto html = std::make_shared<UDMF::Html>(UDMF::HTML, *udmfValue);
408 ASSERT_EQ(html->GetHtmlContent(), text_);
409 ASSERT_EQ(html->GetPlainContent(), extraText_);
410 }
411
412 /**
413 * @tc.name: SetWant001
414 * @tc.desc: Get the source of the data.
415 * @tc.type: FUNC
416 * @tc.require:
417 * @tc.author:
418 */
419 HWTEST_F(PasteboardUnifiedDataTest, SetWant001, TestSize.Level0)
420 {
421 auto data = InitWantData();
422 PasteboardClient::GetInstance()->SetUnifiedData(data);
423 UDMF::UnifiedData newData;
424 PasteboardClient::GetInstance()->GetUnifiedData(newData);
425 ASSERT_EQ(1, newData.GetRecords().size());
426 auto newRecord = newData.GetRecordAt(0);
427 auto newType = newRecord->GetType();
428 ASSERT_EQ(newType, UDMF::OPENHARMONY_WANT);
429 auto recordValue = newRecord->GetValue();
430 auto wantValue = std::get_if<std::shared_ptr<OHOS::AAFwk::Want>>(&recordValue);
431 ASSERT_NE(wantValue, nullptr);
432 int32_t idValue2 = (*(wantValue))->GetIntParam("id", 0);
433 ASSERT_EQ(idValue2, 123);
434
435 PasteData pasteData;
436 PasteboardClient::GetInstance()->GetPasteData(pasteData);
437 ASSERT_EQ(1, pasteData.GetRecordCount());
438 auto record = pasteData.GetRecordAt(0);
439 auto type = record->GetMimeType();
440 ASSERT_EQ(type, MIMETYPE_TEXT_WANT);
441 auto udType = record->GetUDType();
442 ASSERT_EQ(udType, int32_t(UDMF::UDType::OPENHARMONY_WANT));
443 auto want1 = record->GetWant();
444 int32_t idValue1 = want1->GetIntParam("id", 0);
445 ASSERT_EQ(idValue1, 123);
446 }
447
448 /**
449 * @tc.name: SetFile001
450 * @tc.desc: Get the source of the data.
451 * @tc.type: FUNC
452 * @tc.require:
453 * @tc.author:
454 */
455 HWTEST_F(PasteboardUnifiedDataTest, SetFile001, TestSize.Level0)
456 {
457 auto data = InitFileData();
458 PasteboardClient::GetInstance()->SetUnifiedData(data);
459
460 UDMF::UnifiedData newData;
461 PasteboardClient::GetInstance()->GetUnifiedData(newData);
462 ASSERT_EQ(1, newData.GetRecords().size());
463 auto newRecord = newData.GetRecordAt(0);
464 auto newType = newRecord->GetType();
465 ASSERT_EQ(newType, UDMF::FILE);
466 auto newFileRecord = static_cast<UDMF::File*>(newRecord.get());
467 auto newDetails = newFileRecord->GetDetails();
468 ASSERT_EQ(newDetails, details_);
469 auto uri2 = newFileRecord->GetUri();
470 ASSERT_EQ(uri2, uri_);
471
472 PasteData pasteData;
473 PasteboardClient::GetInstance()->GetPasteData(pasteData);
474 ASSERT_EQ(1, pasteData.GetRecordCount());
475 auto record = pasteData.GetRecordAt(0);
476 auto type = record->GetMimeType();
477 ASSERT_EQ(type, MIMETYPE_TEXT_URI);
478 auto udType = record->GetUDType();
479 ASSERT_EQ(udType, UDMF::UDType::FILE);
480 auto uri1 = record->GetUri()->ToString();
481 ASSERT_EQ(uri1, uri_);
482 auto details1 = record->GetDetails();
483 ASSERT_EQ(*details1, details_);
484 }
485
486 /**
487 * @tc.name: SetImage001
488 * @tc.desc: Get the source of the data.
489 * @tc.type: FUNC
490 * @tc.require:
491 * @tc.author:
492 */
493 HWTEST_F(PasteboardUnifiedDataTest, SetImage001, TestSize.Level0)
494 {
495 auto data = InitImageData();
496 PasteboardClient::GetInstance()->SetUnifiedData(data);
497 UDMF::UnifiedData newData;
498 PasteboardClient::GetInstance()->GetUnifiedData(newData);
499 ASSERT_EQ(1, newData.GetRecords().size());
500 auto newRecord = newData.GetRecordAt(0);
501 auto newType = newRecord->GetType();
502 ASSERT_EQ(newType, UDMF::IMAGE);
503 auto newImageRecord = static_cast<UDMF::Image*>(newRecord.get());
504 auto newDetails = newImageRecord->GetDetails();
505 ASSERT_EQ(newDetails, details_);
506 auto uri2 = newImageRecord->GetUri();
507 ASSERT_EQ(uri2, uri_);
508
509 PasteData pasteData;
510 PasteboardClient::GetInstance()->GetPasteData(pasteData);
511 ASSERT_EQ(1, pasteData.GetRecordCount());
512 auto record = pasteData.GetRecordAt(0);
513 auto type = record->GetMimeType();
514 ASSERT_EQ(type, MIMETYPE_TEXT_URI);
515 auto udType = record->GetUDType();
516 ASSERT_EQ(udType, UDMF::UDType::IMAGE);
517 auto uri1 = record->GetUri()->ToString();
518 ASSERT_EQ(uri1, uri_);
519 auto details1 = record->GetDetails();
520 ASSERT_EQ(*details1, details_);
521 }
522
523 /**
524 * @tc.name: SetAudio001
525 * @tc.desc: Get the source of the data.
526 * @tc.type: FUNC
527 * @tc.require:
528 * @tc.author:
529 */
530 HWTEST_F(PasteboardUnifiedDataTest, SetAudio001, TestSize.Level0)
531 {
532 auto data = InitAudioData();
533 PasteboardClient::GetInstance()->SetUnifiedData(data);
534 UDMF::UnifiedData newData;
535 PasteboardClient::GetInstance()->GetUnifiedData(newData);
536 ASSERT_EQ(1, newData.GetRecords().size());
537 auto newRecord = newData.GetRecordAt(0);
538 auto newType = newRecord->GetType();
539 ASSERT_EQ(newType, UDMF::AUDIO);
540 auto newAudioRecord = static_cast<UDMF::Audio*>(newRecord.get());
541 auto newDetails = newAudioRecord->GetDetails();
542 ASSERT_EQ(newDetails, details_);
543 auto uri2 = newAudioRecord->GetUri();
544 ASSERT_EQ(uri2, uri_);
545
546 PasteData pasteData;
547 PasteboardClient::GetInstance()->GetPasteData(pasteData);
548 ASSERT_EQ(1, pasteData.GetRecordCount());
549 auto record = pasteData.GetRecordAt(0);
550 auto type = record->GetMimeType();
551 ASSERT_EQ(type, MIMETYPE_TEXT_URI);
552 auto udType = record->GetUDType();
553 ASSERT_EQ(udType, UDMF::UDType::AUDIO);
554 auto uri1 = record->GetUri()->ToString();
555 ASSERT_EQ(uri1, uri_);
556 auto details1 = record->GetDetails();
557 ASSERT_EQ(*details1, details_);
558 }
559
560 /**
561 * @tc.name: SetVideo001
562 * @tc.desc: Get the source of the data.
563 * @tc.type: FUNC
564 * @tc.require:
565 * @tc.author:
566 */
567 HWTEST_F(PasteboardUnifiedDataTest, SetVideo001, TestSize.Level0)
568 {
569 auto data = InitVideoData();
570 PasteboardClient::GetInstance()->SetUnifiedData(data);
571 UDMF::UnifiedData newData;
572 PasteboardClient::GetInstance()->GetUnifiedData(newData);
573 ASSERT_EQ(1, newData.GetRecords().size());
574 auto newRecord = newData.GetRecordAt(0);
575 auto newType = newRecord->GetType();
576 ASSERT_EQ(newType, UDMF::VIDEO);
577 auto newVideoRecord = static_cast<UDMF::Video*>(newRecord.get());
578 auto newDetails = newVideoRecord->GetDetails();
579 ASSERT_EQ(newDetails, details_);
580 auto uri2 = newVideoRecord->GetUri();
581 ASSERT_EQ(uri2, uri_);
582
583 PasteData pasteData;
584 PasteboardClient::GetInstance()->GetPasteData(pasteData);
585 ASSERT_EQ(1, pasteData.GetRecordCount());
586 auto record = pasteData.GetRecordAt(0);
587 auto type = record->GetMimeType();
588 ASSERT_EQ(type, MIMETYPE_TEXT_URI);
589 auto udType = record->GetUDType();
590 ASSERT_EQ(udType, UDMF::UDType::VIDEO);
591 auto uri1 = record->GetUri()->ToString();
592 ASSERT_EQ(uri1, uri_);
593 auto details1 = record->GetDetails();
594 ASSERT_EQ(*details1, details_);
595 }
596
597 /**
598 * @tc.name: SetFolder001
599 * @tc.desc: Get the source of the data.
600 * @tc.type: FUNC
601 * @tc.require:
602 * @tc.author:
603 */
604 HWTEST_F(PasteboardUnifiedDataTest, SetFolder001, TestSize.Level0)
605 {
606 auto data = InitFolderData();
607 PasteboardClient::GetInstance()->SetUnifiedData(data);
608 UDMF::UnifiedData newData;
609 PasteboardClient::GetInstance()->GetUnifiedData(newData);
610 ASSERT_EQ(1, newData.GetRecords().size());
611 auto newRecord = newData.GetRecordAt(0);
612 auto newType = newRecord->GetType();
613 ASSERT_EQ(newType, UDMF::FOLDER);
614 auto newFolderRecord = static_cast<UDMF::Folder*>(newRecord.get());
615 auto newDetails = newFolderRecord->GetDetails();
616 ASSERT_EQ(newDetails, details_);
617 auto uri2 = newFolderRecord->GetUri();
618 ASSERT_EQ(uri2, uri_);
619
620 PasteData pasteData;
621 PasteboardClient::GetInstance()->GetPasteData(pasteData);
622 ASSERT_EQ(1, pasteData.GetRecordCount());
623 auto record = pasteData.GetRecordAt(0);
624 auto type = record->GetMimeType();
625 ASSERT_EQ(type, MIMETYPE_TEXT_URI);
626 auto udType = record->GetUDType();
627 ASSERT_EQ(udType, UDMF::UDType::FOLDER);
628 auto uri1 = record->GetUri()->ToString();
629 ASSERT_EQ(uri1, uri_);
630 auto details1 = record->GetDetails();
631 ASSERT_EQ(*details1, details_);
632 }
633
634 /**
635 * @tc.name: SetSystemDefined001
636 * @tc.desc: Get the source of the data.
637 * @tc.type: FUNC
638 * @tc.require:
639 * @tc.author:
640 */
641 HWTEST_F(PasteboardUnifiedDataTest, SetSystemDefined001, TestSize.Level0)
642 {
643 auto data = InitSystemRecordData();
644 PasteboardClient::GetInstance()->SetUnifiedData(data);
645 UDMF::UnifiedData newData;
646 PasteboardClient::GetInstance()->GetUnifiedData(newData);
647 ASSERT_EQ(1, newData.GetRecords().size());
648 auto newRecord = newData.GetRecordAt(0);
649 auto newType = newRecord->GetType();
650 ASSERT_EQ(newType, UDMF::SYSTEM_DEFINED_RECORD);
651 auto newSystemRecord = static_cast<UDMF::SystemDefinedRecord*>(newRecord.get());
652 ASSERT_EQ(newSystemRecord->GetDetails(), details_);
653
654 PasteData pasteData;
655 PasteboardClient::GetInstance()->GetPasteData(pasteData);
656 ASSERT_EQ(1, pasteData.GetRecordCount());
657 auto record = pasteData.GetRecordAt(0);
658 auto type = record->GetMimeType();
659 ASSERT_EQ(type, UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::SYSTEM_DEFINED_RECORD));
660 auto udType = record->GetUDType();
661 ASSERT_EQ(udType, UDMF::SYSTEM_DEFINED_RECORD);
662 auto details = record->GetDetails();
663 ASSERT_EQ(*details, details_);
664 }
665
666 /**
667 * @tc.name: SetAppItem001
668 * @tc.desc: Get the source of the data.
669 * @tc.type: FUNC
670 * @tc.require:
671 * @tc.author:
672 */
673 HWTEST_F(PasteboardUnifiedDataTest, SetAppItem001, TestSize.Level0)
674 {
675 auto data = InitSystemAppItemData();
676 PasteboardClient::GetInstance()->SetUnifiedData(data);
677
678 UDMF::UnifiedData newData;
679 PasteboardClient::GetInstance()->GetUnifiedData(newData);
680 ASSERT_EQ(1, newData.GetRecords().size());
681 auto newRecord = newData.GetRecordAt(0);
682 auto newType = newRecord->GetType();
683 ASSERT_EQ(newType, UDMF::SYSTEM_DEFINED_APP_ITEM);
684 auto newAppItem = static_cast<UDMF::SystemDefinedAppItem*>(newRecord.get());
685 ASSERT_EQ(newAppItem->GetAppId(), "appId");
686 ASSERT_EQ(newAppItem->GetAppIconId(), "appIconId");
687 ASSERT_EQ(newAppItem->GetAppName(), "appName");
688 ASSERT_EQ(newAppItem->GetAppLabelId(), "appLabelId");
689 ASSERT_EQ(newAppItem->GetBundleName(), "bundleName");
690 ASSERT_EQ(newAppItem->GetAbilityName(), "abilityName");
691 ASSERT_EQ(newAppItem->GetDetails(), details_);
692
693 PasteData pasteData;
694 PasteboardClient::GetInstance()->GetPasteData(pasteData);
695 ASSERT_EQ(1, pasteData.GetRecordCount());
696 auto record = pasteData.GetRecordAt(0);
697 auto type = record->GetMimeType();
698 ASSERT_EQ(type, UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::SYSTEM_DEFINED_APP_ITEM));
699 auto udType = record->GetUDType();
700 ASSERT_EQ(udType, UDMF::SYSTEM_DEFINED_APP_ITEM);
701 auto details1 = record->GetDetails();
702 ASSERT_NE(details1, nullptr);
703 ASSERT_EQ(*details1, details_);
704 auto udmfValue = record->GetUDMFValue();
705 ASSERT_NE(udmfValue, nullptr);
706 auto newAppItem1 = std::make_shared<UDMF::SystemDefinedAppItem>(UDMF::SYSTEM_DEFINED_APP_ITEM, *udmfValue);
707 ASSERT_EQ(newAppItem1->GetAppId(), "appId");
708 ASSERT_EQ(newAppItem1->GetAppIconId(), "appIconId");
709 ASSERT_EQ(newAppItem1->GetAppName(), "appName");
710 ASSERT_EQ(newAppItem1->GetAppLabelId(), "appLabelId");
711 ASSERT_EQ(newAppItem1->GetBundleName(), "bundleName");
712 ASSERT_EQ(newAppItem1->GetAbilityName(), "abilityName");
713 }
714
715 /**
716 * @tc.name: SetForm001
717 * @tc.desc: Get the source of the data.
718 * @tc.type: FUNC
719 * @tc.require:
720 * @tc.author:
721 */
722 HWTEST_F(PasteboardUnifiedDataTest, SetForm001, TestSize.Level0)
723 {
724 auto data = InitSysteFormData();
725 PasteboardClient::GetInstance()->SetUnifiedData(data);
726 UDMF::UnifiedData newData;
727 PasteboardClient::GetInstance()->GetUnifiedData(newData);
728 ASSERT_EQ(1, newData.GetRecords().size());
729 auto newRecord = newData.GetRecordAt(0);
730 auto newType = newRecord->GetType();
731 ASSERT_EQ(newType, UDMF::SYSTEM_DEFINED_FORM);
732 auto newForm = static_cast<UDMF::SystemDefinedForm*>(newRecord.get());
733 ASSERT_EQ(newForm->GetFormId(), 1);
734 ASSERT_EQ(newForm->GetFormName(), "formName");
735 ASSERT_EQ(newForm->GetModule(), "module");
736 ASSERT_EQ(newForm->GetBundleName(), "bundleName");
737 ASSERT_EQ(newForm->GetAbilityName(), "abilityName");
738 ASSERT_EQ(newForm->GetDetails(), details_);
739
740 PasteData pasteData;
741 PasteboardClient::GetInstance()->GetPasteData(pasteData);
742 ASSERT_EQ(1, pasteData.GetRecordCount());
743 auto record = pasteData.GetRecordAt(0);
744 auto type = record->GetMimeType();
745 ASSERT_EQ(type, UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::SYSTEM_DEFINED_FORM));
746 auto udType = record->GetUDType();
747 ASSERT_EQ(udType, UDMF::SYSTEM_DEFINED_FORM);
748 auto details1 = record->GetDetails();
749 auto content = *(record->GetSystemDefinedContent());
750 ASSERT_EQ(*details1, details_);
751 auto formId1 = std::get<std::int32_t>(content["formId"]);
752 auto formName1 = std::get<std::string>(content["formName"]);
753 auto module1 = std::get<std::string>(content["module"]);
754 auto bundleName1 = std::get<std::string>(content["bundleName"]);
755 auto abilityName1 = std::get<std::string>(content["abilityName"]);
756 ASSERT_EQ(1, formId1);
757 ASSERT_EQ("formName", formName1);
758 ASSERT_EQ("module", module1);
759 ASSERT_EQ("bundleName", bundleName1);
760 ASSERT_EQ("abilityName", abilityName1);
761 }
762
763 /**
764 * @tc.name: SetAppDefined001
765 * @tc.desc: Get the source of the data.
766 * @tc.type: FUNC
767 * @tc.require:
768 * @tc.author:
769 */
770 HWTEST_F(PasteboardUnifiedDataTest, SetAppDefined001, TestSize.Level0)
771 {
772 auto data = InitAppDefinedData();
773 PasteboardClient::GetInstance()->SetUnifiedData(data);
774
775 UDMF::UnifiedData newData;
776 PasteboardClient::GetInstance()->GetUnifiedData(newData);
777 ASSERT_EQ(1, newData.GetRecords().size());
778 auto newRecord = newData.GetRecordAt(0);
779 auto newType = newRecord->GetType();
780 ASSERT_EQ(newType, UDMF::APPLICATION_DEFINED_RECORD);
781 auto newSystemRecord = static_cast<UDMF::ApplicationDefinedRecord*>(newRecord.get());
782 ASSERT_EQ(newSystemRecord->GetRawData(), rawData_);
783
784 PasteData pasteData;
785 PasteboardClient::GetInstance()->GetPasteData(pasteData);
786 ASSERT_EQ(1, pasteData.GetRecordCount());
787 auto record = pasteData.GetRecordAt(0);
788 auto type = record->GetMimeType();
789 ASSERT_EQ(type, UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::APPLICATION_DEFINED_RECORD));
790 auto udType = record->GetUDType();
791 ASSERT_EQ(udType, UDMF::APPLICATION_DEFINED_RECORD);
792 auto items = record->GetCustomData()->GetItemData();
793 std::map<std::string, std::vector<uint8_t>> customData{ { type, rawData_ } };
794 ASSERT_EQ(items, customData);
795 }
796
797 /**
798 * @tc.name: SetPixelMap001
799 * @tc.desc: Get the source of the data.
800 * @tc.type: FUNC
801 * @tc.require:
802 * @tc.author:
803 */
804 HWTEST_F(PasteboardUnifiedDataTest, SetPixelMap001, TestSize.Level0)
805 {
806 auto data = InitSystemPixelMapData();
807 PasteboardClient::GetInstance()->SetUnifiedData(data);
808 // GetUnifiedData
809 UDMF::UnifiedData newData;
810 PasteboardClient::GetInstance()->GetUnifiedData(newData);
811 ASSERT_EQ(1, newData.GetRecords().size());
812 auto newRecord = newData.GetRecordAt(0);
813 auto newType = newRecord->GetType();
814 ASSERT_EQ(newType, UDMF::SYSTEM_DEFINED_PIXEL_MAP);
815 auto recordValue = newRecord->GetValue();
816 auto newPixelMap1 = std::get_if<std::shared_ptr<OHOS::Media::PixelMap>>(&recordValue);
817 ASSERT_NE(newPixelMap1, nullptr);
818 ImageInfo imageInfo1 = {};
819 (*newPixelMap1)->GetImageInfo(imageInfo1);
820 ASSERT_TRUE(imageInfo1.size.height == 7);
821 ASSERT_TRUE(imageInfo1.size.width == 5);
822 ASSERT_TRUE(imageInfo1.pixelFormat == PixelFormat::ARGB_8888);
823 // GetPasteData
824 PasteData pasteData;
825 PasteboardClient::GetInstance()->GetPasteData(pasteData);
826 ASSERT_EQ(1, pasteData.GetRecordCount());
827 auto record = pasteData.GetRecordAt(0);
828 auto type = record->GetMimeType();
829 ASSERT_EQ(type, MIMETYPE_PIXELMAP);
830 auto udType = record->GetUDType();
831 ASSERT_EQ(udType, int32_t(UDMF::UDType::SYSTEM_DEFINED_PIXEL_MAP));
832 auto newPixelMap = record->GetPixelMap();
833 ASSERT_TRUE(newPixelMap != nullptr);
834 ImageInfo imageInfo = {};
835 newPixelMap->GetImageInfo(imageInfo);
836 ASSERT_TRUE(imageInfo.size.height == imageInfo1.size.height);
837 ASSERT_TRUE(imageInfo.size.width == imageInfo1.size.width);
838 ASSERT_TRUE(imageInfo.pixelFormat == imageInfo1.pixelFormat);
839 }
840 } // namespace OHOS::MiscServices