• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include "displaynames_test.h"
16 
17 #include <map>
18 #include <vector>
19 
20 #include "displaynames.h"
21 
22 using namespace OHOS::Global::I18n;
23 using testing::ext::TestSize;
24 using namespace std;
25 
26 namespace OHOS {
27 namespace Global {
28 namespace I18n {
29 
SetUpTestCase(void)30 void DisplaynamesTest::SetUpTestCase(void)
31 {}
32 
TearDownTestCase(void)33 void DisplaynamesTest::TearDownTestCase(void)
34 {}
35 
SetUp(void)36 void DisplaynamesTest::SetUp(void)
37 {}
38 
TearDown(void)39 void DisplaynamesTest::TearDown(void)
40 {}
41 
42 /**
43  * @tc.name: DisplaynamesTest0001
44  * @tc.desc: Test Intl DisplayNames.SupportedLocalesOf
45  * @tc.type: FUNC
46  */
47 HWTEST_F(DisplaynamesTest, DisplaynamesTest0001, TestSize.Level1)
48 {
49     std::vector<std::string> localeTags = {};
50     std::map<std::string, std::string> options = {{"type", "language"}};
51     DisplayNames displayNames(localeTags, options);
52     I18nErrorCode status = displayNames.GetError();
53     EXPECT_EQ(status, I18nErrorCode::SUCCESS);
54     std::map<std::string, std::string> resolvedOptions = displayNames.ResolvedOptions();
55     auto it = resolvedOptions.find("locale");
56     ASSERT_TRUE(it != resolvedOptions.end());
57     std::string defaultLocale = it->second;
58 
59     std::vector<std::string> requestLocales = {"zxx"};
60     requestLocales.push_back(defaultLocale);
61     status = I18nErrorCode::SUCCESS;
62     std::vector<std::string> resultLocales = DisplayNames::SupportedLocalesOf(requestLocales, options, status);
63     EXPECT_EQ(status, I18nErrorCode::SUCCESS);
64     EXPECT_EQ(resultLocales.size(), 1);
65     EXPECT_EQ(resultLocales[0], defaultLocale);
66 }
67 
68 /**
69  * @tc.name: DisplaynamesTest0002
70  * @tc.desc: Test Intl DisplayNames.SupportedLocalesOf
71  * @tc.type: FUNC
72  */
73 HWTEST_F(DisplaynamesTest, DisplaynamesTest0002, TestSize.Level1)
74 {
75     std::vector<std::string> localeTags = {"ban", "id-u-co-pinyin", "de-ID"};
76     std::map<std::string, std::string> options = {{"localeMatcher", "lookup"}};
77     I18nErrorCode status = I18nErrorCode::SUCCESS;
78     std::vector<std::string> resultLocales = DisplayNames::SupportedLocalesOf(localeTags, options, status);
79     EXPECT_EQ(status, I18nErrorCode::SUCCESS);
80     EXPECT_EQ(resultLocales.size(), 2);
81     EXPECT_EQ(resultLocales[0], "id-u-co-pinyin");
82     EXPECT_EQ(resultLocales[1], "de-ID");
83     std::map<std::string, std::string> optionsBogus = {{"localeMatcher", "bogus"}};
84     std::vector<std::string> resultLocalesBogus = DisplayNames::SupportedLocalesOf(localeTags, optionsBogus, status);
85     EXPECT_EQ(status, I18nErrorCode::INVALID_PARAM);
86     EXPECT_EQ(resultLocalesBogus.size(), 0);
87 }
88 
89 /**
90  * @tc.name: DisplaynamesTest0003
91  * @tc.desc: Test Intl DisplayNames.Display
92  * @tc.type: FUNC
93  */
94 HWTEST_F(DisplaynamesTest, DisplaynamesTest0003, TestSize.Level1)
95 {
96     std::vector<std::string> localeTags = {"ban", "zxx", "zh"};
97     std::map<std::string, std::string> options = {{"type", "calendar"}};
98     DisplayNames displayNames(localeTags, options);
99     I18nErrorCode status = displayNames.GetError();
100     EXPECT_EQ(status, I18nErrorCode::SUCCESS);
101     const std::vector<std::string> bogusCalendarCode = {"00", "000000000", "-00000000", "_00000000", " abcdef",
102         "abcdef ", "abc def", "123_abc", "123_abc_ABC", "12345678_abcdefgh_ABCDEFGH"};
103     std::string result = "";
104     std::string errorMessage = "";
105     for (const std::string& code : bogusCalendarCode) {
106         result = displayNames.Display(code);
107         status = displayNames.GetError();
108         errorMessage = displayNames.GetErrorMessage();
109         EXPECT_EQ(status, I18nErrorCode::INVALID_PARAM);
110         EXPECT_EQ(errorMessage, "invalid calendar");
111         EXPECT_EQ(result, "");
112     }
113     const std::vector<std::string> validCalendarCode = {"01234567", "899", "abcdefgh", "ijklmnop",
114         "qrstuvwx", "yzz", "ABCDEFGH", "IJKLMNOP", "QRSTUVWX", "YZZ", "123-abc", "12345678-abcdefgh",
115         "123-abc-ABC", "12345678-abcdefgh-ABCDEFGH"};
116     for (const std::string& code : validCalendarCode) {
117         result = displayNames.Display(code);
118         status = displayNames.GetError();
119         errorMessage = displayNames.GetErrorMessage();
120         EXPECT_EQ(status, I18nErrorCode::SUCCESS);
121         EXPECT_EQ(errorMessage, "");
122         EXPECT_EQ(result, code);
123     }
124 }
125 
126 /**
127  * @tc.name: DisplaynamesTest0004
128  * @tc.desc: Test Intl DisplayNames.Display
129  * @tc.type: FUNC
130  */
131 HWTEST_F(DisplaynamesTest, DisplaynamesTest0004, TestSize.Level1)
132 {
133     std::vector<std::string> localeTags = {"ban", "zxx", "en"};
134     std::map<std::string, std::string> options = {{"type", "dateTimeField"}};
135     DisplayNames displayNames(localeTags, options);
136     I18nErrorCode status = displayNames.GetError();
137     EXPECT_EQ(status, I18nErrorCode::SUCCESS);
138     const std::vector<std::string> bogusCode = {"", "timezoneName", "timezonename",
139         "millisecond", "seconds", " year", "year "};
140     std::string result = "";
141     std::string errorMessage = "";
142     for (const std::string& code : bogusCode) {
143         result = displayNames.Display(code);
144         status = displayNames.GetError();
145         errorMessage = displayNames.GetErrorMessage();
146         EXPECT_EQ(status, I18nErrorCode::INVALID_PARAM);
147         EXPECT_EQ(errorMessage, "invalid datetimefield");
148         EXPECT_EQ(result, "");
149     }
150     std::vector<std::string> validCode = {"era", "year", "quarter", "month", "weekOfYear",
151         "weekday", "day", "dayPeriod", "hour", "minute", "second", "timeZoneName"};
152     std::vector<std::string> validCodeResult = {"era", "year", "quarter", "month", "week",
153             "day of the week", "day", "AM/PM", "hour", "minute", "second", "time zone"};
154     for (auto index = 0; index < validCode.size(); ++index) {
155         result = displayNames.Display(validCode[index]);
156         status = displayNames.GetError();
157         errorMessage = displayNames.GetErrorMessage();
158         EXPECT_EQ(status, I18nErrorCode::SUCCESS);
159         EXPECT_EQ(errorMessage, "");
160         EXPECT_EQ(result, validCodeResult[index]);
161     }
162 }
163 
164 /**
165  * @tc.name: DisplaynamesTest0005
166  * @tc.desc: Test Intl DisplayNames.ResolvedOptions deafault
167  * @tc.type: FUNC
168  */
169 HWTEST_F(DisplaynamesTest, DisplaynamesTest0005, TestSize.Level1)
170 {
171     std::vector<std::string> localeTags = {"en-US"};
172     std::map<std::string, std::string> options = {{"type", "language"}};
173     DisplayNames displayNames(localeTags, options);
174     I18nErrorCode status = displayNames.GetError();
175     EXPECT_EQ(status, I18nErrorCode::SUCCESS);
176     std::map<std::string, std::string> resolvedOptions = displayNames.ResolvedOptions();
177     EXPECT_EQ(resolvedOptions["style"], "long");
178     EXPECT_EQ(resolvedOptions["type"], "language");
179     EXPECT_EQ(resolvedOptions["fallback"], "code");
180 }
181 
182 /**
183  * @tc.name: DisplaynamesTest0006
184  * @tc.desc: Test Intl DisplayNames.ResolvedOptions fallback
185  * @tc.type: FUNC
186  */
187 HWTEST_F(DisplaynamesTest, DisplaynamesTest0006, TestSize.Level1)
188 {
189     std::vector<std::string> localeTags = {"en-US"};
190     const std::vector<std::string> fallbackOptions = { "code", "none" };
191     for (auto fallback : fallbackOptions) {
192         std::map<std::string, std::string> options = {{"type", "language"}, { "fallback", fallback }};
193         DisplayNames displayNames(localeTags, options);
194         I18nErrorCode status = displayNames.GetError();
195         EXPECT_EQ(status, I18nErrorCode::SUCCESS);
196         std::map<std::string, std::string> resolvedOptions = displayNames.ResolvedOptions();
197         EXPECT_EQ(resolvedOptions["style"], "long");
198         EXPECT_EQ(resolvedOptions["type"], "language");
199         EXPECT_EQ(resolvedOptions["fallback"], fallback);
200     }
201     std::map<std::string, std::string> bogusOptions = {{"type", "language"}, { "fallback", "bogus" }};
202     DisplayNames displayNames(localeTags, bogusOptions);
203     I18nErrorCode status = displayNames.GetError();
204     EXPECT_EQ(status, I18nErrorCode::INVALID_PARAM);
205 }
206 
207 /**
208  * @tc.name: DisplaynamesTest0007
209  * @tc.desc: Test Intl DisplayNames.ResolvedOptions languageDisplay
210  * @tc.type: FUNC
211  */
212 HWTEST_F(DisplaynamesTest, DisplaynamesTest0007, TestSize.Level1)
213 {
214     std::vector<std::string> localeTags = {"en-US"};
215     const std::vector<std::string> languageDisplayOptions = { "dialect", "standard" };
216     for (auto languageDisplay : languageDisplayOptions) {
217         std::map<std::string, std::string> options = {{"type", "language"}, { "languageDisplay", languageDisplay }};
218         DisplayNames displayNames(localeTags, options);
219         I18nErrorCode status = displayNames.GetError();
220         EXPECT_EQ(status, I18nErrorCode::SUCCESS);
221         std::map<std::string, std::string> resolvedOptions = displayNames.ResolvedOptions();
222         EXPECT_EQ(resolvedOptions["languageDisplay"], languageDisplay);
223     }
224     std::map<std::string, std::string> bogusOptions = {{"type", "language"}, { "languageDisplay", "bogus" }};
225     DisplayNames displayNames(localeTags, bogusOptions);
226     I18nErrorCode status = displayNames.GetError();
227     EXPECT_EQ(status, I18nErrorCode::INVALID_PARAM);
228 }
229 
230 /**
231  * @tc.name: DisplaynamesTest0008
232  * @tc.desc: Test Intl DisplayNames.ResolvedOptions style
233  * @tc.type: FUNC
234  */
235 HWTEST_F(DisplaynamesTest, DisplaynamesTest0008, TestSize.Level1)
236 {
237     std::vector<std::string> localeTags = {"en-US"};
238     const std::vector<std::string> styleOptions = { "narrow", "short", "long" };
239     for (auto style : styleOptions) {
240         std::map<std::string, std::string> options = {{"type", "language"}, { "style", style }};
241         DisplayNames displayNames(localeTags, options);
242         I18nErrorCode status = displayNames.GetError();
243         EXPECT_EQ(status, I18nErrorCode::SUCCESS);
244         std::map<std::string, std::string> resolvedOptions = displayNames.ResolvedOptions();
245         EXPECT_EQ(resolvedOptions["style"], style);
246     }
247     std::map<std::string, std::string> bogusOptions = {{"type", "language"}, { "style", "bogus" }};
248     DisplayNames displayNames(localeTags, bogusOptions);
249     I18nErrorCode status = displayNames.GetError();
250     EXPECT_EQ(status, I18nErrorCode::INVALID_PARAM);
251 }
252 
253 /**
254  * @tc.name: DisplaynamesTest0009
255  * @tc.desc: Test Intl DisplayNames.ResolvedOptions type
256  * @tc.type: FUNC
257  */
258 HWTEST_F(DisplaynamesTest, DisplaynamesTest0009, TestSize.Level1)
259 {
260     std::vector<std::string> localeTags = {"en-US"};
261     const std::vector<std::string> typeOptions = { "language", "region", "script", "currency",
262         "calendar", "dateTimeField" };
263     for (auto type : typeOptions) {
264         std::map<std::string, std::string> options = {{"type", type}};
265         DisplayNames displayNames(localeTags, options);
266         I18nErrorCode status = displayNames.GetError();
267         EXPECT_EQ(status, I18nErrorCode::SUCCESS);
268         std::map<std::string, std::string> resolvedOptions = displayNames.ResolvedOptions();
269         EXPECT_EQ(resolvedOptions["type"], type);
270     }
271     std::map<std::string, std::string> bogusOptions = {{"type", "bogus"}};
272     DisplayNames displayNames(localeTags, bogusOptions);
273     I18nErrorCode status = displayNames.GetError();
274     EXPECT_EQ(status, I18nErrorCode::INVALID_PARAM);
275 
276     std::map<std::string, std::string> missOptions = {{"localeMatcher", "lookup"}};
277     DisplayNames missTypeDisplayNames(localeTags, missOptions);
278     status = missTypeDisplayNames.GetError();
279     EXPECT_EQ(status, I18nErrorCode::MISSING_PARAM);
280 }
281 } // namespace I18n
282 } // namespace Global
283 } // namespace OHOS