• 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" };
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 };
73     string countries[] = { "CN", "US" };
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 
102 /**
103  * @tc.name: ZoneUtilFuncTest004
104  * @tc.desc: Test ZoneUtil GetDefaultZoneList for GB
105  * @tc.type: FUNC
106  */
107 HWTEST_F(ZoneUtilTest,  ZoneUtilFuncTest004, TestSize.Level1)
108 {
109     vector<string> expects = { "Europe/London" };
110     string country = "GB";
111     vector<string> out;
112     ZoneUtil util;
113     util.GetZoneList(country, out);
114     EXPECT_EQ(expects.size(), out.size());
115     if (expects.size() == out.size()) {
116         for (decltype(expects.size()) i = 0; i < expects.size(); ++i) {
117             EXPECT_EQ(expects[i], out[i]);
118         }
119     }
120 }
121 
122 /**
123  * @tc.name: ZoneUtilFuncTest003
124  * @tc.desc: Test ZoneUtil GetDefaultZoneList for de
125  * @tc.type: FUNC
126  */
127 HWTEST_F(ZoneUtilTest,  ZoneUtilFuncTest005, TestSize.Level1)
128 {
129     vector<string> expects = { "Europe/Berlin", "Europe/Busingen" };
130     string country = "DE";
131     vector<string> out;
132     ZoneUtil util;
133     util.GetZoneList(country, out);
134     EXPECT_EQ(expects.size(), out.size());
135     if (expects.size() == out.size()) {
136         for (decltype(expects.size()) i = 0; i < expects.size(); ++i) {
137             EXPECT_EQ(expects[i], out[i]);
138         }
139     }
140 }
141 
142 /**
143  * @tc.name: ZoneUtilFuncTest006
144  * @tc.desc: Test ZoneUtil GetDefaultZoneList for CN with offset 8 hours
145  * @tc.type: FUNC
146  */
147 HWTEST_F(ZoneUtilTest,  ZoneUtilFuncTest006, TestSize.Level1)
148 {
149     vector<string> expects = { "Asia/Shanghai" };
150     string country = "CN";
151     vector<string> out;
152     ZoneUtil util;
153     util.GetZoneList(country, 3600 * 1000 * 8, out);
154     EXPECT_EQ(expects.size(), out.size());
155     if (expects.size() == out.size()) {
156         for (decltype(expects.size()) i = 0; i < expects.size(); ++i) {
157             EXPECT_EQ(expects[i], out[i]);
158         }
159     }
160 }
161 
162 /**
163  * @tc.name: ZoneUtilFuncTest007
164  * @tc.desc: Test ZoneUtil GetDefaultZoneList for 86 with offset 8 hours
165  * @tc.type: FUNC
166  */
167 HWTEST_F(ZoneUtilTest, ZoneUtilFuncTest007, TestSize.Level1)
168 {
169     vector<string> expects = { "Asia/Shanghai" };
170     int32_t number = 86;
171     ZoneUtil util;
172     string out = util.GetDefaultZone(number);
173     EXPECT_EQ(expects[0], out);
174 }
175 
176 /**
177  * @tc.name: ZoneUtilFuncTest008
178  * @tc.desc: Test ZoneUtil GetDefaultZoneList for 86 with offset 8 hours
179  * @tc.type: FUNC
180  */
181 HWTEST_F(ZoneUtilTest, ZoneUtilFuncTest008, TestSize.Level1)
182 {
183     vector<string> expects = { "Asia/Shanghai" };
184     int32_t number = 86;
185     ZoneUtil util;
186     string out = util.GetDefaultZone(number, 8 * 3600 * 1000);
187     EXPECT_EQ(expects[0], out);
188 }
189 } // namespace I18n
190 } // namespace Global
191 } // namespace OHOS