• 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 "UnifiedDataTest"
16 
17 #include <unistd.h>
18 #include <gtest/gtest.h>
19 #include <string>
20 
21 #include "file.h"
22 #include "image.h"
23 #include "logger.h"
24 #include "plain_text.h"
25 #include "udmf_capi_common.h"
26 #include "udmf_utils.h"
27 #include "unified_data.h"
28 
29 using namespace testing::ext;
30 using namespace OHOS::UDMF;
31 using namespace OHOS;
32 namespace OHOS::Test {
33 using namespace std;
34 
35 class UnifiedDataTest : public testing::Test {
36 public:
37     static void SetUpTestCase();
38     static void TearDownTestCase();
39     void SetUp() override;
40     void TearDown() override;
41     void TransferToEntriesCompareEntries(UnifiedRecord* recordFirst);
42 };
43 
SetUpTestCase()44 void UnifiedDataTest::SetUpTestCase()
45 {
46 }
47 
TearDownTestCase()48 void UnifiedDataTest::TearDownTestCase()
49 {
50 }
51 
SetUp()52 void UnifiedDataTest::SetUp()
53 {
54 }
55 
TearDown()56 void UnifiedDataTest::TearDown()
57 {
58 }
59 
TransferToEntriesCompareEntries(UnifiedRecord * recordFirst)60 void UnifiedDataTest::TransferToEntriesCompareEntries(UnifiedRecord* recordFirst)
61 {
62     auto plainTextFirst = static_cast<PlainText*>(recordFirst);
63     EXPECT_EQ(plainTextFirst->GetAbstract(), "abstract");
64     EXPECT_EQ(plainTextFirst->GetContent(), "http://1111/a.img");
65     std::set<std::string> utdIds = recordFirst->GetUtdIds();
66     EXPECT_TRUE(utdIds.find("general.plain-text") != utdIds.end());
67     EXPECT_TRUE(utdIds.find("general.file-uri") != utdIds.end());
68     auto fileEntry = recordFirst->GetEntry("general.file-uri");
69     std::shared_ptr<Object> fileEntryObj = std::get<std::shared_ptr<Object>>(fileEntry);
70     std::string getUri;
71     fileEntryObj->GetValue(ORI_URI, getUri);
72     EXPECT_EQ(getUri, "http://1111/a.mp4");
73     auto plainTextEntry = recordFirst->GetEntry("general.plain-text");
74     std::shared_ptr<Object> plainTextEntryObj = std::get<std::shared_ptr<Object>>(plainTextEntry);
75     std::string getContent;
76     plainTextEntryObj->GetValue(CONTENT, getContent);
77     EXPECT_EQ(getContent, "http://1111/a.img");
78     std::string getAbstract;
79     plainTextEntryObj->GetValue(ABSTRACT, getAbstract);
80     EXPECT_EQ(getAbstract, "abstract");
81     auto entries = recordFirst->GetEntries();
82     EXPECT_NE(entries, nullptr);
83     int entrySize = 2;
84     EXPECT_EQ(entries->size(), entrySize);
85     auto plainTextEntry1 = (*entries)["general.plain-text"];
86     std::shared_ptr<Object> plainTextEntryObj1 = std::get<std::shared_ptr<Object>>(plainTextEntry1);
87     std::string content;
88     plainTextEntryObj1->GetValue(CONTENT, content);
89     EXPECT_EQ(content, "http://1111/a.img");
90     std::string abstract;
91     plainTextEntryObj1->GetValue(ABSTRACT, abstract);
92     EXPECT_EQ(abstract, "abstract");
93     auto fileUriEntry1 = (*entries)["general.file-uri"];
94     std::shared_ptr<Object> fileUriEntryObj1 = std::get<std::shared_ptr<Object>>(fileUriEntry1);
95     std::string oriUri;
96     fileUriEntryObj1->GetValue(FILE_URI_PARAM, oriUri);
97     EXPECT_EQ(oriUri, "http://1111/a.mp4");
98     std::string fileType;
99     fileUriEntryObj1->GetValue(FILE_TYPE, fileType);
100     EXPECT_EQ(fileType, "general.media");
101 }
102 
103 /**
104 * @tc.name: UnifiedData001
105 * @tc.desc: Normal testcase of UnifiedData
106 * @tc.type: FUNC
107 */
108 HWTEST_F(UnifiedDataTest, UnifiedData001, TestSize.Level1)
109 {
110     LOG_INFO(UDMF_TEST, "UnifiedData001 begin.");
111     std::shared_ptr<UnifiedDataProperties> properties = std::make_shared<UnifiedDataProperties>();
112     UnifiedData unifiedData(properties);
113     EXPECT_EQ(unifiedData.properties_, properties);
114     LOG_INFO(UDMF_TEST, "UnifiedData001 end.");
115 }
116 
117 /**
118 * @tc.name: GetGroupId001
119 * @tc.desc: Normal testcase of GetGroupId
120 * @tc.type: FUNC
121 */
122 HWTEST_F(UnifiedDataTest, GetGroupId001, TestSize.Level1)
123 {
124     LOG_INFO(UDMF_TEST, "GetGroupId001 begin.");
125     UnifiedData unifiedData;
126     unifiedData.runtime_ = std::make_shared<Runtime>();
127     std::string ret = unifiedData.GetGroupId();
128     EXPECT_EQ(ret, unifiedData.runtime_->key.groupId);
129     LOG_INFO(UDMF_TEST, "GetGroupId001 end.");
130 }
131 
132 /**
133 * @tc.name: GetRuntime001
134 * @tc.desc: Normal testcase of GetRuntime
135 * @tc.type: FUNC
136 */
137 HWTEST_F(UnifiedDataTest, GetRuntime001, TestSize.Level1)
138 {
139     LOG_INFO(UDMF_TEST, "GetRuntime001 begin.");
140     UnifiedData unifiedData;
141     unifiedData.runtime_ = std::make_shared<Runtime>();
142     std::shared_ptr<Runtime> ret = unifiedData.GetRuntime();
143     EXPECT_EQ(ret, unifiedData.runtime_);
144     LOG_INFO(UDMF_TEST, "GetRuntime001 end.");
145 }
146 
147 /**
148 * @tc.name: SetRuntime001
149 * @tc.desc: Normal testcase of SetRuntime
150 * @tc.type: FUNC
151 */
152 HWTEST_F(UnifiedDataTest, SetRuntime001, TestSize.Level1)
153 {
154     LOG_INFO(UDMF_TEST, "SetRuntime001 begin.");
155     UnifiedData unifiedData;
156     Runtime runtime{};
157     unifiedData.SetRuntime(runtime);
158     EXPECT_NE(unifiedData.runtime_, nullptr);
159     LOG_INFO(UDMF_TEST, "SetRuntime001 end.");
160 }
161 
162 /**
163 * @tc.name: AddRecord001
164 * @tc.desc: Abnormal testcase of AddRecord, because record is nullptr
165 * @tc.type: FUNC
166 */
167 HWTEST_F(UnifiedDataTest, AddRecord001, TestSize.Level1)
168 {
169     LOG_INFO(UDMF_TEST, "AddRecord001 begin.");
170     const std::shared_ptr<UnifiedRecord> record = nullptr;
171     UnifiedData unifiedData;
172     unifiedData.AddRecord(record);
173     EXPECT_EQ(unifiedData.records_.size(), 0);
174     LOG_INFO(UDMF_TEST, "AddRecord001 end.");
175 }
176 
177 /**
178 * @tc.name: AddRecords001
179 * @tc.desc: Abnormal testcase of AddRecords, because record is nullptr
180 * @tc.type: FUNC
181 */
182 HWTEST_F(UnifiedDataTest, AddRecords001, TestSize.Level1)
183 {
184     LOG_INFO(UDMF_TEST, "AddRecords001 begin.");
185     const std::vector<std::shared_ptr<UnifiedRecord>> record = {nullptr};
186     UnifiedData unifiedData;
187     unifiedData.AddRecords(record);
188     EXPECT_EQ(unifiedData.records_.size(), 0);
189     LOG_INFO(UDMF_TEST, "AddRecords001 end.");
190 }
191 
192 /**
193 * @tc.name: GetRecordAt001
194 * @tc.desc: Abnormal testcase of GetRecordAt, because the length of records_ is equal to the length of index
195 * @tc.type: FUNC
196 */
197 HWTEST_F(UnifiedDataTest, GetRecordAt001, TestSize.Level1)
198 {
199     LOG_INFO(UDMF_TEST, "GetRecordAt001 begin.");
200     UnifiedData unifiedData;
201     unifiedData.records_ = std::vector<std::shared_ptr<UnifiedRecord>>();
202     std::size_t index = unifiedData.records_.size();
203     std::shared_ptr<UnifiedRecord> ret = unifiedData.GetRecordAt(index);
204     EXPECT_EQ(ret, nullptr);
205     LOG_INFO(UDMF_TEST, "GetRecordAt001 end.");
206 }
207 
208 /**
209 * @tc.name: TransferToEntries001
210 * @tc.desc: Normal testcase of TransferToEntries
211 * @tc.type: FUNC
212 */
213 HWTEST_F(UnifiedDataTest, TransferToEntries001, TestSize.Level1)
214 {
215     UnifiedData unifiedData;
216     std::shared_ptr<UnifiedDataProperties> properties = std::make_shared<UnifiedDataProperties>();
217     properties->tag = "records_to_entries_data_format";
218     unifiedData.SetProperties(properties);
219     std::shared_ptr<PlainText> plainText = std::make_shared<PlainText>();
220     plainText->SetContent("http://1111/a.img");
221     plainText->SetAbstract("abstract");
222     std::shared_ptr<File> file = std::make_shared<File>();
223     file->SetUri("http://1111/a.img");
224     unifiedData.AddRecord(plainText);
225     unifiedData.AddRecord(file);
226     unifiedData.ConvertRecordsToEntries();
227     auto records = unifiedData.GetRecords();
228     int recordSize = 1;
229     EXPECT_EQ(records.size(), recordSize);
230     auto recordFirst = records[0].get();
231     auto plainTextFirst = static_cast<PlainText*>(recordFirst);
232     EXPECT_EQ(plainTextFirst->GetAbstract(), "abstract");
233     EXPECT_EQ(plainTextFirst->GetContent(), "http://1111/a.img");
234     std::set<std::string> utdIds = recordFirst->GetUtdIds();
235     EXPECT_TRUE(utdIds.find("general.plain-text") != utdIds.end());
236     EXPECT_TRUE(utdIds.find("general.file-uri") != utdIds.end());
237     auto fileEntry = recordFirst->GetEntry("general.file-uri");
238     std::shared_ptr<Object> fileEntryObj = std::get<std::shared_ptr<Object>>(fileEntry);
239     std::string getUri;
240     fileEntryObj->GetValue(ORI_URI, getUri);
241     EXPECT_EQ(getUri, "http://1111/a.img");
242     auto plainTextEntry = recordFirst->GetEntry("general.plain-text");
243     EXPECT_FALSE(std::holds_alternative<std::monostate>(plainTextEntry));
244     std::shared_ptr<Object> plainTextEntryObj = std::get<std::shared_ptr<Object>>(plainTextEntry);
245     std::string getContent;
246     plainTextEntryObj->GetValue(CONTENT, getContent);
247     EXPECT_EQ(getContent, "http://1111/a.img");
248     auto entries = recordFirst->GetEntries();
249     EXPECT_NE(entries, nullptr);
250     int entrySize = 2;
251     EXPECT_EQ(entries->size(), entrySize);
252     auto fileEntry1 = (*entries)["general.file-uri"];
253     std::shared_ptr<Object> fileEntryObj1 = std::get<std::shared_ptr<Object>>(fileEntry1);
254     std::string getUri1;
255     fileEntryObj1->GetValue(ORI_URI, getUri1);
256     EXPECT_EQ(getUri1, "http://1111/a.img");
257     auto plainTextEntry1 = (*entries)["general.plain-text"];
258     std::shared_ptr<Object> plainTextEntryObj1 = std::get<std::shared_ptr<Object>>(plainTextEntry1);
259     std::string content;
260     plainTextEntryObj1->GetValue(CONTENT, content);
261     EXPECT_EQ(content, "http://1111/a.img");
262     std::string abstract;
263     plainTextEntryObj1->GetValue(ABSTRACT, abstract);
264     EXPECT_EQ(abstract, "abstract");
265 }
266 
267 /**
268 * @tc.name: TransferToEntries002
269 * @tc.desc: Normal testcase of TransferToEntries
270 * @tc.type: FUNC
271 */
272 HWTEST_F(UnifiedDataTest, TransferToEntries002, TestSize.Level1)
273 {
274     LOG_INFO(UDMF_TEST, "TransferToEntries002 begin.");
275     UnifiedData unifiedData;
276     std::shared_ptr<PlainText> plainText = std::make_shared<PlainText>();
277     plainText->SetContent("http://1111/a.img");
278     plainText->SetAbstract("abstract");
279     std::shared_ptr<File> file = std::make_shared<File>();
280     file->SetUri("http://1111/a.txt");
281     std::shared_ptr<Object> fileUriObj = std::make_shared<Object>();
282     fileUriObj->value_[UNIFORM_DATA_TYPE] = "general.file-uri";
283     fileUriObj->value_[FILE_URI_PARAM] = "http://1111/a.img";
284     fileUriObj->value_[FILE_TYPE] = "general.img";
285     std::shared_ptr<UnifiedRecord> fileUri = std::make_shared<UnifiedRecord>(FILE_URI, fileUriObj);
286     std::shared_ptr<Object> fileUriObj1 = std::make_shared<Object>();
287     fileUriObj1->value_[UNIFORM_DATA_TYPE] = "general.file-uri";
288     fileUriObj1->value_[FILE_URI_PARAM] = "http://1111/a.mp4";
289     fileUriObj1->value_[FILE_TYPE] = "general.media";
290     std::shared_ptr<UnifiedRecord> fileUri1 = std::make_shared<UnifiedRecord>(FILE_URI, fileUriObj1);
291     bool isNeed = unifiedData.IsNeedTransferToEntries();
292     EXPECT_FALSE(isNeed);
293     unifiedData.AddRecord(plainText);
294     isNeed = unifiedData.IsNeedTransferToEntries();
295     EXPECT_FALSE(isNeed);
296     unifiedData.AddRecord(file);
297     isNeed = unifiedData.IsNeedTransferToEntries();
298     EXPECT_FALSE(isNeed);
299     unifiedData.AddRecord(fileUri);
300     unifiedData.AddRecord(fileUri1);
301     std::shared_ptr<UnifiedDataProperties> properties = std::make_shared<UnifiedDataProperties>();
302     properties->tag = "records_to_entries_data_format";
303     unifiedData.SetProperties(properties);
304     isNeed = unifiedData.IsNeedTransferToEntries();
305     EXPECT_TRUE(isNeed);
306     unifiedData.ConvertRecordsToEntries();
307     auto records = unifiedData.GetRecords();
308     int recordSize = 1;
309     EXPECT_EQ(records.size(), recordSize);
310     auto recordFirst = records[0].get();
311     TransferToEntriesCompareEntries(recordFirst);
312     LOG_INFO(UDMF_TEST, "TransferToEntries001 end.");
313 }
314 
315 /**
316 * @tc.name: HasHigherFileTypeTest001
317 * @tc.desc: Normal test of HasHigherFileType return true.
318 * @tc.type: FUNC
319 */
320 HWTEST_F(UnifiedDataTest, HasHigherFileTypeTest001, TestSize.Level1)
321 {
322     LOG_INFO(UDMF_TEST, "HasHigherFileTypeTest001 begin.");
323     std::shared_ptr<Object> fileObj = std::make_shared<Object>();
324     fileObj->value_[UNIFORM_DATA_TYPE] = "general.file-uri";
325     fileObj->value_[FILE_URI_PARAM] = "file://1111/a.jpeg";
326     fileObj->value_[FILE_TYPE] = "general.jpeg";
327     std::shared_ptr<UnifiedRecord> file = std::make_shared<UnifiedRecord>(FILE_URI, fileObj);
328 
329     std::shared_ptr<Object> fileObj1 = std::make_shared<Object>();
330     fileObj1->value_[UNIFORM_DATA_TYPE] = "general.file-uri";
331     fileObj1->value_[FILE_URI_PARAM] = "file://1111/a.mp4";
332     fileObj1->value_[FILE_TYPE] = "general.mpeg-4";
333     std::shared_ptr<UnifiedRecord> file1 = std::make_shared<UnifiedRecord>(FILE_URI, fileObj1);
334 
335     std::shared_ptr<Object> fileObj2 = std::make_shared<Object>();
336     fileObj2->value_[UNIFORM_DATA_TYPE] = "general.file-uri";
337     fileObj2->value_[FILE_URI_PARAM] = "file://1111/a.txt";
338     fileObj2->value_[FILE_TYPE] = "general.file";
339     std::shared_ptr<UnifiedRecord> file2 = std::make_shared<UnifiedRecord>(FILE_URI, fileObj2);
340     UnifiedData unifiedData;
341     unifiedData.AddRecord(file);
342     unifiedData.AddRecord(file1);
343     unifiedData.AddRecord(file2);
344     EXPECT_TRUE(unifiedData.HasHigherFileType("general.file-uri"));
345     EXPECT_TRUE(unifiedData.HasHigherFileType("general.image"));
346     EXPECT_TRUE(unifiedData.HasHigherFileType("general.file"));
347     EXPECT_TRUE(unifiedData.HasHigherFileType("general.video"));
348     EXPECT_TRUE(unifiedData.HasHigherFileType("general.jpeg"));
349     EXPECT_TRUE(unifiedData.HasHigherFileType("general.mpeg-4"));
350     EXPECT_FALSE(unifiedData.HasHigherFileType("general.folder"));
351     EXPECT_FALSE(unifiedData.HasHigherFileType("general.audio"));
352     LOG_INFO(UDMF_TEST, "HasHigherFileTypeTest001 end.");
353 }
354 
355 /**
356 * @tc.name: IsCompleteTest001
357 * @tc.desc: Normal test of HasHigherFileType return true.
358 * @tc.type: FUNC
359 */
360 HWTEST_F(UnifiedDataTest, IsCompleteTest001, TestSize.Level1)
361 {
362     LOG_INFO(UDMF_TEST, "IsCompleteTest001 begin.");
363     UnifiedData unifiedData;
364     EXPECT_FALSE(unifiedData.IsComplete());
365 
366     Runtime runtime;
367     runtime.recordTotalNum = 1;
368     unifiedData.SetRuntime(runtime);
369     EXPECT_FALSE(unifiedData.IsComplete());
370 
371     std::shared_ptr<Object> fileObj = std::make_shared<Object>();
372     fileObj->value_[UNIFORM_DATA_TYPE] = "general.file-uri";
373     fileObj->value_[FILE_URI_PARAM] = "file://1111/a.jpeg";
374     fileObj->value_[FILE_TYPE] = "general.jpeg";
375     std::shared_ptr<UnifiedRecord> fileRecord = std::make_shared<UnifiedRecord>(FILE_URI, fileObj);
376     unifiedData.AddRecord(fileRecord);
377     EXPECT_TRUE(unifiedData.IsComplete());
378 
379     std::shared_ptr<Object> plainTextObj = std::make_shared<Object>();
380     plainTextObj->value_[UNIFORM_DATA_TYPE] = "general.plain-text";
381     plainTextObj->value_[CONTENT] = "Hello, World!";
382     plainTextObj->value_[ABSTRACT] = "This is a test.";
383     std::shared_ptr<UnifiedRecord> plainTextRecord = std::make_shared<UnifiedRecord>(PLAIN_TEXT, plainTextObj);
384     unifiedData.AddRecord(plainTextRecord);
385     EXPECT_FALSE(unifiedData.IsComplete());
386     LOG_INFO(UDMF_TEST, "IsCompleteTest001 end.");
387 }
388 
389 /**
390 * @tc.name: GetFileUrisTest001
391 * @tc.desc: Normal test of HasHigherFileType return true.
392 * @tc.type: FUNC
393 */
394 HWTEST_F(UnifiedDataTest, GetFileUrisTest001, TestSize.Level1)
395 {
396     LOG_INFO(UDMF_TEST, "GetFileUrisTest001 begin.");
397     UnifiedData unifiedData;
398     std::shared_ptr<Object> fileObj = std::make_shared<Object>();
399     fileObj->value_[UNIFORM_DATA_TYPE] = "general.file-uri";
400     fileObj->value_[FILE_URI_PARAM] = "file://1111/a.jpeg";
401     fileObj->value_[FILE_TYPE] = "general.jpeg";
402     std::shared_ptr<UnifiedRecord> fileRecord = std::make_shared<UnifiedRecord>(FILE_URI, fileObj);
403     unifiedData.AddRecord(fileRecord);
404 
405     std::shared_ptr<Object> plainTextObj = std::make_shared<Object>();
406     plainTextObj->value_[UNIFORM_DATA_TYPE] = "general.plain-text";
407     plainTextObj->value_[CONTENT] = "Hello, World!";
408     plainTextObj->value_[ABSTRACT] = "This is a test.";
409     std::shared_ptr<UnifiedRecord> plainTextRecord = std::make_shared<UnifiedRecord>(PLAIN_TEXT, plainTextObj);
410     unifiedData.AddRecord(plainTextRecord);
411 
412     std::shared_ptr<Image> image = std::make_shared<Image>();
413     image->SetUri("file://1111/a.png");
414     unifiedData.AddRecord(image);
415 
416     unifiedData.AddRecord(nullptr);
417     std::vector<std::string> fileUris = unifiedData.GetFileUris();
418     EXPECT_EQ(fileUris.size(), 2);
419     EXPECT_TRUE(std::find(fileUris.begin(), fileUris.end(), "file://1111/a.jpeg") != fileUris.end());
420     EXPECT_TRUE(std::find(fileUris.begin(), fileUris.end(), "file://1111/a.png") != fileUris.end());
421     LOG_INFO(UDMF_TEST, "GetFileUrisTest001 end.");
422 }
423 
424 /**
425  * @tc.name: GetDataId_001
426  * @tc.desc: Get dataId.
427  * @tc.type: FUNC
428  */
429 HWTEST_F(UnifiedDataTest, GetDataId_001, TestSize.Level0)
430 {
431     uint32_t dataId = 2;
432     UnifiedData unifiedData;
433     unifiedData.SetDataId(dataId);
434     auto getDataId = unifiedData.GetDataId();
435     EXPECT_EQ(getDataId, dataId);
436 }
437 
438 /**
439  * @tc.name: SetChannelName_001
440  * @tc.desc: Set channelName.
441  * @tc.type: FUNC
442  */
443 HWTEST_F(UnifiedDataTest, SetChannelName_001, TestSize.Level0)
444 {
445     const std::string name = "channelName";
446     UnifiedData unifiedData;
447     EXPECT_NO_FATAL_FAILURE(unifiedData.SetChannelName(name));
448 }
449 } // OHOS::Test