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