• 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 "logger.h"
23 #include "plain_text.h"
24 #include "udmf_capi_common.h"
25 #include "udmf_utils.h"
26 #include "unified_data.h"
27 
28 using namespace testing::ext;
29 using namespace OHOS::UDMF;
30 using namespace OHOS;
31 namespace OHOS::Test {
32 using namespace std;
33 
34 class UnifiedDataTest : public testing::Test {
35 public:
36     static void SetUpTestCase();
37     static void TearDownTestCase();
38     void SetUp() override;
39     void TearDown() override;
40     void TransferToEntriesCompareEntries(UnifiedRecord* recordFirst);
41 };
42 
SetUpTestCase()43 void UnifiedDataTest::SetUpTestCase()
44 {
45 }
46 
TearDownTestCase()47 void UnifiedDataTest::TearDownTestCase()
48 {
49 }
50 
SetUp()51 void UnifiedDataTest::SetUp()
52 {
53 }
54 
TearDown()55 void UnifiedDataTest::TearDown()
56 {
57 }
58 
TransferToEntriesCompareEntries(UnifiedRecord * recordFirst)59 void UnifiedDataTest::TransferToEntriesCompareEntries(UnifiedRecord* recordFirst)
60 {
61     auto plainTextFirst = static_cast<PlainText*>(recordFirst);
62     EXPECT_EQ(plainTextFirst->GetAbstract(), "abstract");
63     EXPECT_EQ(plainTextFirst->GetContent(), "http://1111/a.img");
64     std::set<std::string> utdIds = recordFirst->GetUtdIds();
65     EXPECT_TRUE(utdIds.find("general.plain-text") != utdIds.end());
66     EXPECT_TRUE(utdIds.find("general.file-uri") != utdIds.end());
67     auto fileEntry = recordFirst->GetEntry("general.file-uri");
68     std::shared_ptr<Object> fileEntryObj = std::get<std::shared_ptr<Object>>(fileEntry);
69     std::string getUri;
70     fileEntryObj->GetValue(ORI_URI, getUri);
71     EXPECT_EQ(getUri, "http://1111/a.mp4");
72     auto plainTextEntry = recordFirst->GetEntry("general.plain-text");
73     std::shared_ptr<Object> plainTextEntryObj = std::get<std::shared_ptr<Object>>(plainTextEntry);
74     std::string getContent;
75     plainTextEntryObj->GetValue(CONTENT, getContent);
76     EXPECT_EQ(getContent, "http://1111/a.img");
77     std::string getAbstract;
78     plainTextEntryObj->GetValue(ABSTRACT, getAbstract);
79     EXPECT_EQ(getAbstract, "abstract");
80     auto entries = recordFirst->GetEntries();
81     EXPECT_NE(entries, nullptr);
82     int entrySize = 2;
83     EXPECT_EQ(entries->size(), entrySize);
84     auto plainTextEntry1 = (*entries)["general.plain-text"];
85     std::shared_ptr<Object> plainTextEntryObj1 = std::get<std::shared_ptr<Object>>(plainTextEntry1);
86     std::string content;
87     plainTextEntryObj1->GetValue(CONTENT, content);
88     EXPECT_EQ(content, "http://1111/a.img");
89     std::string abstract;
90     plainTextEntryObj1->GetValue(ABSTRACT, abstract);
91     EXPECT_EQ(abstract, "abstract");
92     auto fileUriEntry1 = (*entries)["general.file-uri"];
93     std::shared_ptr<Object> fileUriEntryObj1 = std::get<std::shared_ptr<Object>>(fileUriEntry1);
94     std::string oriUri;
95     fileUriEntryObj1->GetValue(FILE_URI_PARAM, oriUri);
96     EXPECT_EQ(oriUri, "http://1111/a.mp4");
97     std::string fileType;
98     fileUriEntryObj1->GetValue(FILE_TYPE, fileType);
99     EXPECT_EQ(fileType, "general.media");
100 }
101 
102 /**
103 * @tc.name: UnifiedData001
104 * @tc.desc: Normal testcase of UnifiedData
105 * @tc.type: FUNC
106 */
107 HWTEST_F(UnifiedDataTest, UnifiedData001, TestSize.Level1)
108 {
109     LOG_INFO(UDMF_TEST, "UnifiedData001 begin.");
110     std::shared_ptr<UnifiedDataProperties> properties = std::make_shared<UnifiedDataProperties>();
111     UnifiedData unifiedData(properties);
112     auto duration = std::chrono::system_clock::now().time_since_epoch();
113     EXPECT_EQ(unifiedData.properties_, properties);
114     EXPECT_EQ(unifiedData.properties_->timestamp,
115         std::chrono::duration_cast<std::chrono::milliseconds>(duration).count());
116     LOG_INFO(UDMF_TEST, "UnifiedData001 end.");
117 }
118 
119 /**
120 * @tc.name: GetGroupId001
121 * @tc.desc: Normal testcase of GetGroupId
122 * @tc.type: FUNC
123 */
124 HWTEST_F(UnifiedDataTest, GetGroupId001, TestSize.Level1)
125 {
126     LOG_INFO(UDMF_TEST, "GetGroupId001 begin.");
127     UnifiedData unifiedData;
128     unifiedData.runtime_ = std::make_shared<Runtime>();
129     std::string ret = unifiedData.GetGroupId();
130     EXPECT_EQ(ret, unifiedData.runtime_->key.groupId);
131     LOG_INFO(UDMF_TEST, "GetGroupId001 end.");
132 }
133 
134 /**
135 * @tc.name: GetRuntime001
136 * @tc.desc: Normal testcase of GetRuntime
137 * @tc.type: FUNC
138 */
139 HWTEST_F(UnifiedDataTest, GetRuntime001, TestSize.Level1)
140 {
141     LOG_INFO(UDMF_TEST, "GetRuntime001 begin.");
142     UnifiedData unifiedData;
143     unifiedData.runtime_ = std::make_shared<Runtime>();
144     std::shared_ptr<Runtime> ret = unifiedData.GetRuntime();
145     EXPECT_EQ(ret, unifiedData.runtime_);
146     LOG_INFO(UDMF_TEST, "GetRuntime001 end.");
147 }
148 
149 /**
150 * @tc.name: SetRuntime001
151 * @tc.desc: Normal testcase of SetRuntime
152 * @tc.type: FUNC
153 */
154 HWTEST_F(UnifiedDataTest, SetRuntime001, TestSize.Level1)
155 {
156     LOG_INFO(UDMF_TEST, "SetRuntime001 begin.");
157     UnifiedData unifiedData;
158     Runtime runtime{};
159     unifiedData.SetRuntime(runtime);
160     EXPECT_NE(unifiedData.runtime_, nullptr);
161     LOG_INFO(UDMF_TEST, "SetRuntime001 end.");
162 }
163 
164 /**
165 * @tc.name: AddRecord001
166 * @tc.desc: Abnormal testcase of AddRecord, because record is nullptr
167 * @tc.type: FUNC
168 */
169 HWTEST_F(UnifiedDataTest, AddRecord001, TestSize.Level1)
170 {
171     LOG_INFO(UDMF_TEST, "AddRecord001 begin.");
172     const std::shared_ptr<UnifiedRecord> record = nullptr;
173     UnifiedData unifiedData;
174     unifiedData.AddRecord(record);
175     EXPECT_EQ(unifiedData.records_.size(), 0);
176     LOG_INFO(UDMF_TEST, "AddRecord001 end.");
177 }
178 
179 /**
180 * @tc.name: AddRecords001
181 * @tc.desc: Abnormal testcase of AddRecords, because record is nullptr
182 * @tc.type: FUNC
183 */
184 HWTEST_F(UnifiedDataTest, AddRecords001, TestSize.Level1)
185 {
186     LOG_INFO(UDMF_TEST, "AddRecords001 begin.");
187     const std::vector<std::shared_ptr<UnifiedRecord>> record = {nullptr};
188     UnifiedData unifiedData;
189     unifiedData.AddRecords(record);
190     EXPECT_EQ(unifiedData.records_.size(), 0);
191     LOG_INFO(UDMF_TEST, "AddRecords001 end.");
192 }
193 
194 /**
195 * @tc.name: GetRecordAt001
196 * @tc.desc: Abnormal testcase of GetRecordAt, because the length of records_ is equal to the length of index
197 * @tc.type: FUNC
198 */
199 HWTEST_F(UnifiedDataTest, GetRecordAt001, TestSize.Level1)
200 {
201     LOG_INFO(UDMF_TEST, "GetRecordAt001 begin.");
202     UnifiedData unifiedData;
203     unifiedData.records_ = std::vector<std::shared_ptr<UnifiedRecord>>();
204     std::size_t index = unifiedData.records_.size();
205     std::shared_ptr<UnifiedRecord> ret = unifiedData.GetRecordAt(index);
206     EXPECT_EQ(ret, nullptr);
207     LOG_INFO(UDMF_TEST, "GetRecordAt001 end.");
208 }
209 
210 /**
211 * @tc.name: TransferToEntries001
212 * @tc.desc: Normal testcase of TransferToEntries
213 * @tc.type: FUNC
214 */
215 HWTEST_F(UnifiedDataTest, TransferToEntries001, TestSize.Level1)
216 {
217     UnifiedData unifiedData;
218     std::shared_ptr<UnifiedDataProperties> properties = std::make_shared<UnifiedDataProperties>();
219     properties->tag = "records_to_entries_data_format";
220     unifiedData.SetProperties(properties);
221     std::shared_ptr<PlainText> plainText = std::make_shared<PlainText>();
222     plainText->SetContent("http://1111/a.img");
223     plainText->SetAbstract("abstract");
224     std::shared_ptr<File> file = std::make_shared<File>();
225     file->SetUri("http://1111/a.img");
226     unifiedData.AddRecord(plainText);
227     unifiedData.AddRecord(file);
228     unifiedData.ConvertRecordsToEntries();
229     auto records = unifiedData.GetRecords();
230     int recordSize = 1;
231     EXPECT_EQ(records.size(), recordSize);
232     auto recordFirst = records[0].get();
233     auto plainTextFirst = static_cast<PlainText*>(recordFirst);
234     EXPECT_EQ(plainTextFirst->GetAbstract(), "abstract");
235     EXPECT_EQ(plainTextFirst->GetContent(), "http://1111/a.img");
236     std::set<std::string> utdIds = recordFirst->GetUtdIds();
237     EXPECT_TRUE(utdIds.find("general.plain-text") != utdIds.end());
238     EXPECT_TRUE(utdIds.find("general.file-uri") != utdIds.end());
239     auto fileEntry = recordFirst->GetEntry("general.file-uri");
240     std::shared_ptr<Object> fileEntryObj = std::get<std::shared_ptr<Object>>(fileEntry);
241     std::string getUri;
242     fileEntryObj->GetValue(ORI_URI, getUri);
243     EXPECT_EQ(getUri, "http://1111/a.img");
244     auto plainTextEntry = recordFirst->GetEntry("general.plain-text");
245     EXPECT_FALSE(std::holds_alternative<std::monostate>(plainTextEntry));
246     std::shared_ptr<Object> plainTextEntryObj = std::get<std::shared_ptr<Object>>(plainTextEntry);
247     std::string getContent;
248     plainTextEntryObj->GetValue(CONTENT, getContent);
249     EXPECT_EQ(getContent, "http://1111/a.img");
250     auto entries = recordFirst->GetEntries();
251     EXPECT_NE(entries, nullptr);
252     int entrySize = 2;
253     EXPECT_EQ(entries->size(), entrySize);
254     auto fileEntry1 = (*entries)["general.file-uri"];
255     std::shared_ptr<Object> fileEntryObj1 = std::get<std::shared_ptr<Object>>(fileEntry1);
256     std::string getUri1;
257     fileEntryObj1->GetValue(ORI_URI, getUri1);
258     EXPECT_EQ(getUri1, "http://1111/a.img");
259     auto plainTextEntry1 = (*entries)["general.plain-text"];
260     std::shared_ptr<Object> plainTextEntryObj1 = std::get<std::shared_ptr<Object>>(plainTextEntry1);
261     std::string content;
262     plainTextEntryObj1->GetValue(CONTENT, content);
263     EXPECT_EQ(content, "http://1111/a.img");
264     std::string abstract;
265     plainTextEntryObj1->GetValue(ABSTRACT, abstract);
266     EXPECT_EQ(abstract, "abstract");
267 }
268 
269 /**
270 * @tc.name: TransferToEntries002
271 * @tc.desc: Normal testcase of TransferToEntries
272 * @tc.type: FUNC
273 */
274 HWTEST_F(UnifiedDataTest, TransferToEntries002, TestSize.Level1)
275 {
276     LOG_INFO(UDMF_TEST, "TransferToEntries002 begin.");
277     UnifiedData unifiedData;
278     std::shared_ptr<PlainText> plainText = std::make_shared<PlainText>();
279     plainText->SetContent("http://1111/a.img");
280     plainText->SetAbstract("abstract");
281     std::shared_ptr<File> file = std::make_shared<File>();
282     file->SetUri("http://1111/a.txt");
283     std::shared_ptr<Object> fileUriObj = std::make_shared<Object>();
284     fileUriObj->value_[UNIFORM_DATA_TYPE] = "general.file-uri";
285     fileUriObj->value_[FILE_URI_PARAM] = "http://1111/a.img";
286     fileUriObj->value_[FILE_TYPE] = "general.img";
287     std::shared_ptr<UnifiedRecord> fileUri = std::make_shared<UnifiedRecord>(FILE_URI, fileUriObj);
288     std::shared_ptr<Object> fileUriObj1 = std::make_shared<Object>();
289     fileUriObj1->value_[UNIFORM_DATA_TYPE] = "general.file-uri";
290     fileUriObj1->value_[FILE_URI_PARAM] = "http://1111/a.mp4";
291     fileUriObj1->value_[FILE_TYPE] = "general.media";
292     std::shared_ptr<UnifiedRecord> fileUri1 = std::make_shared<UnifiedRecord>(FILE_URI, fileUriObj1);
293     bool isNeed = unifiedData.IsNeedTransferToEntries();
294     EXPECT_FALSE(isNeed);
295     unifiedData.AddRecord(plainText);
296     isNeed = unifiedData.IsNeedTransferToEntries();
297     EXPECT_FALSE(isNeed);
298     unifiedData.AddRecord(file);
299     isNeed = unifiedData.IsNeedTransferToEntries();
300     EXPECT_FALSE(isNeed);
301     unifiedData.AddRecord(fileUri);
302     unifiedData.AddRecord(fileUri1);
303     std::shared_ptr<UnifiedDataProperties> properties = std::make_shared<UnifiedDataProperties>();
304     properties->tag = "records_to_entries_data_format";
305     unifiedData.SetProperties(properties);
306     isNeed = unifiedData.IsNeedTransferToEntries();
307     EXPECT_TRUE(isNeed);
308     unifiedData.ConvertRecordsToEntries();
309     auto records = unifiedData.GetRecords();
310     int recordSize = 1;
311     EXPECT_EQ(records.size(), recordSize);
312     auto recordFirst = records[0].get();
313     TransferToEntriesCompareEntries(recordFirst);
314     LOG_INFO(UDMF_TEST, "TransferToEntries001 end.");
315 }
316 } // OHOS::Test