diff --git a/services/audio_policy/BUILD.gn b/services/audio_policy/BUILD.gn index cba57a80c58d5c6f4b201f02a21f336e3ce79102..a3e8b25ea2e08fef006a50b8740267bc8f73806b 100644 --- a/services/audio_policy/BUILD.gn +++ b/services/audio_policy/BUILD.gn @@ -621,6 +621,7 @@ group("audio_policy_test_packages") { "server/domain/device:audio_device_common_unit_next_test", "server/domain/device/test:audio_device_status_extended_test", "server/domain/effect/test:audio_spatialization_service_extended_test", + "server/domain/device/test:audio_active_device_new_unit_test", "server/domain/pipe/test:audio_policy_pipe_unittest_packages", "server/domain/router/test:audio_router_unittest_packages", "server/domain/session/test:audio_policy_session_unit_test", diff --git a/services/audio_policy/server/domain/device/test/BUILD.gn b/services/audio_policy/server/domain/device/test/BUILD.gn index 1dce62afc8a2ace5c09f58bd747d4e381351503a..757a8b876151f44afb6567e7f95c7a2afccf87e0 100644 --- a/services/audio_policy/server/domain/device/test/BUILD.gn +++ b/services/audio_policy/server/domain/device/test/BUILD.gn @@ -66,6 +66,71 @@ ohos_unittest("audio_device_status_extended_test") { sources = [ "./unittest/audio_device_status_unit_test/src/audio_device_status_extended_test.cpp" ] + deps = [ + "${audio_framework_root}/services/audio_service/idl:audio_framework_interface", + "../../../../../audio_policy:audio_policy_service_static", + ] +} + +ohos_unittest("audio_active_device_new_unit_test") { + module_out_path = module_output_path + include_dirs = [ + "../../../../server/include", + "./unittest/audio_active_device_new_unit_test/include", + ] + + use_exceptions = true + + cflags = [ + "-Wall", + "-Werror", + "-Wno-macro-redefined", + "-fno-access-control", + ] + + external_deps = [ + "ability_base:session_info", + "ability_base:want", + "ability_base:zuri", + "ability_runtime:ability_connect_callback_stub", + "ability_runtime:ability_context_native", + "ability_runtime:app_context", + "ability_runtime:dataobs_manager", + "ability_runtime:extension_manager", + "access_token:libaccesstoken_sdk", + "access_token:libprivacy_sdk", + "access_token:libtokenid_sdk", + "access_token:libtokensetproc_shared", + "bluetooth:btframework", + "bounds_checking_function:libsec_shared", + "bundle_framework:appexecfwk_base", + "bundle_framework:appexecfwk_core", + "c_utils:utils", + "data_share:datashare_common", + "data_share:datashare_consumer", + "drivers_interface_audio:libaudio_proxy_5.0", + "eventhandler:libeventhandler", + "googletest:gmock", + "hdf_core:libhdf_host", + "hdf_core:libhdf_ipc_adapter", + "hdf_core:libhdf_utils", + "hdf_core:libhdi", + "hdf_core:libpub_utils", + "hilog:libhilog", + "hisysevent:libhisysevent", + "init:libbegetutil", + "ipc:ipc_single", + "media_foundation:media_monitor_client", + "media_foundation:media_monitor_common", + "os_account:os_account_innerkits", + "power_manager:power_setting", + "power_manager:powermgr_client", + "safwk:system_ability_fwk", + "samgr:samgr_proxy", + ] + + sources = [ "./unittest/audio_active_device_new_unit_test/src/audio_active_device_new_unit_test.cpp" ] + deps = [ "${audio_framework_root}/services/audio_service/idl:audio_framework_interface", "../../../../../audio_policy:audio_policy_service_static", diff --git a/services/audio_policy/server/domain/device/test/unittest/audio_active_device_new_unit_test/include/audio_active_device_new_unit_test.h b/services/audio_policy/server/domain/device/test/unittest/audio_active_device_new_unit_test/include/audio_active_device_new_unit_test.h new file mode 100644 index 0000000000000000000000000000000000000000..d057e2d1d5f76d52564e098ac0db3b3c21aea738 --- /dev/null +++ b/services/audio_policy/server/domain/device/test/unittest/audio_active_device_new_unit_test/include/audio_active_device_new_unit_test.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef AUDIO_ACTIVE_DEVICE_UNIT_NEW_TEST_H +#define AUDIO_ACTIVE_DEVICE_UNIT_NEW_TEST_H +#include "gtest/gtest.h" +#include "gmock/gmock.h" +#include "audio_active_device.h" + +namespace OHOS { +namespace AudioStandard { + +class AudioActiveDeviceNewUnitTest : public testing::Test { +public: + // SetUpTestCase: Called before all test cases + static void SetUpTestCase(void); + // TearDownTestCase: Called after all test case + static void TearDownTestCase(void); + // SetUp: Called before each test cases + void SetUp(void); + // TearDown: Called after each test cases + void TearDown(void); +}; +} // namespace AudioStandard +} // namespace OHOS +#endif //AUDIO_ACTIVE_DEVICE_UNIT_NEW_TEST_H diff --git a/services/audio_policy/server/domain/device/test/unittest/audio_active_device_new_unit_test/src/audio_active_device_new_unit_test.cpp b/services/audio_policy/server/domain/device/test/unittest/audio_active_device_new_unit_test/src/audio_active_device_new_unit_test.cpp new file mode 100644 index 0000000000000000000000000000000000000000..c8079dda89972a2ec11de78362fededf669e02dd --- /dev/null +++ b/services/audio_policy/server/domain/device/test/unittest/audio_active_device_new_unit_test/src/audio_active_device_new_unit_test.cpp @@ -0,0 +1,297 @@ +/* + * Copyright (c) 2025 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "audio_policy_utils.h" +#include "audio_active_device_new_unit_test.h" + +using namespace testing::ext; + +namespace OHOS { +namespace AudioStandard { + +void AudioActiveDeviceNewUnitTest::SetUpTestCase(void) {} +void AudioActiveDeviceNewUnitTest::TearDownTestCase(void) {} +void AudioActiveDeviceNewUnitTest::SetUp(void) {} +void AudioActiveDeviceNewUnitTest::TearDown(void) {} + +/** +* @tc.name : Test GetActiveA2dpDeviceStreamInfo. +* @tc.number: AudioActiveDeviceNewUnitTest_GetActiveA2dpDeviceStreamInfo_001. +* @tc.desc : Test GetActiveA2dpDeviceStreamInfo interface. +*/ +HWTEST_F(AudioActiveDeviceNewUnitTest, GetActiveA2dpDeviceStreamInfo_001, TestSize.Level4) +{ + auto audioActiveDevice = std::make_shared(); + audioActiveDevice->activeBTInDevice_ = "AA:BB:CC:DD:EE:FF"; + auto& a2dpDevice = audioActiveDevice->audioA2dpDevice_; + A2dpDeviceConfigInfo info; + info.streamInfo.samplingRate = {SAMPLE_RATE_8000, SAMPLE_RATE_48000}; + info.streamInfo.format = AudioSampleFormat::SAMPLE_S16LE; + info.streamInfo.channelLayout = {AudioChannelLayout::CH_LAYOUT_STEREO}; + info.absVolumeSupport = true; + info.volumeLevel = 8; + info.mute = false; + a2dpDevice.connectedA2dpInDeviceMap_["AA:BB:CC:DD:EE:FF"] = info; + AudioStreamInfo streamInfo; + bool result = audioActiveDevice->GetActiveA2dpDeviceStreamInfo( + DeviceType::DEVICE_TYPE_BLUETOOTH_A2DP_IN, streamInfo); + + EXPECT_EQ(result, true); +} + +/** +* @tc.name : Test AudioActiveDevice. +* @tc.number: AudioActiveDeviceNewUnitTest_GetMaxAmplitude_001. +* @tc.desc : Test GetMaxAmplitude. +*/ +HWTEST_F(AudioActiveDeviceNewUnitTest, GetMaxAmplitude_001, TestSize.Level4) +{ + auto audioActiveDevice = std::make_shared(); + audioActiveDevice->currentActiveDevice_ = AudioDeviceDescriptor(DEVICE_TYPE_SPEAKER, OUTPUT_DEVICE); + audioActiveDevice->currentActiveDevice_.deviceId_ = 1; + audioActiveDevice->currentActiveInputDevice_ = AudioDeviceDescriptor(DEVICE_TYPE_MIC, INPUT_DEVICE); + audioActiveDevice->currentActiveInputDevice_.deviceId_ = 0; + AudioInterrupt audioInterrupt; + float result = audioActiveDevice->GetMaxAmplitude(0, audioInterrupt); + EXPECT_EQ(result, 0); +} + +/** +* @tc.name : Test AudioActiveDevice. +* @tc.number: AudioActiveDeviceNewUnitTest_GetMaxAmplitude_002. +* @tc.desc : Test GetMaxAmplitude. +*/ +HWTEST_F(AudioActiveDeviceNewUnitTest, GetMaxAmplitude_002, TestSize.Level4) +{ + auto audioActiveDevice = std::make_shared(); + AudioInterrupt audioInterrupt; + float result = audioActiveDevice->GetMaxAmplitude(0, audioInterrupt); + EXPECT_NE(audioActiveDevice, nullptr); +} + +/** +* @tc.name : Test AudioActiveDevice. +* @tc.number: AudioActiveDeviceNewUnitTest_NotifyUserSelectionEventToBt_001. +* @tc.desc : Test NotifyUserSelectionEventToBt. +*/ +HWTEST_F(AudioActiveDeviceNewUnitTest, NotifyUserSelectionEventToBt_001, TestSize.Level4) +{ + auto audioActiveDevice = std::make_shared(); + audioActiveDevice->currentActiveDevice_ = AudioDeviceDescriptor(DEVICE_TYPE_SPEAKER, INPUT_DEVICE); + audioActiveDevice->currentActiveDevice_.deviceId_ = 1; + auto nearLinkDevice = std::make_shared(DEVICE_TYPE_NEARLINK, INPUT_DEVICE, 0, 0, + "nearlink_net_001" + ); + nearLinkDevice->macAddress_ = "AA:BB:CC:DD:EE:FF"; + StreamUsage streamUsage = STREAM_USAGE_MEDIA; + audioActiveDevice->NotifyUserSelectionEventToBt(nearLinkDevice, streamUsage); +} + +/** +* @tc.name : Test AudioActiveDevice. +* @tc.number: AudioActiveDeviceNewUnitTest_NotifyUserSelectionEventForInput_001. +* @tc.desc : Test NotifyUserSelectionEventForInput. +*/ +HWTEST_F(AudioActiveDeviceNewUnitTest, NotifyUserSelectionEventForInput_001, TestSize.Level4) +{ + auto audioActiveDevice = std::make_shared(); + EXPECT_NE(audioActiveDevice, nullptr); + audioActiveDevice->currentActiveInputDevice_ = AudioDeviceDescriptor(DEVICE_TYPE_MIC, INPUT_DEVICE); + audioActiveDevice->currentActiveInputDevice_.deviceId_ = 1; + auto scoDevice = std::make_shared( + DEVICE_TYPE_BLUETOOTH_SCO, + INPUT_DEVICE, + 0, 0, "bt_network_001" + ); + scoDevice->macAddress_ = "AA:BB:CC:DD:EE:01"; // 必须设置,否则 SendUserSelection 可能出错 + SourceType sourceType = SOURCE_TYPE_MIC; + audioActiveDevice->NotifyUserSelectionEventForInput(scoDevice, sourceType); +} + +/** +* @tc.name : Test AudioActiveDevice. +* @tc.number: AudioActiveDeviceNewUnitTest_NotifyUserSelectionEventForInput_002. +* @tc.desc : Test NotifyUserSelectionEventForInput. +*/ +HWTEST_F(AudioActiveDeviceNewUnitTest, NotifyUserSelectionEventForInput_002, TestSize.Level4) +{ + auto audioActiveDevice = std::make_shared(); + EXPECT_NE(audioActiveDevice, nullptr); + audioActiveDevice->currentActiveInputDevice_ = AudioDeviceDescriptor(DEVICE_TYPE_MIC, INPUT_DEVICE); + audioActiveDevice->currentActiveInputDevice_.deviceId_ = 1; + auto scoDevice = std::make_shared( + DEVICE_TYPE_BLUETOOTH_A2DP_IN, + INPUT_DEVICE, + 0, 0, "bt_network_001" + ); + scoDevice->macAddress_ = "AA:BB:CC:DD:EE:01"; + SourceType sourceType = SOURCE_TYPE_MIC; + audioActiveDevice->NotifyUserSelectionEventForInput(scoDevice, sourceType); +} + +/** +* @tc.name : Test AudioActiveDevice. +* @tc.number: AudioActiveDeviceNewUnitTest_NotifyUserSelectionEventForInput_003. +* @tc.desc : Test NotifyUserSelectionEventForInput. +*/ +HWTEST_F(AudioActiveDeviceNewUnitTest, NotifyUserSelectionEventForInput_003, TestSize.Level4) +{ + auto audioActiveDevice = std::make_shared(); + EXPECT_NE(audioActiveDevice, nullptr); + audioActiveDevice->currentActiveInputDevice_ = AudioDeviceDescriptor(DEVICE_TYPE_MIC, INPUT_DEVICE); + audioActiveDevice->currentActiveInputDevice_.deviceId_ = 1; + auto scoDevice = std::make_shared( + DEVICE_TYPE_NEARLINK_IN, + INPUT_DEVICE, + 0, 0, "bt_network_001" + ); + scoDevice->macAddress_ = "AA:BB:CC:DD:EE:01"; + SourceType sourceType = SOURCE_TYPE_MIC; + audioActiveDevice->NotifyUserSelectionEventForInput(scoDevice, sourceType); +} + +/** +* @tc.name : Test AudioActiveDevice. +* @tc.number: AudioActiveDeviceNewUnitTest_UpdateDevice_001. +* @tc.desc : Test UpdateDevice. +*/ +HWTEST_F(AudioActiveDeviceNewUnitTest, UpdateDevice_001, TestSize.Level4) +{ + auto audioActiveDevice = std::make_shared(); + audioActiveDevice->currentActiveDevice_ = AudioDeviceDescriptor(DEVICE_TYPE_SPEAKER, OUTPUT_DEVICE); + audioActiveDevice->currentActiveDevice_.deviceId_ = 1; + auto desc = std::make_shared(DEVICE_TYPE_BLUETOOTH_A2DP, OUTPUT_DEVICE); + desc->deviceId_ = 2; + desc->macAddress_ = "AA:BB:CC:DD:EE:01"; + desc->networkId_ = "bt_net_001"; + auto preferredDesc = std::make_shared(DEVICE_TYPE_WIRED_HEADSET, OUTPUT_DEVICE); + preferredDesc->deviceId_ = 3; + preferredDesc->macAddress_ = "FF:EE:DD:CC:BB:01"; + preferredDesc->networkId_ = "wired_net_001"; + auto& affinityManager = audioActiveDevice->audioAffinityManager_; + affinityManager.activeRendererDeviceMap_[1001] = preferredDesc; + AudioStreamDeviceChangeReasonExt reason(AudioStreamDeviceChangeReason::NEW_DEVICE_AVAILABLE); + auto rendererChangeInfo = std::make_shared(); + rendererChangeInfo->clientUID = 1001; + bool result = audioActiveDevice->UpdateDevice(desc, reason, rendererChangeInfo); + EXPECT_TRUE(result); +} + +/** +* @tc.name : Test AudioActiveDevice. +* @tc.number: AudioActiveDeviceNewUnitTest_HandleNegtiveBt_001. +* @tc.desc : Test HandleNegtiveBt. +*/ +HWTEST_F(AudioActiveDeviceNewUnitTest, HandleNegtiveBt_001, TestSize.Level4) +{ + auto audioActiveDevice = std::make_shared(); + AudioDeviceDescriptor deviceDescriptor; + + deviceDescriptor.deviceType_ = DEVICE_TYPE_NEARLINK; + audioActiveDevice->SetCurrentOutputDevice(deviceDescriptor); + audioActiveDevice->HandleNegtiveBt(DeviceType::DEVICE_TYPE_NEARLINK); + + deviceDescriptor.deviceType_ = DEVICE_TYPE_NEARLINK; + audioActiveDevice->SetCurrentOutputDevice(deviceDescriptor); + audioActiveDevice->HandleNegtiveBt(DeviceType::DEVICE_TYPE_BLUETOOTH_SCO); + + deviceDescriptor.deviceType_ = DEVICE_TYPE_SPEAKER; + audioActiveDevice->SetCurrentOutputDevice(deviceDescriptor); + audioActiveDevice->HandleNegtiveBt(DeviceType::DEVICE_TYPE_NEARLINK); + + deviceDescriptor.deviceType_ = DEVICE_TYPE_EARPIECE; + audioActiveDevice->SetCurrentOutputDevice(deviceDescriptor); + audioActiveDevice->HandleNegtiveBt(DeviceType::DEVICE_TYPE_BLUETOOTH_A2DP); +} + +/** +* @tc.name : Test AudioActiveDevice. +* @tc.number: AudioActiveDeviceNewUnitTest_HandleNegtiveBt_001. +* @tc.desc : Test SetDeviceActive. +*/ +HWTEST_F(AudioActiveDeviceNewUnitTest, SetDeviceActive_001, TestSize.Level4) +{ + auto fakeDevice = std::make_shared(); + fakeDevice->deviceType_ = DEVICE_TYPE_SPEAKER; + fakeDevice->deviceRole_ = OUTPUT_DEVICE; + fakeDevice->networkId_ = "local_network"; + fakeDevice->deviceName_ = "TestSpeaker"; + auto& deviceManager = AudioDeviceManager::GetAudioDeviceManager(); + deviceManager.AddNewDevice(fakeDevice); + + auto callDevices = AudioPolicyUtils::GetInstance().GetAvailableDevicesInner(CALL_OUTPUT_DEVICES); + auto audioActiveDevice = std::make_shared(); + int32_t result = audioActiveDevice->SetDeviceActive( + DEVICE_TYPE_SPEAKER, + false, + 1001 + ); + + EXPECT_EQ(result, SUCCESS); +} + +/** +* @tc.name : Test AudioActiveDevice. +* @tc.number: AudioActiveDeviceNewUnitTest_SetCallDeviceActive_001. +* @tc.desc : Test SetCallDeviceActive. +*/ +HWTEST_F(AudioActiveDeviceNewUnitTest, SetCallDeviceActive_001, TestSize.Level4) +{ + auto fakeDevice = std::make_shared(); + fakeDevice->deviceType_ = DEVICE_TYPE_SPEAKER; + fakeDevice->deviceRole_ = OUTPUT_DEVICE; + fakeDevice->macAddress_ = ""; + + auto& deviceManager = AudioDeviceManager::GetAudioDeviceManager(); + deviceManager.AddNewDevice(fakeDevice); + auto callDevices = AudioPolicyUtils::GetInstance().GetAvailableDevicesInner(CALL_OUTPUT_DEVICES); + EXPECT_GT(callDevices.size(), 0); + + auto audioActiveDevice = std::make_shared(); + int32_t result = audioActiveDevice->SetCallDeviceActive( + DEVICE_TYPE_SPEAKER, + true, + "" + ); + EXPECT_EQ(result, SUCCESS); +} + +/** +* @tc.name : Test AudioActiveDevice. +* @tc.number: AudioActiveDeviceNewUnitTest_SetCallDeviceActive_002. +* @tc.desc : Test SetCallDeviceActive. +*/ +HWTEST_F(AudioActiveDeviceNewUnitTest, SetCallDeviceActive_002, TestSize.Level4) +{ + auto fakeDevice = std::make_shared(); + fakeDevice->deviceType_ = DEVICE_TYPE_SPEAKER; + fakeDevice->deviceRole_ = OUTPUT_DEVICE; + fakeDevice->macAddress_ = ""; + + auto& deviceManager = AudioDeviceManager::GetAudioDeviceManager(); + deviceManager.AddNewDevice(fakeDevice); + auto callDevices = AudioPolicyUtils::GetInstance().GetAvailableDevicesInner(CALL_OUTPUT_DEVICES); + EXPECT_GT(callDevices.size(), 0); + + auto audioActiveDevice = std::make_shared(); + int32_t result = audioActiveDevice->SetCallDeviceActive( + DEVICE_TYPE_SPEAKER, + false, + "" + ); + EXPECT_EQ(result, SUCCESS); +} +} // namespace AudioStandard +} // namespace OHOS