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 #define private public 16 #define protected public 17 #include "ability_connect_callback_interface.h" 18 #include "ability_manager_client.h" 19 #include "call_ability_connect_callback.h" 20 #include "call_ability_report_proxy.h" 21 #include "call_connect_ability.h" 22 #include "call_earthquake_alarm_locator.h" 23 #include "call_earthquake_alarm_subscriber.h" 24 #include "call_manager_inner_type.h" 25 #include "call_number_utils.h" 26 #include "call_object_manager.h" 27 #include "common_event_manager.h" 28 #include "common_event_subscribe_info.h" 29 #include "common_event_subscriber.h" 30 #include "common_event_support.h" 31 #include "datashare_helper.h" 32 #include "datashare_predicates.h" 33 #include "gtest/gtest.h" 34 #include "i_locator_callback.h" 35 #include "int_wrapper.h" 36 #include "iremote_stub.h" 37 #include "iservice_registry.h" 38 #include "location_log.h" 39 #include "location.h" 40 #include "locator_impl.h" 41 #include "locator.h" 42 #include "matching_skills.h" 43 #include "os_account_manager_wrapper.h" 44 #include "os_account_manager.h" 45 #include "rdb_errno.h" 46 #include "request_config.h" 47 #include "securec.h" 48 #include "singleton.h" 49 #include "string_wrapper.h" 50 #include "system_ability_definition.h" 51 #include "system_ability_status_change_stub.h" 52 #include "telephony_errors.h" 53 #include "telephony_log_wrapper.h" 54 #include "telephony_permission.h" 55 #include "uri.h" 56 #include "want.h" 57 #include "call_manager_base.h" 58 #include "call_base.h" 59 #include <iostream> 60 #include <memory> 61 #include <mutex> 62 #include <unordered_map> 63 64 namespace OHOS { 65 namespace Telephony { 66 using namespace testing::ext; 67 using namespace std; 68 69 class LocationEngineTest : public testing::Test { 70 public: SetUpTestCase()71 static void SetUpTestCase() {}; TearDownTestCase()72 static void TearDownTestCase() {}; SetUp()73 virtual void SetUp() {}; TearDown()74 virtual void TearDown() {}; 75 }; 76 77 /** 78 * @tc.number Telephony_MyLocationEngine_001 79 * @tc.name test normal branch 80 * @tc.desc Function test 81 */ 82 HWTEST_F(LocationEngineTest, Telephony_MyLocationEngine_001, TestSize.Level0) 83 { 84 auto engine = std::make_shared<MyLocationEngine>(); 85 if (engine == nullptr) { 86 TELEPHONY_LOGI("engine is null. MyLocationEngine"); 87 return; 88 } 89 engine->mylocator = engine; 90 engine = engine->GetInstance(); 91 engine->SetValue(); 92 std::string key1 = "testkey"; 93 std::string value1 = "testvalue"; 94 engine->IsSwitchOn(key1, value1); 95 engine->BootComplete(true); 96 engine->BootComplete(false); 97 engine->OOBEComplete(); 98 engine->RegisterLocationChange(); 99 engine->RegisterSwitchCallback(); 100 engine->LocationSwitchChange(); 101 engine->UnregisterLocationChange(); 102 engine->UnRegisterSwitchCallback(); 103 engine->OnInit(); 104 engine->SetValue(); 105 engine->RegisterLocationChange(); 106 engine->RegisterSwitchCallback(); 107 ASSERT_TRUE(engine->GetInstance() != nullptr); 108 } 109 110 /** 111 * @tc.number Telephony_MyLocationEngine_002 112 * @tc.name test normal branch 113 * @tc.desc Function test 114 */ 115 HWTEST_F(LocationEngineTest, Telephony_MyLocationEngine_002, TestSize.Level0) 116 { 117 auto engine1 = std::make_shared<MyLocationEngine>(); 118 engine1->mylocator = engine1; 119 auto engine = engine1->GetInstance(); 120 engine->SetValue(); 121 engine->RegisterLocationChange(); 122 engine->RegisterSwitchCallback(); 123 if (engine->locatorCallback_ == nullptr) { 124 return; 125 } 126 Parcel parcel; 127 std::unique_ptr<Location::Location> location = Location::Location::UnmarshallingMakeUnique(parcel); 128 engine->locatorCallback_->OnLocationReport(location); 129 uint32_t code = 0; 130 MessageParcel data1; 131 MessageParcel reply; 132 MessageOption option; 133 engine->locatorCallback_->OnRemoteRequest(code, data1, reply, option); 134 MessageParcel data2; 135 data2.WriteInterfaceToken(MyLocationEngine::MyLocationCallBack::GetDescriptor()); 136 engine->locatorCallback_->OnRemoteRequest(code, data2, reply, option); 137 MessageParcel data3; 138 data3.WriteInterfaceToken(MyLocationEngine::MyLocationCallBack::GetDescriptor()); 139 code = Location::ILocatorCallback::RECEIVE_LOCATION_INFO_EVENT; 140 engine->locatorCallback_->OnRemoteRequest(code, data3, reply, option); 141 MessageParcel data4; 142 data4.WriteInterfaceToken(MyLocationEngine::MyLocationCallBack::GetDescriptor()); 143 code = Location::ILocatorCallback::RECEIVE_ERROR_INFO_EVENT; 144 engine->locatorCallback_->OnRemoteRequest(code, data4, reply, option); 145 MessageParcel data5; 146 data5.WriteInterfaceToken(MyLocationEngine::MyLocationCallBack::GetDescriptor()); 147 code = Location::ILocatorCallback::RECEIVE_LOCATION_STATUS_EVENT; 148 engine->locatorCallback_->OnRemoteRequest(code, data5, reply, option); 149 engine->SetValue(); 150 engine->RegisterLocationChange(); 151 engine->RegisterSwitchCallback(); 152 std::string key2 = "testkey2222"; 153 std::string value2 = "valuetst2222"; 154 ASSERT_TRUE(engine->IsSwitchOn(key2, value2) == false); 155 } 156 157 /** 158 * @tc.number Telephony_MyLocationEngine_003 159 * @tc.name test normal branch 160 * @tc.desc Function test 161 */ 162 HWTEST_F(LocationEngineTest, Telephony_MyLocationEngine_003, TestSize.Level0) 163 { 164 auto engine1 = std::make_shared<MyLocationEngine>(); 165 engine1->mylocator = engine1; 166 auto engine = engine1->GetInstance(); 167 engine->SetValue(); 168 engine->RegisterLocationChange(); 169 engine->RegisterSwitchCallback(); 170 if (engine->switchCallback_ == nullptr) { 171 return; 172 } 173 int state = 0; 174 engine->switchCallback_->OnSwitchChange(state); 175 uint32_t code = 0; 176 MessageParcel data1; 177 MessageParcel reply; 178 MessageOption option; 179 engine->switchCallback_->OnRemoteRequest(code, data1, reply, option); 180 MessageParcel data2; 181 data2.WriteInterfaceToken(MyLocationEngine::MySwitchCallback::GetDescriptor()); 182 engine->switchCallback_->OnRemoteRequest(code, data2, reply, option); 183 MessageParcel data3; 184 data3.WriteInterfaceToken(MyLocationEngine::MySwitchCallback::GetDescriptor()); 185 code = Location::ISwitchCallback::RECEIVE_SWITCH_STATE_EVENT; 186 engine->switchCallback_->OnRemoteRequest(code, data3, reply, option); 187 engine->SetValue(); 188 engine->RegisterLocationChange(); 189 engine->RegisterSwitchCallback(); 190 std::string key3 = "keytest3333"; 191 std::string value3 = "valuetest3333"; 192 ASSERT_TRUE(engine->IsSwitchOn(key3, value3) == false); 193 } 194 195 /** 196 * @tc.number Telephony_EmergencyCallConnectCallback_001 197 * @tc.name test normal branch 198 * @tc.desc Function test 199 */ 200 HWTEST_F(LocationEngineTest, Telephony_EmergencyCallConnectCallback_001, TestSize.Level1) 201 { 202 auto engine1 = std::make_shared<MyLocationEngine>(); 203 engine1->mylocator = engine1; 204 auto engine = engine1->GetInstance(); 205 sptr<AAFwk::IAbilityConnection> connectcallback = sptr<EmergencyCallConnectCallback>::MakeSptr(); 206 CallDetailInfo info; 207 AAFwk::Want want; 208 engine->ConnectAbility("valuetest1111", connectcallback, want); 209 engine->StartEccService(nullptr, info); 210 engine->StopEccService(-1); 211 std::string bundle = "111"; 212 std::string ability = "222"; 213 AppExecFwk::ElementName element("", bundle, ability); 214 sptr<IRemoteObject> remoteObject = sptr<EmergencyCallConnectCallback>::MakeSptr(); 215 int resultCode = 0; 216 sptr<AAFwk::IAbilityConnection> connectcallback3 = sptr<EmergencyCallConnectCallback>::MakeSptr(); 217 connectcallback3->OnAbilityConnectDone(element, remoteObject, resultCode); 218 connectcallback3->OnAbilityDisconnectDone(element, resultCode); 219 ASSERT_TRUE(connectcallback3 != nullptr); 220 } 221 222 /** 223 * @tc.number Telephony_LocationSubscriber_001 224 * @tc.name test normal branch 225 * @tc.desc Function test 226 */ 227 HWTEST_F(LocationEngineTest, Telephony_LocationSubscriber_001, TestSize.Level0) 228 { 229 const char* keyword = "switchtest"; 230 std::string value1 = "true"; 231 std::string value2 = "false"; 232 std::string event = "test.test.test"; 233 EventFwk::MatchingSkills matchingSkills; 234 matchingSkills.AddEvent(event); 235 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills); 236 auto subscribe = std::make_shared<LocationSubscriber>(subscribeInfo); 237 subscribe->subscriber_ = subscribe; 238 if (subscribe->subscriber_ == nullptr) { 239 return; 240 } 241 subscribe->Subscriber(); 242 EventFwk::CommonEventData eventData; 243 AAFwk::Want want; 244 want.SetAction(event); 245 want.SetParam(keyword, value1); 246 eventData.SetWant(want); 247 subscribe->subscriber_->OnReceiveEvent(eventData); 248 want.SetParam(keyword, value2); 249 eventData.SetWant(want); 250 subscribe->subscriber_->OnReceiveEvent(eventData); 251 ASSERT_TRUE(subscribe->subscriber_ != nullptr); 252 } 253 254 /** 255 * @tc.number Telephony_DataShareSwitchState_001 256 * @tc.name test normal branch 257 * @tc.desc Function test 258 */ 259 HWTEST_F(LocationEngineTest, Telephony_DataShareSwitchState_001, TestSize.Level0) 260 { 261 auto datashareHelper = std::make_shared<DataShareSwitchState>(); 262 OHOS::Uri uriTest(string("datashare:///com.ohos.settingsdata/entry/settingsdata/SETTINGSDATA?") 263 + string("Proxy=true&key=testKeyWord")); 264 const std::string key = "testKeyWord"; 265 std::string key2 = "testKeyWord2"; 266 std::string value1 = "test"; 267 int code = -1; 268 auto ret = datashareHelper->QueryData(uriTest, key, value1); 269 sptr<AAFwk::IDataAbilityObserver> callback1 = sptr<OOBESwitchObserver>::MakeSptr(key2); 270 callback1->OnChange(); 271 auto datashareHelper2 = std::make_shared<DataShareSwitchState>(); 272 datashareHelper2->RegisterListenSettingsKey(key2, true, callback1); 273 auto datashareHelper3 = std::make_shared<DataShareSwitchState>(); 274 datashareHelper3->RegisterListenSettingsKey(key2, false, callback1); 275 callback1 = nullptr; 276 auto datashareHelper4 = std::make_shared<DataShareSwitchState>(); 277 datashareHelper4->RegisterListenSettingsKey(key2, false, callback1); 278 ASSERT_TRUE(ret == code); 279 ASSERT_TRUE(value1 == "test"); 280 } 281 282 /** 283 * @tc.number Telephony_LocationSystemAbilityListener_001 284 * @tc.name test normal branch 285 * @tc.desc Function test 286 */ 287 HWTEST_F(LocationEngineTest, Telephony_LocationSystemAbilityListener_001, TestSize.Level0) 288 { 289 int32_t ability1301 = OHOS::DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID; 290 int32_t ability2802 = OHOS::LOCATION_LOCATOR_SA_ID; 291 int32_t ability2805 = OHOS::LOCATION_NOPOWER_LOCATING_SA_ID; 292 const std::string deviceId = "11111111111test"; 293 sptr<LocationSystemAbilityListener> locationAbility = sptr<LocationSystemAbilityListener>::MakeSptr(); 294 locationAbility->systemAbilityStatus = {}; 295 locationAbility->GetSystemAbility(ability1301); 296 locationAbility->GetSystemAbility(ability2802); 297 locationAbility->GetSystemAbility(ability2805); 298 locationAbility->statusChangeListener_ = sptr<LocationSystemAbilityListener>::MakeSptr(); 299 locationAbility->SystemAbilitySubscriber(); 300 locationAbility->statusChangeListener_ = nullptr; 301 locationAbility->SystemAbilitySubscriber(); 302 sptr<LocationSystemAbilityListener> callback = sptr<LocationSystemAbilityListener>::MakeSptr(); 303 callback->OnAddSystemAbility(ability1301, deviceId); 304 callback->OnAddSystemAbility(ability2802, deviceId); 305 callback->OnAddSystemAbility(ability2805, deviceId); 306 callback->OnRemoveSystemAbility(ability1301, deviceId); 307 callback->OnRemoveSystemAbility(ability2802, deviceId); 308 callback->OnRemoveSystemAbility(ability2805, deviceId); 309 ASSERT_TRUE(locationAbility->SystemAbilitySubscriber() == true); 310 ASSERT_TRUE(locationAbility->statusChangeListener_ != nullptr); 311 } 312 } 313 // namespace Telephony 314 315 } 316 // namespace OHOS