• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * @tc.name: Constructor_001
93  * @tc.desc: Verify the constructor.
94  * @tc.type: FUNC
95  */
96 HWTEST_F(UnifiedRecordTest, Constructor_001, TestSize.Level0)
97 {
98     UnifiedRecord record;
99 
100     auto type = record.GetType();
101     EXPECT_EQ(type, UD_BUTT);
102 
103     auto types = record.GetUtdIds();
104     EXPECT_TRUE(types.empty());
105 
106     auto originValue = record.GetOriginValue();
107     EXPECT_TRUE(std::holds_alternative<std::monostate>(originValue));
108 
109     auto entry = record.GetEntry("");
110     EXPECT_TRUE(std::holds_alternative<std::monostate>(entry));
111 
112     auto entries = record.GetEntries();
113     EXPECT_TRUE(entries->empty());
114 }
115 
116 /**
117  * @tc.name: Constructor_002
118  * @tc.desc: Verify the constructor.
119  * @tc.type: FUNC
120  */
121 HWTEST_F(UnifiedRecordTest, Constructor_002, TestSize.Level0)
122 {
123     auto utdId = UtdUtils::GetUtdIdFromUtdEnum(TEXT);
124 
125     UnifiedRecord record(TEXT);
126     auto type = record.GetType();
127     EXPECT_EQ(type, TEXT);
128     auto types = record.GetUtdIds();
129     EXPECT_TRUE(types.find(utdId) != types.end());
130 
131     auto originValue = record.GetOriginValue();
132     EXPECT_TRUE(std::holds_alternative<std::monostate>(originValue));
133 
134     auto entry = record.GetEntry(utdId);
135     EXPECT_TRUE(std::holds_alternative<std::monostate>(entry));
136 
137     auto entries = record.GetEntries();
138     auto it = entries->find(utdId);
139     ASSERT_TRUE(it != entries->end());
140     EXPECT_FALSE(std::holds_alternative<std::monostate>(it->second));
141     EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(it->second));
142 }
143 
144 /**
145  * @tc.name: GetDataId_001
146  * @tc.desc: Get dataId.
147  * @tc.type: FUNC
148  */
149 HWTEST_F(UnifiedRecordTest, GetDataId_001, TestSize.Level0)
150 {
151     UnifiedRecord record(TEXT);
152     uint32_t dataId = 1;
153     record.SetDataId(dataId);
154     auto data = record.GetDataId();
155     EXPECT_EQ(data, 1);
156 }
157 
158 /**
159  * @tc.name: SetChannelName_001
160  * @tc.desc: Set channelName.
161  * @tc.type: FUNC
162  */
163 HWTEST_F(UnifiedRecordTest, SetChannelName_001, TestSize.Level0)
164 {
165     UnifiedRecord record(TEXT);
166     std::string channelName = "channelName";
167     EXPECT_NO_FATAL_FAILURE(record.SetChannelName(channelName););
168 }
169 
170 /**
171  * @tc.name: Constructor_003
172  * @tc.desc: Verify the constructor.
173  * @tc.type: FUNC
174  */
175 HWTEST_F(UnifiedRecordTest, Constructor_003, TestSize.Level0)
176 {
177     auto utdId = UtdUtils::GetUtdIdFromUtdEnum(TEXT);
178     UnifiedRecord record(TEXT, "123456");
179     auto type = record.GetType();
180     EXPECT_EQ(type, TEXT);
181     auto types = record.GetUtdIds();
182     EXPECT_TRUE(types.find(utdId) != types.end());
183 
184     auto originValue = record.GetOriginValue();
185     EXPECT_TRUE(std::holds_alternative<std::string>(originValue));
186     auto originValueStr = std::get_if<std::string>(&originValue);
187     EXPECT_EQ(*originValueStr, "123456");
188 
189     auto entry = record.GetEntry(utdId);
190     EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(entry));
191     auto entryStr = std::get<std::shared_ptr<Object>>(entry);
192     EXPECT_EQ(std::get<std::string>(entryStr->value_[VALUE_TYPE]), "123456");
193 
194     auto entries = record.GetEntries();
195     auto it = entries->find(utdId);
196     ASSERT_TRUE(it != entries->end());
197     auto entry2 = it->second;
198     EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(entry2));
199 }
200 
201 /**
202  * @tc.name: AddEntry_001
203  * @tc.desc: Normal testcase of AddEntry
204  * @tc.type: FUNC
205  */
206 HWTEST_F(UnifiedRecordTest, AddEntry_001, TestSize.Level0)
207 {
208     std::string utdId = "utdId";
209     ValueType value = "value";
210     UnifiedRecord unifiedRecord;
211     std::thread t1(&UnifiedRecord::AddEntry, std::ref(unifiedRecord), utdId, value);
212     EXPECT_NO_FATAL_FAILURE(t1.join());
213 
214     std::string utdId1 = "utdId1";
215     ValueType value1 = "value1";
216     std::thread t2(&UnifiedRecord::AddEntry, std::ref(unifiedRecord), utdId1, value1);
217     EXPECT_NO_FATAL_FAILURE(t2.join());
218 
219     std::string utdId2 = "utdId2";
220     ValueType value2 = "value2";
221     std::thread t3(&UnifiedRecord::AddEntry, std::ref(unifiedRecord), utdId2, value2);
222     EXPECT_NO_FATAL_FAILURE(t3.join());
223 
224     std::string utdId3 = "utdId3";
225     ValueType value3 = "value3";
226     std::thread t4(&UnifiedRecord::AddEntry, std::ref(unifiedRecord), utdId3, value3);
227     EXPECT_NO_FATAL_FAILURE(t4.join());
228 
229     std::string utdId4 = "utdId4";
230     ValueType value4 = "value4";
231     std::thread t5(&UnifiedRecord::AddEntry, std::ref(unifiedRecord), utdId4, value4);
232     EXPECT_NO_FATAL_FAILURE(t5.join());
233 
234     std::string utdId5 = "utdId5";
235     ValueType value5 = "value5";
236     std::thread t6(&UnifiedRecord::AddEntry, std::ref(unifiedRecord), utdId5, value5);
237     EXPECT_NO_FATAL_FAILURE(t6.join());
238 
239     std::string utdId6 = "utdId6";
240     ValueType value6 = "value6";
241     std::thread t7(&UnifiedRecord::AddEntry, std::ref(unifiedRecord), utdId6, value6);
242     EXPECT_NO_FATAL_FAILURE(t7.join());
243 
244     std::string utdId7 = "utdId7";
245     ValueType value7 = "value7";
246     std::thread t8(&UnifiedRecord::AddEntry, std::ref(unifiedRecord), utdId7, value7);
247     EXPECT_NO_FATAL_FAILURE(t8.join());
248 
249     std::string utdId8 = "utdId8";
250     ValueType value8 = "value8";
251     std::thread t9(&UnifiedRecord::AddEntry, std::ref(unifiedRecord), utdId8, value8);
252     EXPECT_NO_FATAL_FAILURE(t9.join());
253 
254     std::string utdId9 = "utdId9";
255     ValueType value9 = "value9";
256     std::thread t10(&UnifiedRecord::AddEntry, std::ref(unifiedRecord), utdId9, value9);
257     EXPECT_NO_FATAL_FAILURE(t10.join());
258 }
259 
260 /**
261 * @tc.name: GetEntryTest001
262 * @tc.desc: Test set a UDC data, then get data with GetEntry function.
263 * @tc.type: FUNC
264 */
265 HWTEST_F(UnifiedRecordTest, GetEntryTest001, TestSize.Level1)
266 {
267     LOG_INFO(UDMF_TEST, "GetEntryTest001 begin.");
268     UnifiedData data;
269     std::shared_ptr<File> file = std::make_shared<File>();
270     file->SetUri("https://file/txt.txt");
271     data.AddRecord(file);
272     auto image = std::make_shared<Image>();
273     image->SetUri("https://file/txt.txt");
274     data.AddRecord(image);
275     auto applicationDefinedRecord = std::make_shared<ApplicationDefinedRecord>();
276     applicationDefinedRecord->SetRawData({ 'a', 'b' });
277     data.AddRecord(applicationDefinedRecord);
278     auto html = std::make_shared<Html>();
279     html->SetHtmlContent("content");
280     data.AddRecord(html);
281     auto plainText = std::make_shared<PlainText>();
282     plainText->SetContent("content");
283     data.AddRecord(plainText);
284     CustomOption option1 = { .intention = Intention::UD_INTENTION_DRAG };
285     std::string key;
286     auto status = UdmfClient::GetInstance().SetData(option1, data, key);
287     ASSERT_EQ(status, E_OK);
288     QueryOption option2 = { .key = key };
289     UnifiedData readData;
290     status = UdmfClient::GetInstance().GetData(option2, readData);
291     auto recordFile = readData.GetRecordAt(0);
292     auto udsValue = recordFile->GetEntry("general.file-uri");
293     EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(udsValue));
294     std::shared_ptr<Object> fileUds = std::get<std::shared_ptr<Object>>(udsValue);
295     EXPECT_EQ(std::get<std::string>(fileUds->value_[ORI_URI]), "https://file/txt.txt");
296     auto recordImage = readData.GetRecordAt(1);
297     auto imageValue = recordImage->GetEntry("general.file-uri");
298     EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(imageValue));
299     std::shared_ptr<Object> imageUds = std::get<std::shared_ptr<Object>>(imageValue);
300     EXPECT_EQ(std::get<std::string>(imageUds->value_[ORI_URI]), "https://file/txt.txt");
301     auto recordAppDefined = readData.GetRecordAt(2);
302     auto appDefinedValue = recordAppDefined->GetEntry("ApplicationDefinedType");
303     EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(appDefinedValue));
304     std::shared_ptr<Object> appDefinedUds = std::get<std::shared_ptr<Object>>(appDefinedValue);
305     EXPECT_EQ(std::get<std::vector<uint8_t>>(appDefinedUds->value_[ARRAY_BUFFER])[0], 'a');
306     LOG_INFO(UDMF_TEST, "GetEntryTest001 end.");
307 }
308 
309 /**
310 * @tc.name: GetEntryTest002
311 * @tc.desc: Test set a UDC data, then get data with GetEntry function.
312 * @tc.type: FUNC
313 */
314 HWTEST_F(UnifiedRecordTest, GetEntryTest002, TestSize.Level1)
315 {
316     UnifiedData data;
317     std::shared_ptr<File> file = std::make_shared<File>("https://file/txt.txt");
318     data.AddRecord(file);
319     auto image = std::make_shared<Image>("https://file/txt.txt");
320     data.AddRecord(image);
321     std::vector<uint8_t> value =  { 'a', 'b' };
322     auto applicationDefinedRecord = std::make_shared<ApplicationDefinedRecord>("test", value);
323     data.AddRecord(applicationDefinedRecord);
324     auto html = std::make_shared<Html>("content", "plaintext content");
325     data.AddRecord(html);
326     auto plainText = std::make_shared<PlainText>("content", "abstract");
327     data.AddRecord(plainText);
328     CustomOption option1 = { .intention = Intention::UD_INTENTION_DRAG };
329     std::string key;
330     auto status = UdmfClient::GetInstance().SetData(option1, data, key);
331     QueryOption option2 = { .key = key };
332     UnifiedData readData;
333     status = UdmfClient::GetInstance().GetData(option2, readData);
334     auto recordFile = readData.GetRecordAt(0);
335     auto udsValue = recordFile->GetEntry("general.file-uri");
336     EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(udsValue));
337     std::shared_ptr<Object> fileUds = std::get<std::shared_ptr<Object>>(udsValue);
338     EXPECT_EQ(std::get<std::string>(fileUds->value_[ORI_URI]), "https://file/txt.txt");
339     auto recordImage = readData.GetRecordAt(1);
340     auto imageValue = recordImage->GetEntry("general.file-uri");
341     EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(imageValue));
342     std::shared_ptr<Object> imageUds = std::get<std::shared_ptr<Object>>(imageValue);
343     EXPECT_EQ(std::get<std::string>(imageUds->value_[ORI_URI]), "https://file/txt.txt");
344     auto recordAppDefined = readData.GetRecordAt(2);
345     auto appDefinedValue = recordAppDefined->GetEntry("test");
346     EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(appDefinedValue));
347     std::shared_ptr<Object> appDefinedUds = std::get<std::shared_ptr<Object>>(appDefinedValue);
348     EXPECT_EQ(std::get<std::vector<uint8_t>>(appDefinedUds->value_[ARRAY_BUFFER])[0], 'a');
349     auto recordHtml = readData.GetRecordAt(3);
350     auto htmlValue = recordHtml->GetEntry("general.html");
351     EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(htmlValue));
352     std::shared_ptr<Object> htmlUds = std::get<std::shared_ptr<Object>>(htmlValue);
353     EXPECT_EQ(std::get<std::string>(htmlUds->value_[HTML_CONTENT]), "content");
354     EXPECT_EQ(std::get<std::string>(htmlUds->value_[PLAIN_CONTENT]), "plaintext content");
355     auto recordPlainText = readData.GetRecordAt(4);
356     auto plainTextValue = recordPlainText->GetEntry("general.plain-text");
357     EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(plainTextValue));
358     std::shared_ptr<Object> plainTextUds = std::get<std::shared_ptr<Object>>(plainTextValue);
359     EXPECT_EQ(std::get<std::string>(plainTextUds->value_[CONTENT]), "content");
360     EXPECT_EQ(std::get<std::string>(plainTextUds->value_[ABSTRACT]), "abstract");
361 }
362 
363 /**
364 * @tc.name: GetEntryTest003
365 * @tc.desc: Test set a base UnifiedRecord data, then get data with GetEntry function.
366 * @tc.type: FUNC
367 */
368 HWTEST_F(UnifiedRecordTest, GetEntryTest003, TestSize.Level1)
369 {
370     UnifiedData data;
371     std::shared_ptr<UnifiedRecord> record1 = std::make_shared<UnifiedRecord>(VIDEO, "video");
372     data.AddRecord(record1);
373     std::shared_ptr<UnifiedRecord> record2 = std::make_shared<UnifiedRecord>(HTML, "html");
374     data.AddRecord(record2);
375     std::shared_ptr<UnifiedRecord> record3 = std::make_shared<UnifiedRecord>(KINGSOFT_WRITER_WPT, "abc");
376     data.AddRecord(record3);
377     auto obj = std::make_shared<Object>();
378     obj->value_["uniformDataType"] = "general.plain-text";
379     obj->value_["textContent"] = "plainTextContent";
380     std::shared_ptr<UnifiedRecord> record4 = std::make_shared<UnifiedRecord>(PLAIN_TEXT, obj);
381     data.AddRecord(record4);
382     std::shared_ptr<UnifiedRecord> record5 = std::make_shared<UnifiedRecord>(HYPERLINK);
383     data.AddRecord(record5);
384     CustomOption option1 = { .intention = Intention::UD_INTENTION_DRAG };
385     std::string key;
386     auto status = UdmfClient::GetInstance().SetData(option1, data, key);
387     QueryOption option2 = { .key = key };
388     UnifiedData readData;
389     status = UdmfClient::GetInstance().GetData(option2, readData);
390     auto recordFirst = readData.GetRecordAt(0);
391     auto firstValue = recordFirst->GetEntry("general.video");
392     EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(firstValue));
393     std::shared_ptr<Object> firstUds = std::get<std::shared_ptr<Object>>(firstValue);
394     EXPECT_EQ(std::get<std::string>(firstUds->value_[VALUE_TYPE]), "video");
395     auto recordSecond = readData.GetRecordAt(1);
396     auto secondValue = recordSecond->GetEntry("general.html");
397     EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(secondValue));
398     std::shared_ptr<Object> secondUds = std::get<std::shared_ptr<Object>>(secondValue);
399     EXPECT_EQ(std::get<std::string>(secondUds->value_[VALUE_TYPE]), "html");
400     auto recordThird = readData.GetRecordAt(2);
401     auto thirdValue = recordThird->GetEntry("com.kingsoft.office.writer.wpt");
402     EXPECT_TRUE(std::holds_alternative<std::string>(thirdValue));
403     EXPECT_EQ(std::get<std::string>(thirdValue), "abc");
404     auto recordFourth = readData.GetRecordAt(3);
405     auto fourthValue = recordFourth->GetEntry("general.plain-text");
406     EXPECT_TRUE(std::holds_alternative<std::shared_ptr<Object>>(fourthValue));
407     std::shared_ptr<Object> fourthUds = std::get<std::shared_ptr<Object>>(fourthValue);
408     EXPECT_EQ(std::get<std::string>(fourthUds->value_["textContent"]), "plainTextContent");
409     auto recordFifth = readData.GetRecordAt(4);
410     auto fifthValue = recordFifth->GetEntry("general.hyperlink");
411     EXPECT_TRUE(std::holds_alternative<std::monostate>(
412         std::get<std::shared_ptr<Object>>(fifthValue)->value_[VALUE_TYPE]));
413 }
414 
415 /**
416  * @tc.name: ComputeUris_001
417  * @tc.desc: Compute uri
418  * @tc.type: FUNC
419  */
420 HWTEST_F(UnifiedRecordTest, ComputeUris_001, TestSize.Level0)
421 {
422     UnifiedRecord record(TEXT);
423     const std::function<bool(UriInfo &)> action;
424     EXPECT_NO_FATAL_FAILURE(record.ComputeUris(action));
425 }
426 } // OHOS::Test