1 /* 2 * Copyright (c) 2025 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 <gtest/gtest.h> 17 #include <gmock/gmock.h> 18 #include "audio_errors.h" 19 #include "bluetooth_sco_manager.h" 20 #include "bluetooth_hfp_mock_interface.h" 21 #include "bluetooth_errorcode.h" 22 #include "audio_bluetooth_manager.h" 23 #include "bluetooth_device_manager.h" 24 25 namespace OHOS { 26 namespace Bluetooth { 27 using namespace AudioStandard; 28 29 using namespace testing::ext; 30 using namespace testing; 31 32 #define HFP_DEVICE_MAC1 "28:FA:19:1E:41:0E" 33 #define HFP_DEVICE_MAC2 "24:E9:CA:60:2F:CB" 34 #define TEST_VIRTUAL_CALL_BUNDLE_NAME "test.service" 35 36 class BluetoothHfpManagerTest : public testing::Test { 37 public: SetUp(void)38 void SetUp(void) override 39 { 40 BluetoothHfpMockInterface::mockInterface_ = std::make_shared<BluetoothHfpMockInterface>(); 41 42 HfpBluetoothDeviceManager::hfpBluetoothDeviceMap_[HFP_DEVICE_MAC1] = 43 BluetoothRemoteDevice(HFP_DEVICE_MAC1); 44 HfpBluetoothDeviceManager::hfpBluetoothDeviceMap_[HFP_DEVICE_MAC2] = 45 BluetoothRemoteDevice(HFP_DEVICE_MAC2); 46 } 47 TearDown(void)48 void TearDown(void) override 49 { 50 BluetoothScoManager::GetInstance().scoTimer_ = nullptr; 51 BluetoothScoManager::GetInstance().currentScoState_ = AudioScoState::DISCONNECTED; 52 BluetoothScoManager::GetInstance().currentScoCategory_ = ScoCategory::SCO_DEFAULT; 53 BluetoothScoManager::GetInstance().cacheReq_ = nullptr; 54 BluetoothScoManager::GetInstance().currentScoDevice_ = BluetoothRemoteDevice(); 55 BluetoothHfpMockInterface::mockInterface_ = nullptr; 56 57 AudioHfpManager::hfpListener_ = nullptr; 58 AudioHfpManager::scene_ = AUDIO_SCENE_DEFAULT; 59 AudioHfpManager::isRecognitionScene_.store(false); 60 AudioHfpManager::isRecordScene_.store(false); 61 AudioHfpManager::virtualCalls_.clear(); 62 AudioHfpManager::virtualCallStreams_.clear(); 63 AudioHfpManager::activeHfpDevice_ = BluetoothRemoteDevice(); 64 } 65 }; 66 67 /** 68 * @tc.name : Test BluetoothHfpManagerTest. 69 * @tc.number: BluetoothHfpManagerTest_001 70 * @tc.desc : Test hfp device manager. 71 */ 72 HWTEST_F(BluetoothHfpManagerTest, BluetoothHfpManagerTest_001, TestSize.Level1) 73 { 74 EXPECT_CALL(*(BluetoothHfpMockInterface::mockInterface_.get()), GetScoState(_)) 75 .Times(1) 76 .WillOnce(Return(AudioScoState::DISCONNECTED)); 77 EXPECT_CALL(*(BluetoothHfpMockInterface::mockInterface_.get()), GetActiveDevice()) 78 .Times(AnyNumber()); 79 EXPECT_CALL(*(BluetoothHfpMockInterface::mockInterface_.get()), SetActiveDevice(_)) 80 .Times(2) 81 .WillOnce(Return(SUCCESS)) 82 .WillOnce(Return(SUCCESS)); 83 84 EXPECT_NE(AudioHfpManager::SetActiveHfpDevice("33:33:33"), SUCCESS); 85 EXPECT_EQ(AudioHfpManager::SetActiveHfpDevice(HFP_DEVICE_MAC1), SUCCESS); 86 EXPECT_EQ(AudioHfpManager::SetActiveHfpDevice(HFP_DEVICE_MAC2), SUCCESS); 87 } 88 89 /** 90 * @tc.name : Test BluetoothHfpManagerTest. 91 * @tc.number: BluetoothHfpManagerTest_002 92 * @tc.desc : Test hfp device manager. 93 */ 94 HWTEST_F(BluetoothHfpManagerTest, BluetoothHfpManagerTest_002, TestSize.Level1) 95 { 96 EXPECT_CALL(*(BluetoothHfpMockInterface::mockInterface_.get()), SetActiveDevice(_)) 97 .Times(1) 98 .WillOnce(Return(SUCCESS)); 99 EXPECT_CALL(*(BluetoothHfpMockInterface::mockInterface_.get()), IsInbandRingingEnabled(_)) 100 .Times(1) __anon3709a44f0102(bool &enable) 101 .WillOnce(Invoke([](bool &enable) ->int32_t { 102 enable = true; 103 return SUCCESS; 104 })); 105 EXPECT_CALL(*(BluetoothHfpMockInterface::mockInterface_.get()), ConnectSco(_)) 106 .Times(1) 107 .WillOnce(Return(SUCCESS)); 108 109 EXPECT_EQ(AudioHfpManager::SetActiveHfpDevice(HFP_DEVICE_MAC1), SUCCESS); 110 AudioHfpManager::UpdateAudioScene(AUDIO_SCENE_VOICE_RINGING); 111 EXPECT_EQ(BluetoothScoManager::GetInstance().GetAudioScoState(), AudioScoState::CONNECTING); 112 EXPECT_EQ(BluetoothScoManager::GetInstance().IsInScoCategory(ScoCategory::SCO_CALLULAR), true); 113 } 114 115 /** 116 * @tc.name : Test BluetoothHfpManagerTest. 117 * @tc.number: BluetoothHfpManagerTest_003 118 * @tc.desc : Test hfp device manager. 119 */ 120 HWTEST_F(BluetoothHfpManagerTest, BluetoothHfpManagerTest_003, TestSize.Level1) 121 { 122 EXPECT_CALL(*(BluetoothHfpMockInterface::mockInterface_.get()), SetActiveDevice(_)) 123 .Times(1) 124 .WillOnce(Return(SUCCESS)); 125 EXPECT_CALL(*(BluetoothHfpMockInterface::mockInterface_.get()), ConnectSco(_)) 126 .Times(1) 127 .WillOnce(Return(SUCCESS)); 128 EXPECT_CALL(*(BluetoothHfpMockInterface::mockInterface_.get()), IsInbandRingingEnabled(_)) __anon3709a44f0202(bool &enable) 129 .WillRepeatedly(Invoke([](bool &enable) ->int32_t { 130 enable = true; 131 return SUCCESS; 132 })); 133 134 EXPECT_EQ(AudioHfpManager::SetActiveHfpDevice(HFP_DEVICE_MAC2), SUCCESS); 135 AudioHfpManager::UpdateAudioScene(AUDIO_SCENE_PHONE_CHAT); 136 EXPECT_EQ(BluetoothScoManager::GetInstance().GetAudioScoState(), AudioScoState::CONNECTING); 137 EXPECT_EQ(BluetoothScoManager::GetInstance().IsInScoCategory(ScoCategory::SCO_VIRTUAL), true); 138 } 139 140 /** 141 * @tc.name : Test BluetoothHfpManagerTest. 142 * @tc.number: BluetoothHfpManagerTest_004 143 * @tc.desc : Test hfp device manager. 144 */ 145 HWTEST_F(BluetoothHfpManagerTest, BluetoothHfpManagerTest_004, TestSize.Level1) 146 { 147 EXPECT_CALL(*(BluetoothHfpMockInterface::mockInterface_.get()), ConnectSco(_)) 148 .Times(1) 149 .WillOnce(Return(SUCCESS)); 150 EXPECT_CALL(*(BluetoothHfpMockInterface::mockInterface_.get()), IsInbandRingingEnabled(_)) __anon3709a44f0302(bool &enable) 151 .WillRepeatedly(Invoke([](bool &enable) ->int32_t { 152 enable = true; 153 return SUCCESS; 154 })); 155 AudioHfpManager::activeHfpDevice_ = BluetoothRemoteDevice(HFP_DEVICE_MAC2); 156 157 AudioHfpManager::UpdateAudioScene(AUDIO_SCENE_DEFAULT, true); 158 EXPECT_EQ(AudioHfpManager::IsAudioScoStateConnect(), true); 159 EXPECT_EQ(BluetoothScoManager::GetInstance().GetAudioScoState(), AudioScoState::CONNECTING); 160 EXPECT_EQ(BluetoothScoManager::GetInstance().IsInScoCategory(ScoCategory::SCO_DEFAULT), true); 161 } 162 163 /** 164 * @tc.name : Test BluetoothHfpManagerTest. 165 * @tc.number: BluetoothHfpManagerTest_005 166 * @tc.desc : Test hfp device manager. 167 */ 168 HWTEST_F(BluetoothHfpManagerTest, BluetoothHfpManagerTest_005, TestSize.Level1) 169 { 170 EXPECT_CALL(*(BluetoothHfpMockInterface::mockInterface_.get()), OpenVoiceRecognition(_)) 171 .Times(1) 172 .WillOnce(Return(SUCCESS)); 173 EXPECT_CALL(*(BluetoothHfpMockInterface::mockInterface_.get()), IsInbandRingingEnabled(_)) __anon3709a44f0402(bool &enable) 174 .WillRepeatedly(Invoke([](bool &enable) ->int32_t { 175 enable = true; 176 return SUCCESS; 177 })); 178 AudioHfpManager::activeHfpDevice_ = BluetoothRemoteDevice(HFP_DEVICE_MAC2); 179 180 AudioHfpManager::HandleScoWithRecongnition(true); 181 EXPECT_EQ(BluetoothScoManager::GetInstance().GetAudioScoState(), AudioScoState::CONNECTING); 182 EXPECT_EQ(BluetoothScoManager::GetInstance().IsInScoCategory(ScoCategory::SCO_RECOGNITION), true); 183 } 184 } // namespace Bluetooth 185 } // namespace OHOS