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 = {
238 {5, 7},
239 PixelFormat::ARGB_8888, PixelFormat::ARGB_8888
240 };
241 std::unique_ptr<PixelMap> pixelMap = PixelMap::Create(color, sizeof(color) / sizeof(color[0]), opts);
242 std::shared_ptr<PixelMap> pixelMapIn = move(pixelMap);
243 std::shared_ptr<UDMF::UnifiedRecord> pixelMapRecord =
244 std::make_shared<UDMF::SystemDefinedPixelMap>(UDMF::SYSTEM_DEFINED_PIXEL_MAP, pixelMapIn);
245 UDMF::UnifiedData data;
246 data.AddRecord(pixelMapRecord);
247 return data;
248 }
249
InitAppDefinedData()250 UDMF::UnifiedData PasteboardUnifiedDataTest::InitAppDefinedData()
251 {
252 UDMF::UnifiedData data;
253 std::shared_ptr<UDMF::ApplicationDefinedRecord> appRecord = std::make_shared<UDMF::ApplicationDefinedRecord>();
254 std::map<std::string, std::vector<uint8_t>> customData;
255 customData[UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::APPLICATION_DEFINED_RECORD)] = rawData_;
256 appRecord->SetApplicationDefinedType(UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::APPLICATION_DEFINED_RECORD));
257 appRecord->SetRawData(rawData_);
258 data.AddRecord(appRecord);
259 return data;
260 }
261
262 /**
263 * @tc.name: SetText001
264 * @tc.desc: Get the source of the data.
265 * @tc.type: FUNC
266 * @tc.require:
267 * @tc.author:
268 */
269 HWTEST_F(PasteboardUnifiedDataTest, SetText001, TestSize.Level0)
270 {
271 auto data = InitTextData();
272 PasteboardClient::GetInstance()->SetUnifiedData(data);
273 UDMF::UnifiedData newData;
274 PasteboardClient::GetInstance()->GetUnifiedData(newData);
275 ASSERT_EQ(1, newData.GetRecords().size());
276 auto newRecord = newData.GetRecordAt(0);
277 auto newType = newRecord->GetType();
278 ASSERT_EQ(newType, UDMF::TEXT);
279 auto newPlainRecord = static_cast<UDMF::Text *>(newRecord.get());
280 auto newDetails = newPlainRecord->GetDetails();
281 ASSERT_EQ(newDetails, details_);
282
283 PasteData pasteData;
284 PasteboardClient::GetInstance()->GetPasteData(pasteData);
285 ASSERT_EQ(1, pasteData.GetRecordCount());
286 auto record = pasteData.GetRecordAt(0);
287 auto type = record->GetMimeType();
288 ASSERT_EQ(type, UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::TEXT));
289 auto udType = record->GetUDType();
290 ASSERT_EQ(udType, UDMF::UDType::TEXT);
291 auto details1 = record->GetDetails();
292 ASSERT_EQ(*details1, details_);
293 }
294
295 /**
296 * @tc.name: SetPlainText001
297 * @tc.desc: Get the source of the data.
298 * @tc.type: FUNC
299 * @tc.require:
300 * @tc.author:
301 */
302 HWTEST_F(PasteboardUnifiedDataTest, SetPlainText001, TestSize.Level0)
303 {
304 auto data = InitPlainData();
305 PasteboardClient::GetInstance()->SetUnifiedData(data);
306 UDMF::UnifiedData newData;
307 PasteboardClient::GetInstance()->GetUnifiedData(newData);
308 ASSERT_EQ(1, newData.GetRecords().size());
309 auto newRecord = newData.GetRecordAt(0);
310 auto newType = newRecord->GetType();
311 ASSERT_EQ(newType, UDMF::PLAIN_TEXT);
312 auto newPlainRecord = static_cast<UDMF::PlainText *>(newRecord.get());
313 auto newPlainText = newPlainRecord->GetContent();
314 auto newAbstract = newPlainRecord->GetAbstract();
315 auto newDetails = newPlainRecord->GetDetails();
316 ASSERT_EQ(newPlainText, text_);
317 ASSERT_EQ(newAbstract, extraText_);
318 ASSERT_EQ(newDetails, details_);
319
320 PasteData pasteData;
321 PasteboardClient::GetInstance()->GetPasteData(pasteData);
322 ASSERT_EQ(1, pasteData.GetRecordCount());
323 auto record = pasteData.GetRecordAt(0);
324 auto type = record->GetMimeType();
325 ASSERT_EQ(type, MIMETYPE_TEXT_PLAIN);
326 auto udType = record->GetUDType();
327 ASSERT_EQ(udType, UDMF::UDType::PLAIN_TEXT);
328 auto udmfValue = record->GetUDMFValue();
329 ASSERT_NE(udmfValue, nullptr);
330 auto plainText = std::make_shared<UDMF::PlainText>(UDMF::PLAIN_TEXT, *udmfValue);
331 ASSERT_EQ(plainText->GetContent(), text_);
332 ASSERT_EQ(plainText->GetAbstract(), extraText_);
333 }
334
335 /**
336 * @tc.name: SetLink001
337 * @tc.desc: Get the source of the data.
338 * @tc.type: FUNC
339 * @tc.require:
340 * @tc.author:
341 */
342 HWTEST_F(PasteboardUnifiedDataTest, SetLink001, TestSize.Level0)
343 {
344 auto data = InitLinkData();
345 PasteboardClient::GetInstance()->SetUnifiedData(data);
346 UDMF::UnifiedData newData;
347 PasteboardClient::GetInstance()->GetUnifiedData(newData);
348 ASSERT_EQ(1, newData.GetRecords().size());
349 auto newRecord = newData.GetRecordAt(0);
350 auto newType = newRecord->GetType();
351 ASSERT_EQ(newType, UDMF::HYPERLINK);
352 auto newPlainRecord = static_cast<UDMF::Link *>(newRecord.get());
353 auto newUrl = newPlainRecord->GetUrl();
354 auto newDescription = newPlainRecord->GetDescription();
355 auto newDetails = newPlainRecord->GetDetails();
356 ASSERT_EQ(newUrl, text_);
357 ASSERT_EQ(newDescription, extraText_);
358 ASSERT_EQ(newDetails, details_);
359
360 PasteData pasteData;
361 PasteboardClient::GetInstance()->GetPasteData(pasteData);
362 ASSERT_EQ(1, pasteData.GetRecordCount());
363 auto record = pasteData.GetRecordAt(0);
364 auto type = record->GetMimeType();
365 ASSERT_EQ(type, MIMETYPE_TEXT_PLAIN);
366 auto udType = record->GetUDType();
367 ASSERT_EQ(udType, UDMF::UDType::HYPERLINK);
368 auto udmfValue = record->GetUDMFValue();
369 ASSERT_NE(udmfValue, nullptr);
370 auto link = std::make_shared<UDMF::Link>(UDMF::HYPERLINK, *udmfValue);
371 ASSERT_EQ(link->GetUrl(), text_);
372 ASSERT_EQ(link->GetDescription(), extraText_);
373 }
374
375 /**
376 * @tc.name: SetHtml001
377 * @tc.desc: Get the source of the data.
378 * @tc.type: FUNC
379 * @tc.require:
380 * @tc.author:
381 */
382 HWTEST_F(PasteboardUnifiedDataTest, SetHtml001, TestSize.Level0)
383 {
384 auto data = InitHtmlData();
385 PasteboardClient::GetInstance()->SetUnifiedData(data);
386 UDMF::UnifiedData newData;
387 PasteboardClient::GetInstance()->GetUnifiedData(newData);
388 ASSERT_EQ(1, newData.GetRecords().size());
389 auto newRecord = newData.GetRecordAt(0);
390 auto newType = newRecord->GetType();
391 ASSERT_EQ(newType, UDMF::HTML);
392 auto newPlainRecord = static_cast<UDMF::Html *>(newRecord.get());
393 auto newPlainText = newPlainRecord->GetHtmlContent();
394 auto newAbstract = newPlainRecord->GetPlainContent();
395 auto newDetails = newPlainRecord->GetDetails();
396 ASSERT_EQ(newPlainText, text_);
397 ASSERT_EQ(newAbstract, extraText_);
398 ASSERT_EQ(newDetails, details_);
399
400 PasteData pasteData;
401 PasteboardClient::GetInstance()->GetPasteData(pasteData);
402 ASSERT_EQ(1, pasteData.GetRecordCount());
403 auto record = pasteData.GetRecordAt(0);
404 auto type = record->GetMimeType();
405 ASSERT_EQ(type, MIMETYPE_TEXT_HTML);
406 auto udType = record->GetUDType();
407 ASSERT_EQ(udType, UDMF::UDType::HTML);
408 auto udmfValue = record->GetUDMFValue();
409 ASSERT_NE(udmfValue, nullptr);
410 auto html = std::make_shared<UDMF::Html>(UDMF::HTML, *udmfValue);
411 ASSERT_EQ(html->GetHtmlContent(), text_);
412 ASSERT_EQ(html->GetPlainContent(), extraText_);
413 }
414
415 /**
416 * @tc.name: SetWant001
417 * @tc.desc: Get the source of the data.
418 * @tc.type: FUNC
419 * @tc.require:
420 * @tc.author:
421 */
422 HWTEST_F(PasteboardUnifiedDataTest, SetWant001, TestSize.Level0)
423 {
424 auto data = InitWantData();
425 PasteboardClient::GetInstance()->SetUnifiedData(data);
426 UDMF::UnifiedData newData;
427 PasteboardClient::GetInstance()->GetUnifiedData(newData);
428 ASSERT_EQ(1, newData.GetRecords().size());
429 auto newRecord = newData.GetRecordAt(0);
430 auto newType = newRecord->GetType();
431 ASSERT_EQ(newType, UDMF::OPENHARMONY_WANT);
432 auto recordValue = newRecord->GetValue();
433 auto wantValue = std::get_if<std::shared_ptr<OHOS::AAFwk::Want>>(&recordValue);
434 ASSERT_NE(wantValue, nullptr);
435 int32_t idValue2 = (*(wantValue))->GetIntParam("id", 0);
436 ASSERT_EQ(idValue2, 123);
437
438 PasteData pasteData;
439 PasteboardClient::GetInstance()->GetPasteData(pasteData);
440 ASSERT_EQ(1, pasteData.GetRecordCount());
441 auto record = pasteData.GetRecordAt(0);
442 auto type = record->GetMimeType();
443 ASSERT_EQ(type, MIMETYPE_TEXT_WANT);
444 auto udType = record->GetUDType();
445 ASSERT_EQ(udType, int32_t(UDMF::UDType::OPENHARMONY_WANT));
446 auto want1 = record->GetWant();
447 int32_t idValue1 = want1->GetIntParam("id", 0);
448 ASSERT_EQ(idValue1, 123);
449 }
450
451 /**
452 * @tc.name: SetFile001
453 * @tc.desc: Get the source of the data.
454 * @tc.type: FUNC
455 * @tc.require:
456 * @tc.author:
457 */
458 HWTEST_F(PasteboardUnifiedDataTest, SetFile001, TestSize.Level0)
459 {
460 auto data = InitFileData();
461 PasteboardClient::GetInstance()->SetUnifiedData(data);
462
463 UDMF::UnifiedData newData;
464 PasteboardClient::GetInstance()->GetUnifiedData(newData);
465 ASSERT_EQ(1, newData.GetRecords().size());
466 auto newRecord = newData.GetRecordAt(0);
467 auto newType = newRecord->GetType();
468 ASSERT_EQ(newType, UDMF::FILE);
469 auto newFileRecord = static_cast<UDMF::File *>(newRecord.get());
470 auto newDetails = newFileRecord->GetDetails();
471 ASSERT_EQ(newDetails, details_);
472 auto uri2 = newFileRecord->GetUri();
473 ASSERT_EQ(uri2, uri_);
474
475 PasteData pasteData;
476 PasteboardClient::GetInstance()->GetPasteData(pasteData);
477 ASSERT_EQ(1, pasteData.GetRecordCount());
478 auto record = pasteData.GetRecordAt(0);
479 auto type = record->GetMimeType();
480 ASSERT_EQ(type, MIMETYPE_TEXT_URI);
481 auto udType = record->GetUDType();
482 ASSERT_EQ(udType, UDMF::UDType::FILE);
483 auto uri1 = record->GetUri()->ToString();
484 ASSERT_EQ(uri1, uri_);
485 auto details1 = record->GetDetails();
486 ASSERT_EQ(*details1, details_);
487 }
488
489 /**
490 * @tc.name: SetImage001
491 * @tc.desc: Get the source of the data.
492 * @tc.type: FUNC
493 * @tc.require:
494 * @tc.author:
495 */
496 HWTEST_F(PasteboardUnifiedDataTest, SetImage001, TestSize.Level0)
497 {
498 auto data = InitImageData();
499 PasteboardClient::GetInstance()->SetUnifiedData(data);
500 UDMF::UnifiedData newData;
501 PasteboardClient::GetInstance()->GetUnifiedData(newData);
502 ASSERT_EQ(1, newData.GetRecords().size());
503 auto newRecord = newData.GetRecordAt(0);
504 auto newType = newRecord->GetType();
505 ASSERT_EQ(newType, UDMF::IMAGE);
506 auto newImageRecord = static_cast<UDMF::Image *>(newRecord.get());
507 auto newDetails = newImageRecord->GetDetails();
508 ASSERT_EQ(newDetails, details_);
509 auto uri2 = newImageRecord->GetUri();
510 ASSERT_EQ(uri2, uri_);
511
512 PasteData pasteData;
513 PasteboardClient::GetInstance()->GetPasteData(pasteData);
514 ASSERT_EQ(1, pasteData.GetRecordCount());
515 auto record = pasteData.GetRecordAt(0);
516 auto type = record->GetMimeType();
517 ASSERT_EQ(type, MIMETYPE_TEXT_URI);
518 auto udType = record->GetUDType();
519 ASSERT_EQ(udType, UDMF::UDType::IMAGE);
520 auto uri1 = record->GetUri()->ToString();
521 ASSERT_EQ(uri1, uri_);
522 auto details1 = record->GetDetails();
523 ASSERT_EQ(*details1, details_);
524 }
525
526 /**
527 * @tc.name: SetAudio001
528 * @tc.desc: Get the source of the data.
529 * @tc.type: FUNC
530 * @tc.require:
531 * @tc.author:
532 */
533 HWTEST_F(PasteboardUnifiedDataTest, SetAudio001, TestSize.Level0)
534 {
535 auto data = InitAudioData();
536 PasteboardClient::GetInstance()->SetUnifiedData(data);
537 UDMF::UnifiedData newData;
538 PasteboardClient::GetInstance()->GetUnifiedData(newData);
539 ASSERT_EQ(1, newData.GetRecords().size());
540 auto newRecord = newData.GetRecordAt(0);
541 auto newType = newRecord->GetType();
542 ASSERT_EQ(newType, UDMF::AUDIO);
543 auto newAudioRecord = static_cast<UDMF::Audio *>(newRecord.get());
544 auto newDetails = newAudioRecord->GetDetails();
545 ASSERT_EQ(newDetails, details_);
546 auto uri2 = newAudioRecord->GetUri();
547 ASSERT_EQ(uri2, uri_);
548
549 PasteData pasteData;
550 PasteboardClient::GetInstance()->GetPasteData(pasteData);
551 ASSERT_EQ(1, pasteData.GetRecordCount());
552 auto record = pasteData.GetRecordAt(0);
553 auto type = record->GetMimeType();
554 ASSERT_EQ(type, MIMETYPE_TEXT_URI);
555 auto udType = record->GetUDType();
556 ASSERT_EQ(udType, UDMF::UDType::AUDIO);
557 auto uri1 = record->GetUri()->ToString();
558 ASSERT_EQ(uri1, uri_);
559 auto details1 = record->GetDetails();
560 ASSERT_EQ(*details1, details_);
561 }
562
563 /**
564 * @tc.name: SetVideo001
565 * @tc.desc: Get the source of the data.
566 * @tc.type: FUNC
567 * @tc.require:
568 * @tc.author:
569 */
570 HWTEST_F(PasteboardUnifiedDataTest, SetVideo001, TestSize.Level0)
571 {
572 auto data = InitVideoData();
573 PasteboardClient::GetInstance()->SetUnifiedData(data);
574 UDMF::UnifiedData newData;
575 PasteboardClient::GetInstance()->GetUnifiedData(newData);
576 ASSERT_EQ(1, newData.GetRecords().size());
577 auto newRecord = newData.GetRecordAt(0);
578 auto newType = newRecord->GetType();
579 ASSERT_EQ(newType, UDMF::VIDEO);
580 auto newVideoRecord = static_cast<UDMF::Video *>(newRecord.get());
581 auto newDetails = newVideoRecord->GetDetails();
582 ASSERT_EQ(newDetails, details_);
583 auto uri2 = newVideoRecord->GetUri();
584 ASSERT_EQ(uri2, uri_);
585
586 PasteData pasteData;
587 PasteboardClient::GetInstance()->GetPasteData(pasteData);
588 ASSERT_EQ(1, pasteData.GetRecordCount());
589 auto record = pasteData.GetRecordAt(0);
590 auto type = record->GetMimeType();
591 ASSERT_EQ(type, MIMETYPE_TEXT_URI);
592 auto udType = record->GetUDType();
593 ASSERT_EQ(udType, UDMF::UDType::VIDEO);
594 auto uri1 = record->GetUri()->ToString();
595 ASSERT_EQ(uri1, uri_);
596 auto details1 = record->GetDetails();
597 ASSERT_EQ(*details1, details_);
598 }
599
600 /**
601 * @tc.name: SetFolder001
602 * @tc.desc: Get the source of the data.
603 * @tc.type: FUNC
604 * @tc.require:
605 * @tc.author:
606 */
607 HWTEST_F(PasteboardUnifiedDataTest, SetFolder001, TestSize.Level0)
608 {
609 auto data = InitFolderData();
610 PasteboardClient::GetInstance()->SetUnifiedData(data);
611 UDMF::UnifiedData newData;
612 PasteboardClient::GetInstance()->GetUnifiedData(newData);
613 ASSERT_EQ(1, newData.GetRecords().size());
614 auto newRecord = newData.GetRecordAt(0);
615 auto newType = newRecord->GetType();
616 ASSERT_EQ(newType, UDMF::FOLDER);
617 auto newFolderRecord = static_cast<UDMF::Folder *>(newRecord.get());
618 auto newDetails = newFolderRecord->GetDetails();
619 ASSERT_EQ(newDetails, details_);
620 auto uri2 = newFolderRecord->GetUri();
621 ASSERT_EQ(uri2, uri_);
622
623 PasteData pasteData;
624 PasteboardClient::GetInstance()->GetPasteData(pasteData);
625 ASSERT_EQ(1, pasteData.GetRecordCount());
626 auto record = pasteData.GetRecordAt(0);
627 auto type = record->GetMimeType();
628 ASSERT_EQ(type, MIMETYPE_TEXT_URI);
629 auto udType = record->GetUDType();
630 ASSERT_EQ(udType, UDMF::UDType::FOLDER);
631 auto uri1 = record->GetUri()->ToString();
632 ASSERT_EQ(uri1, uri_);
633 auto details1 = record->GetDetails();
634 ASSERT_EQ(*details1, details_);
635 }
636
637 /**
638 * @tc.name: SetSystemDefined001
639 * @tc.desc: Get the source of the data.
640 * @tc.type: FUNC
641 * @tc.require:
642 * @tc.author:
643 */
644 HWTEST_F(PasteboardUnifiedDataTest, SetSystemDefined001, TestSize.Level0)
645 {
646 auto data = InitSystemRecordData();
647 PasteboardClient::GetInstance()->SetUnifiedData(data);
648 UDMF::UnifiedData newData;
649 PasteboardClient::GetInstance()->GetUnifiedData(newData);
650 ASSERT_EQ(1, newData.GetRecords().size());
651 auto newRecord = newData.GetRecordAt(0);
652 auto newType = newRecord->GetType();
653 ASSERT_EQ(newType, UDMF::SYSTEM_DEFINED_RECORD);
654 auto newSystemRecord = static_cast<UDMF::SystemDefinedRecord *>(newRecord.get());
655 ASSERT_EQ(newSystemRecord->GetDetails(), details_);
656
657 PasteData pasteData;
658 PasteboardClient::GetInstance()->GetPasteData(pasteData);
659 ASSERT_EQ(1, pasteData.GetRecordCount());
660 auto record = pasteData.GetRecordAt(0);
661 auto type = record->GetMimeType();
662 ASSERT_EQ(type, UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::SYSTEM_DEFINED_RECORD));
663 auto udType = record->GetUDType();
664 ASSERT_EQ(udType, UDMF::SYSTEM_DEFINED_RECORD);
665 auto details = record->GetDetails();
666 ASSERT_EQ(*details, details_);
667 }
668
669 /**
670 * @tc.name: SetAppItem001
671 * @tc.desc: Get the source of the data.
672 * @tc.type: FUNC
673 * @tc.require:
674 * @tc.author:
675 */
676 HWTEST_F(PasteboardUnifiedDataTest, SetAppItem001, TestSize.Level0)
677 {
678 auto data = InitSystemAppItemData();
679 PasteboardClient::GetInstance()->SetUnifiedData(data);
680
681 UDMF::UnifiedData newData;
682 PasteboardClient::GetInstance()->GetUnifiedData(newData);
683 ASSERT_EQ(1, newData.GetRecords().size());
684 auto newRecord = newData.GetRecordAt(0);
685 auto newType = newRecord->GetType();
686 ASSERT_EQ(newType, UDMF::SYSTEM_DEFINED_APP_ITEM);
687 auto newAppItem = static_cast<UDMF::SystemDefinedAppItem *>(newRecord.get());
688 ASSERT_EQ(newAppItem->GetAppId(), "appId");
689 ASSERT_EQ(newAppItem->GetAppIconId(), "appIconId");
690 ASSERT_EQ(newAppItem->GetAppName(), "appName");
691 ASSERT_EQ(newAppItem->GetAppLabelId(), "appLabelId");
692 ASSERT_EQ(newAppItem->GetBundleName(), "bundleName");
693 ASSERT_EQ(newAppItem->GetAbilityName(), "abilityName");
694 ASSERT_EQ(newAppItem->GetDetails(), details_);
695
696 PasteData pasteData;
697 PasteboardClient::GetInstance()->GetPasteData(pasteData);
698 ASSERT_EQ(1, pasteData.GetRecordCount());
699 auto record = pasteData.GetRecordAt(0);
700 auto type = record->GetMimeType();
701 ASSERT_EQ(type, UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::SYSTEM_DEFINED_APP_ITEM));
702 auto udType = record->GetUDType();
703 ASSERT_EQ(udType, UDMF::SYSTEM_DEFINED_APP_ITEM);
704 auto details1 = record->GetDetails();
705 ASSERT_NE(details1, nullptr);
706 ASSERT_EQ(*details1, details_);
707 auto udmfValue = record->GetUDMFValue();
708 ASSERT_NE(udmfValue, nullptr);
709 auto newAppItem1 = std::make_shared<UDMF::SystemDefinedAppItem>(UDMF::SYSTEM_DEFINED_APP_ITEM, *udmfValue);
710 ASSERT_EQ(newAppItem1->GetAppId(), "appId");
711 ASSERT_EQ(newAppItem1->GetAppIconId(), "appIconId");
712 ASSERT_EQ(newAppItem1->GetAppName(), "appName");
713 ASSERT_EQ(newAppItem1->GetAppLabelId(), "appLabelId");
714 ASSERT_EQ(newAppItem1->GetBundleName(), "bundleName");
715 ASSERT_EQ(newAppItem1->GetAbilityName(), "abilityName");
716 }
717
718 /**
719 * @tc.name: SetForm001
720 * @tc.desc: Get the source of the data.
721 * @tc.type: FUNC
722 * @tc.require:
723 * @tc.author:
724 */
725 HWTEST_F(PasteboardUnifiedDataTest, SetForm001, TestSize.Level0)
726 {
727 auto data = InitSysteFormData();
728 PasteboardClient::GetInstance()->SetUnifiedData(data);
729 UDMF::UnifiedData newData;
730 PasteboardClient::GetInstance()->GetUnifiedData(newData);
731 ASSERT_EQ(1, newData.GetRecords().size());
732 auto newRecord = newData.GetRecordAt(0);
733 auto newType = newRecord->GetType();
734 ASSERT_EQ(newType, UDMF::SYSTEM_DEFINED_FORM);
735 auto newForm = static_cast<UDMF::SystemDefinedForm *>(newRecord.get());
736 ASSERT_EQ(newForm->GetFormId(), 1);
737 ASSERT_EQ(newForm->GetFormName(), "formName");
738 ASSERT_EQ(newForm->GetModule(), "module");
739 ASSERT_EQ(newForm->GetBundleName(), "bundleName");
740 ASSERT_EQ(newForm->GetAbilityName(), "abilityName");
741 ASSERT_EQ(newForm->GetDetails(), details_);
742
743 PasteData pasteData;
744 PasteboardClient::GetInstance()->GetPasteData(pasteData);
745 ASSERT_EQ(1, pasteData.GetRecordCount());
746 auto record = pasteData.GetRecordAt(0);
747 auto type = record->GetMimeType();
748 ASSERT_EQ(type, UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::SYSTEM_DEFINED_FORM));
749 auto udType = record->GetUDType();
750 ASSERT_EQ(udType, UDMF::SYSTEM_DEFINED_FORM);
751 auto details1 = record->GetDetails();
752 auto content = *(record->GetSystemDefinedContent());
753 ASSERT_EQ(*details1, details_);
754 auto formId1 = std::get<std::int32_t>(content["formId"]);
755 auto formName1 = std::get<std::string>(content["formName"]);
756 auto module1 = std::get<std::string>(content["module"]);
757 auto bundleName1 = std::get<std::string>(content["bundleName"]);
758 auto abilityName1 = std::get<std::string>(content["abilityName"]);
759 ASSERT_EQ(1, formId1);
760 ASSERT_EQ("formName", formName1);
761 ASSERT_EQ("module", module1);
762 ASSERT_EQ("bundleName", bundleName1);
763 ASSERT_EQ("abilityName", abilityName1);
764 }
765
766 /**
767 * @tc.name: SetAppDefined001
768 * @tc.desc: Get the source of the data.
769 * @tc.type: FUNC
770 * @tc.require:
771 * @tc.author:
772 */
773 HWTEST_F(PasteboardUnifiedDataTest, SetAppDefined001, TestSize.Level0)
774 {
775 auto data = InitAppDefinedData();
776 PasteboardClient::GetInstance()->SetUnifiedData(data);
777
778 UDMF::UnifiedData newData;
779 PasteboardClient::GetInstance()->GetUnifiedData(newData);
780 ASSERT_EQ(1, newData.GetRecords().size());
781 auto newRecord = newData.GetRecordAt(0);
782 auto newType = newRecord->GetType();
783 ASSERT_EQ(newType, UDMF::APPLICATION_DEFINED_RECORD);
784 auto newSystemRecord = static_cast<UDMF::ApplicationDefinedRecord *>(newRecord.get());
785 ASSERT_EQ(newSystemRecord->GetRawData(), rawData_);
786
787 PasteData pasteData;
788 PasteboardClient::GetInstance()->GetPasteData(pasteData);
789 ASSERT_EQ(1, pasteData.GetRecordCount());
790 auto record = pasteData.GetRecordAt(0);
791 auto type = record->GetMimeType();
792 ASSERT_EQ(type, UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::APPLICATION_DEFINED_RECORD));
793 auto udType = record->GetUDType();
794 ASSERT_EQ(udType, UDMF::APPLICATION_DEFINED_RECORD);
795 auto items = record->GetCustomData()->GetItemData();
796 std::map<std::string, std::vector<uint8_t>> customData {
797 {type, rawData_}
798 };
799 ASSERT_EQ(items, customData);
800 }
801
802 /**
803 * @tc.name: SetPixelMap001
804 * @tc.desc: Get the source of the data.
805 * @tc.type: FUNC
806 * @tc.require:
807 * @tc.author:
808 */
809 HWTEST_F(PasteboardUnifiedDataTest, SetPixelMap001, TestSize.Level0)
810 {
811 auto data = InitSystemPixelMapData();
812 PasteboardClient::GetInstance()->SetUnifiedData(data);
813 // GetUnifiedData
814 UDMF::UnifiedData newData;
815 PasteboardClient::GetInstance()->GetUnifiedData(newData);
816 ASSERT_EQ(1, newData.GetRecords().size());
817 auto newRecord = newData.GetRecordAt(0);
818 auto newType = newRecord->GetType();
819 ASSERT_EQ(newType, UDMF::SYSTEM_DEFINED_PIXEL_MAP);
820 auto recordValue = newRecord->GetValue();
821 auto newPixelMap1 = std::get_if<std::shared_ptr<OHOS::Media::PixelMap>>(&recordValue);
822 ASSERT_NE(newPixelMap1, nullptr);
823 ImageInfo imageInfo1 = {};
824 (*newPixelMap1)->GetImageInfo(imageInfo1);
825 ASSERT_TRUE(imageInfo1.size.height == 7);
826 ASSERT_TRUE(imageInfo1.size.width == 5);
827 ASSERT_TRUE(imageInfo1.pixelFormat == PixelFormat::ARGB_8888);
828 // GetPasteData
829 PasteData pasteData;
830 PasteboardClient::GetInstance()->GetPasteData(pasteData);
831 ASSERT_EQ(1, pasteData.GetRecordCount());
832 auto record = pasteData.GetRecordAt(0);
833 auto type = record->GetMimeType();
834 ASSERT_EQ(type, MIMETYPE_PIXELMAP);
835 auto udType = record->GetUDType();
836 ASSERT_EQ(udType, int32_t(UDMF::UDType::SYSTEM_DEFINED_PIXEL_MAP));
837 auto newPixelMap = record->GetPixelMap();
838 ASSERT_TRUE(newPixelMap != nullptr);
839 ImageInfo imageInfo = {};
840 newPixelMap->GetImageInfo(imageInfo);
841 ASSERT_TRUE(imageInfo.size.height == imageInfo1.size.height);
842 ASSERT_TRUE(imageInfo.size.width == imageInfo1.size.width);
843 ASSERT_TRUE(imageInfo.pixelFormat == imageInfo1.pixelFormat);
844 }
845 } // namespace OHOS::MiscServices