diff --git a/frameworks/intl/src/utils.cpp b/frameworks/intl/src/utils.cpp index b90a12e9..f89f75ba 100644 --- a/frameworks/intl/src/utils.cpp +++ b/frameworks/intl/src/utils.cpp @@ -91,6 +91,7 @@ void GetAllValidLocalesTag(std::unordered_set& allValidLocalesLangu for (int i = 0; i < validCount; i++) { allValidLocalesLanguageTag.insert(validLocales[i].getLanguage()); } + delete[] validLocales; init = true; } } // namespace I18n diff --git a/frameworks/intl/test/BUILD.gn b/frameworks/intl/test/BUILD.gn index b773ef4c..4f7f0038 100644 --- a/frameworks/intl/test/BUILD.gn +++ b/frameworks/intl/test/BUILD.gn @@ -22,6 +22,7 @@ ohos_unittest("intl_test") { "unittest/i18n_test.cpp", "unittest/intl_test.cpp", "unittest/locale_config_test.cpp", + "unittest/mock/src/generate_ics_file.cpp", "unittest/mock/src/i18n_timezone_mock.cpp", "unittest/mock/src/phone_number_format_mock.cpp", ] @@ -43,6 +44,7 @@ ohos_unittest("intl_test") { "//third_party/googletest:gtest_main", "//third_party/icu/icu4c:shared_icui18n", "//third_party/icu/icu4c:shared_icuuc", + "//third_party/libphonenumber/cpp:phonenumber_standard", "//third_party/libpng:libpng", ] } diff --git a/frameworks/intl/test/unittest/intl_test.cpp b/frameworks/intl/test/unittest/intl_test.cpp index 5a4397eb..6c92ea01 100644 --- a/frameworks/intl/test/unittest/intl_test.cpp +++ b/frameworks/intl/test/unittest/intl_test.cpp @@ -19,7 +19,9 @@ #include "character.h" #include "collator.h" +#include "date_time_filter.h" #include "date_time_format.h" +#include "date_time_rule.h" #include "holiday_manager.h" #include "i18n_break_iterator.h" #include "i18n_calendar.h" @@ -34,11 +36,14 @@ #include "phone_number_format.h" #include "plural_rules.h" #include "preferred_language.h" +#include "regex_rule.h" #include "relative_time_format.h" #include "system_locale_manager.h" #include "taboo_utils.h" +#include "taboo.h" #include "utils.h" #include "intl_test.h" +#include "generate_ics_file.h" using namespace OHOS::Global::I18n; using testing::ext::TestSize; @@ -80,7 +85,7 @@ HWTEST_F(IntlTest, IntlFuncTest001, TestSize.Level1) locales.push_back("jessie"); locales.push_back(locale); map options = { { "year", "numeric" }, - { "month", "long" }, + { "month", "narrow" }, { "day", "numeric" }, { "hour", "numeric" }, { "minute", "2-digit" }, @@ -96,7 +101,7 @@ HWTEST_F(IntlTest, IntlFuncTest001, TestSize.Level1) string out = dateFormat->Format(milliseconds); EXPECT_EQ(out, expects); EXPECT_EQ(dateFormat->GetYear(), "numeric"); - EXPECT_EQ(dateFormat->GetMonth(), "long"); + EXPECT_EQ(dateFormat->GetMonth(), "narrow"); EXPECT_EQ(dateFormat->GetDay(), "numeric"); EXPECT_EQ(dateFormat->GetHour(), "numeric"); EXPECT_EQ(dateFormat->GetMinute(), "2-digit"); @@ -432,7 +437,8 @@ HWTEST_F(IntlTest, IntlFuncTest0012, TestSize.Level1) EXPECT_EQ(out, expects); options = { { "dateStyle", "long" }, - { "hourCycle", "h11" } + { "hourCycle", "h11" }, + { "hour12", "fakeValue" } }; DateTimeFormat *dateFormatH11 = new (std::nothrow) DateTimeFormat(locales, options); options.insert({ "hourCycle", "h12" }); @@ -447,9 +453,15 @@ HWTEST_F(IntlTest, IntlFuncTest0012, TestSize.Level1) delete dateFormatH24; locales.clear(); DateTimeFormat *dateFormatEmpty = new (std::nothrow) DateTimeFormat(locales, options); - locales.push_back("fake locale"); - options = {}; + locales.push_back("@@@&&"); + options = { { "dayPeriod", "short" } }; DateTimeFormat *dateFormatFake = new (std::nothrow) DateTimeFormat(locales, options); + options.insert({ "dayPeriod", "long" }); + DateTimeFormat *dateFormatDayPeriod = new (std::nothrow) DateTimeFormat(locales, options); + delete dateFormatDayPeriod; + options.insert({ "dayPeriod", "narrow" }); + DateTimeFormat *dateFormatDayPeriod2 = new (std::nothrow) DateTimeFormat(locales, options); + delete dateFormatDayPeriod2; options.insert({ "dateStyle", "long" }); DateTimeFormat *dateFormatFake2 = new (std::nothrow) DateTimeFormat(locales, options); delete dateFormatEmpty; @@ -515,14 +527,14 @@ HWTEST_F(IntlTest, IntlFuncTest0015, TestSize.Level1) vector locales; locales.push_back("jessie"); locales.push_back(locale); - map options = { { "timeZone", "America/Los_Angeles" }, { "timeZoneName", "long" } }; + map options = { { "timeZone", "America/Los_Angeles" }, { "timeZoneName", "short" } }; DateTimeFormat *dateFormat = new (std::nothrow) DateTimeFormat(locales, options); if (!dateFormat) { EXPECT_TRUE(false); return; } EXPECT_EQ(dateFormat->GetTimeZone(), "America/Los_Angeles"); - EXPECT_EQ(dateFormat->GetTimeZoneName(), "long"); + EXPECT_EQ(dateFormat->GetTimeZoneName(), "short"); delete dateFormat; } @@ -688,7 +700,7 @@ HWTEST_F(IntlTest, IntlFuncTest0019, TestSize.Level1) { // abnormal test case vector locales; - locales.push_back("fake locale"); + locales.push_back("7776@"); map inputOptions = { { "localeMatcher", "fake value" }, { "usage", "fake value"}, @@ -737,7 +749,7 @@ HWTEST_F(IntlTest, IntlFuncTest0020, TestSize.Level1) { // abnormal test case vector locales; - locales.push_back("fake locale"); + locales.push_back("$$##"); map inputOptions = { { "ignorePunctuation", "fake value" }, { "collation", "fake value"}, @@ -881,7 +893,7 @@ HWTEST_F(IntlTest, IntlFuncTest0024, TestSize.Level1) // abnormal test cast // normal test case vector locales; - locales.push_back("fake_locale"); + locales.push_back("$$$###"); map options = { { "fake_localeMatcher", "best fit" }, { "type", "fake_cardinal" }, @@ -1099,8 +1111,8 @@ HWTEST_F(IntlTest, IntlFuncTest0028, TestSize.Level1) HWTEST_F(IntlTest, IntlFuncTest0029, TestSize.Level1) { vector locales; - locales.push_back("xxxx"); - locales.push_back("zh-Hans"); + locales.push_back("####"); + locales.push_back("zh-Hans-u-nu-latn"); map options = { { "localeMatcher", "best fit" }, { "numeric", "auto" }, @@ -1271,7 +1283,7 @@ HWTEST_F(IntlTest, IntlFuncTest0033, TestSize.Level1) vector locales; locales.push_back("zh-Hans"); map options = { - { "locale", "fake locale" }, + { "locale", "@@##" }, { "currency", "fake currency" }, { "currencySign", "fake sign" }, { "currencyDisplay", "symbol" }, @@ -1319,7 +1331,7 @@ HWTEST_F(IntlTest, IntlFuncTest0034, TestSize.Level1) vector locales; locales.push_back("en-US"); map options = { - { "locale", "fake locale" }, + { "locale", "$$$##43" }, { "currency", "USD" }, { "currencySign", "symbol" }, { "currencyDisplay", "symbol" }, @@ -1546,7 +1558,6 @@ HWTEST_F(IntlTest, IntlFuncTest0039, TestSize.Level1) { "dateStyle", "medium" }, { "timeStyle", "long" }, { "hourCycle", "h12" }, - { "timeZone", "Asia/Shanghai" }, { "numberingSystem", "latn" }, { "hour12", "true" }, { "weekday", "long" }, @@ -1951,7 +1962,7 @@ HWTEST_F(IntlTest, IntlFuncTest0050, TestSize.Level1) { const std::string langugae = "zh-CN"; const std::string langugaeDe = "de-DE"; - const std::string fakeLangugae = "fake-language"; + const std::string fakeLangugae = "1**1"; I18nErrorCode errorCode = I18nErrorCode::SUCCESS; PreferredLanguage::AddPreferredLanguage(langugae, 0, errorCode); EXPECT_TRUE(errorCode != I18nErrorCode::SUCCESS); @@ -2041,6 +2052,8 @@ HWTEST_F(IntlTest, IntlFuncTest0053, TestSize.Level1) std::string number = "+8618622350085"; std::string formated = phoneNumberFormat->format(number); EXPECT_EQ(formated, "186 2235 0085"); + std::string location2 = phoneNumberFormat->getLocationName("fake-number", "zh-CN"); + EXPECT_EQ(location2, ""); delete phoneNumberFormat; } @@ -2056,7 +2069,11 @@ HWTEST_F(IntlTest, IntlFuncTest0054, TestSize.Level1) EXPECT_EQ(res1, "China"); std::string res2 = tabooUtils->ReplaceLanguageName("zh", "en", "chinese"); EXPECT_EQ(res2, "chinese"); + std::string res3 = tabooUtils->ReplaceCountryName("TW", "zh-Hans", "中国台湾"); + EXPECT_EQ(res3, "中国台湾"); delete tabooUtils; + Taboo* taboo = new Taboo(); + delete taboo; } /** @@ -2099,7 +2116,7 @@ HWTEST_F(IntlTest, IntlFuncTest0055, TestSize.Level1) EXPECT_EQ(resultHashEn, 8); int32_t resultHashQaagEn = LocaleCompare::Compare("en-Qaag-WS", "en-001"); EXPECT_EQ(resultHashQaagEn, 8); - I18nBreakIterator *breakIterator = new I18nBreakIterator("fake locale"); + I18nBreakIterator *breakIterator = new I18nBreakIterator("2--**"); breakIterator->Current(); breakIterator->First(); breakIterator->Last(); @@ -2215,7 +2232,7 @@ HWTEST_F(IntlTest, IntlFuncTest0058, TestSize.Level1) */ HWTEST_F(IntlTest, IntlFuncTest0059, TestSize.Level1) { - std::vector localeTags = { "fake locale tag"}; + std::vector localeTags = { "$$@@#"}; std::map configs = { {"style", "unit"}, {"unit", "fake unit"} }; @@ -2231,7 +2248,8 @@ HWTEST_F(IntlTest, IntlFuncTest0059, TestSize.Level1) */ HWTEST_F(IntlTest, IntlFuncTest0060, TestSize.Level1) { - std::string path = "/system/lib/TR.ics"; + IcsFileWriter icsFileWriter; + std::string path = icsFileWriter.GenerateFile(); HolidayManager *holiday_manager = new HolidayManager(path.c_str()); std::map> holidayDataMap; std::vector infoList; @@ -2253,6 +2271,7 @@ HWTEST_F(IntlTest, IntlFuncTest0060, TestSize.Level1) EXPECT_TRUE(flag); flag = holiday_manager->IsHoliday(); EXPECT_TRUE(!flag); + remove(path); delete holiday_manager; } @@ -2280,6 +2299,69 @@ HWTEST_F(IntlTest, IntlFuncTest061, TestSize.Level1) EXPECT_EQ(numFmt->GetStyle(), style); delete numFmt; } + +/** + * @tc.name: IntlFuncTest0062 + * @tc.desc: Test Intl utils.cpp + * @tc.type: FUNC + */ +HWTEST_F(IntlTest, IntlFuncTest062, TestSize.Level1) +{ + string emptyStr = ""; + string sep = ";"; + vector dest; + std::unordered_set allValidLocalesLanguageTag; + Split(emptyStr, sep, dest); + int32_t status = 0; + string numberStr = "12345678901234567890123456789012345678901234567890987654321"; + ConvertString2Int(numberStr, status); + icu::Locale locale("$$$$5%%%"); + bool isValid = IsValidLocaleTag(locale); + EXPECT_TRUE(!isValid); + GetAllValidLocalesTag(allValidLocalesLanguageTag); + EXPECT_TRUE(allValidLocalesLanguageTag.size() == 0); + + std::string localeRule = "zh-Hans"; + DateTimeRule* dateTimeRule = new DateTimeRule(localeRule); + DateTimeFilter* dateTimeFilter = new DateTimeFilter(localeRule, dateTimeRule); + delete dateTimeFilter; + delete dateTimeRule; +} + +/** + * @tc.name: IntlFuncTest0062 + * @tc.desc: Test Intl utils.cpp + * @tc.type: FUNC + */ +HWTEST_F(IntlTest, IntlFuncTest063, TestSize.Level1) +{ + using namespace i18n::phonenumbers; + string regexStr = "[a-z]1?"; + icu::UnicodeString regex(regexStr.c_str()); + std::string isValidType = "PrexxxxSuf"; + std::string handleType = "EndWithMobile"; + std::string insensitive = "False"; + std::string type = "xxx"; + RegexRule* regexRule = new RegexRule(regex, isValidType, handleType, insensitive, type); + handleType = "fake"; + isValidType = "Code"; + RegexRule* regexRule2 = new RegexRule(regex, isValidType, handleType, insensitive, type); + icu::UnicodeString message(type.c_str()); + + i18n::phonenumbers::PhoneNumber phoneNumber; + PhoneNumberUtil* phoneNumberUtil = i18n::phonenumbers::PhoneNumberUtil::GetInstance(); + std::string input = "(010111111111111;ext=0782"; + std::string country = "CN"; + size_t size = input.length(); + phoneNumberUtil->Parse(input, country, &phoneNumber); + PhoneNumberMatch* possibleNumber = new PhoneNumberMatch(size, input, phoneNumber); + PhoneNumberMatch* phoneNumberMatch = regexRule2->IsValid(possibleNumber, message); + EXPECT_TRUE(phoneNumberMatch != nullptr); + delete phoneNumberUtil; + delete phoneNumberMatch; + delete regexRule; + delete regexRule2; +} } // namespace I18n } // namespace Global } // namespace OHOS \ No newline at end of file diff --git a/frameworks/intl/test/unittest/intl_test.h b/frameworks/intl/test/unittest/intl_test.h index 14cf9abf..3dc0cef0 100644 --- a/frameworks/intl/test/unittest/intl_test.h +++ b/frameworks/intl/test/unittest/intl_test.h @@ -80,6 +80,8 @@ int IntlFuncTest0058(void); int IntlFuncTest0059(void); int IntlFuncTest0060(void); int IntlFuncTest0061(void); +int IntlFuncTest0062(void); +int IntlFuncTest0063(void); } // namespace I18n } // namespace Global } // namespace OHOS diff --git a/frameworks/intl/test/unittest/mock/include/generate_ics_file.h b/frameworks/intl/test/unittest/mock/include/generate_ics_file.h new file mode 100644 index 00000000..8eb2d003 --- /dev/null +++ b/frameworks/intl/test/unittest/mock/include/generate_ics_file.h @@ -0,0 +1,30 @@ +/* + * Copyright (c) 2021-2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#ifndef OHOS_GLOBAL_I18N_ICS_FILE_WRITER_H +#define OHOS_GLOBAL_I18N_ICS_FILE_WRITER_H + +namespace OHOS { +namespace Global { +namespace I18n { +class IcsFileWriter { +public: + IcsFileWriter(); + ~IcsFileWriter(); + std::string GenerateFile(); +}; +} // namespace I18n +} // namespace Global +} // namespace OHOS +#endif \ No newline at end of file diff --git a/frameworks/intl/test/unittest/mock/src/generate_ics_file.cpp b/frameworks/intl/test/unittest/mock/src/generate_ics_file.cpp new file mode 100644 index 00000000..7bac9ffb --- /dev/null +++ b/frameworks/intl/test/unittest/mock/src/generate_ics_file.cpp @@ -0,0 +1,71 @@ +/* + * Copyright (c) 2022-2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ +#include +#include +#include +#include "generate_ics_file.h" + +namespace OHOS { +namespace Global { +namespace I18n { + +IcsFileWriter::IcsFileWriter() +{ +} + +IcsFileWriter::~IcsFileWriter() +{ +} + +std::string IcsFileWriter::GenerateFile() +{ + using namespace std; + std::string filePath = "/system/lib/TR.ics"; + ofstream fstream; + fstream.open(filePath, ios::out); + if (!fstream) { + printf("file is noll\r\n"); + return filePath; + } + std::vector list = { + "BEGIN:VCALENDAR\r\n", + "METHOD:PUBLISH\r\n", + "BEGIN:VEVENT\r\n", + "UID:1\r\n", + "DTSTART;VALUE=DATE:20220625\r\n", + "DTEND;VALUE=DATE:20220625\r\n", + "SUMMARY:Sacrifice Feast Holiday\r\n", + "RESOURCES;LANGUAGE=TR:Kurban Bayrami Tatili\r\n" + "END:VEVENT\r\n", + "BEGIN:VEVENT\r\n", + "UID:2\r\n", + "DTSTART;VALUE=DATE:20220626\r\n", + "DTEND;VALUE=DATE:20220626\r\n", + "SUMMARY:The Second Day of Sacrifice Feast\r\n", + "RESOURCES;LANGUAGE=TR:Kurban Bayrami 2. Günü\r\n" + "END:VEVENT\r\n", + "END:VCALENDAR\r\n" + }; + for (size_t i = 0; i < list.size(); i++) { + fstream.write(list[i].c_str(), sizeof(list[i])); + } + + fstream.close(); + return filePath; +} + +} // namespace I18n +} // namespace Global +} // namespace OHOS \ No newline at end of file