1 /*
2 * Copyright (c) 2021-2022 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 <memory>
17 #include <atomic>
18 #include <chrono>
19 #include <ctime>
20 #include <regex>
21 #include <sys/time.h>
22
23 #include "gtest/gtest.h"
24
25 #include "base/utils/base_id.h"
26 #include "base/utils/date_util.h"
27 #include "base/log/log.h"
28 #include "base/utils/utils.h"
29 #include "base/utils/resource_configuration.h"
30 #include "base/utils/string_expression.h"
31 #include "base/utils/string_utils.h"
32 #include "base/utils/time_util.h"
33
34 #ifndef WINDOWS_PLATFORM
35 #include "securec.h"
36 #endif
37
38 using namespace testing;
39 using namespace testing::ext;
40
41 namespace OHOS::Ace {
42 namespace {
43 constexpr uint32_t DEFAULT_YEAR = 1900;
44 constexpr uint32_t DEFAULT_MONTH = 0;
45 constexpr uint32_t DEFAULT_DAY = 1;
46 constexpr uint32_t DEFAULT_WEEK = 0;
47 constexpr uint32_t DAY_OF_MONTH_TWENTY_NINE = 29;
48 constexpr uint32_t DAY_OF_MONTH_THIRTY = 30;
49 constexpr uint32_t DAY_OF_MONTH_THIRTY_ONE = 31;
50 constexpr uint32_t DAY_OF_MONTH_DEFAULT = 0;
51 constexpr uint32_t DAY_OF_WEEK = 4;
52 constexpr uint32_t ADD_ONE = 1;
53 constexpr uint32_t TEST_INPUT_UINT32 = 123456;
54 constexpr uint64_t NUM_OF_ID = 1;
55 constexpr int32_t TEST_INPUT_INT32 = 123456;
56 constexpr int32_t TWELVE_HOUR_BASE = 12;
57 constexpr int32_t TWENTY_FOUR_HOUR_BASE = 24;
58 constexpr int32_t DAY_TIME_LOWER_LIMIT = 6;
59 constexpr int32_t DAY_TIME_UPPER_LIMIT = 18;
60 constexpr int32_t SET_HOURS_FOR_THE_FIRST_TIME = -13;
61 constexpr int32_t SET_HOURS_FOR_THE_SECOND_TIME = 8;
62 constexpr int32_t SET_HOURS_FOR_THE_THIRD_TIME = 24;
63 constexpr int32_t GET_HOURSWEST_FOR_THE_FIRST_TIME = 11;
64 constexpr int32_t GET_HOURSWEST_FOR_THE_SECOND_TIME = 8;
65 constexpr int32_t TEST_INPUT_ARGS_TWO = 2022;
66 constexpr int64_t TEST_INPUT_INT64 = 123456;
67 constexpr int64_t MICROSEC_TO_MILLISEC = 1000;
68 constexpr int64_t NANOSEC_TO_MILLISEC = 1000000;
69 constexpr int64_t CONTENT_OF_RESOURCEHANDLERS = 255;
70 constexpr size_t MAX_STRING_SIZE = 256;
71 constexpr double NORMAL_CALC_RESULT = 6.2;
72 constexpr double ERROR_CALC_RESULT = 0.0;
73 constexpr double CONFIGURATION_OF_DENSITY = 1.0;
74 constexpr double CONFIGURATION_OF_FONT_RATIO = 1.0;
75 constexpr double TEST_INPUT_DOUBLE = 123456;
76 constexpr double STRING_TO_DIMENSION_RESULT = 100.0;
77 constexpr double STRING_TO_DEGREE_RESULT = 360.0;
78 constexpr float TEST_INPUT_FLOAT = 123456;
79 const std::string CONFIGURATION_PATH = "/data/app/el2/100/base";
80 const std::string FORMULA_ONE = "2 * 3 - (2 + 3) / 5 + 6 / 2";
81 const std::string FORMULA_TWO = "8 / 2 + 1 )";
82 const std::string FORMULA_THREE = "931 - 3 * + 102 / +";
83 const std::string TEST_INPUT_FMT_STR = "%s-%d";
84 const std::string FMT_OUT_STR = "TODAY-2022";
85 const std::string TEST_INPUT_U8_STRING = "THIS IS A STRING";
86 const std::string DEFAULT_STRING = "error";
87 const std::string TEST_INPUT_U8_STRING_NULL = "";
88 const std::string TEST_INPUT_U8_STRING_NUMBER = "123456";
89 const std::string STRING_TO_CALC_DIMENSION_RESULT = "100.0calc";
90 const std::u16string TEST_INPUT_U16_STRING = u"THIS IS A STRING";
91 const std::u16string DEFAULT_USTRING = u"error";
92 const std::wstring TEST_INPUT_W_STRING = L"THIS IS A STRING";
93 const std::wstring DEFAULT_WSTRING = L"error";
94 const char TEST_INPUT_ARGS_ONE[MAX_STRING_SIZE] = "TODAY";
95 const std::vector<int64_t> RESOURCEHANDLERS = {255};
96 } // namespace
97
98 class UtilsTest : public testing::Test {
99 public:
100 static void SetUpTestCase();
101 static void TearDownTestCase();
102 void SetUp();
103 void TearDown();
104 };
105
SetUpTestCase()106 void UtilsTest::SetUpTestCase()
107 {
108 GTEST_LOG_(INFO) << "UtilsTest SetUpTestCase";
109 }
110
TearDownTestCase()111 void UtilsTest::TearDownTestCase()
112 {
113 GTEST_LOG_(INFO) << "UtilsTest TearDownTestCase";
114 }
115
SetUp()116 void UtilsTest::SetUp()
117 {
118 GTEST_LOG_(INFO) << "UtilsTest SetUp";
119 }
120
TearDown()121 void UtilsTest::TearDown()
122 {
123 GTEST_LOG_(INFO) << "UtilsTest TearDown";
124 }
125
126 /**
127 * @tc.name: UtilsTest001
128 * @tc.desc: Call BaseId() constructor twice
129 * @tc.type: FUNC
130 */
131 HWTEST_F(UtilsTest, UtilsTest001, TestSize.Level1)
132 {
133 BaseId baseId_1;
134 ASSERT_TRUE(baseId_1.GetId() == NUM_OF_ID);
135 }
136
137 /**
138 * @tc.name: UtilsTest002
139 * @tc.desc: Set year:2022,month:10,day:28
140 * @tc.type: FUNC
141 */
142 HWTEST_F(UtilsTest, UtilsTest002, TestSize.Level1)
143 {
144 Date oneDay;
145 ASSERT_EQ(oneDay.year, DEFAULT_YEAR);
146 ASSERT_EQ(oneDay.month, DEFAULT_MONTH);
147 ASSERT_EQ(oneDay.day, DEFAULT_DAY);
148 ASSERT_EQ(oneDay.week, DEFAULT_WEEK);
149 auto locaDay = oneDay.Current();
150 time_t nowTime;
151 struct tm* localTime;
152 time(&nowTime);
153 localTime = localtime(&nowTime);
154 ASSERT_EQ(locaDay.year, localTime->tm_year + DEFAULT_YEAR);
155 ASSERT_EQ(locaDay.month, localTime->tm_mon + ADD_ONE);
156 ASSERT_EQ(locaDay.day, localTime->tm_mday);
157 ASSERT_EQ(locaDay.week, localTime->tm_wday);
158 ASSERT_EQ(locaDay.DayOfMonth(2000, 1), DAY_OF_MONTH_THIRTY_ONE);
159 ASSERT_EQ(locaDay.DayOfMonth(2000, 2), DAY_OF_MONTH_TWENTY_NINE);
160 ASSERT_EQ(locaDay.DayOfMonth(2000, 3), DAY_OF_MONTH_THIRTY_ONE);
161 ASSERT_EQ(locaDay.DayOfMonth(2000, 4), DAY_OF_MONTH_THIRTY);
162 ASSERT_EQ(locaDay.DayOfMonth(2000, 5), DAY_OF_MONTH_THIRTY_ONE);
163 ASSERT_EQ(locaDay.DayOfMonth(2000, 6), DAY_OF_MONTH_THIRTY);
164 ASSERT_EQ(locaDay.DayOfMonth(2000, 7), DAY_OF_MONTH_THIRTY_ONE);
165 ASSERT_EQ(locaDay.DayOfMonth(2000, 8), DAY_OF_MONTH_THIRTY_ONE);
166 ASSERT_EQ(locaDay.DayOfMonth(2000, 9), DAY_OF_MONTH_THIRTY);
167 ASSERT_EQ(locaDay.DayOfMonth(2000, 10), DAY_OF_MONTH_THIRTY_ONE);
168 ASSERT_EQ(locaDay.DayOfMonth(2000, 11), DAY_OF_MONTH_THIRTY);
169 ASSERT_EQ(locaDay.DayOfMonth(2000, 12), DAY_OF_MONTH_THIRTY_ONE);
170 ASSERT_EQ(locaDay.DayOfMonth(2000, 13), DAY_OF_MONTH_DEFAULT);
171 ASSERT_EQ(locaDay.CalculateWeekDay(2000, 1, 28), DAY_OF_WEEK);
172 }
173
174 /**
175 * @tc.name: UtilsTest003
176 * @tc.desc: Give a normal formula to ConvertDal2Rpn
177 * @tc.type: FUNC
178 */
179 HWTEST_F(UtilsTest, UtilsTest003, TestSize.Level1)
180 {
181 double CalculateExp(const std::string& expression, const std::function<double(const Dimension&)>& calcFunc);
182 auto calcResult =
__anon7da7bc800202(const Dimension& dim) 183 StringExpression::CalculateExp(FORMULA_ONE, [](const Dimension& dim) -> double { return dim.Value(); });
184 ASSERT_EQ(calcResult, NORMAL_CALC_RESULT);
185 }
186
187 /**
188 * @tc.name: UtilsTest004
189 * @tc.desc: Give a error formula to ConvertDal2Rpn
190 * @tc.type: FUNC
191 */
192 HWTEST_F(UtilsTest, UtilsTest004, TestSize.Level1)
193 {
194 double CalculateExp(const std::string& expression, const std::function<double(const Dimension&)>& calcFunc);
195 auto errResultTwo =
__anon7da7bc800302(const Dimension& dim) 196 StringExpression::CalculateExp(FORMULA_TWO, [](const Dimension& dim) -> double { return dim.Value(); });
197 auto errResultThree =
__anon7da7bc800402(const Dimension& dim) 198 StringExpression::CalculateExp(FORMULA_THREE, [](const Dimension& dim) -> double { return dim.Value(); });
199 ASSERT_EQ(errResultTwo, ERROR_CALC_RESULT);
200 ASSERT_EQ(errResultThree, ERROR_CALC_RESULT);
201 }
202
203 /**
204 * @tc.name: UtilsTest005
205 * @tc.desc: Check FormatString
206 * @tc.type: FUNC
207 */
208 HWTEST_F(UtilsTest, UtilsTest005, TestSize.Level1)
209 {
210 auto firstTestText = StringUtils::FormatString("");
211 auto secondTestText =
212 StringUtils::FormatString(TEST_INPUT_FMT_STR.c_str(), TEST_INPUT_ARGS_ONE, TEST_INPUT_ARGS_TWO);
213 ASSERT_TRUE(firstTestText == "");
214 ASSERT_EQ(secondTestText, FMT_OUT_STR);
215 }
216
217 /**
218 * @tc.name: UtilsTest006
219 * @tc.desc: Set the time zone within [-14, -12]
220 * @tc.type: FUNC
221 */
222 HWTEST_F(UtilsTest, UtilsTest006, TestSize.Level1)
223 {
224 time_t nowTime;
225 struct tm* localTime;
226 time(&nowTime);
227 localTime = gmtime(&nowTime);
228 int32_t localTimeHour12, localTimeHour24;
229 auto theTimeOfNow = GetTimeOfNow(SET_HOURS_FOR_THE_FIRST_TIME);
230 auto theTimeOfZone = GetTimeOfZone(SET_HOURS_FOR_THE_FIRST_TIME);
231 localTimeHour24 = localTime->tm_hour - SET_HOURS_FOR_THE_FIRST_TIME;
232 if (localTimeHour24 >= TWENTY_FOUR_HOUR_BASE) {
233 localTimeHour24 -= TWENTY_FOUR_HOUR_BASE;
234 } else if (localTimeHour24 < 0) {
235 localTimeHour24 += TWENTY_FOUR_HOUR_BASE;
236 }
237 if (localTimeHour24 >= TWELVE_HOUR_BASE) {
238 localTimeHour12 = localTimeHour24 - TWELVE_HOUR_BASE;
239 } else {
240 localTimeHour12 = localTimeHour24;
241 }
242 ASSERT_EQ(theTimeOfNow.hoursWest_, GET_HOURSWEST_FOR_THE_FIRST_TIME);
243 ASSERT_EQ(theTimeOfNow.second_, localTime->tm_sec);
244 ASSERT_EQ(theTimeOfNow.minute_, localTime->tm_min);
245 ASSERT_EQ(theTimeOfNow.hour12_, localTimeHour12);
246 ASSERT_EQ(theTimeOfNow.hour24_, localTimeHour24);
247 ASSERT_EQ(theTimeOfZone.hoursWest_, GET_HOURSWEST_FOR_THE_FIRST_TIME);
248 ASSERT_EQ(theTimeOfZone.second_, localTime->tm_sec);
249 ASSERT_EQ(theTimeOfZone.minute_, localTime->tm_min);
250 ASSERT_EQ(theTimeOfZone.hour12_, localTimeHour12);
251 ASSERT_EQ(theTimeOfZone.hour24_, localTimeHour24);
252 }
253
254 /**
255 * @tc.name: UtilsTest007
256 * @tc.desc: Set the time zone within [-12, 12]
257 * @tc.type: FUNC
258 */
259 HWTEST_F(UtilsTest, UtilsTest007, TestSize.Level1)
260 {
261 time_t nowTime;
262 struct tm* localTime;
263 time(&nowTime);
264 localTime = gmtime(&nowTime);
265 int32_t localTimeHour12, localTimeHour24;
266 auto theTimeOfNow = GetTimeOfNow(SET_HOURS_FOR_THE_SECOND_TIME);
267 auto theTimeOfZone = GetTimeOfZone(SET_HOURS_FOR_THE_SECOND_TIME);
268 localTimeHour24 = localTime->tm_hour - SET_HOURS_FOR_THE_SECOND_TIME;
269 if (localTimeHour24 >= TWENTY_FOUR_HOUR_BASE) {
270 localTimeHour24 -= TWENTY_FOUR_HOUR_BASE;
271 } else if (localTimeHour24 < 0) {
272 localTimeHour24 += TWENTY_FOUR_HOUR_BASE;
273 }
274 if (localTimeHour24 >= TWELVE_HOUR_BASE) {
275 localTimeHour12 = localTimeHour24 - TWELVE_HOUR_BASE;
276 } else {
277 localTimeHour12 = localTimeHour24;
278 }
279 bool isDayTimeOfNow = IsDayTime(theTimeOfNow);
280 bool isDayTimeOfZone = IsDayTime(theTimeOfZone);
281 ASSERT_EQ(theTimeOfNow.hoursWest_, GET_HOURSWEST_FOR_THE_SECOND_TIME);
282 ASSERT_EQ(theTimeOfNow.second_, localTime->tm_sec);
283 ASSERT_EQ(theTimeOfNow.minute_, localTime->tm_min);
284 ASSERT_EQ(theTimeOfNow.hour12_, localTimeHour12);
285 ASSERT_EQ(theTimeOfNow.hour24_, localTimeHour24);
286 ASSERT_EQ(theTimeOfZone.hoursWest_, GET_HOURSWEST_FOR_THE_SECOND_TIME);
287 ASSERT_EQ(theTimeOfZone.second_, localTime->tm_sec);
288 ASSERT_EQ(theTimeOfZone.minute_, localTime->tm_min);
289 ASSERT_EQ(theTimeOfZone.hour12_, localTimeHour12);
290 ASSERT_EQ(theTimeOfZone.hour24_, localTimeHour24);
291 if (localTimeHour24 >= DAY_TIME_LOWER_LIMIT && localTimeHour24 < DAY_TIME_UPPER_LIMIT) {
292 ASSERT_EQ(isDayTimeOfNow, true);
293 ASSERT_EQ(isDayTimeOfZone, true);
294 } else {
295 ASSERT_EQ(isDayTimeOfNow, false);
296 ASSERT_EQ(isDayTimeOfZone, false);
297 }
298 }
299
300 /**
301 * @tc.name: UtilsTest008
302 * @tc.desc: Set the time zone beyond the range of [-14, 12]
303 * @tc.type: FUNC
304 */
305 HWTEST_F(UtilsTest, UtilsTest008, TestSize.Level1)
306 {
307 time_t nowTime;
308 struct tm* localTime;
309 time(&nowTime);
310 localTime = gmtime(&nowTime);
311 auto theTimeOfNow = GetTimeOfNow(SET_HOURS_FOR_THE_THIRD_TIME);
312 auto theTimeOfZone = GetTimeOfZone(SET_HOURS_FOR_THE_THIRD_TIME);
313 struct timeval currentTime;
314 struct timezone timeZone;
315 gettimeofday(¤tTime, &timeZone);
316 auto getHoursWest = timeZone.tz_minuteswest / 60;
317 ASSERT_EQ(theTimeOfNow.hoursWest_, getHoursWest);
318 ASSERT_EQ(theTimeOfNow.second_, localTime->tm_sec);
319 ASSERT_EQ(theTimeOfNow.minute_, localTime->tm_min);
320 ASSERT_EQ(theTimeOfZone.hoursWest_, getHoursWest);
321 ASSERT_EQ(theTimeOfZone.second_, localTime->tm_sec);
322 ASSERT_EQ(theTimeOfZone.minute_, localTime->tm_min);
323 }
324
325 /**
326 * @tc.name: UtilsTest009
327 * @tc.desc: Check the time since the device was started
328 * @tc.type: FUNC
329 */
330 HWTEST_F(UtilsTest, UtilsTest009, TestSize.Level1)
331 {
332 auto microTickCount = GetMicroTickCount();
333 auto sysTimestamp = GetSysTimestamp();
334 int64_t microTickCountMsec, sysTimestampMsec;
335 microTickCountMsec = microTickCount / MICROSEC_TO_MILLISEC;
336 sysTimestampMsec = sysTimestamp / NANOSEC_TO_MILLISEC;
337 ASSERT_EQ(microTickCountMsec, sysTimestampMsec);
338 }
339
340 /**
341 * @tc.name: UtilsTest010
342 * @tc.desc: Set ResourceConfiguration
343 * @tc.type: FUNC
344 */
345 HWTEST_F(UtilsTest, UtilsTest010, TestSize.Level1)
346 {
347 ResourceConfiguration resConfiguration;
348 ASSERT_EQ(resConfiguration.TestFlag(ResourceConfiguration::COLOR_MODE_UPDATED_FLAG,
349 ResourceConfiguration::FONT_RATIO_UPDATED_FLAG), false);
350 resConfiguration.SetDeviceType(DeviceType::UNKNOWN);
351 resConfiguration.SetOrientation(DeviceOrientation::ORIENTATION_UNDEFINED);
352 resConfiguration.SetDensity(CONFIGURATION_OF_DENSITY);
353 resConfiguration.SetFontRatio(CONFIGURATION_OF_FONT_RATIO);
354 resConfiguration.SetColorMode(ColorMode::COLOR_MODE_UNDEFINED);
355 resConfiguration.SetDeviceAccess(true);
356 ASSERT_EQ(resConfiguration.GetDeviceType(), DeviceType::UNKNOWN);
357 ASSERT_EQ(resConfiguration.GetOrientation(), DeviceOrientation::ORIENTATION_UNDEFINED);
358 ASSERT_EQ(resConfiguration.GetDensity(), CONFIGURATION_OF_DENSITY);
359 ASSERT_EQ(resConfiguration.GetFontRatio(), CONFIGURATION_OF_FONT_RATIO);
360 ASSERT_EQ(resConfiguration.GetColorMode(), ColorMode::COLOR_MODE_UNDEFINED);
361 ASSERT_EQ(resConfiguration.GetDeviceAccess(), true);
362 }
363
364 /**
365 * @tc.name: UtilsTest011
366 * @tc.desc: Set ResourceInfo
367 * @tc.type: FUNC
368 */
369 HWTEST_F(UtilsTest, UtilsTest011, TestSize.Level1)
370 {
371 ResourceInfo resourceInfo;
372 ResourceConfiguration resConfiguration;
373 resourceInfo.SetResourceConfiguration(resConfiguration);
374 resourceInfo.SetResourceHandlers(RESOURCEHANDLERS);
375 resourceInfo.SetHapPath(CONFIGURATION_PATH);
376 resourceInfo.SetPackagePath(CONFIGURATION_PATH);
377 resourceInfo.SetThemeId(NUM_OF_ID);
378 ASSERT_EQ(resourceInfo.GetResourceConfiguration().GetDeviceType(), DeviceType::PHONE);
379 ASSERT_EQ(resourceInfo.GetResourceHandlers()[0], CONTENT_OF_RESOURCEHANDLERS);
380 ASSERT_EQ(resourceInfo.GetHapPath(), CONFIGURATION_PATH);
381 ASSERT_EQ(resourceInfo.GetPackagePath(), CONFIGURATION_PATH);
382 ASSERT_EQ(resourceInfo.GetThemeId(), NUM_OF_ID);
383 }
384
385 /**
386 * @tc.name: UtilsTest012
387 * @tc.desc: Json does not contain "colorMode"
388 * @tc.type: FUNC
389 */
390 HWTEST_F(UtilsTest, UtilsTest012, TestSize.Level1)
391 {
392 const std::string inputTestJsonStr = "{"
393 " \"deviceType\": \"PHONE\", "
394 " \"fontScale\": 1.0 "
395 "}";
396 ResourceConfiguration resConfiguration;
397 uint32_t updateFlags = ResourceConfiguration::COLOR_MODE_UPDATED_FLAG;
398 bool isUpdateFromJsonString = resConfiguration.UpdateFromJsonString(inputTestJsonStr, updateFlags);
399 ASSERT_EQ(isUpdateFromJsonString, true);
400 }
401
402 /**
403 * @tc.name: UtilsTest013
404 * @tc.desc: Json does not contain "fontScale"
405 * @tc.type: FUNC
406 */
407 HWTEST_F(UtilsTest, UtilsTest013, TestSize.Level1)
408 {
409 const std::string inputTestJsonStr = "{"
410 " \"deviceType\": \"PHONE\", "
411 " \"colorMode\": 1.0 "
412 "}";
413 ResourceConfiguration resConfiguration;
414 uint32_t updateFlags = ResourceConfiguration::COLOR_MODE_UPDATED_FLAG;
415 bool isUpdateFromJsonString = resConfiguration.UpdateFromJsonString(inputTestJsonStr, updateFlags);
416 ASSERT_EQ(isUpdateFromJsonString, true);
417 }
418
419 /**
420 * @tc.name: UtilsTest014
421 * @tc.desc: ColorMode is not "light" or "dark"
422 * @tc.type: FUNC
423 */
424 HWTEST_F(UtilsTest, UtilsTest014, TestSize.Level1)
425 {
426 const std::string inputTestJsonStr = "{"
427 " \"deviceType\": \"PHONE\", "
428 " \"fontScale\": \"1.0\", "
429 " \"colorMode\": \"undefined\" "
430 "}";
431 ResourceConfiguration resConfiguration;
432 uint32_t updateFlags = ResourceConfiguration::COLOR_MODE_UPDATED_FLAG;
433 bool isUpdateFromJsonString = resConfiguration.UpdateFromJsonString(inputTestJsonStr, updateFlags);
434 ASSERT_EQ(isUpdateFromJsonString, true);
435 }
436
437 /**
438 * @tc.name: UtilsTest015
439 * @tc.desc: FontScale is not 1.0
440 * @tc.type: FUNC
441 */
442 HWTEST_F(UtilsTest, UtilsTest015, TestSize.Level1)
443 {
444 const std::string inputTestJsonStr = "{"
445 " \"deviceType\": \"PHONE\", "
446 " \"fontScale\": 2.0, "
447 " \"colorMode\": \"light\" "
448 "}";
449 ResourceConfiguration resConfiguration;
450 uint32_t updateFlags = ResourceConfiguration::COLOR_MODE_UPDATED_FLAG;
451 bool isUpdateFromJsonString = resConfiguration.UpdateFromJsonString(inputTestJsonStr, updateFlags);
452 ASSERT_EQ(isUpdateFromJsonString, true);
453 ASSERT_EQ(updateFlags, ResourceConfiguration::FONT_RATIO_UPDATED_FLAG);
454 }
455
456 /**
457 * @tc.name: UtilsTest016
458 * @tc.desc: Set colorMode "dark"
459 * @tc.type: FUNC
460 */
461 HWTEST_F(UtilsTest, UtilsTest016, TestSize.Level1)
462 {
463 const std::string inputTestJsonStr = "{"
464 " \"deviceType\": \"PHONE\", "
465 " \"fontScale\": 1.0, "
466 " \"colorMode\": \"dark\" "
467 "}";
468 ResourceConfiguration resConfiguration;
469 uint32_t updateFlags = ResourceConfiguration::COLOR_MODE_UPDATED_FLAG;
470 bool isUpdateFromJsonString = resConfiguration.UpdateFromJsonString(inputTestJsonStr, updateFlags);
471 ASSERT_EQ(isUpdateFromJsonString, true);
472 ASSERT_EQ(updateFlags, ResourceConfiguration::COLOR_MODE_UPDATED_FLAG);
473 }
474
475 /**
476 * @tc.name: UtilsTest017
477 * @tc.desc: Check char in bmp and whether the content of string is a number
478 * @tc.type: FUNC
479 */
480 HWTEST_F(UtilsTest, UtilsTest017, TestSize.Level1)
481 {
482 const char16_t testChar16 = 0xD800;
483 const wchar_t testWchar = 0xD800;
484 ASSERT_EQ(StringUtils::NotInUtf16Bmp(testChar16), true);
485 ASSERT_EQ(StringUtils::NotInBmp(testWchar), true);
486 ASSERT_EQ(StringUtils::IsNumber(TEST_INPUT_U8_STRING_NULL), false);
487 ASSERT_EQ(StringUtils::IsNumber(TEST_INPUT_U8_STRING_NUMBER), true);
488 }
489
490 /**
491 * @tc.name: UtilsTest018
492 * @tc.desc: U16string-string-wstring, string-uint32, string-int64 convert
493 * @tc.type: FUNC
494 */
495 HWTEST_F(UtilsTest, UtilsTest018, TestSize.Level1)
496 {
497 ASSERT_EQ(StringUtils::Str8ToStr16(TEST_INPUT_U8_STRING), TEST_INPUT_U16_STRING);
498 ASSERT_EQ(StringUtils::Str16ToStr8(TEST_INPUT_U16_STRING), TEST_INPUT_U8_STRING);
499 ASSERT_EQ(StringUtils::ToWstring(TEST_INPUT_U8_STRING), TEST_INPUT_W_STRING);
500 ASSERT_EQ(StringUtils::ToString(TEST_INPUT_W_STRING), TEST_INPUT_U8_STRING);
501 ASSERT_EQ(StringUtils::Str8ToStr16(StringUtils::DEFAULT_STRING), StringUtils::DEFAULT_USTRING);
502 ASSERT_EQ(StringUtils::Str16ToStr8(StringUtils::DEFAULT_USTRING), StringUtils::DEFAULT_STRING);
503 ASSERT_EQ(StringUtils::ToWstring(StringUtils::DEFAULT_STRING), StringUtils::DEFAULT_WSTRING);
504 ASSERT_EQ(StringUtils::ToString(StringUtils::DEFAULT_WSTRING), StringUtils::DEFAULT_STRING);
505 ASSERT_EQ(StringUtils::DoubleToString(TEST_INPUT_DOUBLE, 0), TEST_INPUT_U8_STRING_NUMBER);
506 ASSERT_EQ(StringUtils::StringToLongInt(TEST_INPUT_U8_STRING_NUMBER), TEST_INPUT_INT64);
507 ASSERT_EQ(StringUtils::StringToUint(TEST_INPUT_U8_STRING_NUMBER), TEST_INPUT_UINT32);
508 }
509
510 /**
511 * @tc.name: UtilsTest019
512 * @tc.desc: Delete all mark : "."
513 * @tc.type: FUNC
514 */
515 HWTEST_F(UtilsTest, UtilsTest019, TestSize.Level1)
516 {
517 std::string markString = "THIS. IS. A. STRING";
518 const char mark = '.';
519 StringUtils::DeleteAllMark(markString, mark);
520 ASSERT_EQ(markString, TEST_INPUT_U8_STRING);
521 }
522
523 /**
524 * @tc.name: UtilsTest020
525 * @tc.desc: Replace tab and newline with space
526 * @tc.type: FUNC
527 */
528 HWTEST_F(UtilsTest, UtilsTest020, TestSize.Level1)
529 {
530 std::string inputTabAndNewLine = "THIS\rIS\nA\tSTRING";
531 StringUtils::ReplaceTabAndNewLine(inputTabAndNewLine);
532 ASSERT_EQ(inputTabAndNewLine, TEST_INPUT_U8_STRING);
533 }
534
535 /**
536 * @tc.name: UtilsTest021
537 * @tc.desc: Input a string contain "auto"
538 * @tc.type: FUNC
539 */
540 HWTEST_F(UtilsTest, UtilsTest021, TestSize.Level1)
541 {
542 const std::string stringToDimensionValue = "auto";
543 auto dim = StringUtils::StringToDimension(stringToDimensionValue, true);
544 auto calcDim = StringUtils::StringToCalcDimension(stringToDimensionValue, true);
545 ASSERT_EQ(dim.Value(), 0);
546 ASSERT_EQ(dim.Unit(), DimensionUnit::AUTO);
547 ASSERT_EQ(calcDim.Value(), 0);
548 ASSERT_EQ(calcDim.Unit(), DimensionUnit::AUTO);
549 }
550
551 /**
552 * @tc.name: UtilsTest022
553 * @tc.desc: Input a string contain "calc"
554 * @tc.type: FUNC
555 */
556 HWTEST_F(UtilsTest, UtilsTest022, TestSize.Level1)
557 {
558 const std::string stringToDimensionValue = "100.0calc";
559 auto dim = StringUtils::StringToDimension(stringToDimensionValue, true);
560 auto calcDim = StringUtils::StringToCalcDimension(stringToDimensionValue, true);
561 ASSERT_EQ(dim.Unit(), DimensionUnit::VP);
562 ASSERT_EQ(calcDim.CalcValue(), STRING_TO_CALC_DIMENSION_RESULT);
563 ASSERT_EQ(calcDim.Unit(), DimensionUnit::CALC);
564 }
565
566 /**
567 * @tc.name: UtilsTest023
568 * @tc.desc: Input a string contain "%"
569 * @tc.type: FUNC
570 */
571 HWTEST_F(UtilsTest, UtilsTest023, TestSize.Level1)
572 {
573 const std::string stringToDimensionValue = "100.0%";
574 auto dim = StringUtils::StringToDimension(stringToDimensionValue, true);
575 ASSERT_EQ(dim.Value(), 1.0);
576 ASSERT_EQ(dim.Unit(), DimensionUnit::PERCENT);
577 }
578
579 /**
580 * @tc.name: UtilsTest024
581 * @tc.desc: Input a string contain "px"
582 * @tc.type: FUNC
583 */
584 HWTEST_F(UtilsTest, UtilsTest024, TestSize.Level1)
585 {
586 const std::string stringToDimensionValue = "100.0px";
587 auto dim = StringUtils::StringToDimension(stringToDimensionValue, true);
588 ASSERT_EQ(dim.Value(), STRING_TO_DIMENSION_RESULT);
589 ASSERT_EQ(dim.Unit(), DimensionUnit::PX);
590 }
591
592 /**
593 * @tc.name: UtilsTest025
594 * @tc.desc: Input a string contain "vp"
595 * @tc.type: FUNC
596 */
597 HWTEST_F(UtilsTest, UtilsTest025, TestSize.Level1)
598 {
599 const std::string stringToDimensionValue = "100.0vp";
600 auto dim = StringUtils::StringToDimension(stringToDimensionValue, true);
601 ASSERT_EQ(dim.Value(), STRING_TO_DIMENSION_RESULT);
602 ASSERT_EQ(dim.Unit(), DimensionUnit::VP);
603 }
604
605 /**
606 * @tc.name: UtilsTest026
607 * @tc.desc: Input a string contain "fp"
608 * @tc.type: FUNC
609 */
610 HWTEST_F(UtilsTest, UtilsTest026, TestSize.Level1)
611 {
612 const std::string stringToDimensionValue = "100.0fp";
613 auto dim = StringUtils::StringToDimension(stringToDimensionValue, true);
614 ASSERT_EQ(dim.Value(), STRING_TO_DIMENSION_RESULT);
615 ASSERT_EQ(dim.Unit(), DimensionUnit::FP);
616 }
617
618 /**
619 * @tc.name: UtilsTest027
620 * @tc.desc: Input a string contain "lpx"
621 * @tc.type: FUNC
622 */
623 HWTEST_F(UtilsTest, UtilsTest027, TestSize.Level1)
624 {
625 const std::string stringToDimensionValue = "100.0lpx";
626 auto dim = StringUtils::StringToDimension(stringToDimensionValue, true);
627 ASSERT_EQ(dim.Value(), STRING_TO_DIMENSION_RESULT);
628 ASSERT_EQ(dim.Unit(), DimensionUnit::LPX);
629 }
630
631 /**
632 * @tc.name: UtilsTest028
633 * @tc.desc: Set an empty string
634 * @tc.type: FUNC
635 */
636 HWTEST_F(UtilsTest, UtilsTest028, TestSize.Level1)
637 {
638 const std::string stringToDimensionValue = "";
639 auto dim = StringUtils::StringToDimension(stringToDimensionValue, true);
640 ASSERT_EQ(dim.Value(), 0.0);
641 ASSERT_EQ(dim.Unit(), DimensionUnit::VP);
642 }
643
644 /**
645 * @tc.name: UtilsTest029
646 * @tc.desc: Set an empty string, check StringToDegree
647 * @tc.type: FUNC
648 */
649 HWTEST_F(UtilsTest, UtilsTest029, TestSize.Level1)
650 {
651 const std::string stringToDegreeValue = "";
652 auto degreeValue = StringUtils::StringToDegree(stringToDegreeValue);
653 ASSERT_EQ(degreeValue, 0.0);
654 }
655
656 /**
657 * @tc.name: UtilsTest030
658 * @tc.desc: Input a string contain "deg"
659 * @tc.type: FUNC
660 */
661 HWTEST_F(UtilsTest, UtilsTest030, TestSize.Level1)
662 {
663 const std::string stringToDegreeValue = "360.0deg";
664 auto degreeValue = StringUtils::StringToDegree(stringToDegreeValue);
665 ASSERT_EQ(degreeValue, STRING_TO_DEGREE_RESULT);
666 }
667
668 /**
669 * @tc.name: UtilsTest031
670 * @tc.desc: Input a string contain "gard"
671 * @tc.type: FUNC
672 */
673 HWTEST_F(UtilsTest, UtilsTest031, TestSize.Level1)
674 {
675 const std::string stringToDegreeValue = "400.0grad";
676 auto degreeValue = StringUtils::StringToDegree(stringToDegreeValue);
677 ASSERT_EQ(degreeValue, STRING_TO_DEGREE_RESULT);
678 }
679
680 /**
681 * @tc.name: UtilsTest032
682 * @tc.desc: Input a string contain "rad"
683 * @tc.type: FUNC
684 */
685 HWTEST_F(UtilsTest, UtilsTest032, TestSize.Level1)
686 {
687 const std::string stringToDegreeValue = "6.28318530717958647692rad";
688 auto degreeValue = StringUtils::StringToDegree(stringToDegreeValue);
689 ASSERT_EQ(degreeValue, STRING_TO_DEGREE_RESULT);
690 }
691
692 /**
693 * @tc.name: UtilsTest033
694 * @tc.desc: Input a string contain "turn"
695 * @tc.type: FUNC
696 */
697 HWTEST_F(UtilsTest, UtilsTest033, TestSize.Level1)
698 {
699 const std::string stringToDegreeValue = "1turn";
700 auto degreeValue = StringUtils::StringToDegree(stringToDegreeValue);
701 ASSERT_EQ(degreeValue, STRING_TO_DEGREE_RESULT);
702 }
703
704 /**
705 * @tc.name: UtilsTest034
706 * @tc.desc: Input a string contain "-", split string to string
707 * @tc.type: FUNC
708 */
709 HWTEST_F(UtilsTest, UtilsTest034, TestSize.Level1)
710 {
711 const std::string stringSplitterValue = "123456-123456";
712 const char delimiter = '-';
713 std::vector<std::string> out;
714 StringUtils::StringSplitter(stringSplitterValue, delimiter, out);
715 ASSERT_EQ(out[0], TEST_INPUT_U8_STRING_NUMBER);
716 }
717
718 /**
719 * @tc.name: UtilsTest035
720 * @tc.desc: Input a string contain "-", split string to int32_t
721 * @tc.type: FUNC
722 */
723 HWTEST_F(UtilsTest, UtilsTest035, TestSize.Level1)
724 {
725 const std::string stringSplitterValue = "123456-123456";
726 const char delimiter = '-';
727 std::vector<int32_t> out;
728 StringUtils::StringSplitter(stringSplitterValue, delimiter, out);
729 ASSERT_EQ(out[0], TEST_INPUT_INT32);
730 }
731
732 /**
733 * @tc.name: UtilsTest036
734 * @tc.desc: Input a string contain "-", split string to double
735 * @tc.type: FUNC
736 */
737 HWTEST_F(UtilsTest, UtilsTest036, TestSize.Level1)
738 {
739 const std::string stringSplitterValue = "123456-123456";
740 const char delimiter = '-';
741 std::vector<double> out;
742 StringUtils::StringSplitter(stringSplitterValue, delimiter, out);
743 ASSERT_EQ(out[0], TEST_INPUT_DOUBLE);
744 }
745
746 /**
747 * @tc.name: UtilsTest037
748 * @tc.desc: Input a string contain "-", split string to float
749 * @tc.type: FUNC
750 */
751 HWTEST_F(UtilsTest, UtilsTest037, TestSize.Level1)
752 {
753 const std::string stringSplitterValue = "123456-123456";
754 const char delimiter = '-';
755 std::vector<float> out;
756 StringUtils::StringSplitter(stringSplitterValue, delimiter, out);
757 ASSERT_EQ(out[0], TEST_INPUT_FLOAT);
758 }
759
760 /**
761 * @tc.name: UtilsTest038
762 * @tc.desc: Input a string contain "-", split string to Dimension
763 * @tc.type: FUNC
764 */
765 HWTEST_F(UtilsTest, UtilsTest038, TestSize.Level1)
766 {
767 const std::string stringSplitterValue = "123456-123456";
768 const char delimiter = '-';
769 std::vector<Dimension> out;
770 StringUtils::StringSplitter(stringSplitterValue, delimiter, out);
771 ASSERT_EQ(out[0].Value(), TEST_INPUT_DOUBLE);
772 ASSERT_EQ(out[0].Unit(), DimensionUnit::PX);
773 }
774
775 /**
776 * @tc.name: UtilsTest039
777 * @tc.desc: No characters found for splitSepValue in splitStrValue
778 * @tc.type: FUNC
779 */
780 HWTEST_F(UtilsTest, UtilsTest039, TestSize.Level1)
781 {
782 const std::string splitStrValue = "THIS IS A STRING";
783 const std::string splitSepValue = "!?";
784 std::vector<std::string> stringOut;
785 std::vector<Dimension> dimOut;
786 StringUtils::SplitStr(splitStrValue, splitSepValue, stringOut, true);
787 StringUtils::SplitStr(splitStrValue, splitSepValue, dimOut, true);
788 ASSERT_EQ(stringOut[0], TEST_INPUT_U8_STRING);
789 ASSERT_EQ(dimOut[0].Value(), 0);
790 ASSERT_EQ(dimOut[0].Unit(), DimensionUnit::PX);
791 }
792
793 /**
794 * @tc.name: UtilsTest040
795 * @tc.desc: There is some characters found for splitSepValue in splitStrValue
796 * @tc.type: FUNC
797 */
798 HWTEST_F(UtilsTest, UtilsTest040, TestSize.Level1)
799 {
800 const std::string splitStrValue = "##THIS IS A STRING***##";
801 const std::string splitSepValue = "#*";
802 std::vector<std::string> stringOut;
803 std::vector<Dimension> dimOut;
804 StringUtils::SplitStr(splitStrValue, splitSepValue, stringOut, true);
805 StringUtils::SplitStr(splitStrValue, splitSepValue, dimOut, true);
806 ASSERT_EQ(stringOut[0], TEST_INPUT_U8_STRING);
807 ASSERT_EQ(dimOut[0].Value(), 0);
808 ASSERT_EQ(dimOut[0].Unit(), DimensionUnit::PX);
809 }
810
811 /**
812 * @tc.name: UtilsTest041
813 * @tc.desc: splitString is empty
814 * @tc.type: FUNC
815 */
816 HWTEST_F(UtilsTest, UtilsTest041, TestSize.Level1)
817 {
818 const std::string splitStrValue = "";
819 const std::string splitSepValue = "";
820 std::vector<std::string> stringOut;
821 std::vector<Dimension> dimOut;
822 StringUtils::SplitStr(splitStrValue, splitSepValue, stringOut, true);
823 StringUtils::SplitStr(splitStrValue, splitSepValue, dimOut, true);
824 ASSERT_TRUE(stringOut.empty());
825 ASSERT_TRUE(dimOut.empty());
826 }
827
828 /**
829 * @tc.name: UtilsTest042
830 * @tc.desc: StartWith, EndWith the same string
831 * @tc.type: FUNC
832 */
833 HWTEST_F(UtilsTest, UtilsTest042, TestSize.Level1)
834 {
835 const std::string startWithValue = "THIS IS A STRING";
836 const std::string prefixString = "THIS IS A STRING";
837 const char* prefixChar = "THIS IS A STRING";
838 size_t prefixLen = startWithValue.size();
839 ASSERT_EQ(StringUtils::StartWith(startWithValue, prefixString), true);
840 ASSERT_EQ(StringUtils::StartWith(startWithValue, prefixChar, prefixLen), true);
841 ASSERT_EQ(StringUtils::EndWith(startWithValue, prefixString), true);
842 ASSERT_EQ(StringUtils::EndWith(startWithValue, prefixString), true);
843 }
844 } // namespace OHOS::Ace