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 <iostream>
17 #include <gtest/gtest.h>
18 #include <gmock/gmock.h>
19 #include "audio_utils.h"
20 #include "common/hdi_adapter_info.h"
21 #include "manager/hdi_adapter_manager.h"
22
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace AudioStandard {
27 class BluetoothAudioRenderSinkUnitTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
30 static void TearDownTestCase();
31 virtual void SetUp();
32 virtual void TearDown();
33
34 protected:
35 static uint32_t id_;
36 static std::shared_ptr<IAudioRenderSink> sink_;
37 static IAudioSinkAttr attr_;
38 };
39
40 uint32_t BluetoothAudioRenderSinkUnitTest::id_ = HDI_INVALID_ID;
41 std::shared_ptr<IAudioRenderSink> BluetoothAudioRenderSinkUnitTest::sink_ = nullptr;
42 IAudioSinkAttr BluetoothAudioRenderSinkUnitTest::attr_ = {};
43
SetUpTestCase()44 void BluetoothAudioRenderSinkUnitTest::SetUpTestCase()
45 {
46 id_ = HdiAdapterManager::GetInstance().GetId(HDI_ID_BASE_RENDER, HDI_ID_TYPE_BLUETOOTH, HDI_ID_INFO_DEFAULT, true);
47 }
48
TearDownTestCase()49 void BluetoothAudioRenderSinkUnitTest::TearDownTestCase()
50 {
51 HdiAdapterManager::GetInstance().ReleaseId(id_);
52 }
53
SetUp()54 void BluetoothAudioRenderSinkUnitTest::SetUp()
55 {
56 sink_ = HdiAdapterManager::GetInstance().GetRenderSink(id_, true);
57 if (sink_ == nullptr) {
58 return;
59 }
60 attr_.adapterName = "bt_a2dp";
61 attr_.channel = 2; // 2: channel
62 sink_->Init(attr_);
63 }
64
TearDown()65 void BluetoothAudioRenderSinkUnitTest::TearDown()
66 {
67 if (sink_ && sink_->IsInited()) {
68 sink_->DeInit();
69 }
70 sink_ = nullptr;
71 }
72
73 /**
74 * @tc.name : Test BluetoothSink API
75 * @tc.number : BluetoothSinkUnitTest_001
76 * @tc.desc : Test bluetooth sink create
77 */
78 HWTEST_F(BluetoothAudioRenderSinkUnitTest, BluetoothSinkUnitTest_001, TestSize.Level1)
79 {
80 EXPECT_TRUE(sink_);
81 }
82
83 /**
84 * @tc.name : Test BluetoothSink API
85 * @tc.number : BluetoothSinkUnitTest_002
86 * @tc.desc : Test bluetooth sink init/deinit
87 */
88 HWTEST_F(BluetoothAudioRenderSinkUnitTest, BluetoothSinkUnitTest_002, TestSize.Level1)
89 {
90 EXPECT_TRUE(sink_);
91 sink_->DeInit();
92 (void)sink_->Init(attr_);
93 (void)sink_->Init(attr_);
94 sink_->DeInit();
95 EXPECT_FALSE(sink_->IsInited());
96 }
97
98 /**
99 * @tc.name : Test BluetoothSink API
100 * @tc.number : BluetoothSinkUnitTest_003
101 * @tc.desc : Test bluetooth sink start, stop, resume, pause, flush, reset
102 */
103 HWTEST_F(BluetoothAudioRenderSinkUnitTest, BluetoothSinkUnitTest_003, TestSize.Level1)
104 {
105 EXPECT_TRUE(sink_);
106 int32_t ret = sink_->Start();
107 EXPECT_EQ(ret, ERR_INVALID_HANDLE);
108 ret = sink_->Stop();
109 EXPECT_EQ(ret, SUCCESS);
110 ret = sink_->Resume();
111 EXPECT_EQ(ret, ERR_INVALID_HANDLE);
112 ret = sink_->Pause();
113 EXPECT_EQ(ret, ERR_INVALID_HANDLE);
114 ret = sink_->Flush();
115 EXPECT_EQ(ret, ERR_INVALID_HANDLE);
116 ret = sink_->Reset();
117 EXPECT_EQ(ret, ERR_INVALID_HANDLE);
118 ret = sink_->Stop();
119 EXPECT_EQ(ret, SUCCESS);
120 }
121
122 /**
123 * @tc.name : Test DirectSink API
124 * @tc.number : BluetoothSinkUnitTest_004
125 * @tc.desc : Test bluetooth sink set/get volume
126 */
127 HWTEST_F(BluetoothAudioRenderSinkUnitTest, BluetoothSinkUnitTest_004, TestSize.Level1)
128 {
129 EXPECT_TRUE(sink_);
130 int32_t ret = sink_->SetVolume(0.0f, 0.0f);
131 EXPECT_EQ(ret, ERR_INVALID_HANDLE);
132 ret = sink_->SetVolume(0.0f, 1.0f);
133 EXPECT_EQ(ret, ERR_INVALID_HANDLE);
134 ret = sink_->SetVolume(1.0f, 0.0f);
135 EXPECT_EQ(ret, ERR_INVALID_HANDLE);
136 ret = sink_->SetVolume(1.0f, 1.0f);
137 EXPECT_EQ(ret, ERR_INVALID_HANDLE);
138 float left;
139 float right;
140 ret = sink_->GetVolume(left, right);
141 EXPECT_EQ(ret, SUCCESS);
142 }
143
144 /**
145 * @tc.name : Test BluetoothSink API
146 * @tc.number : BluetoothSinkUnitTest_005
147 * @tc.desc : Test bluetooth sink set audio scene
148 */
149 HWTEST_F(BluetoothAudioRenderSinkUnitTest, BluetoothSinkUnitTest_005, TestSize.Level1)
150 {
151 EXPECT_TRUE(sink_);
152 std::vector<DeviceType> deviceTypes = { DEVICE_TYPE_SPEAKER };
153 int32_t ret = sink_->SetAudioScene(AUDIO_SCENE_DEFAULT, deviceTypes);
154 EXPECT_EQ(ret, ERR_NOT_SUPPORTED);
155 }
156
157 /**
158 * @tc.name : Test BluetoothSink API
159 * @tc.number : BluetoothSinkUnitTest_006
160 * @tc.desc : Test bluetooth sink update active device
161 */
162 HWTEST_F(BluetoothAudioRenderSinkUnitTest, BluetoothSinkUnitTest_006, TestSize.Level1)
163 {
164 EXPECT_TRUE(sink_);
165 std::vector<DeviceType> deviceTypes = { DEVICE_TYPE_SPEAKER };
166 int32_t ret = sink_->UpdateActiveDevice(deviceTypes);
167 EXPECT_EQ(ret, ERR_NOT_SUPPORTED);
168 }
169
170 } // namespace AudioStandard
171 } // namespace OHOS
172