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