• 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 
16 #include <gtest/gtest.h>
17 #include "intl_date_time_format.h"
18 
19 #include "intl_date_time_format_test.h"
20 
21 using namespace OHOS::Global::I18n;
22 using testing::ext::TestSize;
23 using namespace std;
24 
25 namespace OHOS {
26 namespace Global {
27 namespace I18n {
28 class IntlDateTimeFormatTest : public testing::Test {
29 public:
30     static void SetUpTestCase(void);
31     static void TearDownTestCase(void);
32     void SetUp();
33     void TearDown();
34 };
35 
SetUpTestCase(void)36 void IntlDateTimeFormatTest::SetUpTestCase(void)
37 {
38 }
39 
TearDownTestCase(void)40 void IntlDateTimeFormatTest::TearDownTestCase(void)
41 {
42 }
43 
SetUp(void)44 void IntlDateTimeFormatTest::SetUp(void)
45 {
46 }
47 
TearDown(void)48 void IntlDateTimeFormatTest::TearDown(void)
49 {}
50 
51 /**
52  * @tc.name: IntlDateTimeFormatFuncTest001
53  * @tc.desc: Test Intl date time format
54  * @tc.type: FUNC
55  */
56 HWTEST_F(IntlDateTimeFormatTest, IntlDateTimeFormatFuncTest001, TestSize.Level1)
57 {
58     std::vector<std::string> localeTags = { "zh-Hans-CN" };
59     std::unordered_map<std::string, std::string> configs;
60     std::string errMessage;
61     std::unique_ptr<IntlDateTimeFormat> intlDateTimeFormat = std::make_unique<IntlDateTimeFormat>(localeTags,
62         configs, errMessage);
63     double startMilliseconds = 987654321;
64     double endMilliseconds = 1987987654;
65 
66     std::string formatResult = intlDateTimeFormat->Format(startMilliseconds);
67     EXPECT_EQ(formatResult, "1970/1/12");
68     std::vector<std::pair<std::string, std::string>> formatToPartsResult =
69         intlDateTimeFormat->FormatToParts(startMilliseconds, errMessage);
70     EXPECT_EQ(formatToPartsResult.size(), 5);
71     EXPECT_EQ(formatToPartsResult[0].first, "year");
72     EXPECT_EQ(formatToPartsResult[0].second, "1970");
73     EXPECT_EQ(formatToPartsResult[1].first, "literal");
74     EXPECT_EQ(formatToPartsResult[1].second, "/");
75     EXPECT_EQ(formatToPartsResult[2].first, "month");
76     EXPECT_EQ(formatToPartsResult[2].second, "1");
77     EXPECT_EQ(formatToPartsResult[3].first, "literal");
78     EXPECT_EQ(formatToPartsResult[3].second, "/");
79     EXPECT_EQ(formatToPartsResult[4].first, "day");
80     EXPECT_EQ(formatToPartsResult[4].second, "12");
81     EXPECT_EQ(errMessage, "");
82 
83     std::string formatRangeResult = intlDateTimeFormat->FormatRange(startMilliseconds, endMilliseconds, errMessage);
84     EXPECT_EQ(formatRangeResult, "1970/1/12 \xE2\x80\x93 1970/1/24");
85     EXPECT_EQ(errMessage, "");
86     std::vector<std::pair<std::string, std::string>> formatRangeToPartsResult =
87         intlDateTimeFormat->FormatRangeToParts(startMilliseconds, endMilliseconds, errMessage);
88     EXPECT_EQ(formatRangeToPartsResult.size(), 11);
89     std::string result;
90     for (auto part : formatRangeToPartsResult) {
91         result += part.second;
92     }
93     EXPECT_EQ(result, "1970/1/12 \xE2\x80\x93 1970/1/24");
94     EXPECT_EQ(errMessage, "");
95 }
96 
97 /**
98  * @tc.name: IntlDateTimeFormatFuncTest002
99  * @tc.desc: Test Intl date time format
100  * @tc.type: FUNC
101  */
102 HWTEST_F(IntlDateTimeFormatTest, IntlDateTimeFormatFuncTest002, TestSize.Level1)
103 {
104     std::vector<std::string> localeTags = { "zh-Hans-CN" };
105     std::unordered_map<std::string, std::string> configs;
106     std::string errMessage;
107     std::unique_ptr<IntlDateTimeFormat> intlDateTimeFormat = std::make_unique<IntlDateTimeFormat>(localeTags,
108         configs, errMessage);
109     std::unordered_map<std::string, std::string> resolvedConfigs;
110     intlDateTimeFormat->ResolvedOptions(resolvedConfigs);
111     EXPECT_EQ(resolvedConfigs[IntlDateTimeFormat::LOCALE_TAG], "zh-Hans-CN");
112     EXPECT_EQ(resolvedConfigs[IntlDateTimeFormat::NUMBERING_SYSTEM_TAG], "latn");
113     EXPECT_EQ(resolvedConfigs[IntlDateTimeFormat::HOUR_CYCLE_TAG], "");
114     EXPECT_EQ(resolvedConfigs[IntlDateTimeFormat::HOUR12_TAG], "");
115     EXPECT_EQ(resolvedConfigs[IntlDateTimeFormat::CALENDAR_TAG], "gregory");
116     EXPECT_EQ(resolvedConfigs[IntlDateTimeFormat::TIME_ZONE_TAG], "America/Chicago");
117     EXPECT_EQ(resolvedConfigs[IntlDateTimeFormat::DATE_STYLE_TAG], "");
118     EXPECT_EQ(resolvedConfigs[IntlDateTimeFormat::TIME_STYLE_TAG], "");
119     EXPECT_EQ(resolvedConfigs[IntlDateTimeFormat::FRACTIONAL_SECOND_DIGITS_TAG], "");
120 
121     I18nErrorCode status = I18nErrorCode::SUCCESS;
122     std::map<std::string, std::string> supportedConfigs;
123     std::vector<std::string> supportedLocales =
124         IntlDateTimeFormat::SupportedLocalesOf(localeTags, supportedConfigs, status);
125     EXPECT_EQ(supportedLocales.size(), 1);
126     EXPECT_EQ(supportedLocales[0], "zh-Hans-CN");
127     EXPECT_EQ(status, I18nErrorCode::SUCCESS);
128 }
129 
130 /**
131  * @tc.name: IntlDateTimeFormatFuncTest003
132  * @tc.desc: Test Intl date time format
133  * @tc.type: FUNC
134  */
135 HWTEST_F(IntlDateTimeFormatTest, IntlDateTimeFormatFuncTest003, TestSize.Level1)
136 {
137     std::vector<std::string> localeTags = { "zh-Hans-CN" };
138     std::unordered_map<std::string, std::string> configs = {
139         { IntlDateTimeFormat::LOCALE_MATCHER_TAG, "lookup" },
140         { IntlDateTimeFormat::WEEK_DAY_TAG, "short" },
141         { IntlDateTimeFormat::ERA_TAG, "narrow" },
142         { IntlDateTimeFormat::YEAR_TAG, "2-digit" },
143         { IntlDateTimeFormat::MONTH_TAG, "2-digit" },
144         { IntlDateTimeFormat::DAY_TAG, "2-digit" },
145         { IntlDateTimeFormat::HOUR_TAG, "2-digit" },
146         { IntlDateTimeFormat::MINUTE_TAG, "2-digit" },
147         { IntlDateTimeFormat::SECOND_TAG, "2-digit" },
148         { IntlDateTimeFormat::TIME_ZONE_NAME_TAG, "short" },
149         { IntlDateTimeFormat::FORMAT_MATCHER_TAG, "basic" },
150         { IntlDateTimeFormat::HOUR12_TAG, "false" },
151         { IntlDateTimeFormat::TIME_ZONE_TAG, "Asia/Shanghai" },
152         { IntlDateTimeFormat::DAY_PERIOD_TAG, "narrow" },
153         { IntlDateTimeFormat::NUMBERING_SYSTEM_TAG, "latn" }
154     };
155     std::string errMessage;
156     std::unique_ptr<IntlDateTimeFormat> intlDateTimeFormat = std::make_unique<IntlDateTimeFormat>(localeTags,
157         configs, errMessage);
158     double startMilliseconds = 987654321;
159     double endMilliseconds = 1987987654;
160 
161     std::string formatResult = intlDateTimeFormat->Format(startMilliseconds);
162     EXPECT_EQ(formatResult, "公元70年1月12日周一 GMT+8 18:20:54");
163 
164     std::string formatRangeResult = intlDateTimeFormat->FormatRange(startMilliseconds, endMilliseconds, errMessage);
165     EXPECT_EQ(formatRangeResult, "公元70年1月12日周一 GMT+8 18:20:54 – 公元70年1月24日周六 GMT+8 08:13:07");
166     std::unordered_map<std::string, std::string> resolvedConfigs;
167     intlDateTimeFormat->ResolvedOptions(resolvedConfigs);
168     EXPECT_EQ(resolvedConfigs[IntlDateTimeFormat::LOCALE_TAG], "zh-Hans-CN-u-nu-latn");
169     EXPECT_EQ(resolvedConfigs[IntlDateTimeFormat::NUMBERING_SYSTEM_TAG], "latn");
170     EXPECT_EQ(resolvedConfigs[IntlDateTimeFormat::HOUR_CYCLE_TAG], "h24");
171     EXPECT_EQ(resolvedConfigs[IntlDateTimeFormat::HOUR12_TAG], "false");
172     EXPECT_EQ(resolvedConfigs[IntlDateTimeFormat::CALENDAR_TAG], "gregory");
173     EXPECT_EQ(resolvedConfigs[IntlDateTimeFormat::TIME_ZONE_TAG], "Asia/Shanghai");
174     EXPECT_EQ(resolvedConfigs[IntlDateTimeFormat::DATE_STYLE_TAG], "");
175     EXPECT_EQ(resolvedConfigs[IntlDateTimeFormat::TIME_STYLE_TAG], "");
176     EXPECT_EQ(resolvedConfigs[IntlDateTimeFormat::FRACTIONAL_SECOND_DIGITS_TAG], "");
177 }
178 
179 /**
180  * @tc.name: IntlDateTimeFormatFuncTest004
181  * @tc.desc: Test Intl date time format
182  * @tc.type: FUNC
183  */
184 HWTEST_F(IntlDateTimeFormatTest, IntlDateTimeFormatFuncTest004, TestSize.Level1)
185 {
186     std::vector<std::string> fakeLocaleTags = { "test_locale" };
187     std::unordered_map<std::string, std::string> configs;
188     std::string errMessage;
189     std::unique_ptr<IntlDateTimeFormat> intlDateTimeFormat = std::make_unique<IntlDateTimeFormat>(fakeLocaleTags,
190         configs, errMessage);
191     EXPECT_EQ(errMessage, "invalid locale");
192 
193     std::vector<std::string> weekDayLocale = { "zh-Hans-CN" };
194     std::unordered_map<std::string, std::string> weekDayConfigs = {
195         { IntlDateTimeFormat::WEEK_DAY_TAG, "faker" }
196     };
197     std::unique_ptr<IntlDateTimeFormat> weekDayFormatter = std::make_unique<IntlDateTimeFormat>(weekDayLocale,
198         weekDayConfigs, errMessage);
199     EXPECT_EQ(errMessage, "Value out of range for locale options property");
200 }
201 
202 /**
203  * @tc.name: IntlDateTimeFormatFuncTest005
204  * @tc.desc: Test Intl date time format
205  * @tc.type: FUNC
206  */
207 HWTEST_F(IntlDateTimeFormatTest, IntlDateTimeFormatFuncTest005, TestSize.Level1)
208 {
209     std::string errMessage;
210     std::vector<std::string> timeZoneLocale = { "zh-Hans-CN" };
211     std::unordered_map<std::string, std::string> timeZoneConfigs = {
212         { IntlDateTimeFormat::TIME_ZONE_TAG, "timezone" }
213     };
214     std::unique_ptr<IntlDateTimeFormat> timeZoneFormatter =
215         std::make_unique<IntlDateTimeFormat>(timeZoneLocale, timeZoneConfigs, errMessage);
216     EXPECT_EQ(errMessage, "invalid timeZone");
217 
218     std::vector<std::string> timeStyleLocale = { "zh-Hans-CN" };
219     std::unordered_map<std::string, std::string> timeStyleConfigs = {
220         { IntlDateTimeFormat::TIME_STYLE_TAG, "test" }
221     };
222     std::unique_ptr<IntlDateTimeFormat> timeStyleFormatter =
223         std::make_unique<IntlDateTimeFormat>(timeStyleLocale, timeStyleConfigs, errMessage);
224     EXPECT_EQ(errMessage, "Value out of range for locale options property");
225 }
226 } // namespace I18n
227 } // namespace Global
228 } // namespace OHOS