• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-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 "zone_util.h"
18 
19 using namespace OHOS::Global::I18n;
20 using testing::ext::TestSize;
21 using namespace std;
22 
23 namespace OHOS {
24 namespace Global {
25 namespace I18n {
26 class ZoneUtilTest : public testing::Test {
27 public:
28     static void SetUpTestCase(void);
29     static void TearDownTestCase(void);
30     void SetUp();
31     void TearDown();
32 };
33 
SetUpTestCase(void)34 void ZoneUtilTest::SetUpTestCase(void)
35 {}
36 
TearDownTestCase(void)37 void ZoneUtilTest::TearDownTestCase(void)
38 {}
39 
SetUp(void)40 void ZoneUtilTest::SetUp(void)
41 {
42 }
43 
TearDown(void)44 void ZoneUtilTest::TearDown(void)
45 {}
46 
47 /**
48  * @tc.name: ZoneUtilFuncTest001
49  * @tc.desc: Test ZoneUtil GetDefaultZone
50  * @tc.type: FUNC
51  */
52 HWTEST_F(ZoneUtilTest,  ZoneUtilFuncTest001, TestSize.Level1)
53 {
54     string expects[] = { "Asia/Shanghai", "America/New_York", "Asia/Shanghai", "America/New_York", "", "" };
55     string countries[] = { "CN", "US", "cn", "us", "fake", "GB" };
56     ZoneUtil util;
57     int count = 4;
58     for (int i = 0; i < count; ++i) {
59         string out = util.GetDefaultZone(countries[i].c_str());
60         EXPECT_EQ(out, expects[i]);
61     }
62 }
63 
64 /**
65  * @tc.name: ZoneUtilFuncTest002
66  * @tc.desc: Test ZoneUtil GetDefaultZone with offset
67  * @tc.type: FUNC
68  */
69 HWTEST_F(ZoneUtilTest,  ZoneUtilFuncTest002, TestSize.Level1)
70 {
71     string expects[] = { "Asia/Shanghai", "America/Detroit", "" };
72     int32_t offsets[] = { 3600 * 1000 * 8, -3600 * 1000 * 5, 1234 * 789 * 7 };
73     string countries[] = { "CN", "US", "fake" };
74     int count = 2;
75     ZoneUtil util;
76     for (int i = 0; i < count; ++i) {
77         string out = util.GetDefaultZone(countries[i].c_str(), offsets[i]);
78         EXPECT_EQ(out, expects[i]);
79     }
80 }
81 
82 /**
83  * @tc.name: ZoneUtilFuncTest003
84  * @tc.desc: Test ZoneUtil GetDefaultZoneList for CN
85  * @tc.type: FUNC
86  */
87 HWTEST_F(ZoneUtilTest,  ZoneUtilFuncTest003, TestSize.Level1)
88 {
89     vector<string> expects = { "Asia/Shanghai", "Asia/Urumqi"};
90     string country = "CN";
91     vector<string> out;
92     ZoneUtil util;
93     util.GetZoneList(country, out);
94     EXPECT_EQ(expects.size(), out.size());
95     if (expects.size() == out.size()) {
96         for (decltype(expects.size()) i = 0; i < expects.size(); ++i) {
97             EXPECT_EQ(expects[i], out[i]);
98         }
99     }
100 
101     country = "fake";
102     out.clear();
103     util.GetZoneList(country, out);
104     EXPECT_EQ(out.size(), 0);
105 }
106 
107 /**
108  * @tc.name: ZoneUtilFuncTest004
109  * @tc.desc: Test ZoneUtil GetDefaultZoneList for GB
110  * @tc.type: FUNC
111  */
112 HWTEST_F(ZoneUtilTest,  ZoneUtilFuncTest004, TestSize.Level1)
113 {
114     vector<string> expects = { "Europe/London" };
115     string country = "GB";
116     vector<string> out;
117     ZoneUtil util;
118     util.GetZoneList(country, out);
119     EXPECT_EQ(expects.size(), out.size());
120     if (expects.size() == out.size()) {
121         for (decltype(expects.size()) i = 0; i < expects.size(); ++i) {
122             EXPECT_EQ(expects[i], out[i]);
123         }
124     }
125 }
126 
127 /**
128  * @tc.name: ZoneUtilFuncTest003
129  * @tc.desc: Test ZoneUtil GetDefaultZoneList for de
130  * @tc.type: FUNC
131  */
132 HWTEST_F(ZoneUtilTest,  ZoneUtilFuncTest005, TestSize.Level1)
133 {
134     vector<string> expects = { "Europe/Berlin", "Europe/Busingen" };
135     string country = "DE";
136     vector<string> out;
137     ZoneUtil util;
138     util.GetZoneList(country, out);
139     EXPECT_EQ(expects.size(), out.size());
140     if (expects.size() == out.size()) {
141         for (decltype(expects.size()) i = 0; i < expects.size(); ++i) {
142             EXPECT_EQ(expects[i], out[i]);
143         }
144     }
145 }
146 
147 /**
148  * @tc.name: ZoneUtilFuncTest006
149  * @tc.desc: Test ZoneUtil GetDefaultZoneList for CN with offset 8 hours
150  * @tc.type: FUNC
151  */
152 HWTEST_F(ZoneUtilTest,  ZoneUtilFuncTest006, TestSize.Level1)
153 {
154     vector<string> expects = { "Asia/Shanghai" };
155     string country = "CN";
156     vector<string> out;
157     ZoneUtil util;
158     util.GetZoneList(country, 3600 * 1000 * 8, out);
159     EXPECT_EQ(expects.size(), out.size());
160     if (expects.size() == out.size()) {
161         for (decltype(expects.size()) i = 0; i < expects.size(); ++i) {
162             EXPECT_EQ(expects[i], out[i]);
163         }
164     }
165 
166     country = "fake";
167     out.clear();
168     util.GetZoneList(country, 1234 * 789 * 9, out);
169     EXPECT_EQ(out.size(), 0);
170 }
171 
172 /**
173  * @tc.name: ZoneUtilFuncTest007
174  * @tc.desc: Test ZoneUtil GetDefaultZoneList for 86 with offset 8 hours
175  * @tc.type: FUNC
176  */
177 HWTEST_F(ZoneUtilTest, ZoneUtilFuncTest007, TestSize.Level1)
178 {
179     vector<string> expects = { "Asia/Shanghai" };
180     int32_t number = 86;
181     ZoneUtil util;
182     string out = util.GetDefaultZone(number);
183     EXPECT_EQ(expects[0], out);
184 }
185 
186 /**
187  * @tc.name: ZoneUtilFuncTest008
188  * @tc.desc: Test ZoneUtil GetDefaultZoneList for 86 with offset 8 hours
189  * @tc.type: FUNC
190  */
191 HWTEST_F(ZoneUtilTest, ZoneUtilFuncTest008, TestSize.Level1)
192 {
193     vector<string> expects = { "Asia/Shanghai" };
194     int32_t number = 86;
195     ZoneUtil util;
196     string out = util.GetDefaultZone(number, 8 * 3600 * 1000);
197     EXPECT_EQ(expects[0], out);
198 }
199 
200 /**
201  * @tc.name: ZoneUtilFuncTest009
202  * @tc.desc: Test ZoneUtil LookupTimezoneByCountryAndNITZ
203  * @tc.type: FUNC
204  */
205 HWTEST_F(ZoneUtilTest, ZoneUtilFuncTest009, TestSize.Level1)
206 {
207     ZoneUtil util;
208     std::string region = "CN";
209     NITZData nitzData = { 0, 28800000, 1679969021999 };
210     CountryResult result = util.LookupTimezoneByCountryAndNITZ(region, nitzData);
211     EXPECT_TRUE(result.isOnlyMatch);
212     EXPECT_EQ(result.timezoneId, "Asia/Shanghai");
213 
214     NITZData nitzData2 = { 0, 21600000, 1679969021999 };
215     CountryResult result2 = util.LookupTimezoneByCountryAndNITZ(region, nitzData2);
216     EXPECT_TRUE(result2.isOnlyMatch);
217     EXPECT_EQ(result2.timezoneId, "Asia/Urumqi");
218 }
219 
220 /**
221  * @tc.name: ZoneUtilFuncTest010
222  * @tc.desc: Test ZoneUtil LookupTimezoneByNITZ
223  * @tc.type: FUNC
224  */
225 HWTEST_F(ZoneUtilTest, ZoneUtilFuncTest010, TestSize.Level1)
226 {
227     ZoneUtil util;
228     NITZData nitzData = { 0, 28800000, 1679969021999 };
229     CountryResult result = util.LookupTimezoneByNITZ(nitzData);
230     EXPECT_FALSE(result.isOnlyMatch);
231     EXPECT_EQ(result.timezoneId, "Asia/Shanghai");
232 
233     NITZData nitzData2 = { 1, 10800000, 1679969021999 };
234     CountryResult result2 = util.LookupTimezoneByNITZ(nitzData2);
235     EXPECT_FALSE(result2.isOnlyMatch);
236     EXPECT_EQ(result2.timezoneId, "Asia/Beirut");
237 
238     NITZData nitzData3 = { 1, 37800000, 1679969021999 };
239     CountryResult result3 = util.LookupTimezoneByNITZ(nitzData3);
240     EXPECT_FALSE(result3.isOnlyMatch);
241     EXPECT_EQ(result3.timezoneId, "Australia/Adelaide");
242 
243     NITZData nitzData4 = { 0, 12345678, 1679969021999 };
244     CountryResult result4 = util.LookupTimezoneByNITZ(nitzData4);
245     EXPECT_TRUE(result4.isOnlyMatch);
246     EXPECT_EQ(result4.timezoneId, "");
247 
248     NITZData nitzData5 = { -1, 12345678, 1679969021999 };
249     CountryResult result5 = util.LookupTimezoneByNITZ(nitzData5);
250     EXPECT_TRUE(result5.isOnlyMatch);
251     EXPECT_EQ(result5.timezoneId, "");
252 }
253 
254 /**
255  * @tc.name: ZoneUtilFuncTest011
256  * @tc.desc: Test ZoneUtil LookupTimezoneByCountry
257  * @tc.type: FUNC
258  */
259 HWTEST_F(ZoneUtilTest, ZoneUtilFuncTest011, TestSize.Level1)
260 {
261     ZoneUtil util;
262     std::string region = "AQ";
263     int64_t currentMillis = 1679969021999;
264     CountryResult result = util.LookupTimezoneByCountry(region, currentMillis);
265     EXPECT_EQ(result.timezoneId, "Antarctica/McMurdo");
266     EXPECT_EQ(result.quality, MatchQuality::MULTIPLE_ZONES_DIFFERENT_OFFSET);
267 
268     region = "fake Region";
269     CountryResult result2 = util.LookupTimezoneByCountry(region, currentMillis);
270     EXPECT_EQ(result2.timezoneId, "");
271     EXPECT_EQ(result2.quality, 3);
272 
273     region = "AR";
274     CountryResult result3 = util.LookupTimezoneByCountry(region, currentMillis);
275     EXPECT_EQ(result3.timezoneId, "America/Argentina/Buenos_Aires");
276 	EXPECT_TRUE(result3.quality == 0 || result3.quality == 2);
277 
278     region = "AM";
279     CountryResult result4 = util.LookupTimezoneByCountry(region, currentMillis);
280     EXPECT_EQ(result4.timezoneId, "Asia/Yerevan");
281     EXPECT_EQ(result4.quality, 1);
282 
283     region = "CY";
284     CountryResult result5 = util.LookupTimezoneByCountry(region, currentMillis);
285     EXPECT_EQ(result5.timezoneId, "Asia/Nicosia");
286     EXPECT_EQ(result5.quality, 2);
287 }
288 } // namespace I18n
289 } // namespace Global
290 } // namespace OHOS