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 #include <variant>
18
19 #include "entry_getter.h"
20 #include "pasteboard_client.h"
21 #include "pasteboard_error.h"
22 #include "pasteboard_hilog.h"
23
24 namespace OHOS::MiscServices {
25 using namespace testing;
26 using namespace testing::ext;
27 using namespace OHOS::UDMF;
28 using namespace OHOS::AAFwk;
29 using namespace OHOS::Media;
30 static std::string text_ = "test";
31 static std::string uri_ = "uri";
32 static std::string fileType_ = "test";
33 static std::string html_ = "<div class='disable'>helloWorld</div>";
34 static std::string link_ = "http://abc.com";
35 static std::string plainTextUtdId_ = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::PLAIN_TEXT);
36 static std::string htmlUtdId_ = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::HTML);
37 static std::string fileUriUtdId_ = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::FILE_URI);
38 static std::string pixelMapUtdId_ = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::SYSTEM_DEFINED_PIXEL_MAP);
39 static std::string linkUtdId_ = UDMF::UtdUtils::GetUtdIdFromUtdEnum(UDMF::HYPERLINK);
40 class PasteboardMultiTypeUnifiedDataDelayTest : public testing::Test {
41 public:
42 static void SetUpTestCase(void);
43 static void TearDownTestCase(void);
44 void SetUp();
45 void TearDown();
46
47 static UDMF::ValueType InitPlainUds();
48 static UDMF::ValueType InitHtmlUds();
49 static UDMF::ValueType InitFileUriUds();
50 static UDMF::ValueType InitPixelMapUds();
51 static UDMF::ValueType InitLinkUds();
52
53 void CheckPlainUds(const UDMF::ValueType &value);
54 void CheckHtmlUds(const UDMF::ValueType &value);
55 void CheckFileUriUds(const UDMF::ValueType &value);
56 void CheckPixelMapUds(const UDMF::ValueType &value);
57 void CheckLinkUds(const UDMF::ValueType &value);
58 };
59
SetUpTestCase(void)60 void PasteboardMultiTypeUnifiedDataDelayTest::SetUpTestCase(void)
61 {
62 PasteboardClient::GetInstance()->Clear();
63 }
64
TearDownTestCase(void)65 void PasteboardMultiTypeUnifiedDataDelayTest::TearDownTestCase(void) { }
66
SetUp()67 void PasteboardMultiTypeUnifiedDataDelayTest::SetUp() { }
68
TearDown()69 void PasteboardMultiTypeUnifiedDataDelayTest::TearDown() { }
70
InitPlainUds()71 UDMF::ValueType PasteboardMultiTypeUnifiedDataDelayTest::InitPlainUds()
72 {
73 Object plainUds;
74 plainUds.value_[UDMF::UNIFORM_DATA_TYPE] = plainTextUtdId_;
75 plainUds.value_[UDMF::CONTENT] = text_;
76 return std::make_shared<Object>(plainUds);
77 }
78
InitHtmlUds()79 UDMF::ValueType PasteboardMultiTypeUnifiedDataDelayTest::InitHtmlUds()
80 {
81 Object htmlObject;
82 htmlObject.value_[UDMF::UNIFORM_DATA_TYPE] = htmlUtdId_;
83 htmlObject.value_[UDMF::HTML_CONTENT] = html_;
84 return std::make_shared<Object>(htmlObject);
85 }
86
InitFileUriUds()87 UDMF::ValueType PasteboardMultiTypeUnifiedDataDelayTest::InitFileUriUds()
88 {
89 Object fileUriObject;
90 fileUriObject.value_[UDMF::UNIFORM_DATA_TYPE] = fileUriUtdId_;
91 fileUriObject.value_[UDMF::FILE_URI_PARAM] = uri_;
92 fileUriObject.value_[UDMF::FILE_TYPE] = fileType_;
93 return std::make_shared<Object>(fileUriObject);
94 }
95
InitPixelMapUds()96 UDMF::ValueType PasteboardMultiTypeUnifiedDataDelayTest::InitPixelMapUds()
97 {
98 Object object;
99 object.value_[UDMF::UNIFORM_DATA_TYPE] = pixelMapUtdId_;
100 uint32_t color[100] = { 3, 7, 9, 9, 7, 6 };
101 InitializationOptions opts = {
102 {5, 7},
103 PixelFormat::ARGB_8888, PixelFormat::ARGB_8888
104 };
105 std::unique_ptr<PixelMap> pixelMap = PixelMap::Create(color, sizeof(color) / sizeof(color[0]), opts);
106 std::shared_ptr<PixelMap> pixelMapIn = move(pixelMap);
107 object.value_[UDMF::PIXEL_MAP] = pixelMapIn;
108 return std::make_shared<Object>(object);
109 }
110
InitLinkUds()111 UDMF::ValueType PasteboardMultiTypeUnifiedDataDelayTest::InitLinkUds()
112 {
113 Object linkObject;
114 linkObject.value_[UDMF::UNIFORM_DATA_TYPE] = linkUtdId_;
115 linkObject.value_[UDMF::URL] = link_;
116 return std::make_shared<Object>(linkObject);
117 }
118
119 class EntryGetterImpl : public UDMF::EntryGetter {
120 public:
121 UDMF::ValueType GetValueByType(const std::string &utdId) override;
122 };
123
GetValueByType(const std::string & utdId)124 UDMF::ValueType EntryGetterImpl::GetValueByType(const std::string &utdId)
125 {
126 if (utdId == plainTextUtdId_) {
127 return PasteboardMultiTypeUnifiedDataDelayTest::InitPlainUds();
128 }
129 if (utdId == htmlUtdId_) {
130 return PasteboardMultiTypeUnifiedDataDelayTest::InitHtmlUds();
131 }
132 if (utdId == fileUriUtdId_) {
133 return PasteboardMultiTypeUnifiedDataDelayTest::InitFileUriUds();
134 }
135 if (utdId == pixelMapUtdId_) {
136 return PasteboardMultiTypeUnifiedDataDelayTest::InitPixelMapUds();
137 }
138 if (utdId == linkUtdId_) {
139 return PasteboardMultiTypeUnifiedDataDelayTest::InitLinkUds();
140 }
141 return nullptr;
142 }
143
CheckPlainUds(const UDMF::ValueType & value)144 void PasteboardMultiTypeUnifiedDataDelayTest::CheckPlainUds(const UDMF::ValueType &value)
145 {
146 ASSERT_NE(std::get_if<std::shared_ptr<Object>>(&value), nullptr);
147 auto obj = std::get<std::shared_ptr<Object>>(value);
148 ASSERT_NE(obj, nullptr);
149 ASSERT_NE(std::get_if<std::string>(&obj->value_[UDMF::UNIFORM_DATA_TYPE]), nullptr);
150 ASSERT_EQ(std::get<std::string>(obj->value_[UDMF::UNIFORM_DATA_TYPE]), plainTextUtdId_);
151 ASSERT_NE(std::get_if<std::string>(&obj->value_[UDMF::CONTENT]), nullptr);
152 ASSERT_EQ(std::get<std::string>(obj->value_[UDMF::CONTENT]), text_);
153 }
154
CheckHtmlUds(const UDMF::ValueType & value)155 void PasteboardMultiTypeUnifiedDataDelayTest::CheckHtmlUds(const UDMF::ValueType &value)
156 {
157 ASSERT_NE(std::get_if<std::shared_ptr<Object>>(&value), nullptr);
158 auto obj = std::get<std::shared_ptr<Object>>(value);
159 ASSERT_NE(obj, nullptr);
160 ASSERT_NE(std::get_if<std::string>(&obj->value_[UDMF::UNIFORM_DATA_TYPE]), nullptr);
161 ASSERT_EQ(std::get<std::string>(obj->value_[UDMF::UNIFORM_DATA_TYPE]), htmlUtdId_);
162 ASSERT_NE(std::get_if<std::string>(&obj->value_[UDMF::HTML_CONTENT]), nullptr);
163 ASSERT_EQ(std::get<std::string>(obj->value_[UDMF::HTML_CONTENT]), html_);
164 }
165
CheckFileUriUds(const UDMF::ValueType & value)166 void PasteboardMultiTypeUnifiedDataDelayTest::CheckFileUriUds(const UDMF::ValueType &value)
167 {
168 ASSERT_NE(std::get_if<std::shared_ptr<Object>>(&value), nullptr);
169 auto obj = std::get<std::shared_ptr<Object>>(value);
170 ASSERT_NE(obj, nullptr);
171 ASSERT_NE(std::get_if<std::string>(&obj->value_[UDMF::UNIFORM_DATA_TYPE]), nullptr);
172 ASSERT_EQ(std::get<std::string>(obj->value_[UDMF::UNIFORM_DATA_TYPE]), fileUriUtdId_);
173 ASSERT_NE(std::get_if<std::string>(&obj->value_[UDMF::FILE_URI_PARAM]), nullptr);
174 ASSERT_EQ(std::get<std::string>(obj->value_[UDMF::FILE_URI_PARAM]), uri_);
175 ASSERT_NE(std::get_if<std::string>(&obj->value_[UDMF::FILE_TYPE]), nullptr);
176 ASSERT_EQ(std::get<std::string>(obj->value_[UDMF::FILE_TYPE]), fileType_);
177 }
178
CheckPixelMapUds(const UDMF::ValueType & value)179 void PasteboardMultiTypeUnifiedDataDelayTest::CheckPixelMapUds(const UDMF::ValueType &value)
180 {
181 ASSERT_NE(std::get_if<std::shared_ptr<Object>>(&value), nullptr);
182 auto obj = std::get<std::shared_ptr<Object>>(value);
183 ASSERT_NE(std::get_if<std::string>(&obj->value_[UDMF::UNIFORM_DATA_TYPE]), nullptr);
184 ASSERT_EQ(std::get<std::string>(obj->value_[UDMF::UNIFORM_DATA_TYPE]), pixelMapUtdId_);
185 auto pixelMap = std::get_if<std::shared_ptr<PixelMap>>(&obj->value_[UDMF::PIXEL_MAP]);
186 ASSERT_NE(pixelMap, nullptr);
187 ImageInfo imageInfo = {};
188 (*pixelMap)->GetImageInfo(imageInfo);
189 ASSERT_EQ(imageInfo.size.height, 7);
190 ASSERT_EQ(imageInfo.size.width, 5);
191 ASSERT_EQ(imageInfo.pixelFormat, PixelFormat::ARGB_8888);
192 }
193
CheckLinkUds(const UDMF::ValueType & value)194 void PasteboardMultiTypeUnifiedDataDelayTest::CheckLinkUds(const UDMF::ValueType &value)
195 {
196 ASSERT_NE(std::get_if<std::shared_ptr<Object>>(&value), nullptr);
197 auto obj = std::get<std::shared_ptr<Object>>(value);
198 ASSERT_NE(obj, nullptr);
199 ASSERT_NE(std::get_if<std::string>(&obj->value_[UDMF::UNIFORM_DATA_TYPE]), nullptr);
200 ASSERT_EQ(std::get<std::string>(obj->value_[UDMF::UNIFORM_DATA_TYPE]), linkUtdId_);
201 ASSERT_NE(std::get_if<std::string>(&obj->value_[UDMF::URL]), nullptr);
202 ASSERT_EQ(std::get<std::string>(obj->value_[UDMF::URL]), link_);
203 }
204
205 /**
206 * @tc.name: SetMultiTypeUnifiedDataDelayTest001
207 * @tc.desc: add empty entry with entry getter to pasteboard
208 * @tc.type: FUNC
209 */
210 HWTEST_F(PasteboardMultiTypeUnifiedDataDelayTest, SetMultiTypeUnifiedDataDelayTest001, TestSize.Level1)
211 {
212 UnifiedData inputData;
213 std::shared_ptr<UnifiedRecord> inputRecord = std::make_shared<UnifiedRecord>();
214 std::vector<std::string> inputTypes;
215 inputTypes.emplace_back(plainTextUtdId_);
216 inputTypes.emplace_back(htmlUtdId_);
217 inputTypes.emplace_back(fileUriUtdId_);
218 inputTypes.emplace_back(pixelMapUtdId_);
219 inputTypes.emplace_back(linkUtdId_);
220 std::shared_ptr<EntryGetter> entryGetter = std::make_shared<EntryGetterImpl>();
221 inputRecord->SetEntryGetter(inputTypes, entryGetter);
222 inputData.AddRecord(inputRecord);
223 auto status = PasteboardClient::GetInstance()->SetUdsdData(inputData);
224 ASSERT_EQ(status, static_cast<int32_t>(PasteboardError::E_OK));
225
226 UnifiedData outputData;
227 status = PasteboardClient::GetInstance()->GetUdsdData(outputData);
228 ASSERT_EQ(status, static_cast<int32_t>(PasteboardError::E_OK));
229 auto inputRecords = inputData.GetRecords();
230 auto outputRecords = outputData.GetRecords();
231 ASSERT_EQ(outputRecords.size(), inputRecords.size());
232 auto outputRecord = outputData.GetRecordAt(0);
233 ASSERT_NE(outputRecord, nullptr);
234 auto outputTypes = outputRecord->GetUtdIds();
235 ASSERT_EQ(outputTypes.size(), inputTypes.size());
236
237 ASSERT_TRUE(outputTypes.find(plainTextUtdId_) != outputTypes.end());
238 CheckPlainUds(outputRecord->GetEntry(plainTextUtdId_));
239
240 ASSERT_TRUE(outputTypes.find(htmlUtdId_) != outputTypes.end());
241 CheckHtmlUds(outputRecord->GetEntry(htmlUtdId_));
242
243 ASSERT_TRUE(outputTypes.find(fileUriUtdId_) != outputTypes.end());
244 CheckFileUriUds(outputRecord->GetEntry(fileUriUtdId_));
245
246 ASSERT_TRUE(outputTypes.find(pixelMapUtdId_) != outputTypes.end());
247 CheckPixelMapUds(outputRecord->GetEntry(pixelMapUtdId_));
248
249 ASSERT_TRUE(outputTypes.find(linkUtdId_) != outputTypes.end());
250 CheckLinkUds(outputRecord->GetEntry(linkUtdId_));
251 }
252
253 /**
254 * @tc.name: SetMultiTypeUnifiedDataDelayTest002
255 * @tc.desc: add empty and valid entry with entry getter to pasteboard
256 * @tc.type: FUNC
257 */
258 HWTEST_F(PasteboardMultiTypeUnifiedDataDelayTest, SetMultiTypeUnifiedDataDelayTest002, TestSize.Level1)
259 {
260 UnifiedData inputData;
261 std::shared_ptr<UnifiedRecord> inputRecord = std::make_shared<UnifiedRecord>();
262 inputRecord->AddEntry(plainTextUtdId_, InitPlainUds());
263 inputRecord->AddEntry(pixelMapUtdId_, InitPixelMapUds());
264 std::vector<std::string> inputTypes;
265 inputTypes.emplace_back(htmlUtdId_);
266 inputTypes.emplace_back(fileUriUtdId_);
267 inputTypes.emplace_back(linkUtdId_);
268 std::shared_ptr<EntryGetter> entryGetter = std::make_shared<EntryGetterImpl>();
269 inputRecord->SetEntryGetter(inputTypes, entryGetter);
270 inputData.AddRecord(inputRecord);
271 auto status = PasteboardClient::GetInstance()->SetUdsdData(inputData);
272 ASSERT_EQ(status, static_cast<int32_t>(PasteboardError::E_OK));
273
274 UnifiedData outputData;
275 status = PasteboardClient::GetInstance()->GetUdsdData(outputData);
276 ASSERT_EQ(status, static_cast<int32_t>(PasteboardError::E_OK));
277 auto inputRecords = inputData.GetRecords();
278 auto outputRecords = outputData.GetRecords();
279 ASSERT_EQ(outputRecords.size(), inputRecords.size());
280 auto outputRecord = outputData.GetRecordAt(0);
281 ASSERT_NE(outputRecord, nullptr);
282 auto outputTypes = outputRecord->GetUtdIds();
283 auto tempTypes = inputRecord->GetUtdIds();
284 ASSERT_EQ(outputTypes.size(), tempTypes.size());
285
286 ASSERT_TRUE(outputTypes.find(plainTextUtdId_) != outputTypes.end());
287 CheckPlainUds(outputRecord->GetEntry(plainTextUtdId_));
288
289 ASSERT_TRUE(outputTypes.find(htmlUtdId_) != outputTypes.end());
290 CheckHtmlUds(outputRecord->GetEntry(htmlUtdId_));
291
292 ASSERT_TRUE(outputTypes.find(fileUriUtdId_) != outputTypes.end());
293 CheckFileUriUds(outputRecord->GetEntry(fileUriUtdId_));
294
295 ASSERT_TRUE(outputTypes.find(pixelMapUtdId_) != outputTypes.end());
296 CheckPixelMapUds(outputRecord->GetEntry(pixelMapUtdId_));
297
298 ASSERT_TRUE(outputTypes.find(linkUtdId_) != outputTypes.end());
299 CheckLinkUds(outputRecord->GetEntry(linkUtdId_));
300 }
301
302 /**
303 * @tc.name: SetMultiTypeUnifiedDataDelayTest003
304 * @tc.desc: add more unified record with entry getter to pasteboard
305 * @tc.type: FUNC
306 */
307 HWTEST_F(PasteboardMultiTypeUnifiedDataDelayTest, SetMultiTypeUnifiedDataDelayTest003, TestSize.Level1)
308 {
309 UnifiedData inputData;
310 std::shared_ptr<UnifiedRecord> inputRecord1 = std::make_shared<UnifiedRecord>();
311 std::vector<std::string> inputTypes1;
312 inputTypes1.emplace_back(plainTextUtdId_);
313 inputTypes1.emplace_back(htmlUtdId_);
314 std::shared_ptr<EntryGetter> entryGetter1 = std::make_shared<EntryGetterImpl>();
315 inputRecord1->SetEntryGetter(inputTypes1, entryGetter1);
316 inputData.AddRecord(inputRecord1);
317 std::shared_ptr<UnifiedRecord> inputRecord2 = std::make_shared<UnifiedRecord>();
318 std::vector<std::string> inputTypes2;
319 inputTypes2.emplace_back(fileUriUtdId_);
320 inputTypes2.emplace_back(pixelMapUtdId_);
321 std::shared_ptr<EntryGetter> entryGetter2 = std::make_shared<EntryGetterImpl>();
322 inputRecord2->SetEntryGetter(inputTypes2, entryGetter2);
323 inputData.AddRecord(inputRecord2);
324 std::shared_ptr<UnifiedRecord> inputRecord3 = std::make_shared<UnifiedRecord>();
325 std::vector<std::string> inputTypes3;
326 inputTypes3.emplace_back(linkUtdId_);
327 std::shared_ptr<EntryGetter> entryGetter3 = std::make_shared<EntryGetterImpl>();
328 inputRecord3->SetEntryGetter(inputTypes3, entryGetter3);
329 inputData.AddRecord(inputRecord3);
330 auto status = PasteboardClient::GetInstance()->SetUdsdData(inputData);
331 ASSERT_EQ(status, static_cast<int32_t>(PasteboardError::E_OK));
332 UnifiedData outputData;
333 status = PasteboardClient::GetInstance()->GetUdsdData(outputData);
334 ASSERT_EQ(status, static_cast<int32_t>(PasteboardError::E_OK));
335 auto inputRecords = inputData.GetRecords();
336 auto outputRecords = outputData.GetRecords();
337 ASSERT_EQ(outputRecords.size(), inputRecords.size());
338 auto outputRecord1 = outputData.GetRecordAt(0);
339 ASSERT_NE(outputRecord1, nullptr);
340 auto outputTypes1 = outputRecord1->GetUtdIds();
341 ASSERT_EQ(outputTypes1.size(), inputTypes1.size());
342 ASSERT_TRUE(outputTypes1.find(plainTextUtdId_) != outputTypes1.end());
343 CheckPlainUds(outputRecord1->GetEntry(plainTextUtdId_));
344 ASSERT_TRUE(outputTypes1.find(htmlUtdId_) != outputTypes1.end());
345 CheckHtmlUds(outputRecord1->GetEntry(htmlUtdId_));
346 auto outputRecord2 = outputData.GetRecordAt(1);
347 ASSERT_NE(outputRecord2, nullptr);
348 auto outputTypes2 = outputRecord2->GetUtdIds();
349 ASSERT_GE(outputTypes2.size(), inputTypes2.size());
350 ASSERT_TRUE(outputTypes2.find(fileUriUtdId_) != outputTypes2.end());
351 CheckFileUriUds(outputRecord2->GetEntry(fileUriUtdId_));
352 ASSERT_TRUE(outputTypes2.find(pixelMapUtdId_) != outputTypes2.end());
353 CheckPixelMapUds(outputRecord2->GetEntry(pixelMapUtdId_));
354 auto outputRecord3 = outputData.GetRecordAt(2);
355 ASSERT_NE(outputRecord3, nullptr);
356 auto outputTypes3 = outputRecord3->GetUtdIds();
357 ASSERT_EQ(outputTypes3.size(), inputTypes3.size());
358 ASSERT_TRUE(outputTypes3.find(linkUtdId_) != outputTypes3.end());
359 CheckLinkUds(outputRecord3->GetEntry(linkUtdId_));
360 }
361 } // namespace OHOS::MiscServices