1 /*
2 * Copyright (c) 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 "country_code_manager_test.h"
17
18 #include "country_code_callback_host.h"
19 #include "location_log.h"
20
21 #include "nmea_message_callback_host.h"
22
23 using namespace testing::ext;
24 namespace OHOS {
25 namespace Location {
SetUp()26 void CountryCodeManagerTest::SetUp()
27 {
28 }
29
TearDown()30 void CountryCodeManagerTest::TearDown()
31 {
32 }
33
34 HWTEST_F(CountryCodeManagerTest, GetIsoCountryCode001, TestSize.Level1)
35 {
36 GTEST_LOG_(INFO)
37 << "CountryCodeManagerTest, GetIsoCountryCode001, TestSize.Level1";
38 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] GetIsoCountryCode001 begin");
39 std::shared_ptr<CountryCodeManager> countryCodeManager =
40 std::make_shared<CountryCodeManager>();
41 EXPECT_NE(nullptr, countryCodeManager->GetIsoCountryCode());
42 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] GetIsoCountryCode001 end");
43 }
44
45 HWTEST_F(CountryCodeManagerTest, UnregisterCountryCodeCallback001, TestSize.Level1)
46 {
47 GTEST_LOG_(INFO)
48 << "CountryCodeManagerTest, UnregisterCountryCodeCallback001, TestSize.Level1";
49 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] UnregisterCountryCodeCallback001 begin");
50 std::shared_ptr<CountryCodeManager> countryCodeManager =
51 std::make_shared<CountryCodeManager>();
52 auto callback = sptr<CountryCodeCallbackHost>(new (std::nothrow) CountryCodeCallbackHost());
53 ASSERT_TRUE(countryCodeManager != nullptr);
54 countryCodeManager->UnregisterCountryCodeCallback(callback);
55 EXPECT_EQ(0, countryCodeManager->countryCodeCallback_->size());
56 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] UnregisterCountryCodeCallback001 end");
57 }
58
59 HWTEST_F(CountryCodeManagerTest, UnregisterCountryCodeCallback002, TestSize.Level1)
60 {
61 GTEST_LOG_(INFO)
62 << "CountryCodeManagerTest, UnregisterCountryCodeCallback002, TestSize.Level1";
63 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] UnregisterCountryCodeCallback002 begin");
64 std::shared_ptr<CountryCodeManager> countryCodeManager =
65 std::make_shared<CountryCodeManager>();
66 ASSERT_TRUE(countryCodeManager != nullptr);
67 countryCodeManager->UnregisterCountryCodeCallback(nullptr);
68 EXPECT_EQ(0, countryCodeManager->countryCodeCallback_->size());
69 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] UnregisterCountryCodeCallback002 end");
70 }
71
72 HWTEST_F(CountryCodeManagerTest, UnregisterCountryCodeCallback003, TestSize.Level1)
73 {
74 GTEST_LOG_(INFO)
75 << "CountryCodeManagerTest, UnregisterCountryCodeCallback003, TestSize.Level1";
76 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] UnregisterCountryCodeCallback003 begin");
77 std::shared_ptr<CountryCodeManager> countryCodeManager =
78 std::make_shared<CountryCodeManager>();
79 ASSERT_TRUE(countryCodeManager != nullptr);
80 auto wrongCallback = sptr<NmeaMessageCallbackHost>(new (std::nothrow) NmeaMessageCallbackHost());
81 countryCodeManager->UnregisterCountryCodeCallback(wrongCallback->AsObject());
82 EXPECT_EQ(0, countryCodeManager->countryCodeCallback_->size());
83 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] UnregisterCountryCodeCallback003 end");
84 }
85
86 HWTEST_F(CountryCodeManagerTest, UnregisterCountryCodeCallback004, TestSize.Level1)
87 {
88 GTEST_LOG_(INFO)
89 << "CountryCodeManagerTest, UnregisterCountryCodeCallback004, TestSize.Level1";
90 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] UnregisterCountryCodeCallback004 begin");
91 std::shared_ptr<CountryCodeManager> countryCodeManager =
92 std::make_shared<CountryCodeManager>();
93 ASSERT_TRUE(countryCodeManager != nullptr);
94 auto callback1 = sptr<CountryCodeCallbackHost>(new (std::nothrow) CountryCodeCallbackHost());
95 countryCodeManager->RegisterCountryCodeCallback(callback1, 0);
96
97 auto callback2 = sptr<CountryCodeCallbackHost>(new (std::nothrow) CountryCodeCallbackHost());
98 countryCodeManager->RegisterCountryCodeCallback(callback2, 1);
99
100
101 countryCodeManager->UnregisterCountryCodeCallback(callback1); // size != 0, func will return
102 EXPECT_EQ(1, countryCodeManager->countryCodeCallback_->size());
103 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] UnregisterCountryCodeCallback004 end");
104 }
105
106 HWTEST_F(CountryCodeManagerTest, RegisterCountryCodeCallback001, TestSize.Level1)
107 {
108 GTEST_LOG_(INFO)
109 << "CountryCodeManagerTest, RegisterCountryCodeCallback001, TestSize.Level1";
110 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] RegisterCountryCodeCallback001 begin");
111 std::shared_ptr<CountryCodeManager> countryCodeManager =
112 std::make_shared<CountryCodeManager>();
113 auto callback = sptr<CountryCodeCallbackHost>(new (std::nothrow) CountryCodeCallbackHost());
114 countryCodeManager->RegisterCountryCodeCallback(callback, 0);
115 EXPECT_NE(0, countryCodeManager->countryCodeCallback_->size());
116 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] RegisterCountryCodeCallback001 end");
117 }
118
119 HWTEST_F(CountryCodeManagerTest, RegisterCountryCodeCallback002, TestSize.Level1)
120 {
121 GTEST_LOG_(INFO)
122 << "CountryCodeManagerTest, RegisterCountryCodeCallback002, TestSize.Level1";
123 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] RegisterCountryCodeCallback002 begin");
124 std::shared_ptr<CountryCodeManager> countryCodeManager =
125 std::make_shared<CountryCodeManager>();
126 ASSERT_TRUE(countryCodeManager != nullptr);
127 countryCodeManager->RegisterCountryCodeCallback(nullptr, 0);
128 EXPECT_EQ(0, countryCodeManager->countryCodeCallback_->size());
129 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] RegisterCountryCodeCallback002 end");
130 }
131
132 HWTEST_F(CountryCodeManagerTest, RegisterCountryCodeCallback003, TestSize.Level1)
133 {
134 GTEST_LOG_(INFO)
135 << "CountryCodeManagerTest, RegisterCountryCodeCallback003, TestSize.Level1";
136 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] RegisterCountryCodeCallback003 begin");
137 std::shared_ptr<CountryCodeManager> countryCodeManager =
138 std::make_shared<CountryCodeManager>();
139 ASSERT_TRUE(countryCodeManager != nullptr);
140 auto wrongCallback = sptr<NmeaMessageCallbackHost>(new (std::nothrow) NmeaMessageCallbackHost());
141 countryCodeManager->RegisterCountryCodeCallback(wrongCallback->AsObject(), 0);
142 EXPECT_EQ(0, countryCodeManager->countryCodeCallback_->size());
143 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] RegisterCountryCodeCallback003 end");
144 }
145
146 HWTEST_F(CountryCodeManagerTest, ReSubscribeEvent001, TestSize.Level1)
147 {
148 GTEST_LOG_(INFO)
149 << "CountryCodeManagerTest, ReSubscribeEvent001, TestSize.Level1";
150 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] ReSubscribeEvent001 begin");
151 std::shared_ptr<CountryCodeManager> countryCodeManager =
152 std::make_shared<CountryCodeManager>();
153 EXPECT_EQ(0, countryCodeManager->countryCodeCallback_->size());
154 countryCodeManager->ReSubscribeEvent();
155 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] ReSubscribeEvent001 end");
156 }
157
158 HWTEST_F(CountryCodeManagerTest, ReSubscribeEvent002, TestSize.Level1)
159 {
160 GTEST_LOG_(INFO)
161 << "CountryCodeManagerTest, ReSubscribeEvent002, TestSize.Level1";
162 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] ReSubscribeEvent002 begin");
163 std::shared_ptr<CountryCodeManager> countryCodeManager =
164 std::make_shared<CountryCodeManager>();
165 auto callback = sptr<CountryCodeCallbackHost>(new (std::nothrow) CountryCodeCallbackHost());
166 countryCodeManager->RegisterCountryCodeCallback(callback, 0);
167 EXPECT_NE(0, countryCodeManager->countryCodeCallback_->size());
168 countryCodeManager->ReSubscribeEvent();
169 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] ReSubscribeEvent002 end");
170 }
171
172 HWTEST_F(CountryCodeManagerTest, ReUnsubscribeEvent001, TestSize.Level1)
173 {
174 GTEST_LOG_(INFO)
175 << "CountryCodeManagerTest, ReUnsubscribeEvent001, TestSize.Level1";
176 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] ReUnsubscribeEvent001 begin");
177 std::shared_ptr<CountryCodeManager> countryCodeManager =
178 std::make_shared<CountryCodeManager>();
179 EXPECT_EQ(0, countryCodeManager->countryCodeCallback_->size());
180 countryCodeManager->ReUnsubscribeEvent();
181 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] ReUnsubscribeEvent001 end");
182 }
183
184 HWTEST_F(CountryCodeManagerTest, ReUnsubscribeEvent002, TestSize.Level1)
185 {
186 GTEST_LOG_(INFO)
187 << "CountryCodeManagerTest, ReUnsubscribeEvent002, TestSize.Level1";
188 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] ReUnsubscribeEvent002 begin");
189 std::shared_ptr<CountryCodeManager> countryCodeManager =
190 std::make_shared<CountryCodeManager>();
191 auto callback = sptr<CountryCodeCallbackHost>(new (std::nothrow) CountryCodeCallbackHost());
192 countryCodeManager->RegisterCountryCodeCallback(callback, 0);
193 EXPECT_NE(0, countryCodeManager->countryCodeCallback_->size());
194 countryCodeManager->ReUnsubscribeEvent();
195 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] ReUnsubscribeEvent002 end");
196 }
197
198 HWTEST_F(CountryCodeManagerTest, NotifyAllListener001, TestSize.Level1)
199 {
200 GTEST_LOG_(INFO)
201 << "CountryCodeManagerTest, NotifyAllListener001, TestSize.Level1";
202 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] NotifyAllListener001 begin");
203 std::shared_ptr<CountryCodeManager> countryCodeManager =
204 std::make_shared<CountryCodeManager>();
205 ASSERT_TRUE(countryCodeManager != nullptr);
206 auto callback = sptr<CountryCodeCallbackHost>(new (std::nothrow) CountryCodeCallbackHost());
207 sptr<ICountryCodeCallback> countryCodeCallback1 = iface_cast<ICountryCodeCallback>(callback);
208 countryCodeManager->countryCodeCallback_->insert(std::make_pair(0, countryCodeCallback1));
209 sptr<ICountryCodeCallback> countryCodeCallback2 = nullptr;
210 countryCodeManager->countryCodeCallback_->insert(std::make_pair(1, countryCodeCallback2));
211 countryCodeManager->NotifyAllListener();
212 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] NotifyAllListener001 end");
213 }
214
215 HWTEST_F(CountryCodeManagerTest, GetCountryCodeByLastLocation001, TestSize.Level1)
216 {
217 GTEST_LOG_(INFO)
218 << "CountryCodeManagerTest, GetCountryCodeByLastLocation001, TestSize.Level1";
219 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] GetCountryCodeByLastLocation001 begin");
220 std::shared_ptr<CountryCodeManager> countryCodeManager =
221 std::make_shared<CountryCodeManager>();
222 countryCodeManager->lastCountryByLocation_ = nullptr;
223 EXPECT_EQ("", countryCodeManager->GetCountryCodeByLastLocation());
224 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] GetCountryCodeByLastLocation001 end");
225 }
226
227 HWTEST_F(CountryCodeManagerTest, UpdateCountryCode001, TestSize.Level1)
228 {
229 GTEST_LOG_(INFO)
230 << "CountryCodeManagerTest, UpdateCountryCode001, TestSize.Level1";
231 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] UpdateCountryCode001 begin");
232 std::shared_ptr<CountryCodeManager> countryCodeManager =
233 std::make_shared<CountryCodeManager>();
234 ASSERT_TRUE(countryCodeManager != nullptr);
235 countryCodeManager->lastCountry_->SetCountryCodeType(1);
236 countryCodeManager->UpdateCountryCode("zh", 0); // last type is more reliable
237
238 countryCodeManager->lastCountry_ = nullptr;
239 countryCodeManager->UpdateCountryCode("zh", 0); // lastCountry_ is nullptr
240 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] UpdateCountryCode001 end");
241 }
242
243 HWTEST_F(CountryCodeManagerTest, UpdateCountryCodeByLocation001, TestSize.Level1)
244 {
245 GTEST_LOG_(INFO)
246 << "CountryCodeManagerTest, UpdateCountryCodeByLocation001, TestSize.Level1";
247 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] UpdateCountryCodeByLocation001 begin");
248 std::shared_ptr<CountryCodeManager> countryCodeManager =
249 std::make_shared<CountryCodeManager>();
250 countryCodeManager->lastCountryByLocation_->SetCountryCodeStr("zh");
251 EXPECT_EQ(false, countryCodeManager->UpdateCountryCodeByLocation("zh", 1));
252
253 countryCodeManager->lastCountryByLocation_->SetCountryCodeStr("us");
254 EXPECT_EQ(true, countryCodeManager->UpdateCountryCodeByLocation("zh", 1));
255
256 countryCodeManager->lastCountryByLocation_ = nullptr;
257 EXPECT_EQ(false, countryCodeManager->UpdateCountryCodeByLocation("zh", 1));
258 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] UpdateCountryCodeByLocation001 end");
259 }
260
261 HWTEST_F(CountryCodeManagerTest, GetCountryCodeByLocation001, TestSize.Level1)
262 {
263 GTEST_LOG_(INFO)
264 << "CountryCodeManagerTest, GetCountryCodeByLocation001, TestSize.Level1";
265 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] GetCountryCodeByLocation001 begin");
266 std::shared_ptr<CountryCodeManager> countryCodeManager =
267 std::make_shared<CountryCodeManager>();
268 ASSERT_TRUE(countryCodeManager != nullptr);
269 EXPECT_EQ("", countryCodeManager->GetCountryCodeByLocation(nullptr));
270 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] GetCountryCodeByLocation001 end");
271 }
272
273 HWTEST_F(CountryCodeManagerTest, OnLocationReport001, TestSize.Level1)
274 {
275 GTEST_LOG_(INFO)
276 << "CountryCodeManagerTest, OnLocationReport001, TestSize.Level1";
277 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] OnLocationReport001 begin");
278 std::shared_ptr<CountryCodeManager> countryCodeManager =
279 std::make_shared<CountryCodeManager>();
280 ASSERT_TRUE(countryCodeManager != nullptr);
281 sptr<ILocatorCallback> callback =
282 sptr<ILocatorCallback>(new (std::nothrow) CountryCodeManager::LocatorCallback());
283 ASSERT_TRUE(callback != nullptr);
284 callback->OnLocationReport(nullptr);
285
286 std::unique_ptr<Location> location = std::make_unique<Location>();
287 callback->OnLocationReport(location);
288 callback->OnLocatingStatusChange(1);
289 callback->OnErrorReport(1);
290
291 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] OnLocationReport001 end");
292 }
293
294 HWTEST_F(CountryCodeManagerTest, NetworkSubscriber001, TestSize.Level1)
295 {
296 GTEST_LOG_(INFO)
297 << "CountryCodeManagerTest, NetworkSubscriber001, TestSize.Level1";
298 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] NetworkSubscriber001 begin");
299 std::shared_ptr<CountryCodeManager> countryCodeManager =
300 std::make_shared<CountryCodeManager>();
301 ASSERT_TRUE(countryCodeManager != nullptr);
302 OHOS::EventFwk::MatchingSkills matchingSkills;
303 OHOS::EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
304 auto networkSubscriber = std::make_shared<CountryCodeManager::NetworkSubscriber>(subscriberInfo);
305 OHOS::EventFwk::CommonEventData event;
306 networkSubscriber->OnReceiveEvent(event);
307 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] NetworkSubscriber001 end");
308 }
309
310 HWTEST_F(CountryCodeManagerTest, SimSubscriber001, TestSize.Level1)
311 {
312 GTEST_LOG_(INFO)
313 << "CountryCodeManagerTest, SimSubscriber001, TestSize.Level1";
314 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] SimSubscriber001 begin");
315 std::shared_ptr<CountryCodeManager> countryCodeManager =
316 std::make_shared<CountryCodeManager>();
317 ASSERT_TRUE(countryCodeManager != nullptr);
318 OHOS::EventFwk::MatchingSkills matchingSkills;
319 OHOS::EventFwk::CommonEventSubscribeInfo subscriberInfo(matchingSkills);
320 auto simSubscriber = std::make_shared<CountryCodeManager::SimSubscriber>(subscriberInfo);
321 OHOS::EventFwk::CommonEventData event;
322 simSubscriber->OnReceiveEvent(event);
323 LBSLOGI(COUNTRY_CODE, "[CountryCodeManagerTest] SimSubscriber001 end");
324 }
325 } // namespace Location
326 } // namespace OHOS