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