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 18 #include <map> 19 #include <mutex> 20 21 #include "call_earthquake_alarm_locator.h" 22 #include "data_ability_observer_stub.h" 23 #include "call_manager_inner_type.h" 24 #include "call_state_listener_base.h" 25 #include "call_control_manager.h" 26 #include "audio_control_manager.h" 27 #include "ring.h" 28 #include "common_type.h" 29 #include "call_base.h" 30 #include "cs_call.h" 31 #include "call_voice_assistant_manager.h" 32 #include "gtest/gtest.h" 33 34 namespace OHOS { 35 namespace Telephony { 36 using namespace testing::ext; 37 using namespace std; 38 39 class CallVoiceAssistantManagerTest : public testing::Test { 40 public: SetUpTestCase()41 static void SetUpTestCase() {}; TearDownTestCase()42 static void TearDownTestCase() {}; SetUp()43 virtual void SetUp() {}; TearDown()44 virtual void TearDown() {}; 45 }; 46 47 /** 48 * @tc.number Telephony_CallVoiceAssistantManager_001 49 * @tc.name test normal branch 50 * @tc.desc Function test 51 */ 52 HWTEST_F(CallVoiceAssistantManagerTest, Telephony_CallVoiceAssistantManager_001, TestSize.Level0) 53 { 54 const int32_t callId = -1; 55 const int32_t accountId = -1; 56 const std::string incoming = "come"; 57 const std::string dialing = "dial"; 58 DialParaInfo paraInfo; 59 sptr<CallBase> callObjectPtr = std::make_unique<CSCall>(paraInfo).release(); 60 TelCallState priorState = TelCallState::CALL_STATUS_DIALING; 61 TelCallState nextState = TelCallState::CALL_STATUS_INCOMING; 62 std::shared_ptr<CallVoiceAssistantManager> voicePtr = CallVoiceAssistantManager::GetInstance(); 63 if (voicePtr == nullptr) { 64 TELEPHONY_LOGE("voicePtr is nullptr"); 65 return; 66 } 67 voicePtr->mInstance_ = voicePtr; 68 voicePtr->CallStateUpdated(callObjectPtr, priorState, nextState); 69 voicePtr->CallStatusDialing(callId, accountId); 70 voicePtr->CallStatusIncoming(callId, accountId); 71 voicePtr->CallStatusActive(callId, accountId); 72 voicePtr->CallStatusDisconnected(callId, accountId); 73 voicePtr->ConnectAbility(accountId); 74 voicePtr->OnStartService(incoming, accountId); 75 voicePtr->RegisterListenSwitchState(); 76 voicePtr->PublishCommonEvent(true, incoming); 77 voicePtr->DisconnectAbility(); 78 voicePtr->UnRegisterListenSwitchState(); 79 voicePtr->PublishCommonEvent(false, dialing); 80 voicePtr->OnStopService(); 81 voicePtr->Release(); 82 ASSERT_TRUE(voicePtr->GetInstance() != nullptr); 83 ASSERT_TRUE(voicePtr->settingsCallback_ == nullptr); 84 ASSERT_TRUE(voicePtr->connectCallback_ == nullptr); 85 ASSERT_TRUE(voicePtr->mRemoteObject == nullptr); 86 DelayedSingleton<AudioControlManager>::GetInstance()->UnInit(); 87 } 88 89 /** 90 * @tc.number Telephony_CallVoiceAssistantManager_002 91 * @tc.name test normal branch 92 * @tc.desc Function test 93 */ 94 HWTEST_F(CallVoiceAssistantManagerTest, Telephony_CallVoiceAssistantManager_002, TestSize.Level0) 95 { 96 const int32_t callId = -1; 97 const int32_t accountId = -1; 98 const std::string controlSwitch = "incoming_call_voice_control_switch"; 99 const std::string incoming = "come"; 100 const std::string dialing = "dial"; 101 std::string value = ""; 102 ContactInfo contactInfo; 103 std::shared_ptr<CallVoiceAssistantManager> voicePtr = CallVoiceAssistantManager::GetInstance(); 104 sptr<IRemoteObject> remoteObject = sptr<VoiceAssistantConnectCallback>::MakeSptr(accountId); 105 if (voicePtr == nullptr) { 106 TELEPHONY_LOGE("voicePtr is nullptr"); 107 return; 108 } 109 voicePtr->mInstance_ = voicePtr; 110 voicePtr->SetIsControlSwitchOn(true); 111 voicePtr->GetIsControlSwitchOn(); 112 voicePtr->GetIsPlayRing(); 113 voicePtr->UpdateVoipCallState(accountId); 114 auto infoptr = voicePtr->GetContactInfo(accountId); 115 voicePtr->UpdateNumberLocation(value, accountId); 116 voicePtr->UpdateContactInfo(contactInfo, accountId); 117 voicePtr->UpdateRemoteObject(remoteObject, accountId, nullptr); 118 voicePtr->OnStopService(); 119 voicePtr->Initial(); 120 voicePtr->QueryValue(controlSwitch, value); 121 voicePtr->IsStartVoiceBroadcast(); 122 voicePtr->IsSwitchOn(controlSwitch); 123 voicePtr->OnStartService(dialing, accountId); 124 voicePtr->MuteRinger(); 125 voicePtr->UpdateReplyData(value); 126 voicePtr->GetSendString(infoptr); 127 voicePtr->SendRequest(infoptr, true); 128 voicePtr->OnStopService(); 129 voicePtr->Release(); 130 ASSERT_TRUE(voicePtr->GetInstance() != nullptr); 131 ASSERT_TRUE(voicePtr->settingsCallback_ == nullptr); 132 ASSERT_TRUE(voicePtr->connectCallback_ == nullptr); 133 ASSERT_TRUE(voicePtr->mRemoteObject == nullptr); 134 DelayedSingleton<AudioControlManager>::GetInstance()->UnInit(); 135 } 136 137 /** 138 * @tc.number Telephony_VoiceAssistantSwitchObserver_001 139 * @tc.name test normal branch 140 * @tc.desc Function test 141 */ 142 HWTEST_F(CallVoiceAssistantManagerTest, Telephony_VoiceAssistantSwitchObserver_001, TestSize.Level0) 143 { 144 sptr<AAFwk::IDataAbilityObserver> settingsCallback = sptr<VoiceAssistantSwitchObserver>::MakeSptr(); 145 settingsCallback->OnChange(); 146 ASSERT_TRUE(settingsCallback != nullptr); 147 } 148 149 /** 150 * @tc.number Telephony_VoiceAssistantConnectCallback_001 151 * @tc.name test normal branch 152 * @tc.desc Function test 153 */ 154 HWTEST_F(CallVoiceAssistantManagerTest, Telephony_VoiceAssistantConnectCallback_001, TestSize.Level1) 155 { 156 const int32_t accountId = 1; 157 sptr<IRemoteObject> remoteObject = sptr<VoiceAssistantConnectCallback>::MakeSptr(accountId); 158 AppExecFwk::ElementName element; 159 sptr<AAFwk::IAbilityConnection> connectCallback = sptr<VoiceAssistantConnectCallback>::MakeSptr(accountId); 160 connectCallback->OnAbilityConnectDone(element, remoteObject, accountId); 161 connectCallback->OnAbilityDisconnectDone(element, accountId); 162 ASSERT_TRUE(connectCallback != nullptr); 163 } 164 165 /** 166 * @tc.number Telephony_VoiceAssistantRingSubscriber_001 167 * @tc.name test normal branch 168 * @tc.desc Function test 169 */ 170 HWTEST_F(CallVoiceAssistantManagerTest, Telephony_VoiceAssistantRingSubscriber_001, TestSize.Level0) 171 { 172 std::shared_ptr<CallVoiceAssistantManager> voicePtr = CallVoiceAssistantManager::GetInstance(); 173 std::string event = "test.test.test.test"; 174 EventFwk::MatchingSkills matchingSkills; 175 matchingSkills.AddEvent(event); 176 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills); 177 auto subscriber = std::make_shared<VoiceAssistantRingSubscriber>(subscribeInfo); 178 EventFwk::CommonEventData eventData; 179 AAFwk::Want want; 180 want.SetAction(event); 181 want.SetParam(event, event); 182 eventData.SetWant(want); 183 subscriber->OnReceiveEvent(eventData); 184 subscriber->Initial(); 185 subscriber->Release(); 186 ASSERT_TRUE(subscriber->subscriber_ == nullptr); 187 event = voicePtr->CONTROL_SWITCH_STATE_CHANGE_EVENT; 188 want.SetAction(event); 189 want.SetParam(event, event); 190 eventData.SetWant(want); 191 ASSERT_NO_THROW(subscriber->OnReceiveEvent(eventData)); 192 std::string key1 = "publisher_name"; 193 std::string value1 = "remote_object_send_request"; 194 want.SetParam(key1, value1); 195 eventData.SetWant(want); 196 ASSERT_NO_THROW(subscriber->OnReceiveEvent(eventData)); 197 std::string value2 = "connect_voice_assistant_ability_failed"; 198 want.SetParam(key1, value2); 199 eventData.SetWant(want); 200 ASSERT_NO_THROW(subscriber->OnReceiveEvent(eventData)); 201 want.SetParam(voicePtr->IS_PLAY_RING, voicePtr->SWITCH_TURN_ON); 202 eventData.SetWant(want); 203 ASSERT_NO_THROW(subscriber->OnReceiveEvent(eventData)); 204 DelayedSingleton<AudioControlManager>::GetInstance()->UnInit(); 205 } 206 } 207 } 208