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 #define LOG_TAG "UnifiedRecordTest"
16
17 #include <unistd.h>
18 #include <thread>
19 #include <gtest/gtest.h>
20 #include <string>
21
22 #include "application_defined_record.h"
23 #include "file.h"
24 #include "html.h"
25 #include "image.h"
26 #include "logger.h"
27 #include "plain_text.h"
28 #include "udmf_capi_common.h"
29 #include "udmf_client.h"
30 #include "unified_record.h"
31
32 using namespace testing::ext;
33 using namespace OHOS::UDMF;
34 using namespace OHOS;
35 namespace OHOS::Test {
36 using namespace std;
37
38 class UnifiedRecordTest : public testing::Test {
39 public:
40 static void SetUpTestCase();
41 static void TearDownTestCase();
42 void SetUp() override;
43 void TearDown() override;
44 };
45
SetUpTestCase()46 void UnifiedRecordTest::SetUpTestCase()
47 {
48 }
49
TearDownTestCase()50 void UnifiedRecordTest::TearDownTestCase()
51 {
52 }
53
SetUp()54 void UnifiedRecordTest::SetUp()
55 {
56 }
57
TearDown()58 void UnifiedRecordTest::TearDown()
59 {
60 }
61
62 /**
63 * @tc.name: GetSize001
64 * @tc.desc: Normal testcase of GetSize
65 * @tc.type: FUNC
66 */
67 HWTEST_F(UnifiedRecordTest, GetSize001, TestSize.Level1)
68 {
69 LOG_INFO(UDMF_TEST, "GetSize001 begin.");
70 UnifiedRecord unifiedRecord;
71 int64_t ret = unifiedRecord.GetSize();
72 EXPECT_EQ(ret, 0);
73 LOG_INFO(UDMF_TEST, "GetSize001 end.");
74 }
75
76 /**
77 * @tc.name: GetValue001
78 * @tc.desc: Normal testcase of GetValue
79 * @tc.type: FUNC
80 */
81 HWTEST_F(UnifiedRecordTest, GetValue001, TestSize.Level1)
82 {
83 LOG_INFO(UDMF_TEST, "GetValue001 begin.");
84 UnifiedRecord unifiedRecord;
85 unifiedRecord.value_ = "value";
86 ValueType ret = unifiedRecord.GetValue();
87 EXPECT_EQ(ret, unifiedRecord.value_);
88 LOG_INFO(UDMF_TEST, "GetValue001 end.");
89 }
90
91
92 /**
93 * @tc.name: Constructor_001
94 * @tc.desc: Verify the constructor.
95 * @tc.type: FUNC
96 */
97 HWTEST_F(UnifiedRecordTest, Constructor_001, TestSize.Level0)
98 {
99 UnifiedRecord record;
100
101 auto type = record.GetType();
102 EXPECT_EQ(type, UD_BUTT);
103
104 auto types = record.GetUtdIds();
105 EXPECT_TRUE(types.empty());
106
107 auto originValue = record.GetOriginValue();
108 EXPECT_TRUE(std::holds_alternative<std::monostate>(originValue));
109
110 auto entry = record.GetEntry("");
111 EXPECT_TRUE(std::holds_alternative<std::monostate>(entry));
112
113 auto entries = record.GetEntries();
114 EXPECT_TRUE(entries->empty());
115 }
116
117 /**
118 * @tc.name: Constructor_002
119 * @tc.desc: Verify the constructor.
120 * @tc.type: FUNC
121 */
122 HWTEST_F(UnifiedRecordTest, Constructor_002, TestSize.Level0)
123 {
124 auto utdId = UtdUtils::GetUtdIdFromUtdEnum(TEXT);
125
126 UnifiedRecord record(TEXT);
127 auto type = record.GetType();
128 EXPECT_EQ(type, TEXT);
129 auto types = record.GetUtdIds();
130 EXPECT_TRUE(types.find(utdId) != types.end());
131
132 auto originValue = record.GetOriginValue();
133 EXPECT_TRUE(std::holds_alternative<std::monostate>(originValue));
134
135 auto entry = record.GetEntry(utdId);
136 EXPECT_TRUE(std::holds_alternative<std::monostate>(entry));
137
138 auto entries = record.GetEntries();
139 auto it = entries->find(utdId);
140 ASSERT_TRUE(it != entries->end());
141 EXPECT_FALSE(std::holds_alternative<std::monostate>(it->second));
142 EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(it->second));
143 }
144
145 /**
146 * @tc.name: Constructor_003
147 * @tc.desc: Verify the constructor.
148 * @tc.type: FUNC
149 */
150 HWTEST_F(UnifiedRecordTest, Constructor_003, TestSize.Level0)
151 {
152 auto utdId = UtdUtils::GetUtdIdFromUtdEnum(TEXT);
153 UnifiedRecord record(TEXT, "123456");
154 auto type = record.GetType();
155 EXPECT_EQ(type, TEXT);
156 auto types = record.GetUtdIds();
157 EXPECT_TRUE(types.find(utdId) != types.end());
158
159 auto originValue = record.GetOriginValue();
160 EXPECT_TRUE(std::holds_alternative<std::string>(originValue));
161 auto originValueStr = std::get_if<std::string>(&originValue);
162 EXPECT_EQ(*originValueStr, "123456");
163
164 auto entry = record.GetEntry(utdId);
165 EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(entry));
166 auto entryStr = std::get<std::shared_ptr<Object>>(entry);
167 EXPECT_EQ(std::get<std::string>(entryStr->value_[VALUE_TYPE]), "123456");
168
169 auto entries = record.GetEntries();
170 auto it = entries->find(utdId);
171 ASSERT_TRUE(it != entries->end());
172 auto entry2 = it->second;
173 EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(entry2));
174 }
175
176 /**
177 * @tc.name: AddEntry_001
178 * @tc.desc: Normal testcase of AddEntry
179 * @tc.type: FUNC
180 */
181 HWTEST_F(UnifiedRecordTest, AddEntry_001, TestSize.Level0)
182 {
183 std::string utdId = "utdId";
184 ValueType value = "value";
185 UnifiedRecord unifiedRecord;
186 std::thread t1(&UnifiedRecord::AddEntry, std::ref(unifiedRecord), utdId, value);
187 EXPECT_NO_FATAL_FAILURE(t1.join());
188
189 std::string utdId1 = "utdId1";
190 ValueType value1 = "value1";
191 std::thread t2(&UnifiedRecord::AddEntry, std::ref(unifiedRecord), utdId1, value1);
192 EXPECT_NO_FATAL_FAILURE(t2.join());
193
194 std::string utdId2 = "utdId2";
195 ValueType value2 = "value2";
196 std::thread t3(&UnifiedRecord::AddEntry, std::ref(unifiedRecord), utdId2, value2);
197 EXPECT_NO_FATAL_FAILURE(t3.join());
198
199 std::string utdId3 = "utdId3";
200 ValueType value3 = "value3";
201 std::thread t4(&UnifiedRecord::AddEntry, std::ref(unifiedRecord), utdId3, value3);
202 EXPECT_NO_FATAL_FAILURE(t4.join());
203
204 std::string utdId4 = "utdId4";
205 ValueType value4 = "value4";
206 std::thread t5(&UnifiedRecord::AddEntry, std::ref(unifiedRecord), utdId4, value4);
207 EXPECT_NO_FATAL_FAILURE(t5.join());
208
209 std::string utdId5 = "utdId5";
210 ValueType value5 = "value5";
211 std::thread t6(&UnifiedRecord::AddEntry, std::ref(unifiedRecord), utdId5, value5);
212 EXPECT_NO_FATAL_FAILURE(t6.join());
213
214 std::string utdId6 = "utdId6";
215 ValueType value6 = "value6";
216 std::thread t7(&UnifiedRecord::AddEntry, std::ref(unifiedRecord), utdId6, value6);
217 EXPECT_NO_FATAL_FAILURE(t7.join());
218
219 std::string utdId7 = "utdId7";
220 ValueType value7 = "value7";
221 std::thread t8(&UnifiedRecord::AddEntry, std::ref(unifiedRecord), utdId7, value7);
222 EXPECT_NO_FATAL_FAILURE(t8.join());
223
224 std::string utdId8 = "utdId8";
225 ValueType value8 = "value8";
226 std::thread t9(&UnifiedRecord::AddEntry, std::ref(unifiedRecord), utdId8, value8);
227 EXPECT_NO_FATAL_FAILURE(t9.join());
228
229 std::string utdId9 = "utdId9";
230 ValueType value9 = "value9";
231 std::thread t10(&UnifiedRecord::AddEntry, std::ref(unifiedRecord), utdId9, value9);
232 EXPECT_NO_FATAL_FAILURE(t10.join());
233 }
234
235 /**
236 * @tc.name: GetEntryTest001
237 * @tc.desc: Test set a UDC data, then get data with GetEntry function.
238 * @tc.type: FUNC
239 */
240 HWTEST_F(UnifiedRecordTest, GetEntryTest001, TestSize.Level1)
241 {
242 LOG_INFO(UDMF_TEST, "GetEntryTest001 begin.");
243 UnifiedData data;
244 std::shared_ptr<File> file = std::make_shared<File>();
245 file->SetUri("https://file/txt.txt");
246 data.AddRecord(file);
247 auto image = std::make_shared<Image>();
248 image->SetUri("https://file/txt.txt");
249 data.AddRecord(image);
250 auto applicationDefinedRecord = std::make_shared<ApplicationDefinedRecord>();
251 applicationDefinedRecord->SetRawData({ 'a', 'b' });
252 data.AddRecord(applicationDefinedRecord);
253 auto html = std::make_shared<Html>();
254 html->SetHtmlContent("content");
255 data.AddRecord(html);
256 auto plainText = std::make_shared<PlainText>();
257 plainText->SetContent("content");
258 data.AddRecord(plainText);
259 CustomOption option1 = { .intention = Intention::UD_INTENTION_DRAG };
260 std::string key;
261 auto status = UdmfClient::GetInstance().SetData(option1, data, key);
262 ASSERT_EQ(status, E_OK);
263 QueryOption option2 = { .key = key };
264 UnifiedData readData;
265 status = UdmfClient::GetInstance().GetData(option2, readData);
266 auto recordFile = readData.GetRecordAt(0);
267 auto udsValue = recordFile->GetEntry("general.file-uri");
268 EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(udsValue));
269 std::shared_ptr<Object> fileUds = std::get<std::shared_ptr<Object>>(udsValue);
270 EXPECT_EQ(std::get<std::string>(fileUds->value_[ORI_URI]), "https://file/txt.txt");
271 auto recordImage = readData.GetRecordAt(1);
272 auto imageValue = recordImage->GetEntry("general.file-uri");
273 EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(imageValue));
274 std::shared_ptr<Object> imageUds = std::get<std::shared_ptr<Object>>(imageValue);
275 EXPECT_EQ(std::get<std::string>(imageUds->value_[ORI_URI]), "https://file/txt.txt");
276 auto recordAppDefined = readData.GetRecordAt(2);
277 auto appDefinedValue = recordAppDefined->GetEntry("ApplicationDefinedType");
278 EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(appDefinedValue));
279 std::shared_ptr<Object> appDefinedUds = std::get<std::shared_ptr<Object>>(appDefinedValue);
280 EXPECT_EQ(std::get<std::vector<uint8_t>>(appDefinedUds->value_[ARRAY_BUFFER])[0], 'a');
281 LOG_INFO(UDMF_TEST, "GetEntryTest001 end.");
282 }
283
284 /**
285 * @tc.name: GetEntryTest002
286 * @tc.desc: Test set a UDC data, then get data with GetEntry function.
287 * @tc.type: FUNC
288 */
289 HWTEST_F(UnifiedRecordTest, GetEntryTest002, TestSize.Level1)
290 {
291 UnifiedData data;
292 std::shared_ptr<File> file = std::make_shared<File>("https://file/txt.txt");
293 data.AddRecord(file);
294 auto image = std::make_shared<Image>("https://file/txt.txt");
295 data.AddRecord(image);
296 std::vector<uint8_t> value = { 'a', 'b' };
297 auto applicationDefinedRecord = std::make_shared<ApplicationDefinedRecord>("test", value);
298 data.AddRecord(applicationDefinedRecord);
299 auto html = std::make_shared<Html>("content", "plaintext content");
300 data.AddRecord(html);
301 auto plainText = std::make_shared<PlainText>("content", "abstract");
302 data.AddRecord(plainText);
303 CustomOption option1 = { .intention = Intention::UD_INTENTION_DRAG };
304 std::string key;
305 auto status = UdmfClient::GetInstance().SetData(option1, data, key);
306 QueryOption option2 = { .key = key };
307 UnifiedData readData;
308 status = UdmfClient::GetInstance().GetData(option2, readData);
309 auto recordFile = readData.GetRecordAt(0);
310 auto udsValue = recordFile->GetEntry("general.file-uri");
311 EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(udsValue));
312 std::shared_ptr<Object> fileUds = std::get<std::shared_ptr<Object>>(udsValue);
313 EXPECT_EQ(std::get<std::string>(fileUds->value_[ORI_URI]), "https://file/txt.txt");
314 auto recordImage = readData.GetRecordAt(1);
315 auto imageValue = recordImage->GetEntry("general.file-uri");
316 EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(imageValue));
317 std::shared_ptr<Object> imageUds = std::get<std::shared_ptr<Object>>(imageValue);
318 EXPECT_EQ(std::get<std::string>(imageUds->value_[ORI_URI]), "https://file/txt.txt");
319 auto recordAppDefined = readData.GetRecordAt(2);
320 auto appDefinedValue = recordAppDefined->GetEntry("test");
321 EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(appDefinedValue));
322 std::shared_ptr<Object> appDefinedUds = std::get<std::shared_ptr<Object>>(appDefinedValue);
323 EXPECT_EQ(std::get<std::vector<uint8_t>>(appDefinedUds->value_[ARRAY_BUFFER])[0], 'a');
324 auto recordHtml = readData.GetRecordAt(3);
325 auto htmlValue = recordHtml->GetEntry("general.html");
326 EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(htmlValue));
327 std::shared_ptr<Object> htmlUds = std::get<std::shared_ptr<Object>>(htmlValue);
328 EXPECT_EQ(std::get<std::string>(htmlUds->value_[HTML_CONTENT]), "content");
329 EXPECT_EQ(std::get<std::string>(htmlUds->value_[PLAIN_CONTENT]), "plaintext content");
330 auto recordPlainText = readData.GetRecordAt(4);
331 auto plainTextValue = recordPlainText->GetEntry("general.plain-text");
332 EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(plainTextValue));
333 std::shared_ptr<Object> plainTextUds = std::get<std::shared_ptr<Object>>(plainTextValue);
334 EXPECT_EQ(std::get<std::string>(plainTextUds->value_[CONTENT]), "content");
335 EXPECT_EQ(std::get<std::string>(plainTextUds->value_[ABSTRACT]), "abstract");
336 }
337
338 /**
339 * @tc.name: GetEntryTest003
340 * @tc.desc: Test set a base UnifiedRecord data, then get data with GetEntry function.
341 * @tc.type: FUNC
342 */
343 HWTEST_F(UnifiedRecordTest, GetEntryTest003, TestSize.Level1)
344 {
345 UnifiedData data;
346 std::shared_ptr<UnifiedRecord> record1 = std::make_shared<UnifiedRecord>(VIDEO, "video");
347 data.AddRecord(record1);
348 std::shared_ptr<UnifiedRecord> record2 = std::make_shared<UnifiedRecord>(HTML, "html");
349 data.AddRecord(record2);
350 std::shared_ptr<UnifiedRecord> record3 = std::make_shared<UnifiedRecord>(KINGSOFT_WRITER_WPT, "abc");
351 data.AddRecord(record3);
352 auto obj = std::make_shared<Object>();
353 obj->value_["uniformDataType"] = "general.plain-text";
354 obj->value_["textContent"] = "plainTextContent";
355 std::shared_ptr<UnifiedRecord> record4 = std::make_shared<UnifiedRecord>(PLAIN_TEXT, obj);
356 data.AddRecord(record4);
357 std::shared_ptr<UnifiedRecord> record5 = std::make_shared<UnifiedRecord>(HYPERLINK);
358 data.AddRecord(record5);
359 CustomOption option1 = { .intention = Intention::UD_INTENTION_DRAG };
360 std::string key;
361 auto status = UdmfClient::GetInstance().SetData(option1, data, key);
362 QueryOption option2 = { .key = key };
363 UnifiedData readData;
364 status = UdmfClient::GetInstance().GetData(option2, readData);
365 auto recordFirst = readData.GetRecordAt(0);
366 auto firstValue = recordFirst->GetEntry("general.video");
367 EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(firstValue));
368 std::shared_ptr<Object> firstUds = std::get<std::shared_ptr<Object>>(firstValue);
369 EXPECT_EQ(std::get<std::string>(firstUds->value_[VALUE_TYPE]), "video");
370 auto recordSecond = readData.GetRecordAt(1);
371 auto secondValue = recordSecond->GetEntry("general.html");
372 EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(secondValue));
373 std::shared_ptr<Object> secondUds = std::get<std::shared_ptr<Object>>(secondValue);
374 EXPECT_EQ(std::get<std::string>(secondUds->value_[VALUE_TYPE]), "html");
375 auto recordThird = readData.GetRecordAt(2);
376 auto thirdValue = recordThird->GetEntry("com.kingsoft.office.writer.wpt");
377 EXPECT_TRUE(std::holds_alternative<std::string>(thirdValue));
378 EXPECT_EQ(std::get<std::string>(thirdValue), "abc");
379 auto recordFourth = readData.GetRecordAt(3);
380 auto fourthValue = recordFourth->GetEntry("general.plain-text");
381 EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(fourthValue));
382 std::shared_ptr<Object> fourthUds = std::get<std::shared_ptr<Object>>(fourthValue);
383 EXPECT_EQ(std::get<std::string>(fourthUds->value_["textContent"]), "plainTextContent");
384 auto recordFifth = readData.GetRecordAt(4);
385 auto fifthValue = recordFifth->GetEntry("general.hyperlink");
386 EXPECT_TRUE(std::holds_alternative<std::monostate>(
387 std::get<std::shared_ptr<Object>>(fifthValue)->value_[VALUE_TYPE]));
388 }
389 } // OHOS::Test