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
18 #include "util.h"
19
20 using namespace testing;
21 using namespace testing::ext;
22
23 namespace OHOS::Rosen {
24 class UtilTest : public testing::Test {
25 public:
26 static void SetUpTestCase();
27 static void TearDownTestCase();
28 void SetUp() override;
29 void TearDown() override;
30 };
31
SetUpTestCase()32 void UtilTest::SetUpTestCase() {}
TearDownTestCase()33 void UtilTest::TearDownTestCase() {}
SetUp()34 void UtilTest::SetUp() {}
TearDown()35 void UtilTest::TearDown() {}
36
37 /**
38 * @tc.name: UtilTest_001
39 * @tc.desc: Verify the ParseOldConfigFile
40 * @tc.type:FUNC
41 */
42 HWTEST_F(UtilTest, UtilTest_001, TestSize.Level1)
43 {
44 std::vector<BootAnimationConfig> configs;
45
46 std::string jsonStr1 = "{}";
47 cJSON* jsonData1 = cJSON_Parse(jsonStr1.c_str());
48 OHOS::ParseOldConfigFile(jsonData1, configs);
49
50 std::string jsonStr2 = "{\"cust.bootanimation.pics\":1,\"cust.bootanimation.sounds\":1,\
51 \"cust.bootanimation.video\":1,\"cust.bootanimation.video.extra\":1,\"cust.bootanimation.rotate.screenid\":1,\
52 \"cust.bootanimation.rotate.degree\":1}";
53 cJSON* jsonData2 = cJSON_Parse(jsonStr2.c_str());
54 OHOS::ParseOldConfigFile(jsonData2, configs);
55 EXPECT_EQ(configs.front().rotateDegree, 0);
56
57 std::string jsonStr3 = "{\"cust.bootanimation.pics\":\"abc\",\"cust.bootanimation.sounds\":\"abc\",\
58 \"cust.bootanimation.video\":\"abc\",\"cust.bootanimation.video.extra\":\"abc\",\
59 \"cust.bootanimation.rotate.screenid\":\"0\", \"cust.bootanimation.rotate.degree\":\"270\"}";
60 cJSON* jsonData3 = cJSON_Parse(jsonStr3.c_str());
61 OHOS::ParseOldConfigFile(jsonData3, configs);
62 EXPECT_EQ(configs.back().rotateDegree, 270);
63 }
64
65 /**
66 * @tc.name: UtilTest_002
67 * @tc.desc: Verify the ParseNewConfigFile
68 * @tc.type:FUNC
69 */
70 HWTEST_F(UtilTest, UtilTest_002, TestSize.Level1)
71 {
72 bool isMultiDisplay = false;
73 std::vector<BootAnimationConfig> configs;
74
75 std::string jsonStr1 = "{}";
76 cJSON* jsonData1 = cJSON_Parse(jsonStr1.c_str());
77 OHOS::ParseNewConfigFile(jsonData1, isMultiDisplay, configs);
78
79 std::string jsonStr2 = "{\"cust.bootanimation.multi_display\":1}";
80 cJSON* jsonData2 = cJSON_Parse(jsonStr2.c_str());
81 OHOS::ParseNewConfigFile(jsonData2, isMultiDisplay, configs);
82
83 std::string jsonStr3 = "{\"cust.bootanimation.multi_display\":false}";
84 cJSON* jsonData3 = cJSON_Parse(jsonStr3.c_str());
85 OHOS::ParseNewConfigFile(jsonData3, isMultiDisplay, configs);
86 EXPECT_EQ(isMultiDisplay, false);
87
88 std::string jsonStr4 = "{\"cust.bootanimation.multi_display\":true}";
89 cJSON* jsonData4 = cJSON_Parse(jsonStr4.c_str());
90 OHOS::ParseNewConfigFile(jsonData4, isMultiDisplay, configs);
91
92 std::string jsonStr5 = "{\"screen_config\":[]}";
93 cJSON* jsonData5 = cJSON_Parse(jsonStr5.c_str());
94 OHOS::ParseNewConfigFile(jsonData5, isMultiDisplay, configs);
95
96 std::string jsonStr6 = "{\"screen_config\":[{}]}";
97 cJSON* jsonData6 = cJSON_Parse(jsonStr6.c_str());
98 OHOS::ParseNewConfigFile(jsonData6, isMultiDisplay, configs);
99
100 std::string jsonStr7 = "{\"screen_config\":[{\"cust.bootanimation.screen_id\":1,\
101 \"cust.bootanimation.pics\":1,\"cust.bootanimation.sounds\":1,\
102 \"cust.bootanimation.video_default\":1,\"cust.bootanimation.rotate_degree\":1,\
103 \"cust.bootanimation.video_extensions\":1}]}";
104 cJSON* jsonData7 = cJSON_Parse(jsonStr7.c_str());
105 OHOS::ParseNewConfigFile(jsonData7, isMultiDisplay, configs);
106
107 std::string jsonStr8 = "{\"screen_config\":[{\"cust.bootanimation.screen_id\":\"0\",\
108 \"cust.bootanimation.pics\":\"abc\",\"cust.bootanimation.sounds\":\"abc\",\
109 \"cust.bootanimation.video_default\":\"abc\",\"cust.bootanimation.rotate_degree\":\"270\",\
110 \"cust.bootanimation.video_extensions\":[]}]}";
111 cJSON* jsonData8 = cJSON_Parse(jsonStr8.c_str());
112 OHOS::ParseNewConfigFile(jsonData8, isMultiDisplay, configs);
113 EXPECT_EQ(isMultiDisplay, true);
114 }
115
116 /**
117 * @tc.name: UtilTest_003
118 * @tc.desc: Verify the ParseVideoExtraPath
119 * @tc.type:FUNC
120 */
121 HWTEST_F(UtilTest, UtilTest_003, TestSize.Level1)
122 {
123 BootAnimationConfig config;
124 std::string jsonStr1 = "{}";
125 cJSON* jsonData1 = cJSON_Parse(jsonStr1.c_str());
126 OHOS::ParseVideoExtraPath(jsonData1, config);
127
128 std::string jsonStr2 = "[]";
129 cJSON* jsonData2 = cJSON_Parse(jsonStr2.c_str());
130 OHOS::ParseVideoExtraPath(jsonData2, config);
131
132 std::string jsonStr3 = "{\"1\":\"abc\"}";
133 cJSON* jsonData3 = cJSON_Parse(jsonStr3.c_str());
134 OHOS::ParseVideoExtraPath(jsonData3, config);
135 EXPECT_EQ(config.videoExtPath.size(), 1);
136 }
137
138 /**
139 * @tc.name: UtilTest_004
140 * @tc.desc: Verify the ParseBootDuration
141 * @tc.type:FUNC
142 */
143 HWTEST_F(UtilTest, UtilTest_004, TestSize.Level1)
144 {
145 BootAnimationConfig config;
146 int32_t duration;
147 std::string jsonStr1 = "{}";
148 cJSON* jsonData1 = cJSON_Parse(jsonStr1.c_str());
149 OHOS::ParseBootDuration(jsonData1, duration);
150
151 std::string jsonStr2 = "{\"cust.bootanimation.duration\":10}";
152 cJSON* jsonData2 = cJSON_Parse(jsonStr2.c_str());
153 OHOS::ParseBootDuration(jsonData2, duration);
154
155 std::string jsonStr3 = "{\"cust.bootanimation.duration\":\"10\"}";
156 cJSON* jsonData3 = cJSON_Parse(jsonStr3.c_str());
157 OHOS::ParseBootDuration(jsonData3, duration);
158 EXPECT_EQ(duration, 10);
159 }
160
161 /**
162 * @tc.name: UtilTest_005
163 * @tc.desc: Verify the IsFileExisted
164 * @tc.type:FUNC
165 */
166 HWTEST_F(UtilTest, UtilTest_005, TestSize.Level1)
167 {
168 std::string filePath = "";
169 bool isFileExist = OHOS::IsFileExisted(filePath);
170 EXPECT_EQ(false, isFileExist);
171
172 filePath = "/sys_prod/etc/bootanimation/bootanimation_custom_config.json1";
173 isFileExist = OHOS::IsFileExisted(filePath);
174 EXPECT_EQ(false, isFileExist);
175 }
176
177 /**
178 * @tc.name: UtilTest_006
179 * @tc.desc: Verify the ParseBootConfig
180 * @tc.type:FUNC
181 */
182 HWTEST_F(UtilTest, UtilTest_006, TestSize.Level1)
183 {
184 std::string filePath = "";
185 std::vector<BootAnimationConfig> animationConfigs;
186 bool isMultiDisplay = false;
187 bool isCompatible = false;
188 int32_t duration = 60;
189 bool parseResult = OHOS::ParseBootConfig(filePath, duration, isCompatible, isMultiDisplay, animationConfigs);
190 EXPECT_EQ(false, parseResult);
191
192 filePath = "/sys_prod/etc/bootanimation/bootanimation_custom_config.json1";
193 parseResult = OHOS::ParseBootConfig(filePath, duration, isCompatible, isMultiDisplay, animationConfigs);
194 EXPECT_EQ(false, parseResult);
195
196 filePath = "/sys_prod/etc/bootanimation/bootanimation_custom_config.json";
197 parseResult = OHOS::ParseBootConfig(filePath, duration, isCompatible, isMultiDisplay, animationConfigs);
198 bool isFileExist = OHOS::IsFileExisted(filePath);
199 EXPECT_EQ(isFileExist ? true : false, parseResult);
200 }
201
202 /**
203 * @tc.name: UtilTest_007
204 * @tc.desc: Verify the ReadFile
205 * @tc.type:FUNC
206 */
207 HWTEST_F(UtilTest, UtilTest_007, TestSize.Level1)
208 {
209 std::string filePath = "";
210 std::string content = OHOS::ReadFile(filePath);
211 EXPECT_EQ(0, content.length());
212
213 filePath = "/sys_prod/etc/bootanimation/bootanimation_custom_config.json1";
214 content = OHOS::ReadFile(filePath);
215 EXPECT_EQ(0, content.length());
216
217 filePath = "/sys_prod/etc/bootanimation/bootanimation_custom_config.json";
218 content = OHOS::ReadFile(filePath);
219 bool isFileExist = OHOS::IsFileExisted(filePath);
220 EXPECT_EQ(content.empty(), isFileExist ? false : true);
221 }
222
223 /**
224 * @tc.name: UtilTest_008
225 * @tc.desc: Verify the GetHingeStatus
226 * @tc.type:FUNC
227 */
228 HWTEST_F(UtilTest, UtilTest_008, TestSize.Level1)
229 {
230 std::string HING_STATUS_INFO_PATH = "/sys/class/sensors/hinge_sensor/hinge_status_info";
231 std::string content = OHOS::GetHingeStatus();
232 if (OHOS::IsFileExisted(HING_STATUS_INFO_PATH)) {
233 EXPECT_NE(0, content.length());
234 } else {
235 EXPECT_EQ(0, content.length());
236 }
237 }
238
239 /**
240 * @tc.name: UtilTest_009
241 * @tc.desc: Verify the ParseImageConfig
242 * @tc.type:FUNC
243 */
244 HWTEST_F(UtilTest, UtilTest_009, TestSize.Level1)
245 {
246 const char* fileBuffer;
247 int totalsize = 0;
248 FrameRateConfig frameConfig;
249 bool result = OHOS::ParseImageConfig(fileBuffer, totalsize, frameConfig);
250 EXPECT_FALSE(result);
251 }
252
253 /**
254 * @tc.name: UtilTest_010
255 * @tc.desc: Verify the CheckImageData
256 * @tc.type:FUNC
257 */
258 HWTEST_F(UtilTest, UtilTest_010, TestSize.Level1)
259 {
260 std::string filename = "abc";
261 std::shared_ptr<ImageStruct> imageStruct = std::make_shared<ImageStruct>();
262 int32_t bufferLen = 0;
263 ImageStructVec imgVec;
264 bool result = OHOS::CheckImageData(filename, imageStruct, bufferLen, imgVec);
265 EXPECT_FALSE(result);
266 unsigned long fileSize = 1000;
267 imageStruct->memPtr.SetBufferSize(fileSize);
268 result = OHOS::CheckImageData(filename, imageStruct, bufferLen, imgVec);
269 EXPECT_TRUE(result);
270 }
271
272 /**
273 * @tc.name: UtilTest_011
274 * @tc.desc: Verify the ParseProgressConfig
275 * @tc.type:FUNC
276 */
277 HWTEST_F(UtilTest, UtilTest_011, TestSize.Level2)
278 {
279 std::string filePath = "/sys_prod/etc/bootanimation/bootanimation_custom_config.json1";
280 std::map<int32_t, BootAnimationProgressConfig> configs;
281 OHOS::ParseProgressConfig(filePath, configs);
282 EXPECT_EQ(configs.empty(), true);
283 }
284
285 /**
286 * @tc.name: UtilTest_013
287 * @tc.desc: Verify the ParseProgressData
288 * @tc.type:FUNC
289 */
290 HWTEST_F(UtilTest, UtilTest_012, TestSize.Level1)
291 {
292 std::map<int32_t, BootAnimationProgressConfig> configs;
293 std::string jsonStr = "{\"1\":\"abc\"}";
294 cJSON* jsonData = cJSON_Parse(jsonStr.c_str());
295 OHOS::ParseProgressData(jsonData, configs);
296 EXPECT_EQ(configs.empty(), true);
297
298 jsonStr = "{\"progress_config\":[]}";
299 jsonData = cJSON_Parse(jsonStr.c_str());
300 OHOS::ParseProgressData(jsonData, configs);
301 EXPECT_EQ(configs.empty(), true);
302
303 jsonStr = "{\"progress_config\":[{\"cust.bootanimation.screen_id\":\"0\"}]}";
304 jsonData = cJSON_Parse(jsonStr.c_str());
305 OHOS::ParseProgressData(jsonData, configs);
306 EXPECT_EQ(configs.empty(), false);
307
308 jsonStr = "{\"progress_config\":[{\"cust.bootanimation.progress_screen_id\":\"1\", "
309 "\"cust.bootanimation.progress_font_size\":\"3\", "
310 "\"cust.bootanimation.progress_radius_size\":\"10\", "
311 "\"cust.bootanimation.progress_x_offset\":\"-1088\", "
312 "\"cust.bootanimation.progress_degree90\", "
313 "\"cust.bootanimation.progress_height\":\"2232\", "
314 "\"cust.bootanimation.progress_frame_height\":\"112\"}]}";
315 jsonData = cJSON_Parse(jsonStr.c_str());
316 OHOS::ParseProgressData(jsonData, configs);
317 EXPECT_EQ(configs.empty(), false);
318 }
319 }
320