1 /*
2 * Copyright (c) 2022-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 #include "entity_recognizer.h"
18 #include "i18n_break_iterator.h"
19 #include "i18n_calendar.h"
20 #include "i18n_normalizer.h"
21 #include "i18n_timezone_mock.h"
22 #include "locale_config.h"
23 #include "locale_info.h"
24 #include "locale_matcher.h"
25 #include "locale_util.h"
26 #include "lunar_calendar.h"
27 #include "measure_data.h"
28 #include "phone_number_format_mock.h"
29 #include "preferred_language.h"
30 #include "simple_date_time_format.h"
31 #include "simple_number_format.h"
32 #include "utils.h"
33
34 using namespace OHOS::Global::I18n;
35 using testing::ext::TestSize;
36 using namespace std;
37
38 namespace OHOS {
39 namespace Global {
40 namespace I18n {
41 class I18nTest : public testing::Test {
42 public:
43 static void SetUpTestCase(void);
44 static void TearDownTestCase(void);
45 void SetUp();
46 void TearDown();
47
48 static std::string originalLanguage;
49 static std::string originalRegion;
50 static std::string originalLocale;
51 };
52
53 std::string I18nTest::originalLanguage;
54 std::string I18nTest::originalRegion;
55 std::string I18nTest::originalLocale;
56
SetUpTestCase(void)57 void I18nTest::SetUpTestCase(void)
58 {
59 LocaleConfig::SetSystemLanguage("zh-Hans");
60 LocaleConfig::SetSystemRegion("CN");
61 LocaleConfig::SetSystemLocale("zh-Hans-CN");
62 }
63
TearDownTestCase(void)64 void I18nTest::TearDownTestCase(void)
65 {
66 LocaleConfig::SetSystemLanguage(originalLanguage);
67 LocaleConfig::SetSystemRegion(originalRegion);
68 LocaleConfig::SetSystemLocale(originalLocale);
69 }
70
SetUp(void)71 void I18nTest::SetUp(void)
72 {
73 originalLanguage = LocaleConfig::GetSystemLanguage();
74 originalRegion = LocaleConfig::GetSystemRegion();
75 originalLocale = LocaleConfig::GetSystemLocale();
76 }
77
TearDown(void)78 void I18nTest::TearDown(void)
79 {}
80
81 /**
82 * @tc.name: I18nFuncTest001
83 * @tc.desc: Test I18n PreferredLanguage GetPreferredLocale
84 * @tc.type: FUNC
85 */
86 HWTEST_F(I18nTest, I18nFuncTest001, TestSize.Level1)
87 {
88 string preferredLocale = PreferredLanguage::GetPreferredLocale();
89 EXPECT_TRUE(preferredLocale.size() > 0);
90 string systemLocale = "zh-Hans-CN";
91 I18nErrorCode status = LocaleConfig::SetSystemLocale(systemLocale);
92 EXPECT_EQ(status, I18nErrorCode::SUCCESS);
93 status = PreferredLanguage::AddPreferredLanguage("en-US", 0);
94 EXPECT_EQ(status, I18nErrorCode::SUCCESS);
95 preferredLocale = PreferredLanguage::GetPreferredLocale();
96 EXPECT_EQ(preferredLocale, "en-CN");
97 PreferredLanguage::RemovePreferredLanguage(0);
98 #ifdef SUPPORT_APP_PREFERRED_LANGUAGE
99 I18nErrorCode errCode = I18nErrorCode::SUCCESS;
100 PreferredLanguage::SetAppPreferredLanguage("en-US", errCode);
101 EXPECT_EQ(errCode, I18nErrorCode::SUCCESS);
102 std::string appPreferLanguage = PreferredLanguage::GetAppPreferredLanguage();
103 EXPECT_EQ(appPreferLanguage, "en-US");
104 #endif
105 }
106
107 /**
108 * @tc.name: I18nFuncTest002
109 * @tc.desc: Test I18n Normalizer
110 * @tc.type: FUNC
111 */
112 HWTEST_F(I18nTest, I18nFuncTest002, TestSize.Level1)
113 {
114 I18nErrorCode errorCode = I18nErrorCode::SUCCESS;
115 I18nNormalizer normalizer(I18nNormalizerMode::NFD, errorCode);
116 EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
117
118 string text = "\uFB01"; // \uFB01 is fi
119 string normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
120 EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
121 EXPECT_EQ(normalizedText.length(), 3); // 3 is the NFD normalized length of fi.
122 EXPECT_EQ(normalizedText, "\uFB01");
123
124 text = "\u0032\u2075";
125 normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
126 EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
127 EXPECT_EQ(normalizedText.length(), 4); // 4 is the expected normalized text length.
128 EXPECT_EQ(normalizedText, "\u0032\u2075");
129
130 text = "\u1E9B\u0323";
131 normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
132 EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
133 EXPECT_EQ(normalizedText.length(), 6); // 6 is the expected normalized text length.
134 EXPECT_EQ(normalizedText, "\u017F\u0323\u0307");
135 std::string result = normalizer.Normalize(nullptr, 0, errorCode);
136 EXPECT_EQ(result, "");
137 }
138
139 /**
140 * @tc.name: I18nFuncTest003
141 * @tc.desc: Test I18n Normalizer
142 * @tc.type: FUNC
143 */
144 HWTEST_F(I18nTest, I18nFuncTest003, TestSize.Level1)
145 {
146 I18nErrorCode errorCode = I18nErrorCode::SUCCESS;
147 I18nNormalizer normalizer(I18nNormalizerMode::NFC, errorCode);
148 EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
149
150 string text = "\uFB01"; // \uFB01 is fi
151 string normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
152 EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
153 EXPECT_EQ(normalizedText.length(), 3); // 3 is the NFC normalized length of fi.
154 EXPECT_EQ(normalizedText, "\uFB01");
155
156 text = "\u0032\u2075";
157 normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
158 EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
159 EXPECT_EQ(normalizedText.length(), 4); // 4 is the expected normalized text length.
160 EXPECT_EQ(normalizedText, "\u0032\u2075");
161
162 text = "\u1E9B\u0323";
163 normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
164 EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
165 EXPECT_EQ(normalizedText.length(), 5); // 5 is the expected normalized text length.
166 EXPECT_EQ(normalizedText, "\u1E9B\u0323");
167 }
168
169 /**
170 * @tc.name: I18nFuncTest004
171 * @tc.desc: Test I18n Normalizer
172 * @tc.type: FUNC
173 */
174 HWTEST_F(I18nTest, I18nFuncTest004, TestSize.Level1)
175 {
176 I18nErrorCode errorCode = I18nErrorCode::SUCCESS;
177 I18nNormalizer normalizer(I18nNormalizerMode::NFKD, errorCode);
178 EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
179
180 string text = "\uFB01"; // \uFB01 is fi
181 string normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
182 EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
183 EXPECT_EQ(normalizedText.length(), 2); // 2 is the NFKD normalized length of fi.
184 EXPECT_EQ(normalizedText, "\u0066\u0069");
185
186 text = "\u0032\u2075";
187 normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
188 EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
189 EXPECT_EQ(normalizedText.length(), 2); // 2 is the expected normalized text length.
190 EXPECT_EQ(normalizedText, "\u0032\u0035");
191
192 text = "\u1E9B\u0323";
193 normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
194 EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
195 EXPECT_EQ(normalizedText.length(), 5); // 5 is the expected normalized text length.
196 EXPECT_EQ(normalizedText, "\u0073\u0323\u0307");
197 }
198
199 /**
200 * @tc.name: I18nFuncTest005
201 * @tc.desc: Test I18n Normalizer
202 * @tc.type: FUNC
203 */
204 HWTEST_F(I18nTest, I18nFuncTest005, TestSize.Level1)
205 {
206 I18nErrorCode errorCode = I18nErrorCode::SUCCESS;
207 I18nNormalizer normalizer(I18nNormalizerMode::NFKC, errorCode);
208 EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
209
210 string text = "\uFB01"; // \uFB01 is fi
211 string normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
212 EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
213 EXPECT_EQ(normalizedText.length(), 2); // 2 is the NFKC normalized length of fi.
214 EXPECT_EQ(normalizedText, "\u0066\u0069");
215
216 text = "\u0032\u2075";
217 normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
218 EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
219 EXPECT_EQ(normalizedText.length(), 2); // 2 is the expected normalized text length.
220 EXPECT_EQ(normalizedText, "\u0032\u0035");
221
222 text = "\u1E9B\u0323";
223 normalizedText = normalizer.Normalize(text.c_str(), text.length(), errorCode);
224 EXPECT_EQ(errorCode, I18nErrorCode::SUCCESS);
225 EXPECT_EQ(normalizedText.length(), 3); // 3 is the expected normalized text length.
226 EXPECT_EQ(normalizedText, "\u1E69");
227 }
228
229 /**
230 * @tc.name: I18nFuncTest006
231 * @tc.desc: Test I18n I18nCalendar
232 * @tc.type: FUNC
233 */
234 HWTEST_F(I18nTest, I18nFuncTest006, TestSize.Level1)
235 {
236 I18nCalendar buddhistCalendar("zh-Hans", CalendarType::BUDDHIST);
237 buddhistCalendar.Set(UCalendarDateFields::UCAL_YEAR, 2023);
238 int32_t year = buddhistCalendar.Get(UCalendarDateFields::UCAL_YEAR);
239 EXPECT_EQ(year, 2023);
240 I18nCalendar chineseCalendar("zh-Hans", CalendarType::CHINESE);
241 chineseCalendar.SetMinimalDaysInFirstWeek(3);
242 int32_t minimalDaysInFirstWeek = chineseCalendar.GetMinimalDaysInFirstWeek();
243 EXPECT_EQ(minimalDaysInFirstWeek, 3);
244 I18nCalendar copticCalendar("zh-Hans", CalendarType::COPTIC);
245 copticCalendar.SetFirstDayOfWeek(2);
246 int32_t firstDayOfWeek = copticCalendar.GetFirstDayOfWeek();
247 EXPECT_EQ(firstDayOfWeek, 2);
248 I18nCalendar ethiopicCalendar("zh-Hans", CalendarType::ETHIOPIC);
249 int64_t date = 1687244448234;
250 UErrorCode status = U_ZERO_ERROR;
251 bool isWeekend = ethiopicCalendar.IsWeekend(date, status);
252 EXPECT_EQ(isWeekend, false);
253 I18nCalendar hebrewCalendar("zh-Hans", CalendarType::HEBREW);
254 std::string displayLocaleTag = "en-US";
255 std::string displayName = hebrewCalendar.GetDisplayName(displayLocaleTag);
256 EXPECT_EQ(displayName, "Hebrew Calendar");
257 I18nCalendar gregoryCalendar("zh-Hans", CalendarType::GREGORY);
258 gregoryCalendar.Set(UCalendarDateFields::UCAL_MONTH, 2);
259 int32_t month = gregoryCalendar.Get(UCalendarDateFields::UCAL_MONTH);
260 EXPECT_EQ(month, 2);
261 I18nCalendar indianCalendar("zh-Hans", CalendarType::INDIAN);
262 indianCalendar.Set(UCalendarDateFields::UCAL_WEEK_OF_YEAR, 10);
263 int32_t weekOfYear = indianCalendar.Get(UCalendarDateFields::UCAL_WEEK_OF_YEAR);
264 EXPECT_EQ(weekOfYear, 10);
265 I18nCalendar islamicCivilCalendar("zh-Hans", CalendarType::ISLAMIC_CIVIL);
266 islamicCivilCalendar.Set(UCalendarDateFields::UCAL_WEEK_OF_MONTH, 2);
267 int32_t weekOfMonth = islamicCivilCalendar.Get(UCalendarDateFields::UCAL_WEEK_OF_MONTH);
268 EXPECT_EQ(weekOfMonth, 2);
269 I18nCalendar islamicTblaCalendar("zh-Hans", CalendarType::ISLAMIC_TBLA);
270 islamicTblaCalendar.Set(UCalendarDateFields::UCAL_DATE, 3);
271 int32_t dateValue = islamicTblaCalendar.Get(UCalendarDateFields::UCAL_DATE);
272 EXPECT_EQ(dateValue, 3);
273 I18nCalendar islamicUmalquraCalendar("zh-Hans", CalendarType::ISLAMIC_UMALQURA);
274 islamicUmalquraCalendar.Set(UCalendarDateFields::UCAL_DAY_OF_YEAR, 123);
275 int32_t dayOfYear = islamicUmalquraCalendar.Get(UCalendarDateFields::UCAL_DAY_OF_YEAR);
276 EXPECT_EQ(dayOfYear, 123);
277 I18nCalendar japaneseCalendar("zh-Hans", CalendarType::JAPANESE);
278 japaneseCalendar.Set(UCalendarDateFields::UCAL_DAY_OF_WEEK, 3);
279 int32_t dayOfWeek = japaneseCalendar.Get(UCalendarDateFields::UCAL_DAY_OF_WEEK);
280 EXPECT_EQ(dayOfWeek, 3);
281 }
282
283 /**
284 * @tc.name: I18nFuncTest007
285 * @tc.desc: Test I18n I18nCalendar
286 * @tc.type: FUNC
287 */
288 HWTEST_F(I18nTest, I18nFuncTest007, TestSize.Level1)
289 {
290 I18nCalendar persianTblaCalendar("zh-Hans", CalendarType::PERSIAN);
291 persianTblaCalendar.Set(UCalendarDateFields::UCAL_DAY_OF_WEEK_IN_MONTH, 1);
292 int32_t dayOfWeekInMonth = persianTblaCalendar.Get(UCalendarDateFields::UCAL_DAY_OF_WEEK_IN_MONTH);
293 EXPECT_EQ(dayOfWeekInMonth, 1);
294 I18nCalendar defaultCalendar("zh-Hans", CalendarType::UNDEFINED);
295 defaultCalendar.Set(UCalendarDateFields::UCAL_HOUR, 12);
296 int32_t hour = defaultCalendar.Get(UCalendarDateFields::UCAL_HOUR);
297 EXPECT_EQ(hour, 0);
298 defaultCalendar.Set(UCalendarDateFields::UCAL_MILLISECOND, 1000);
299 defaultCalendar.Add(UCalendarDateFields::UCAL_MILLISECOND, 165);
300 EXPECT_EQ(defaultCalendar.Get(UCalendarDateFields::UCAL_MILLISECOND), 165);
301 defaultCalendar.Set(UCalendarDateFields::UCAL_SECOND, 21);
302 defaultCalendar.Add(UCalendarDateFields::UCAL_SECOND, 51);
303 EXPECT_EQ(defaultCalendar.Get(UCalendarDateFields::UCAL_SECOND), 12);
304 defaultCalendar.Set(UCalendarDateFields::UCAL_MINUTE, 31);
305 defaultCalendar.Add(UCalendarDateFields::UCAL_MINUTE, 31);
306 EXPECT_EQ(defaultCalendar.Get(UCalendarDateFields::UCAL_MINUTE), 2);
307 defaultCalendar.Set(UCalendarDateFields::UCAL_HOUR, 10);
308 defaultCalendar.Add(UCalendarDateFields::UCAL_HOUR, 11);
309 EXPECT_EQ(defaultCalendar.Get(UCalendarDateFields::UCAL_HOUR), 9);
310 defaultCalendar.Set(UCalendarDateFields::UCAL_DATE, 20);
311 defaultCalendar.Add(UCalendarDateFields::UCAL_DATE, 15);
312 EXPECT_EQ(defaultCalendar.Get(UCalendarDateFields::UCAL_DATE), 4);
313 defaultCalendar.Set(UCalendarDateFields::UCAL_MONTH, 10);
314 defaultCalendar.Add(UCalendarDateFields::UCAL_MONTH, 2);
315 EXPECT_EQ(defaultCalendar.Get(UCalendarDateFields::UCAL_MONTH), 0);
316 defaultCalendar.Set(UCalendarDateFields::UCAL_YEAR, 100);
317 defaultCalendar.Add(UCalendarDateFields::UCAL_YEAR, 100);
318 EXPECT_EQ(defaultCalendar.Get(UCalendarDateFields::UCAL_YEAR), 200);
319 defaultCalendar.Set(2023, 8, 2);
320 UDate millis = defaultCalendar.GetTimeInMillis();
321 I18nCalendar checkCalendar("zh-Hans", CalendarType::UNDEFINED);
322 checkCalendar.SetTime(millis);
323 EXPECT_EQ(checkCalendar.Get(UCalendarDateFields::UCAL_YEAR), 2023);
324 EXPECT_EQ(checkCalendar.Get(UCalendarDateFields::UCAL_MONTH), 8);
325 EXPECT_EQ(checkCalendar.Get(UCalendarDateFields::UCAL_DATE), 2);
326 defaultCalendar.SetTime(1684742124645);
327 int32_t compareDays = defaultCalendar.CompareDays(1684742124650);
328 EXPECT_EQ(compareDays, 1);
329 }
330
331 /**
332 * @tc.name: I18nFuncTest008
333 * @tc.desc: Test I18n I18nBreakIterator
334 * @tc.type: FUNC
335 */
336 HWTEST_F(I18nTest, I18nFuncTest008, TestSize.Level1)
337 {
338 std::string fakeLocaleTag = "FakeLocaleTag";
339 I18nBreakIterator brkIterator(fakeLocaleTag);
340 int32_t res = brkIterator.Current();
341 EXPECT_EQ(res, 0);
342 res = brkIterator.First();
343 EXPECT_EQ(res, 0);
344 res = brkIterator.Last();
345 EXPECT_EQ(res, 0);
346 res = brkIterator.Previous();
347 EXPECT_EQ(res, -1);
348 int32_t offset = 1;
349 res = brkIterator.Next(offset);
350 EXPECT_EQ(res, -1);
351 res = brkIterator.Next();
352 EXPECT_EQ(res, -1);
353 res = brkIterator.Following(offset);
354 EXPECT_EQ(res, -1);
355 bool status = brkIterator.IsBoundary(offset);
356 EXPECT_FALSE(status);
357
358 std::string localeTag = "en-Latn-US";
359 I18nBreakIterator enBrkIterator(localeTag);
360 std::string text = "Test I18nBreakIterator";
361 enBrkIterator.SetText(text.c_str());
362 std::string resText;
363 enBrkIterator.GetText(resText);
364 EXPECT_EQ(resText, text);
365 res = enBrkIterator.Next();
366 EXPECT_EQ(res, 5);
367 }
368
369 /**
370 * @tc.name: I18nFuncTest009
371 * @tc.desc: Test I18n MeasureData GetDefaultPreferredUnit
372 * @tc.type: FUNC
373 */
374 HWTEST_F(I18nTest, I18nFuncTest009, TestSize.Level1)
375 {
376 std::string regionTags[] = {"GB", "US", "CN"};
377 std::string usage = "length";
378 std::vector<std::string> units;
379 for (size_t i = 0; i < sizeof(regionTags) / sizeof(std::string); ++i) {
380 GetDefaultPreferredUnit(regionTags[i], usage, units);
381 EXPECT_EQ(units.size(), 3);
382 units.clear();
383 }
384 }
385
386 /**
387 * @tc.name: I18nFuncTest010
388 * @tc.desc: Test I18n MeasureData GetFallbackPreferredUnit
389 * @tc.type: FUNC
390 */
391 HWTEST_F(I18nTest, I18nFuncTest010, TestSize.Level1)
392 {
393 std::string regionTags1[] = {"MX", "NL", "NO", "PL", "RU"};
394 std::string usage = "length-person-informal";
395 std::vector<std::string> units;
396 for (size_t i = 0; i < sizeof(regionTags1) / sizeof(std::string); ++i) {
397 GetFallbackPreferredUnit(regionTags1[i], usage, units);
398 EXPECT_EQ(units.size(), 2);
399 units.clear();
400 }
401
402 std::string regionTag2 = "SE";
403 usage = "length-person";
404 GetFallbackPreferredUnit(regionTag2, usage, units);
405 EXPECT_EQ(units.size(), 2);
406 units.clear();
407
408 std::string regionTags3[] = {"US", "CN"};
409 for (size_t i = 0; i < sizeof(regionTags3) / sizeof(std::string); ++i) {
410 GetFallbackPreferredUnit(regionTags3[i], usage, units);
411 EXPECT_EQ(units.size(), 1);
412 units.clear();
413 }
414 }
415
416 /**
417 * @tc.name: I18nFuncTest011
418 * @tc.desc: Test I18n MeasureData GetRestPreferredUnit
419 * @tc.type: FUNC
420 */
421 HWTEST_F(I18nTest, I18nFuncTest011, TestSize.Level1)
422 {
423 std::string regionTags1[] = {"CA", "IN"};
424 std::string usage = "length-person";
425 std::vector<std::string> units;
426 for (size_t i = 0; i < sizeof(regionTags1) / sizeof(std::string); ++i) {
427 GetRestPreferredUnit(regionTags1[i], usage, units);
428 EXPECT_EQ(units.size(), 1);
429 units.clear();
430 }
431
432 std::string regionTags2[] = {"CN", "DK", "PT", "DE", "GB"};
433 usage = "length-person-informal";
434 for (size_t i = 0; i < sizeof(regionTags2) / sizeof(std::string); ++i) {
435 GetRestPreferredUnit(regionTags2[i], usage, units);
436 EXPECT_EQ(units.size(), 2);
437 units.clear();
438 }
439
440 std::string regionTag3 = "KR";
441 usage = "speed-wind";
442 GetRestPreferredUnit(regionTag3, usage, units);
443 EXPECT_EQ(units.size(), 1);
444 units.clear();
445
446 std::string regionTag4 = "XX";
447 usage = "fake usage";
448 GetRestPreferredUnit(regionTag4, usage, units);
449 EXPECT_EQ(units.size(), 0);
450 }
451
452 /**
453 * @tc.name: I18nFuncTest012
454 * @tc.desc: Test I18n MeasureData GetPreferredUnit
455 * @tc.type: FUNC
456 */
457 HWTEST_F(I18nTest, I18nFuncTest012, TestSize.Level1)
458 {
459 std::string regionTags1[] = {
460 "AT", "BE", "DZ", "EG", "ES", "FR", "HK", "ID", "IL", "IT", "JO", "MY", "SA", "TR", "VN"
461 };
462 std::string usage = "length-person";
463 std::vector<std::string> units;
464 for (size_t i = 0; i < sizeof(regionTags1) / sizeof(std::string); ++i) {
465 GetPreferredUnit(regionTags1[i], usage, units);
466 EXPECT_EQ(units.size(), 2);
467 units.clear();
468 }
469
470 std::string regionTag2 = "BR";
471 usage = "length-person-informal";
472 GetPreferredUnit(regionTag2, usage, units);
473 EXPECT_EQ(units.size(), 2);
474 units.clear();
475
476 std::string regionTags3[] = {"BS", "BZ", "PR", "PW"};
477 usage = "temperature-weather";
478 for (size_t i = 0; i < sizeof(regionTags3) / sizeof(std::string); i++) {
479 GetPreferredUnit(regionTags3[i], usage, units);
480 EXPECT_EQ(units.size(), 1);
481 units.clear();
482 }
483
484 std::string regionTag4 = "XX";
485 usage = "fake usage";
486 GetPreferredUnit(regionTag4, usage, units);
487 EXPECT_EQ(units.size(), 0);
488 }
489
490 /**
491 * @tc.name: I18nFuncTest013
492 * @tc.desc: Test I18n MeasureData
493 * @tc.type: FUNC
494 */
495 HWTEST_F(I18nTest, I18nFuncTest013, TestSize.Level1)
496 {
497 double value = 10.0;
498 std::string fromUnit = "acre";
499 std::string fromMeasSys = "US";
500 std::string toUnit = "foot";
501 std::string toMeasSys = "US";
502 int status = Convert(value, fromUnit, fromMeasSys, toUnit, toMeasSys);
503 EXPECT_EQ(status, 0);
504
505 std::string fakeUnit = "fake unit";
506 status = Convert(value, fakeUnit, fromMeasSys, toUnit, toMeasSys);
507 EXPECT_EQ(status, 0);
508 status = Convert(value, fromUnit, fromMeasSys, fakeUnit, toMeasSys);
509 EXPECT_EQ(status, 0);
510
511 toUnit = "hectare";
512 status = Convert(value, fromUnit, fromMeasSys, toUnit, toMeasSys);
513 EXPECT_EQ(status, 1);
514 EXPECT_TRUE(Eq(value, 4.0468564));
515
516 std::string unit = "tablespoon";
517 std::string measSys = "UK";
518 std::vector<double> factors = {0.0, 0.0};
519 ComputeFactorValue(unit, measSys, factors);
520 EXPECT_EQ(factors.size(), 2);
521 EXPECT_TRUE(Eq(factors[0], 1.77582e-05));
522 unit = "acre";
523 factors = {0.0, 0.0};
524 ComputeFactorValue(unit, measSys, factors);
525 EXPECT_EQ(factors.size(), 2);
526 EXPECT_TRUE(Eq(factors[0], 4046.856422));
527
528 double res = ComputeSIPrefixValue(unit);
529 EXPECT_TRUE(Eq(res, 0));
530 unit = "deci";
531 res = ComputeSIPrefixValue(unit);
532 EXPECT_TRUE(Eq(res, 0.1));
533
534 unit = "square-acre";
535 factors = {0.0, 0.0};
536 ComputePowerValue(unit, measSys, factors);
537 EXPECT_EQ(factors.size(), 2);
538
539 unit = "fake-per-hour";
540 factors = {0.0, 0.0};
541 status = ComputeValue(unit, measSys, factors);
542 EXPECT_EQ(status, 0);
543 unit = "kilometer-per-fake";
544 factors = {0.0, 0.0};
545 status = ComputeValue(unit, measSys, factors);
546 EXPECT_EQ(status, 0);
547 }
548
549 /**
550 * @tc.name: I18nFuncTest014
551 * @tc.desc: Test I18n PhoneNumberFormat
552 * @tc.type: FUNC
553 */
554 HWTEST_F(I18nTest, I18nFuncTest014, TestSize.Level1)
555 {
556 std::string countryTag = "";
557 std::map<std::string, std::string> options = {
558 {"type", "INTERNATIONAL"}
559 };
560 std::unique_ptr<PhoneNumberFormat> formatter = PhoneNumberFormat::CreateInstance(countryTag, options);
561 std::string fakePhoneNumber = "93827393872723482";
562 bool isValid = formatter->isValidPhoneNumber(fakePhoneNumber);
563 EXPECT_FALSE(isValid);
564 std::string formattedPhoneNumber = formatter->format(fakePhoneNumber);
565 EXPECT_EQ(formattedPhoneNumber, "");
566 std::unique_ptr<PhoneNumberFormat> formatter1 = std::make_unique<PhoneNumberFormatMock>("XK", options);
567 std::string blocklRegion = formatter1->getLocationName("0038312345678", "zh-CN");
568 EXPECT_EQ(blocklRegion, "");
569 std::unique_ptr<PhoneNumberFormat> formatter2 = std::make_unique<PhoneNumberFormatMock>("CN", options);
570 std::string blocklCity = formatter2->getLocationName("13731630016", "zh-CN");
571 EXPECT_EQ(blocklCity, "");
572 std::string replaceCity = formatter2->getLocationName("13731930016", "zh-CN");
573 EXPECT_EQ(replaceCity, "安徽省宣城市2");
574 std::string number192 = "19200707087";
575 std::string formattedNumber = formatter2->format(number192);
576 EXPECT_EQ(formattedNumber, "+86 192 0070 7087");
577 }
578
579 /**
580 * @tc.name: I18nFuncTest015
581 * @tc.desc: Test I18n I18nTimeZone
582 * @tc.type: FUNC
583 */
584 HWTEST_F(I18nTest, I18nFuncTest015, TestSize.Level1)
585 {
586 std::string id = "Asia/Shanghai";
587 bool isZoneID = true;
588 I18nTimeZone timezone1(id, isZoneID);
589 std::string timezoneId = timezone1.GetID();
590 EXPECT_EQ(timezoneId, "Asia/Shanghai");
591
592 id = "Shanghai";
593 isZoneID = false;
594 I18nTimeZone timezone2(id, isZoneID);
595 timezoneId = timezone2.GetID();
596 EXPECT_EQ(timezoneId, "Asia/Shanghai");
597
598 id = "Auckland";
599 I18nTimeZone timezone3(id, isZoneID);
600 timezoneId = timezone3.GetID();
601 EXPECT_EQ(timezoneId, "Pacific/Auckland");
602 std::string localeTag = "en-Latn-US";
603 std::string displayName = timezone3.GetDisplayName(localeTag);
604 EXPECT_EQ(timezoneId, "Pacific/Auckland");
605
606 std::string fakeId = "fake city id";
607 std::string cityDisplayName = I18nTimeZone::GetCityDisplayName(fakeId, localeTag);
608 EXPECT_EQ(cityDisplayName, "");
609
610 cityDisplayName = I18nTimeZone::GetCityDisplayName(id, localeTag);
611 EXPECT_EQ(cityDisplayName, "Auckland (New Zealand)");
612
613 std::string fakeLocale = "fake locale tag";
614 cityDisplayName = I18nTimeZone::GetCityDisplayName(id, fakeLocale);
615 EXPECT_EQ(cityDisplayName, "");
616
617 id = "Pacific/Enderbury";
618 std::unique_ptr<I18nTimeZone> timezone4 = std::make_unique<I18nTimeZoneMock>(id, true);
619 std::string cityName = timezone4->GetDisplayName("zh-CN", false);
620 EXPECT_EQ(cityName, "菲尼克斯群岛标准时间");
621 }
622
623 /**
624 * @tc.name: I18nFuncTest016
625 * @tc.desc: Test I18n EntityRecognizer
626 * @tc.type: FUNC
627 */
628 HWTEST_F(I18nTest, I18nFuncTest016, TestSize.Level1)
629 {
630 std::string localeStr = "zh-CN";
631 UErrorCode status = U_ZERO_ERROR;
632 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
633 std::string message = "您好,您的包裹已送至指定地点,请尽快签收:快递员:刘某某,手机:15912345678,QQ:123456789。";
634 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
635 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
636 EXPECT_EQ(res[0][0], 1);
637 EXPECT_EQ(res[0][1], 32);
638 EXPECT_EQ(res[0][2], 43);
639 }
640
641 /**
642 * @tc.name: I18nFuncTest017
643 * @tc.desc: Test I18n EntityRecognizer
644 * @tc.type: FUNC
645 */
646 HWTEST_F(I18nTest, I18nFuncTest017, TestSize.Level1)
647 {
648 std::string localeStr = "zh-CN";
649 UErrorCode status = U_ZERO_ERROR;
650 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
651 std::string message = "您好,您的包裹已送至指定地点,请尽快签收:快递员:刘某某,手机:15912345678 15512345678。";
652 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
653 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
654 EXPECT_EQ(res[0][0], 2);
655 EXPECT_EQ(res[0][1], 32);
656 EXPECT_EQ(res[0][2], 43);
657 EXPECT_EQ(res[0][3], 44);
658 EXPECT_EQ(res[0][4], 55);
659 }
660
661 /**
662 * @tc.name: I18nFuncTest018
663 * @tc.desc: Test I18n EntityRecognizer
664 * @tc.type: FUNC
665 */
666 HWTEST_F(I18nTest, I18nFuncTest018, TestSize.Level1)
667 {
668 std::string localeStr = "en-GB";
669 UErrorCode status = U_ZERO_ERROR;
670 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
671 std::string message = "The stunning xxxxxx xxx in Verbier is availble for Christmas - call +44 (0)20 1234 5678.";
672 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
673 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
674 EXPECT_EQ(res[0][0], 1);
675 EXPECT_EQ(res[0][1], 68);
676 EXPECT_EQ(res[0][2], 87);
677 }
678
679 /**
680 * @tc.name: I18nFuncTest019
681 * @tc.desc: Test I18n EntityRecognizer
682 * @tc.type: FUNC
683 */
684 HWTEST_F(I18nTest, I18nFuncTest019, TestSize.Level1)
685 {
686 std::string localeStr = "en-GB";
687 UErrorCode status = U_ZERO_ERROR;
688 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
689 std::string message = "RT @missingpeople: RT 32 yo missing since 26/09/2013 from Newry, NI. Seen him? Call 116000.";
690 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
691 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
692 EXPECT_EQ(res[0][0], 1);
693 EXPECT_EQ(res[0][1], 84);
694 EXPECT_EQ(res[0][2], 90);
695 }
696
697 /**
698 * @tc.name: I18nFuncTest020
699 * @tc.desc: Test I18n EntityRecognizer
700 * @tc.type: FUNC
701 */
702 HWTEST_F(I18nTest, I18nFuncTest020, TestSize.Level1)
703 {
704 std::string localeStr = "zh-CN";
705 UErrorCode status = U_ZERO_ERROR;
706 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
707 std::string message = "xxxx海滩 xxxxx xxxxxx街上的饰品店,皮革花朵戒指10000印尼盾,不到10块人民币。";
708 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
709 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
710 EXPECT_EQ(res[0][0], 0);
711 }
712
713 /**
714 * @tc.name: I18nFuncTest021
715 * @tc.desc: Test I18n EntityRecognizer
716 * @tc.type: FUNC
717 */
718 HWTEST_F(I18nTest, I18nFuncTest021, TestSize.Level1)
719 {
720 std::string localeStr = "en-GB";
721 UErrorCode status = U_ZERO_ERROR;
722 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
723 std::string message = "New Job: Java Developer, Dublin, Republic of Ireland, $350000.00 - $45000 per annum.";
724 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
725 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
726 EXPECT_EQ(res[0][0], 0);
727 }
728
729 /**
730 * @tc.name: I18nFuncTest022
731 * @tc.desc: Test I18n EntityRecognizer
732 * @tc.type: FUNC
733 */
734 HWTEST_F(I18nTest, I18nFuncTest022, TestSize.Level1)
735 {
736 std::string localeStr = "zh-CN";
737 UErrorCode status = U_ZERO_ERROR;
738 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
739 std::string message = "您好,关于您的问题,可以拨打(0511) 8812 1234咨询,如果还有其他疑问,可联系刘某某(15512345678)";
740 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
741 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
742 EXPECT_EQ(res[0][0], 2);
743 EXPECT_EQ(res[0][1], 14);
744 EXPECT_EQ(res[0][2], 30);
745 EXPECT_EQ(res[0][3], 49);
746 EXPECT_EQ(res[0][4], 60);
747 }
748
749 /**
750 * @tc.name: I18nFuncTest023
751 * @tc.desc: Test I18n EntityRecognizer
752 * @tc.type: FUNC
753 */
754 HWTEST_F(I18nTest, I18nFuncTest023, TestSize.Level1)
755 {
756 std::string localeStr = "zh-CN";
757 UErrorCode status = U_ZERO_ERROR;
758 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
759 std::string message = "给10086/10010发了一条短信:“我爱你”,收到了回复:尊敬的用户,我也爱您。";
760 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
761 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
762 EXPECT_EQ(res[0][0], 2);
763 EXPECT_EQ(res[0][1], 1);
764 EXPECT_EQ(res[0][2], 6);
765 EXPECT_EQ(res[0][3], 7);
766 EXPECT_EQ(res[0][4], 12);
767 }
768
769 /**
770 * @tc.name: I18nFuncTest024
771 * @tc.desc: Test I18n EntityRecognizer
772 * @tc.type: FUNC
773 */
774 HWTEST_F(I18nTest, I18nFuncTest024, TestSize.Level1)
775 {
776 std::string localeStr = "zh-CN";
777 UErrorCode status = U_ZERO_ERROR;
778 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
779 std::string message = "您好,您的包裹已送至指定地点,请尽快签收:快递员:刘某某,手机:159/1234/5678,QQ:123456789。";
780 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
781 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
782 EXPECT_EQ(res[0][0], 1);
783 EXPECT_EQ(res[0][1], 32);
784 EXPECT_EQ(res[0][2], 45);
785 }
786
787 /**
788 * @tc.name: I18nFuncTest025
789 * @tc.desc: Test I18n EntityRecognizer
790 * @tc.type: FUNC
791 */
792 HWTEST_F(I18nTest, I18nFuncTest025, TestSize.Level1)
793 {
794 std::string localeStr = "zh-CN";
795 UErrorCode status = U_ZERO_ERROR;
796 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
797 std::string message = "您好,您的包裹已送至指定地点,请尽快签收:快递员:刘某某,手机:15912345678/15512345678。";
798 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
799 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
800 EXPECT_EQ(res[0][0], 2);
801 EXPECT_EQ(res[0][1], 32);
802 EXPECT_EQ(res[0][2], 43);
803 EXPECT_EQ(res[0][3], 44);
804 EXPECT_EQ(res[0][4], 55);
805 }
806
807 /**
808 * @tc.name: I18nFuncTest026
809 * @tc.desc: Test I18n EntityRecognizer
810 * @tc.type: FUNC
811 */
812 HWTEST_F(I18nTest, I18nFuncTest026, TestSize.Level1)
813 {
814 std::string localeStr = "zh-CN";
815 UErrorCode status = U_ZERO_ERROR;
816 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
817 std::string message = "今天一起去看的那个电影太搞笑了,2333333";
818 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
819 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
820 EXPECT_EQ(res[0][0], 0);
821 }
822
823 /**
824 * @tc.name: I18nFuncTest027
825 * @tc.desc: Test I18n EntityRecognizer
826 * @tc.type: FUNC
827 */
828 HWTEST_F(I18nTest, I18nFuncTest027, TestSize.Level1)
829 {
830 std::string localeStr = "pt-PT";
831 UErrorCode status = U_ZERO_ERROR;
832 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
833 std::string message = "Se você tiver alguma dúvida, ligue para 912345678 ou 1820 para consulta";
834 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
835 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
836 EXPECT_EQ(res[0][0], 2);
837 EXPECT_EQ(res[0][1], 40);
838 EXPECT_EQ(res[0][2], 49);
839 EXPECT_EQ(res[0][3], 53);
840 EXPECT_EQ(res[0][4], 57);
841 }
842
843 /**
844 * @tc.name: I18nFuncTest028
845 * @tc.desc: Test I18n EntityRecognizer
846 * @tc.type: FUNC
847 */
848 HWTEST_F(I18nTest, I18nFuncTest028, TestSize.Level1)
849 {
850 std::string localeStr = "en-GB";
851 UErrorCode status = U_ZERO_ERROR;
852 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
853 std::string message = "+44 (0)20 1234 5678 is my phone number. If you need anything, please contact me.";
854 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
855 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
856 EXPECT_EQ(res[0][0], 1);
857 EXPECT_EQ(res[0][1], 0);
858 EXPECT_EQ(res[0][2], 19);
859 }
860
861 /**
862 * @tc.name: I18nFuncTest029
863 * @tc.desc: Test I18n EntityRecognizer
864 * @tc.type: FUNC
865 */
866 HWTEST_F(I18nTest, I18nFuncTest029, TestSize.Level1)
867 {
868 std::string localeStr = "en-GB";
869 UErrorCode status = U_ZERO_ERROR;
870 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
871 std::string message = "To book a room, please call (+44 (0)20 1234 5678;ext=588)";
872 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
873 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
874 EXPECT_EQ(res[0][0], 1);
875 EXPECT_EQ(res[0][1], 29);
876 EXPECT_EQ(res[0][2], 48);
877 }
878
879 /**
880 * @tc.name: I18nFuncTest030
881 * @tc.desc: Test I18n EntityRecognizer
882 * @tc.type: FUNC
883 */
884 HWTEST_F(I18nTest, I18nFuncTest030, TestSize.Level1)
885 {
886 std::string localeStr = "zh-CN";
887 UErrorCode status = U_ZERO_ERROR;
888 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
889 std::string message = "我们提供23 24两种尺寸的屏幕,如有需要,请拨打11808 15512345678。";
890 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
891 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
892 EXPECT_EQ(res[0][0], 1);
893 EXPECT_EQ(res[0][1], 25);
894 EXPECT_EQ(res[0][2], 42);
895 }
896
897 /**
898 * @tc.name: I18nFuncTest031
899 * @tc.desc: Test I18n EntityRecognizer
900 * @tc.type: FUNC
901 */
902 HWTEST_F(I18nTest, I18nFuncTest031, TestSize.Level1)
903 {
904 std::string localeStr = "zh-CN";
905 UErrorCode status = U_ZERO_ERROR;
906 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
907 std::string message = "售后问题请拨打95528|95188。";
908 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
909 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
910 EXPECT_EQ(res[0][0], 2);
911 EXPECT_EQ(res[0][1], 7);
912 EXPECT_EQ(res[0][2], 12);
913 EXPECT_EQ(res[0][3], 13);
914 EXPECT_EQ(res[0][4], 18);
915 }
916
917 /**
918 * @tc.name: I18nFuncTest032
919 * @tc.desc: Test I18n EntityRecognizer
920 * @tc.type: FUNC
921 */
922 HWTEST_F(I18nTest, I18nFuncTest032, TestSize.Level1)
923 {
924 std::string localeStr = "en-GB";
925 UErrorCode status = U_ZERO_ERROR;
926 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
927 std::string message = "If you need anything, please contact mob:(0)20 1234 5678.";
928 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
929 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
930 EXPECT_EQ(res[0][0], 1);
931 EXPECT_EQ(res[0][1], 41);
932 EXPECT_EQ(res[0][2], 56);
933 }
934
935 /**
936 * @tc.name: I18nFuncTest033
937 * @tc.desc: Test I18n EntityRecognizer
938 * @tc.type: FUNC
939 */
940 HWTEST_F(I18nTest, I18nFuncTest033, TestSize.Level1)
941 {
942 std::string localeStr = "zh-CN";
943 UErrorCode status = U_ZERO_ERROR;
944 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
945 std::string message = "尊敬的客户您好!4G套餐火热申办中,每月最高可获得20G手机上网流量,升级4G套餐享更多优惠。咨询及办理请致电10086。中国移动";
946 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
947 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
948 EXPECT_EQ(res[0][0], 1);
949 EXPECT_EQ(res[0][1], 55);
950 EXPECT_EQ(res[0][2], 60);
951 }
952
953 /**
954 * @tc.name: I18nFuncTest034
955 * @tc.desc: Test I18n EntityRecognizer
956 * @tc.type: FUNC
957 */
958 HWTEST_F(I18nTest, I18nFuncTest034, TestSize.Level1)
959 {
960 std::string localeStr = "zh-CN";
961 UErrorCode status = U_ZERO_ERROR;
962 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
963 std::string message = "今天晚上10点,我们商量一下10月1日至7日的旅游安排。";
964 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
965 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
966 EXPECT_EQ(res[1][0], 2);
967 EXPECT_EQ(res[1][1], 0);
968 EXPECT_EQ(res[1][2], 7);
969 EXPECT_EQ(res[1][3], 14);
970 EXPECT_EQ(res[1][4], 22);
971 }
972
973 /**
974 * @tc.name: I18nFuncTest035
975 * @tc.desc: Test I18n EntityRecognizer
976 * @tc.type: FUNC
977 */
978 HWTEST_F(I18nTest, I18nFuncTest035, TestSize.Level1)
979 {
980 std::string localeStr = "zh-CN";
981 UErrorCode status = U_ZERO_ERROR;
982 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
983 std::string message = "7月1日周一到7月5日周五都是工作日,所以在2023年7月6日下午17:00到19:00聚餐。";
984 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
985 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
986 EXPECT_EQ(res[1][0], 2);
987 EXPECT_EQ(res[1][1], 0);
988 EXPECT_EQ(res[1][2], 13);
989 EXPECT_EQ(res[1][3], 22);
990 EXPECT_EQ(res[1][4], 44);
991 }
992
993 /**
994 * @tc.name: I18nFuncTest036
995 * @tc.desc: Test I18n EntityRecognizer
996 * @tc.type: FUNC
997 */
998 HWTEST_F(I18nTest, I18nFuncTest036, TestSize.Level1)
999 {
1000 std::string localeStr = "zh-CN";
1001 UErrorCode status = U_ZERO_ERROR;
1002 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
1003 std::string message = "昨天8:30:00的会议没有讨论出结果,我们2023年11月11日的8:30:00再开一次会吧。";
1004 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
1005 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
1006 EXPECT_EQ(res[1][0], 1);
1007 EXPECT_EQ(res[1][1], 22);
1008 EXPECT_EQ(res[1][2], 41);
1009 }
1010
1011 /**
1012 * @tc.name: I18nFuncTest037
1013 * @tc.desc: Test I18n EntityRecognizer
1014 * @tc.type: FUNC
1015 */
1016 HWTEST_F(I18nTest, I18nFuncTest037, TestSize.Level1)
1017 {
1018 std::string localeStr = "zh-CN";
1019 UErrorCode status = U_ZERO_ERROR;
1020 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
1021 std::string message = "我们2023年10月23日(周六)一起完成作业,并且2023年10月24日周天 晚上8:00上交。";
1022 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
1023 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
1024 EXPECT_EQ(res[1][0], 2);
1025 EXPECT_EQ(res[1][1], 2);
1026 EXPECT_EQ(res[1][2], 17);
1027 EXPECT_EQ(res[1][3], 26);
1028 EXPECT_EQ(res[1][4], 46);
1029 }
1030
1031 /**
1032 * @tc.name: I18nFuncTest038
1033 * @tc.desc: Test I18n EntityRecognizer
1034 * @tc.type: FUNC
1035 */
1036 HWTEST_F(I18nTest, I18nFuncTest038, TestSize.Level1)
1037 {
1038 std::string localeStr = "zh-CN";
1039 UErrorCode status = U_ZERO_ERROR;
1040 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
1041 std::string message = "2023/10/1是国庆节,我们可以在GMT+0800 上午9時30分00秒去参观博物馆,后续星期六 晚上 7:00我们一起吃饭。";
1042 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
1043 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
1044 EXPECT_EQ(res[1][0], 3);
1045 EXPECT_EQ(res[1][1], 0);
1046 EXPECT_EQ(res[1][2], 9);
1047 EXPECT_EQ(res[1][3], 19);
1048 EXPECT_EQ(res[1][4], 38);
1049 EXPECT_EQ(res[1][5], 47);
1050 EXPECT_EQ(res[1][6], 58);
1051 }
1052
1053 /**
1054 * @tc.name: I18nFuncTest039
1055 * @tc.desc: Test I18n EntityRecognizer
1056 * @tc.type: FUNC
1057 */
1058 HWTEST_F(I18nTest, I18nFuncTest039, TestSize.Level1)
1059 {
1060 std::string localeStr = "zh-CN";
1061 UErrorCode status = U_ZERO_ERROR;
1062 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
1063 std::string message = "我们上午 7:30:00 (GMT+8:30)可以去看日出,下午5:00-晚上7:00看日落。";
1064 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
1065 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
1066 EXPECT_EQ(res[1][0], 2);
1067 EXPECT_EQ(res[1][1], 2);
1068 EXPECT_EQ(res[1][2], 23);
1069 EXPECT_EQ(res[1][3], 30);
1070 EXPECT_EQ(res[1][4], 43);
1071 }
1072
1073 /**
1074 * @tc.name: I18nFuncTest040
1075 * @tc.desc: Test I18n EntityRecognizer
1076 * @tc.type: FUNC
1077 */
1078 HWTEST_F(I18nTest, I18nFuncTest040, TestSize.Level1)
1079 {
1080 std::string localeStr = "en-GB";
1081 UErrorCode status = U_ZERO_ERROR;
1082 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
1083 std::string message = "We'll have dinner at GMT-8:15 PM 17:30:00, and go shopping at tomorrow 8:00.";
1084 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
1085 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
1086 EXPECT_EQ(res[1][0], 2);
1087 EXPECT_EQ(res[1][1], 21);
1088 EXPECT_EQ(res[1][2], 41);
1089 EXPECT_EQ(res[1][3], 62);
1090 EXPECT_EQ(res[1][4], 75);
1091 }
1092
1093 /**
1094 * @tc.name: I18nFuncTest041
1095 * @tc.desc: Test I18n EntityRecognizer
1096 * @tc.type: FUNC
1097 */
1098 HWTEST_F(I18nTest, I18nFuncTest041, TestSize.Level1)
1099 {
1100 std::string localeStr = "en-GB";
1101 UErrorCode status = U_ZERO_ERROR;
1102 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
1103 std::string message = "Our time zone is GMT+12:00.";
1104 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
1105 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
1106 EXPECT_EQ(res[1][0], 0);
1107 }
1108
1109 /**
1110 * @tc.name: I18nFuncTest042
1111 * @tc.desc: Test I18n EntityRecognizer
1112 * @tc.type: FUNC
1113 */
1114 HWTEST_F(I18nTest, I18nFuncTest042, TestSize.Level1)
1115 {
1116 std::string localeStr = "en-GB";
1117 UErrorCode status = U_ZERO_ERROR;
1118 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
1119 std::string message = "The festivities will take place on Sunday, jan 1, 2023, and run until Wed, 1/4/2023";
1120 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
1121 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
1122 EXPECT_EQ(res[1][0], 2);
1123 EXPECT_EQ(res[1][1], 35);
1124 EXPECT_EQ(res[1][2], 54);
1125 EXPECT_EQ(res[1][3], 70);
1126 EXPECT_EQ(res[1][4], 83);
1127 }
1128
1129 /**
1130 * @tc.name: I18nFuncTest043
1131 * @tc.desc: Test I18n EntityRecognizer
1132 * @tc.type: FUNC
1133 */
1134 HWTEST_F(I18nTest, I18nFuncTest043, TestSize.Level1)
1135 {
1136 std::string localeStr = "zh-CN";
1137 UErrorCode status = U_ZERO_ERROR;
1138 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
1139 // std::string message = "2023年1月2日至3日周三是一个好日子,上午可以11:00:00(周三)去逛街";
1140 std::string message = "2023年1月2日至3日是一个好日子,12:00:00(2023年1月3日)一起吃饭。";
1141 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
1142 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
1143 EXPECT_EQ(res[1][0], 2);
1144 EXPECT_EQ(res[1][1], 0);
1145 EXPECT_EQ(res[1][2], 12);
1146 EXPECT_EQ(res[1][3], 19);
1147 EXPECT_EQ(res[1][4], 38);
1148 }
1149
1150 /**
1151 * @tc.name: I18nFuncTest044
1152 * @tc.desc: Test I18n EntityRecognizer
1153 * @tc.type: FUNC
1154 */
1155 HWTEST_F(I18nTest, I18nFuncTest044, TestSize.Level1)
1156 {
1157 std::string localeStr = "zh-CN";
1158 UErrorCode status = U_ZERO_ERROR;
1159 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
1160 std::string message = "(2023年1月1日)12:00:00聚会,2023年1月2日,16:00:00返回深圳。";
1161 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
1162 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
1163 EXPECT_EQ(res[1][0], 2);
1164 EXPECT_EQ(res[1][1], 0);
1165 EXPECT_EQ(res[1][2], 19);
1166 EXPECT_EQ(res[1][3], 22);
1167 EXPECT_EQ(res[1][4], 40);
1168 }
1169
1170
1171 /**
1172 * @tc.name: I18nFuncTest045
1173 * @tc.desc: Test I18n EntityRecognizer
1174 * @tc.type: FUNC
1175 */
1176 HWTEST_F(I18nTest, I18nFuncTest045, TestSize.Level1)
1177 {
1178 std::string localeStr = "zh-CN";
1179 UErrorCode status = U_ZERO_ERROR;
1180 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
1181 std::string message = "2023年1月3日(今天(本周五))和2023年1月3日(12:00:00)是指同一天";
1182 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
1183 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
1184 EXPECT_EQ(res[1][0], 3);
1185 EXPECT_EQ(res[1][1], 0);
1186 EXPECT_EQ(res[1][2], 17);
1187 EXPECT_EQ(res[1][3], 19);
1188 EXPECT_EQ(res[1][4], 28);
1189 EXPECT_EQ(res[1][5], 29);
1190 EXPECT_EQ(res[1][6], 37);
1191 }
1192
1193 /**
1194 * @tc.name: I18nFuncTest046
1195 * @tc.desc: Test I18n EntityRecognizer
1196 * @tc.type: FUNC
1197 */
1198 HWTEST_F(I18nTest, I18nFuncTest046, TestSize.Level1)
1199 {
1200 std::string localeStr = "zh-CN";
1201 UErrorCode status = U_ZERO_ERROR;
1202 icu::Locale locale = icu::Locale::forLanguageTag(localeStr, status);
1203 std::string message = "2023年1月3日今天(本周五)在7:00:00的2023年1月3日可以看日出";
1204 std::unique_ptr<EntityRecognizer> m = std::make_unique<EntityRecognizer>(locale);
1205 std::vector<std::vector<int>> res = m->FindEntityInfo(message);
1206 EXPECT_EQ(res[1][0], 2);
1207 EXPECT_EQ(res[1][1], 0);
1208 EXPECT_EQ(res[1][2], 16);
1209 EXPECT_EQ(res[1][3], 17);
1210 EXPECT_EQ(res[1][4], 34);
1211 }
1212
1213 /**
1214 * @tc.name: I18nFuncTest047
1215 * @tc.desc: Test I18n LocaleUtil
1216 * @tc.type: FUNC
1217 */
1218 HWTEST_F(I18nTest, I18nFuncTest047, TestSize.Level1)
1219 {
1220 LocaleInfo *locale = new LocaleInfo("zh-Hans-CN");
1221 uint16_t encodedLanguage = LocaleUtil::EncodeLanguageByLocaleInfo(locale);
1222 EXPECT_EQ(encodedLanguage, 31336);
1223 delete locale;
1224 }
1225
1226 /**
1227 * @tc.name: I18nFuncTest048
1228 * @tc.desc: Test I18n LocaleUtil
1229 * @tc.type: FUNC
1230 */
1231 HWTEST_F(I18nTest, I18nFuncTest048, TestSize.Level1)
1232 {
1233 LocaleInfo *locale = new LocaleInfo("zh-Hans-CN");
1234 uint16_t encodedScript = LocaleUtil::EncodeScriptByLocaleInfo(locale);
1235 EXPECT_EQ(encodedScript, 28275);
1236 delete locale;
1237 }
1238
1239 /**
1240 * @tc.name: I18nFuncTest049
1241 * @tc.desc: Test I18n LocaleUtil
1242 * @tc.type: FUNC
1243 */
1244 HWTEST_F(I18nTest, I18nFuncTest049, TestSize.Level1)
1245 {
1246 LocaleInfo *locale = new LocaleInfo("zh-Hans-CN");
1247 uint16_t encodedRegion = LocaleUtil::EncodeRegionByLocaleInfo(locale);
1248 EXPECT_EQ(encodedRegion, 17230);
1249 uint16_t result = LocaleUtil::EncodeRegionByLocaleInfo(nullptr);
1250 EXPECT_EQ(result, 0);
1251 delete locale;
1252 }
1253
1254 /**
1255 * @tc.name: I18nFuncTest050
1256 * @tc.desc: Test I18n LocaleUtil
1257 * @tc.type: FUNC
1258 */
1259 HWTEST_F(I18nTest, I18nFuncTest050, TestSize.Level1)
1260 {
1261 LocaleInfo locale("zh-Hans-CN");
1262 uint16_t encodedLanguage = LocaleUtil::EncodeLanguage(locale.GetLanguage().c_str());
1263 EXPECT_EQ(encodedLanguage, 31336);
1264 }
1265
1266 /**
1267 * @tc.name: I18nFuncTest051
1268 * @tc.desc: Test I18n LocaleUtil
1269 * @tc.type: FUNC
1270 */
1271 HWTEST_F(I18nTest, I18nFuncTest051, TestSize.Level1)
1272 {
1273 LocaleInfo locale("zh-Hans-CN");
1274 uint16_t encodedScript = LocaleUtil::EncodeScript(locale.GetScript().c_str());
1275 EXPECT_EQ(encodedScript, 28275);
1276 }
1277
1278 /**
1279 * @tc.name: I18nFuncTest052
1280 * @tc.desc: Test I18n LocaleUtil
1281 * @tc.type: FUNC
1282 */
1283 HWTEST_F(I18nTest, I18nFuncTest052, TestSize.Level1)
1284 {
1285 LocaleInfo locale("zh-Hans-CN");
1286 uint16_t encodedRegion = LocaleUtil::EncodeRegion(locale.GetRegion().c_str());
1287 EXPECT_EQ(encodedRegion, 17230);
1288 }
1289
1290 /**
1291 * @tc.name: I18nFuncTest053
1292 * @tc.desc: Test I18n LocaleUtil
1293 * @tc.type: FUNC
1294 */
1295 HWTEST_F(I18nTest, I18nFuncTest053, TestSize.Level1)
1296 {
1297 LocaleInfo locale("zh-Hans-CN");
1298 uint16_t encodedLocale = LocaleUtil::EncodeLocale(locale.GetLanguage().c_str(), locale.GetScript().c_str(),
1299 locale.GetRegion().c_str());
1300 EXPECT_EQ(encodedLocale, 17230);
1301 }
1302
1303 /**
1304 * @tc.name: I18nFuncTest054
1305 * @tc.desc: Test I18n LocaleUtil
1306 * @tc.type: FUNC
1307 */
1308 HWTEST_F(I18nTest, I18nFuncTest054, TestSize.Level1)
1309 {
1310 std::string str1 = "non-empty";
1311 std::string str2 = "";
1312 bool isEmpty = LocaleUtil::IsStrEmpty(str1.c_str());
1313 EXPECT_FALSE(isEmpty);
1314 isEmpty = LocaleUtil::IsStrEmpty(str2.c_str());
1315 EXPECT_TRUE(isEmpty);
1316 }
1317
1318 /**
1319 * @tc.name: I18nFuncTest055
1320 * @tc.desc: Test I18n LocaleUtil
1321 * @tc.type: FUNC
1322 */
1323 HWTEST_F(I18nTest, I18nFuncTest055, TestSize.Level1)
1324 {
1325 LocaleInfo locale("zh-Hans-CN");
1326 uint32_t encodedScript = LocaleUtil::EncodeScript(locale.GetScript().c_str());
1327 char decodedScript[5] = { 0 };
1328 LocaleUtil::DecodeScript(encodedScript, decodedScript);
1329 std::string originScript = "Hans";
1330 EXPECT_EQ(originScript.compare(decodedScript), 0);
1331 LocaleUtil::DecodeScript(encodedScript, nullptr);
1332 }
1333
1334 /**
1335 * @tc.name: I18nFuncTest056
1336 * @tc.desc: Test I18n LocaleUtil
1337 * @tc.type: FUNC
1338 */
1339 HWTEST_F(I18nTest, I18nFuncTest056, TestSize.Level1)
1340 {
1341 std::string alphaString = "abc";
1342 std::string nonAlphaString = "abc123abc";
1343 bool isAlpha = LocaleUtil::IsAlphaString(alphaString.c_str(), alphaString.length());
1344 EXPECT_TRUE(isAlpha);
1345 isAlpha = LocaleUtil::IsAlphaString(nonAlphaString.c_str(), nonAlphaString.length());
1346 EXPECT_FALSE(isAlpha);
1347 isAlpha = LocaleUtil::IsAlphaString(nullptr, nonAlphaString.length());
1348 EXPECT_FALSE(isAlpha);
1349 }
1350
1351 /**
1352 * @tc.name: I18nFuncTest057
1353 * @tc.desc: Test I18n LocaleUtil
1354 * @tc.type: FUNC
1355 */
1356 HWTEST_F(I18nTest, I18nFuncTest057, TestSize.Level1)
1357 {
1358 std::string numericString = "123";
1359 std::string nonNumericString = "123abc123";
1360 bool isNumeric = LocaleUtil::IsNumericString(numericString.c_str(), numericString.length());
1361 EXPECT_TRUE(isNumeric);
1362 isNumeric = LocaleUtil::IsNumericString(nonNumericString.c_str(), nonNumericString.length());
1363 EXPECT_FALSE(isNumeric);
1364 isNumeric = LocaleUtil::IsNumericString(nullptr, nonNumericString.length());
1365 EXPECT_FALSE(isNumeric);
1366 std::string locale = "ug-CN";
1367 bool rtl = LocaleUtil::IsRTL(locale);
1368 EXPECT_TRUE(rtl);
1369 }
1370
1371 /**
1372 * @tc.name: I18nFuncTest058
1373 * @tc.desc: Test I18n LocaleUtil
1374 * @tc.type: FUNC
1375 */
1376 HWTEST_F(I18nTest, I18nFuncTest058, TestSize.Level1)
1377 {
1378 LocaleInfo *locale1 = new LocaleInfo("zh-Hans-CN");
1379 LocaleInfo *locale2 = new LocaleInfo("en-Latn-US");
1380 bool isMatched = LocaleMatcher::Match(locale1, locale2);
1381 EXPECT_FALSE(isMatched);
1382 delete locale1;
1383 delete locale2;
1384
1385 locale1 = new LocaleInfo("zh-Hans-CN");
1386 locale2 = new LocaleInfo("zh-Hant-TW");
1387 isMatched = LocaleMatcher::Match(locale1, locale2);
1388 EXPECT_FALSE(isMatched);
1389 delete locale1;
1390 delete locale2;
1391
1392 locale1 = new LocaleInfo("zh-Hans-CN");
1393 locale2 = new LocaleInfo("zh-Hant-TW");
1394 isMatched = LocaleMatcher::Match(locale1, locale2);
1395 EXPECT_FALSE(isMatched);
1396 delete locale1;
1397 delete locale2;
1398
1399 locale1 = new LocaleInfo("zh-Hans-CN");
1400 locale2 = new LocaleInfo("zh-Hans-MO");
1401 isMatched = LocaleMatcher::Match(locale1, locale2);
1402 EXPECT_TRUE(isMatched);
1403 delete locale1;
1404 delete locale2;
1405 }
1406
1407 /**
1408 * @tc.name: I18nFuncTest059
1409 * @tc.desc: Test I18n LocaleUtil
1410 * @tc.type: FUNC
1411 */
1412 HWTEST_F(I18nTest, I18nFuncTest059, TestSize.Level1)
1413 {
1414 LocaleInfo *request = nullptr;
1415 LocaleInfo *current = new LocaleInfo("zh-Hans-CN");
1416 LocaleInfo *other = nullptr;
1417 int8_t res = LocaleMatcher::IsMoreSuitable(current, other, request);
1418 EXPECT_TRUE(res < 0);
1419 delete current;
1420 current = nullptr;
1421
1422 res = LocaleMatcher::IsMoreSuitable(current, other, request);
1423 EXPECT_TRUE(res == 0);
1424
1425 request = new LocaleInfo("en-GB");
1426 current = new LocaleInfo("en-AU");
1427 res = LocaleMatcher::IsMoreSuitable(current, other, request);
1428 EXPECT_TRUE(res > 0);
1429
1430 other = new LocaleInfo("en-GB");
1431 res = LocaleMatcher::IsMoreSuitable(current, other, request);
1432 EXPECT_TRUE(res < 0);
1433
1434 delete request;
1435 delete current;
1436 delete other;
1437 request = new LocaleInfo("iw-Lant-GB");
1438 current = new LocaleInfo("iw-Lant-AU");
1439 other = new LocaleInfo("he-Latn-AU");
1440 res = LocaleMatcher::IsMoreSuitable(current, other, request);
1441 EXPECT_TRUE(res > 0);
1442 delete other;
1443
1444 other = new LocaleInfo("iw-Latn-AG");
1445 res = LocaleMatcher::IsMoreSuitable(current, other, request);
1446 EXPECT_TRUE(res < 0);
1447 delete request;
1448 delete current;
1449 delete other;
1450 }
1451
1452 /**
1453 * @tc.name: I18nFuncTest061
1454 * @tc.desc: Test I18n GetISO3Language
1455 * @tc.type: FUNC
1456 */
1457 HWTEST_F(I18nTest, I18nFuncTest061, TestSize.Level1)
1458 {
1459 std::string language = GetISO3Language("zh");
1460 EXPECT_EQ(language, "zho");
1461 language = GetISO3Language("en");
1462 EXPECT_EQ(language, "eng");
1463 language = GetISO3Language("zh-CN");
1464 EXPECT_EQ(language, "zho");
1465 language = GetISO3Language("en-US");
1466 EXPECT_EQ(language, "eng");
1467 language = GetISO3Language("zz");
1468 EXPECT_EQ(language, "");
1469 language = GetISO3Language("sdfsdf");
1470 EXPECT_EQ(language, "");
1471 }
1472
1473 /**
1474 * @tc.name: I18nFuncTest062
1475 * @tc.desc: Test I18n GetISO3Country
1476 * @tc.type: FUNC
1477 */
1478 HWTEST_F(I18nTest, I18nFuncTest062, TestSize.Level1)
1479 {
1480 std::string country = GetISO3Country("CN");
1481 EXPECT_EQ(country, "CHN");
1482 country = GetISO3Country("US");
1483 EXPECT_EQ(country, "USA");
1484 country = GetISO3Country("zh-CN");
1485 EXPECT_EQ(country, "CHN");
1486 country = GetISO3Country("en-US");
1487 EXPECT_EQ(country, "USA");
1488 country = GetISO3Country("ZX");
1489 EXPECT_EQ(country, "");
1490 country = GetISO3Country("zh");
1491 EXPECT_EQ(country, "");
1492 country = GetISO3Country("sdfsdf");
1493 EXPECT_EQ(country, "");
1494 }
1495
1496 /**
1497 * @tc.name: I18nFuncTest063
1498 * @tc.desc: Test I18n lunar calendar
1499 * @tc.type: FUNC
1500 */
1501 HWTEST_F(I18nTest, I18nFuncTest063, TestSize.Level1)
1502 {
1503 std::unique_ptr<LunarCalendar> ptr = std::make_unique<LunarCalendar>();
1504 ASSERT_TRUE(ptr != nullptr);
1505
1506 ptr->SetGregorianDate(1900, 1, 31);
1507 int32_t year = ptr->GetLunarYear();
1508 int32_t month = ptr->GetLunarMonth();
1509 int32_t day = ptr->GetLunarDay();
1510 bool isLeap = ptr->IsLeapMonth();
1511 EXPECT_EQ(year, 1900);
1512 EXPECT_EQ(month, 1);
1513 EXPECT_EQ(day, 1);
1514 EXPECT_EQ(isLeap, false);
1515
1516 ptr->SetGregorianDate(1900, 6, 15);
1517 year = ptr->GetLunarYear();
1518 month = ptr->GetLunarMonth();
1519 day = ptr->GetLunarDay();
1520 isLeap = ptr->IsLeapMonth();
1521 EXPECT_EQ(year, 1900);
1522 EXPECT_EQ(month, 5);
1523 EXPECT_EQ(day, 19);
1524 EXPECT_EQ(isLeap, false);
1525
1526 ptr->SetGregorianDate(2100, 2, 15);
1527 year = ptr->GetLunarYear();
1528 month = ptr->GetLunarMonth();
1529 day = ptr->GetLunarDay();
1530 isLeap = ptr->IsLeapMonth();
1531 EXPECT_EQ(year, 2100);
1532 EXPECT_EQ(month, 1);
1533 EXPECT_EQ(day, 7);
1534 EXPECT_EQ(isLeap, false);
1535
1536 ptr->SetGregorianDate(2100, 12, 31);
1537 year = ptr->GetLunarYear();
1538 month = ptr->GetLunarMonth();
1539 day = ptr->GetLunarDay();
1540 isLeap = ptr->IsLeapMonth();
1541 EXPECT_EQ(year, 2100);
1542 EXPECT_EQ(month, 12);
1543 EXPECT_EQ(day, 1);
1544 EXPECT_EQ(isLeap, false);
1545 }
1546
1547 /**
1548 * @tc.name: I18nFuncTest064
1549 * @tc.desc: Test I18n lunar calendar
1550 * @tc.type: FUNC
1551 */
1552 HWTEST_F(I18nTest, I18nFuncTest064, TestSize.Level1)
1553 {
1554 std::unique_ptr<LunarCalendar> ptr = std::make_unique<LunarCalendar>();
1555 ASSERT_TRUE(ptr != nullptr);
1556
1557 ptr->SetGregorianDate(2087, 1, 11);
1558 int32_t year = ptr->GetLunarYear();
1559 int32_t month = ptr->GetLunarMonth();
1560 int32_t day = ptr->GetLunarDay();
1561 bool isLeap = ptr->IsLeapMonth();
1562 EXPECT_EQ(year, 2086);
1563 EXPECT_EQ(month, 12);
1564 EXPECT_EQ(day, 7);
1565 EXPECT_EQ(isLeap, false);
1566
1567 ptr->SetGregorianDate(2024, 10, 11);
1568 year = ptr->GetLunarYear();
1569 month = ptr->GetLunarMonth();
1570 day = ptr->GetLunarDay();
1571 isLeap = ptr->IsLeapMonth();
1572 EXPECT_EQ(year, 2024);
1573 EXPECT_EQ(month, 9);
1574 EXPECT_EQ(day, 9);
1575 EXPECT_EQ(isLeap, false);
1576
1577 ptr->SetGregorianDate(1963, 6, 15);
1578 year = ptr->GetLunarYear();
1579 month = ptr->GetLunarMonth();
1580 day = ptr->GetLunarDay();
1581 isLeap = ptr->IsLeapMonth();
1582 EXPECT_EQ(year, 1963);
1583 EXPECT_EQ(month, 4);
1584 EXPECT_EQ(day, 24);
1585 EXPECT_EQ(isLeap, true);
1586
1587 ptr->SetGregorianDate(1923, 1, 11);
1588 year = ptr->GetLunarYear();
1589 month = ptr->GetLunarMonth();
1590 day = ptr->GetLunarDay();
1591 isLeap = ptr->IsLeapMonth();
1592 EXPECT_EQ(year, 1922);
1593 EXPECT_EQ(month, 11);
1594 EXPECT_EQ(day, 25);
1595 EXPECT_EQ(isLeap, false);
1596 }
1597
1598 /**
1599 * @tc.name: I18nFuncTest065
1600 * @tc.desc: Test I18n lunar calendar
1601 * @tc.type: FUNC
1602 */
1603 HWTEST_F(I18nTest, I18nFuncTest065, TestSize.Level1)
1604 {
1605 std::unique_ptr<LunarCalendar> ptr = std::make_unique<LunarCalendar>();
1606 ASSERT_TRUE(ptr != nullptr);
1607
1608 ptr->SetGregorianDate(2024, 8, 51);
1609 int32_t year = ptr->GetLunarYear();
1610 int32_t month = ptr->GetLunarMonth();
1611 int32_t day = ptr->GetLunarDay();
1612 bool isLeap = ptr->IsLeapMonth();
1613 EXPECT_EQ(year, 2024);
1614 EXPECT_EQ(month, 8);
1615 EXPECT_EQ(day, 18);
1616 EXPECT_EQ(isLeap, false);
1617
1618 ptr->SetGregorianDate(2024, 12, 43);
1619 year = ptr->GetLunarYear();
1620 month = ptr->GetLunarMonth();
1621 day = ptr->GetLunarDay();
1622 isLeap = ptr->IsLeapMonth();
1623 EXPECT_EQ(year, 2024);
1624 EXPECT_EQ(month, 12);
1625 EXPECT_EQ(day, 13);
1626 EXPECT_EQ(isLeap, false);
1627
1628 ptr->SetGregorianDate(2000, 7, 13);
1629 EXPECT_EQ(ptr->GetLunarDay(), 12);
1630
1631 ptr->SetGregorianDate(2024, 8, -11);
1632 EXPECT_EQ(ptr->GetLunarDay(), 15);
1633 ptr->SetGregorianDate(2024, -3, -11);
1634 EXPECT_EQ(ptr->GetLunarMonth(), 7);
1635 ptr->SetGregorianDate(-24, -3, -11);
1636 EXPECT_EQ(ptr->GetLunarYear(), -1);
1637 EXPECT_FALSE(ptr->IsLeapMonth());
1638 }
1639
1640 /**
1641 * @tc.name: I18nFuncTest066
1642 * @tc.desc: Test I18n set ext param
1643 * @tc.type: FUNC
1644 */
1645 HWTEST_F(I18nTest, I18nFuncTest066, TestSize.Level1)
1646 {
1647 I18nErrorCode err = LocaleConfig::SetTemperatureType(TemperatureType::KELVIN);
1648 EXPECT_EQ(err, I18nErrorCode::SUCCESS);
1649
1650 TemperatureType type = LocaleConfig::GetTemperatureType();
1651 EXPECT_EQ(type, TemperatureType::KELVIN);
1652
1653 std::string typeName = LocaleConfig::GetTemperatureName(type);
1654 EXPECT_EQ(typeName, "kelvin");
1655
1656 type = LocaleConfig::GetTemperatureTypeFromLocale("zh-Hans-CN-u-mu-kelvin");
1657 EXPECT_EQ(type, TemperatureType::KELVIN);
1658
1659 WeekDay weekDayType = LocaleConfig::GetFirstDayOfWeek();
1660 EXPECT_EQ(weekDayType, WeekDay::SUN);
1661
1662 err = LocaleConfig::SetFirstDayOfWeek(WeekDay::FRI);
1663 EXPECT_EQ(err, I18nErrorCode::SUCCESS);
1664
1665 weekDayType = LocaleConfig::GetFirstDayOfWeek();
1666 EXPECT_EQ(weekDayType, WeekDay::FRI);
1667 }
1668
1669 /**
1670 * @tc.name: I18nFuncTest067
1671 * @tc.desc: Test I18n simplified language
1672 * @tc.type: FUNC
1673 */
1674 HWTEST_F(I18nTest, I18nFuncTest067, TestSize.Level1)
1675 {
1676 LocaleConfig::SetSystemLanguage("en-Latn");
1677 LocaleConfig::SetSystemRegion("US");
1678 EXPECT_EQ(LocaleConfig::GetSimplifiedSystemLanguage(), "en");
1679 LocaleConfig::SetSystemRegion("GB");
1680 EXPECT_EQ(LocaleConfig::GetSimplifiedSystemLanguage(), "en-GB");
1681 LocaleConfig::SetSystemLanguage("zh-Hant");
1682 LocaleConfig::SetSystemRegion("CN");
1683 EXPECT_EQ(LocaleConfig::GetSimplifiedSystemLanguage(), "zh-Hant");
1684 LocaleConfig::SetSystemRegion("HK");
1685 EXPECT_EQ(LocaleConfig::GetSimplifiedSystemLanguage(), "zh-Hant-HK");
1686 LocaleConfig::SetSystemLanguage("es");
1687 LocaleConfig::SetSystemRegion("ES");
1688 EXPECT_EQ(LocaleConfig::GetSimplifiedSystemLanguage(), "es");
1689 LocaleConfig::SetSystemRegion("US");
1690 EXPECT_EQ(LocaleConfig::GetSimplifiedSystemLanguage(), "es");
1691 LocaleConfig::SetSystemLanguage("pt");
1692 LocaleConfig::SetSystemRegion("PT");
1693 EXPECT_EQ(LocaleConfig::GetSimplifiedSystemLanguage(), "pt-PT");
1694 LocaleConfig::SetSystemRegion("BR");
1695 EXPECT_EQ(LocaleConfig::GetSimplifiedSystemLanguage(), "pt");
1696 LocaleConfig::SetSystemLanguage("ug-Arab");
1697 LocaleConfig::SetSystemRegion("CN");
1698 EXPECT_EQ(LocaleConfig::GetSimplifiedSystemLanguage(), "ug");
1699
1700 int32_t code = 0;
1701 EXPECT_EQ(LocaleConfig::GetSimplifiedLanguage("zh-Hant-CN", code), "zh-Hant-CN");
1702 EXPECT_EQ(code, 0);
1703 EXPECT_EQ(LocaleConfig::GetSimplifiedLanguage("cr-Cans-CA", code), "cr");
1704 EXPECT_EQ(code, 0);
1705 LocaleConfig::SetSystemLanguage("zh-Hans");
1706 }
1707
1708 /**
1709 * @tc.name: I18nFuncTest068
1710 * @tc.desc: Test I18n simple number format
1711 * @tc.type: FUNC
1712 */
1713 HWTEST_F(I18nTest, I18nFuncTest068, TestSize.Level1)
1714 {
1715 I18nErrorCode err = I18nErrorCode::SUCCESS;
1716 std::string skeleton = "percent";
1717 std::shared_ptr<LocaleInfo> localeInfo = std::make_shared<LocaleInfo>("zh-Hans-CN");
1718 ASSERT_TRUE(localeInfo != nullptr);
1719 std::unique_ptr<SimpleNumberFormat> formatter = std::make_unique<SimpleNumberFormat>(skeleton, localeInfo, err);
1720 EXPECT_EQ(err, I18nErrorCode::SUCCESS);
1721 ASSERT_TRUE(formatter != nullptr);
1722 std::string result = formatter->Format(10);
1723 EXPECT_EQ(result, "10%");
1724
1725 skeleton = "%";
1726 std::shared_ptr<LocaleInfo> nullLocaleInfo = nullptr;
1727 std::unique_ptr<SimpleNumberFormat> formatterWithoutLocale =
1728 std::make_unique<SimpleNumberFormat>(skeleton, nullLocaleInfo, err);
1729 EXPECT_EQ(err, I18nErrorCode::SUCCESS);
1730 ASSERT_TRUE(formatterWithoutLocale != nullptr);
1731 result = formatterWithoutLocale->Format(10);
1732 EXPECT_EQ(result, "10%");
1733
1734 skeleton = "5%";
1735 std::unique_ptr<SimpleNumberFormat> formatterWithError =
1736 std::make_unique<SimpleNumberFormat>(skeleton, nullLocaleInfo, err);
1737 EXPECT_EQ(err, I18nErrorCode::INVALID_NUMBER_FORMAT_SKELETON);
1738
1739 std::unique_ptr<SimpleNumberFormat> formatterWithEmpty =
1740 std::make_unique<SimpleNumberFormat>("", nullLocaleInfo, err);
1741 EXPECT_EQ(err, I18nErrorCode::INVALID_NUMBER_FORMAT_SKELETON);
1742 }
1743
1744 /**
1745 * @tc.name: I18nFuncTest069
1746 * @tc.desc: Test I18n simple date time format
1747 * @tc.type: FUNC
1748 */
1749 HWTEST_F(I18nTest, I18nFuncTest069, TestSize.Level1)
1750 {
1751 I18nErrorCode err = I18nErrorCode::SUCCESS;
1752 std::shared_ptr<LocaleInfo> localeInfo = std::make_shared<LocaleInfo>("zh-Hans-CN");
1753 ASSERT_TRUE(localeInfo != nullptr);
1754
1755 std::string skeleton = "yMd";
1756 std::unique_ptr<SimpleDateTimeFormat> skeletonFormatter =
1757 std::make_unique<SimpleDateTimeFormat>(skeleton, localeInfo, false, err);
1758 EXPECT_EQ(err, I18nErrorCode::SUCCESS);
1759 ASSERT_TRUE(skeletonFormatter != nullptr);
1760
1761 int64_t milliseconds = 98765432100;
1762 std::string result = skeletonFormatter->Format(milliseconds);
1763 EXPECT_EQ(result, "1973/2/17");
1764 std::shared_ptr<LocaleInfo> nullLocaleInfo = nullptr;
1765 std::unique_ptr<SimpleDateTimeFormat> formatterWithoutLocale =
1766 std::make_unique<SimpleDateTimeFormat>(skeleton, nullLocaleInfo, false, err);
1767 EXPECT_EQ(err, I18nErrorCode::SUCCESS);
1768
1769 skeleton = "y/M/d";
1770 std::unique_ptr<SimpleDateTimeFormat> formatterInvalidSkeleton =
1771 std::make_unique<SimpleDateTimeFormat>(skeleton, localeInfo, false, err);
1772 EXPECT_EQ(err, I18nErrorCode::INVALID_DATE_TIME_FORMAT_SKELETON);
1773
1774 std::unique_ptr<SimpleDateTimeFormat> formatterWithEmptySkeleton =
1775 std::make_unique<SimpleDateTimeFormat>("", localeInfo, false, err);
1776 EXPECT_EQ(err, I18nErrorCode::INVALID_DATE_TIME_FORMAT_SKELETON);
1777 }
1778
1779 /**
1780 * @tc.name: I18nFuncTest070
1781 * @tc.desc: Test I18n simple date time format
1782 * @tc.type: FUNC
1783 */
1784 HWTEST_F(I18nTest, I18nFuncTest070, TestSize.Level1)
1785 {
1786 I18nErrorCode err = I18nErrorCode::SUCCESS;
1787 std::shared_ptr<LocaleInfo> localeInfo = std::make_shared<LocaleInfo>("zh-Hans-CN");
1788 ASSERT_TRUE(localeInfo != nullptr);
1789
1790 std::string pattern = "yMd";
1791 std::unique_ptr<SimpleDateTimeFormat> patternFormatter =
1792 std::make_unique<SimpleDateTimeFormat>(pattern, localeInfo, true, err);
1793 EXPECT_EQ(err, I18nErrorCode::SUCCESS);
1794 ASSERT_TRUE(patternFormatter != nullptr);
1795
1796 int64_t milliseconds = 98765432100;
1797 std::string result = patternFormatter->Format(milliseconds);
1798 EXPECT_EQ(result, "1973217");
1799
1800 pattern = "'year('y')month('M')day('d')'";
1801 std::unique_ptr<SimpleDateTimeFormat> formatterWithContent =
1802 std::make_unique<SimpleDateTimeFormat>(pattern, localeInfo, true, err);
1803 EXPECT_EQ(err, I18nErrorCode::SUCCESS);
1804 ASSERT_TRUE(formatterWithContent != nullptr);
1805 result = formatterWithContent->Format(milliseconds);
1806 EXPECT_EQ(result, "year(1973)month(2)day(17)");
1807
1808 pattern = "y''Md";
1809 std::unique_ptr<SimpleDateTimeFormat> formatterInvalidPattern =
1810 std::make_unique<SimpleDateTimeFormat>(pattern, localeInfo, true, err);
1811 EXPECT_EQ(err, I18nErrorCode::INVALID_DATE_TIME_FORMAT_PATTERN);
1812
1813 std::unique_ptr<SimpleDateTimeFormat> formatterWithEmptyPattern =
1814 std::make_unique<SimpleDateTimeFormat>("", localeInfo, true, err);
1815 EXPECT_EQ(err, I18nErrorCode::INVALID_DATE_TIME_FORMAT_PATTERN);
1816 }
1817
1818 /**
1819 * @tc.name: I18nFuncTest071
1820 * @tc.desc: Test I18n simple number format
1821 * @tc.type: FUNC
1822 */
1823 HWTEST_F(I18nTest, I18nFuncTest071, TestSize.Level1)
1824 {
1825 I18nErrorCode err = I18nErrorCode::SUCCESS;
1826 std::string skeleton = "percent";
1827 std::string localeTag = "zh-Hans-CN";
1828 std::unique_ptr<SimpleNumberFormat> formatter = std::make_unique<SimpleNumberFormat>(skeleton, localeTag, err);
1829 EXPECT_EQ(err, I18nErrorCode::SUCCESS);
1830 ASSERT_TRUE(formatter != nullptr);
1831 std::string result = formatter->Format(10);
1832 EXPECT_EQ(result, "10%");
1833
1834 skeleton = "%";
1835 std::unique_ptr<SimpleNumberFormat> formatterWithoutLocale =
1836 std::make_unique<SimpleNumberFormat>(skeleton, "", err);
1837 EXPECT_EQ(err, I18nErrorCode::SUCCESS);
1838 ASSERT_TRUE(formatterWithoutLocale != nullptr);
1839 result = formatterWithoutLocale->Format(10);
1840 EXPECT_EQ(result, "10%");
1841
1842 skeleton = "5%";
1843 std::unique_ptr<SimpleNumberFormat> formatterWithError =
1844 std::make_unique<SimpleNumberFormat>(skeleton, "", err);
1845 EXPECT_EQ(err, I18nErrorCode::INVALID_NUMBER_FORMAT_SKELETON);
1846
1847 std::unique_ptr<SimpleNumberFormat> formatterWithEmpty =
1848 std::make_unique<SimpleNumberFormat>("", "", err);
1849 EXPECT_EQ(err, I18nErrorCode::INVALID_NUMBER_FORMAT_SKELETON);
1850 }
1851
1852 /**
1853 * @tc.name: I18nFuncTest072
1854 * @tc.desc: Test I18n simple date time format
1855 * @tc.type: FUNC
1856 */
1857 HWTEST_F(I18nTest, I18nFuncTest072, TestSize.Level1)
1858 {
1859 I18nErrorCode err = I18nErrorCode::SUCCESS;
1860 std::string localeTag = "zh-Hans-CN";
1861 std::string pattern = "yMd";
1862 std::unique_ptr<SimpleDateTimeFormat> patternFormatter =
1863 std::make_unique<SimpleDateTimeFormat>(pattern, localeTag, true, err);
1864 EXPECT_EQ(err, I18nErrorCode::SUCCESS);
1865 ASSERT_TRUE(patternFormatter != nullptr);
1866
1867 int64_t milliseconds = 98765432100;
1868 std::string result = patternFormatter->Format(milliseconds);
1869 EXPECT_EQ(result, "1973217");
1870
1871 pattern = "'year('y')month('M')day('d')'";
1872 std::unique_ptr<SimpleDateTimeFormat> formatterWithContent =
1873 std::make_unique<SimpleDateTimeFormat>(pattern, localeTag, true, err);
1874 EXPECT_EQ(err, I18nErrorCode::SUCCESS);
1875 ASSERT_TRUE(formatterWithContent != nullptr);
1876 result = formatterWithContent->Format(milliseconds);
1877 EXPECT_EQ(result, "year(1973)month(2)day(17)");
1878
1879 pattern = "y''Md";
1880 std::unique_ptr<SimpleDateTimeFormat> formatterInvalidPattern =
1881 std::make_unique<SimpleDateTimeFormat>(pattern, localeTag, true, err);
1882 EXPECT_EQ(err, I18nErrorCode::INVALID_DATE_TIME_FORMAT_PATTERN);
1883
1884 std::unique_ptr<SimpleDateTimeFormat> formatterWithEmptyPattern =
1885 std::make_unique<SimpleDateTimeFormat>("", localeTag, true, err);
1886 EXPECT_EQ(err, I18nErrorCode::INVALID_DATE_TIME_FORMAT_PATTERN);
1887 }
1888
1889 /**
1890 * @tc.name: I18nFuncTest073
1891 * @tc.desc: Test I18n GetUnicodeWrappedFilePath
1892 * @tc.type: FUNC
1893 */
1894 HWTEST_F(I18nTest, I18nFuncTest073, TestSize.Level1)
1895 {
1896 std::string localeTag = "ar";
1897 std::string path = "data>log>faultlog";
1898 char delimiter = '>';
1899 std::string errorCode;
1900 std::string result = LocaleConfig::GetUnicodeWrappedFilePath(path, delimiter, localeTag, errorCode);
1901 EXPECT_EQ(result, "\u200f\u200edata\u200e\u200f>\u200elog\u200e\u200f>\u200efaultlog\u200e");
1902 }
1903
1904 /**
1905 * @tc.name: I18nFuncTest074
1906 * @tc.desc: Test I18n calendar
1907 * @tc.type: FUNC
1908 */
1909 HWTEST_F(I18nTest, I18nFuncTest074, TestSize.Level1)
1910 {
1911 std::string systemLanguage = LocaleConfig::GetSystemLanguage();
1912 std::string systemLocale = LocaleConfig::GetSystemLocale();
1913 LocaleConfig::SetFirstDayOfWeek(WeekDay::THU);
1914
1915 I18nCalendar defaultCalendar("zh-Hans-CN", CalendarType::CHINESE);
1916 int32_t firstDayOfWeek = defaultCalendar.GetFirstDayOfWeek();
1917 EXPECT_EQ(firstDayOfWeek, 1);
1918 defaultCalendar.SetFirstDayOfWeek(6);
1919 firstDayOfWeek = defaultCalendar.GetFirstDayOfWeek();
1920 EXPECT_EQ(firstDayOfWeek, 6);
1921
1922 I18nCalendar satCalendar("zh-Hans-CN-u-fw-sat", CalendarType::CHINESE);
1923 firstDayOfWeek = satCalendar.GetFirstDayOfWeek();
1924 EXPECT_EQ(firstDayOfWeek, 7);
1925 satCalendar.SetFirstDayOfWeek(3);
1926 firstDayOfWeek = satCalendar.GetFirstDayOfWeek();
1927 EXPECT_EQ(firstDayOfWeek, 3);
1928
1929 LocaleConfig::SetSystemLanguage(systemLanguage);
1930 LocaleConfig::SetSystemLocale(systemLocale);
1931 }
1932 } // namespace I18n
1933 } // namespace Global
1934 } // namespace OHOS