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 #include "audio_policy_utils.h"
16 #include "audio_a2dp_device_unit_test.h"
17
18 using namespace testing::ext;
19
20 namespace OHOS {
21 namespace AudioStandard {
22
SetUpTestCase(void)23 void AudioA2dpDeviceUnitTest::SetUpTestCase(void) {}
TearDownTestCase(void)24 void AudioA2dpDeviceUnitTest::TearDownTestCase(void) {}
SetUp(void)25 void AudioA2dpDeviceUnitTest::SetUp(void) {}
TearDown(void)26 void AudioA2dpDeviceUnitTest::TearDown(void) {}
27
28 /**
29 * @tc.name: GetA2dpDeviceInfo_001
30 * @tc.desc: Test GetA2dpDeviceInfo when the device exists in connectedA2dpDeviceMap_.
31 * @tc.type: FUNC
32 * @tc.require: #I5Y4MZ
33 */
34 HWTEST_F(AudioA2dpDeviceUnitTest, GetA2dpDeviceInfo_001, TestSize.Level1)
35 {
36 DeviceStreamInfo streamInfo(AudioSamplingRate::SAMPLE_RATE_44100, AudioEncodingType::ENCODING_PCM,
37 AudioSampleFormat::SAMPLE_S16LE, AudioChannelLayout::CH_LAYOUT_STEREO);
38 A2dpDeviceConfigInfo configInfo;
39 configInfo.streamInfo = streamInfo;
40 configInfo.absVolumeSupport = true;
41 configInfo.volumeLevel = 50;
42 configInfo.mute = false;
43 std::string device = "test_device";
44 AudioA2dpDevice::GetInstance().AddA2dpDevice(device, configInfo);
45 A2dpDeviceConfigInfo info;
46 bool result = AudioA2dpDevice::GetInstance().GetA2dpDeviceInfo(device, info);
47 EXPECT_TRUE(result);
48 EXPECT_EQ(info.streamInfo.encoding, AudioEncodingType::ENCODING_PCM);
49 EXPECT_EQ(info.streamInfo.format, AudioSampleFormat::SAMPLE_S16LE);
50 EXPECT_EQ(info.streamInfo.samplingRate.size(), 1);
51 EXPECT_EQ(*info.streamInfo.samplingRate.begin(), AudioSamplingRate::SAMPLE_RATE_44100);
52 EXPECT_EQ(info.streamInfo.channelLayout.size(), 1);
53 EXPECT_EQ(*info.streamInfo.channelLayout.begin(), AudioChannelLayout::CH_LAYOUT_STEREO);
54 EXPECT_TRUE(info.absVolumeSupport);
55 EXPECT_EQ(info.volumeLevel, 50);
56 EXPECT_FALSE(info.mute);
57 AudioA2dpDevice::GetInstance().DelA2dpDevice(device);
58 }
59
60 /**
61 * @tc.name: GetA2dpDeviceInfo_002
62 * @tc.desc: Test GetA2dpDeviceInfo when the device does not exist in connectedA2dpDeviceMap_.
63 * @tc.type: FUNC
64 * @tc.require: #I5Y4MZ
65 */
66 HWTEST_F(AudioA2dpDeviceUnitTest, GetA2dpDeviceInfo_002, TestSize.Level1)
67 {
68 std::string device = "non_existent_device";
69 A2dpDeviceConfigInfo info;
70 bool result = AudioA2dpDevice::GetInstance().GetA2dpDeviceInfo(device, info);
71 EXPECT_FALSE(result);
72 }
73
74 /**
75 * @tc.name: GetA2dpInDeviceInfo_001
76 * @tc.desc: Test GetA2dpInDeviceInfo when the device exists in connectedA2dpInDeviceMap_.
77 * @tc.type: FUNC
78 * @tc.require: #I5Y4MZ
79 */
80 HWTEST_F(AudioA2dpDeviceUnitTest, GetA2dpInDeviceInfo_001, TestSize.Level1)
81 {
82 DeviceStreamInfo streamInfo(AudioSamplingRate::SAMPLE_RATE_44100, AudioEncodingType::ENCODING_PCM,
83 AudioSampleFormat::SAMPLE_S16LE, AudioChannelLayout::CH_LAYOUT_STEREO);
84 A2dpDeviceConfigInfo configInfo;
85 configInfo.streamInfo = streamInfo;
86 configInfo.absVolumeSupport = true;
87 configInfo.volumeLevel = 50;
88 configInfo.mute = false;
89 std::string device = "test_device";
90 AudioA2dpDevice::GetInstance().AddA2dpInDevice(device, configInfo);
91 A2dpDeviceConfigInfo info;
92 bool result = AudioA2dpDevice::GetInstance().GetA2dpInDeviceInfo(device, info);
93 EXPECT_TRUE(result);
94 EXPECT_EQ(info.streamInfo.encoding, AudioEncodingType::ENCODING_PCM);
95 EXPECT_EQ(info.streamInfo.format, AudioSampleFormat::SAMPLE_S16LE);
96 EXPECT_EQ(info.streamInfo.samplingRate.size(), 1);
97 EXPECT_EQ(*info.streamInfo.samplingRate.begin(), AudioSamplingRate::SAMPLE_RATE_44100);
98 EXPECT_EQ(info.streamInfo.channelLayout.size(), 1);
99 EXPECT_EQ(*info.streamInfo.channelLayout.begin(), CH_LAYOUT_STEREO);
100 EXPECT_TRUE(info.absVolumeSupport);
101 EXPECT_EQ(info.volumeLevel, 50);
102 EXPECT_FALSE(info.mute);
103 AudioA2dpDevice::GetInstance().DelA2dpInDevice(device);
104 }
105
106 /**
107 * @tc.name: GetA2dpDeviceVolumeLevel_001
108 * @tc.desc: Test GetA2dpDeviceVolumeLevel when the device exists in connectedA2dpDeviceMap_.
109 * @tc.type: FUNC
110 * @tc.require: #I5Y4MZ
111 */
112 HWTEST_F(AudioA2dpDeviceUnitTest, GetA2dpDeviceVolumeLevel_001, TestSize.Level1)
113 {
114 A2dpDeviceConfigInfo configInfo;
115 configInfo.volumeLevel = 50;
116 std::string device = "test_device";
117 AudioA2dpDevice::GetInstance().AddA2dpDevice(device, configInfo);
118 int32_t volumeLevel;
119 bool result = AudioA2dpDevice::GetInstance().GetA2dpDeviceVolumeLevel(device, volumeLevel);
120 EXPECT_TRUE(result);
121 EXPECT_EQ(volumeLevel, 50);
122 AudioA2dpDevice::GetInstance().DelA2dpDevice(device);
123 }
124
125 /**
126 * @tc.name: GetA2dpDeviceVolumeLevel_002
127 * @tc.desc: Test GetA2dpDeviceVolumeLevel when the device does not exist in connectedA2dpDeviceMap_.
128 * @tc.type: FUNC
129 * @tc.require: #I5Y4MZ
130 */
131 HWTEST_F(AudioA2dpDeviceUnitTest, GetA2dpDeviceVolumeLevel_002, TestSize.Level1)
132 {
133 std::string device = "non_existent_device";
134 int32_t volumeLevel;
135 bool result = AudioA2dpDevice::GetInstance().GetA2dpDeviceVolumeLevel(device, volumeLevel);
136 EXPECT_FALSE(result);
137 }
138
139 /**
140 * @tc.name: CheckA2dpDeviceExist_001
141 * @tc.desc: Test CheckA2dpDeviceExist when the device exists in connectedA2dpDeviceMap_.
142 * @tc.type: FUNC
143 * @tc.require: #I5Y4MZ
144 */
145 HWTEST_F(AudioA2dpDeviceUnitTest, CheckA2dpDeviceExist_001, TestSize.Level1)
146 {
147 A2dpDeviceConfigInfo configInfo;
148 std::string device = "test_device";
149 AudioA2dpDevice::GetInstance().AddA2dpDevice(device, configInfo);
150 bool result = AudioA2dpDevice::GetInstance().CheckA2dpDeviceExist(device);
151 EXPECT_TRUE(result);
152 AudioA2dpDevice::GetInstance().DelA2dpDevice(device);
153 }
154
155 /**
156 * @tc.name: SetA2dpDeviceMute_001
157 * @tc.desc: Test SetA2dpDeviceMute when the device does not exist in connectedA2dpDeviceMap_.
158 * @tc.type: FUNC
159 * @tc.require: #I5Y4MZ
160 */
161 HWTEST_F(AudioA2dpDeviceUnitTest, SetA2dpDeviceMute_001, TestSize.Level1)
162 {
163 std::string device = "non_existent_device";
164 bool result = AudioA2dpDevice::GetInstance().SetA2dpDeviceMute(device, true);
165 EXPECT_FALSE(result);
166 }
167
168 /**
169 * @tc.name: SetA2dpDeviceMute_002
170 * @tc.desc: Test SetA2dpDeviceMute when the device exists but does not support absolute volume control.
171 * @tc.type: FUNC
172 * @tc.require: #I5Y4MZ
173 */
174 HWTEST_F(AudioA2dpDeviceUnitTest, SetA2dpDeviceMute_002, TestSize.Level1)
175 {
176 A2dpDeviceConfigInfo configInfo;
177 configInfo.absVolumeSupport = false;
178 std::string device = "test_device";
179 AudioA2dpDevice::GetInstance().AddA2dpDevice(device, configInfo);
180 bool result = AudioA2dpDevice::GetInstance().SetA2dpDeviceMute(device, true);
181 EXPECT_FALSE(result);
182 AudioA2dpDevice::GetInstance().DelA2dpDevice(device);
183 }
184
185 /**
186 * @tc.name: SetA2dpDeviceMute_003
187 * @tc.desc: Test SetA2dpDeviceMute when the device exists and supports absolute volume control.
188 * @tc.type: FUNC
189 * @tc.require: #I5Y4MZ
190 */
191 HWTEST_F(AudioA2dpDeviceUnitTest, SetA2dpDeviceMute_003, TestSize.Level1)
192 {
193 A2dpDeviceConfigInfo configInfo;
194 configInfo.absVolumeSupport = true;
195 std::string device = "test_device";
196 AudioA2dpDevice::GetInstance().AddA2dpDevice(device, configInfo);
197 bool result = AudioA2dpDevice::GetInstance().SetA2dpDeviceMute(device, true);
198 EXPECT_TRUE(result);
199 A2dpDeviceConfigInfo info;
200 bool getInfoResult = AudioA2dpDevice::GetInstance().GetA2dpDeviceInfo(device, info);
201 EXPECT_TRUE(getInfoResult);
202 EXPECT_TRUE(info.mute);
203 AudioA2dpDevice::GetInstance().DelA2dpDevice(device);
204 }
205
206 /**
207 * @tc.name: GetA2dpDeviceMute_001
208 * @tc.desc: Test GetA2dpDeviceMute when the device does not exist in connectedA2dpDeviceMap_.
209 * @tc.type: FUNC
210 * @tc.require: #I5Y4MZ
211 */
212 HWTEST_F(AudioA2dpDeviceUnitTest, GetA2dpDeviceMute_001, TestSize.Level1)
213 {
214 std::string device = "non_existent_device";
215 bool isMute = false;
216 bool result = AudioA2dpDevice::GetInstance().GetA2dpDeviceMute(device, isMute);
217 EXPECT_FALSE(result);
218 }
219
220 /**
221 * @tc.name: GetA2dpDeviceMute_002
222 * @tc.desc: Test GetA2dpDeviceMute when the device exists but does not support absolute volume control.
223 * @tc.type: FUNC
224 * @tc.require: #I5Y4MZ
225 */
226 HWTEST_F(AudioA2dpDeviceUnitTest, GetA2dpDeviceMute_002, TestSize.Level1)
227 {
228 A2dpDeviceConfigInfo configInfo;
229 configInfo.absVolumeSupport = false;
230 std::string device = "test_device";
231 AudioA2dpDevice::GetInstance().AddA2dpDevice(device, configInfo);
232
233 bool isMute = false;
234 bool result = AudioA2dpDevice::GetInstance().GetA2dpDeviceMute(device, isMute);
235 EXPECT_FALSE(result);
236 AudioA2dpDevice::GetInstance().DelA2dpDevice(device);
237 }
238
239 /**
240 * @tc.name: GetA2dpDeviceMute_003
241 * @tc.desc: Test GetA2dpDeviceMute when the device exists and supports absolute volume control.
242 * @tc.type: FUNC
243 * @tc.require: #I5Y4MZ
244 */
245 HWTEST_F(AudioA2dpDeviceUnitTest, GetA2dpDeviceMute_003, TestSize.Level1)
246 {
247 A2dpDeviceConfigInfo configInfo;
248 configInfo.absVolumeSupport = true;
249 configInfo.mute = true;
250 std::string device = "test_device";
251 AudioA2dpDevice::GetInstance().AddA2dpDevice(device, configInfo);
252
253 bool isMute = false;
254 bool result = AudioA2dpDevice::GetInstance().GetA2dpDeviceMute(device, isMute);
255 EXPECT_TRUE(result);
256 EXPECT_TRUE(isMute);
257 AudioA2dpDevice::GetInstance().DelA2dpDevice(device);
258 }
259
260 /**
261 * @tc.name: SetA2dpDeviceAbsVolumeSupport_001
262 * @tc.desc: Test SetA2dpDeviceAbsVolumeSupport when the device exists in connectedA2dpDeviceMap_.
263 * @tc.type: FUNC
264 * @tc.require: #I5Y4MZ
265 */
266 HWTEST_F(AudioA2dpDeviceUnitTest, SetA2dpDeviceAbsVolumeSupport_001, TestSize.Level1)
267 {
268 A2dpDeviceConfigInfo configInfo;
269 configInfo.absVolumeSupport = false;
270 std::string device = "test_device";
271 AudioA2dpDevice::GetInstance().AddA2dpDevice(device, configInfo);
272 bool support = true;
273 int32_t volume = 0;
274 bool mute = true;
275 bool result = AudioA2dpDevice::GetInstance().SetA2dpDeviceAbsVolumeSupport(device, support, volume, mute);
276 EXPECT_TRUE(result);
277 A2dpDeviceConfigInfo info;
278 bool getInfoResult = AudioA2dpDevice::GetInstance().GetA2dpDeviceInfo(device, info);
279 EXPECT_TRUE(getInfoResult);
280 EXPECT_TRUE(info.absVolumeSupport);
281 AudioA2dpDevice::GetInstance().DelA2dpDevice(device);
282 }
283
284 /**
285 * @tc.name: SetA2dpDeviceAbsVolumeSupport_002
286 * @tc.desc: Test SetA2dpDeviceAbsVolumeSupport when the device does not exist in connectedA2dpDeviceMap_.
287 * @tc.type: FUNC
288 * @tc.require: #I5Y4MZ
289 */
290 HWTEST_F(AudioA2dpDeviceUnitTest, SetA2dpDeviceAbsVolumeSupport_002, TestSize.Level1)
291 {
292 std::string device = "non_existent_device";
293 bool support = true;
294 int32_t volume = 0;
295 bool mute = true;
296 bool result = AudioA2dpDevice::GetInstance().SetA2dpDeviceAbsVolumeSupport(device, support, volume, mute);
297 EXPECT_FALSE(result);
298 }
299
300 /**
301 * @tc.name: SetA2dpDeviceVolumeLevel_001
302 * @tc.desc: Test SetA2dpDeviceVolumeLevel when the device does not exist in connectedA2dpDeviceMap_.
303 * @tc.type: FUNC
304 * @tc.require: #I5Y4MZ
305 */
306 HWTEST_F(AudioA2dpDeviceUnitTest, SetA2dpDeviceVolumeLevel_001, TestSize.Level1)
307 {
308 std::string device = "non_existent_device";
309 int32_t volumeLevel = 50;
310 bool result = AudioA2dpDevice::GetInstance().SetA2dpDeviceVolumeLevel(device, volumeLevel);
311 EXPECT_FALSE(result);
312 }
313
314 /**
315 * @tc.name: SetA2dpDeviceVolumeLevel_002
316 * @tc.desc: Test SetA2dpDeviceVolumeLevel when the device exists but does not support absolute volume control.
317 * @tc.type: FUNC
318 * @tc.require: #I5Y4MZ
319 */
320 HWTEST_F(AudioA2dpDeviceUnitTest, SetA2dpDeviceVolumeLevel_002, TestSize.Level1)
321 {
322 A2dpDeviceConfigInfo configInfo;
323 configInfo.absVolumeSupport = false;
324 std::string device = "test_device";
325 AudioA2dpDevice::GetInstance().AddA2dpDevice(device, configInfo);
326 int32_t volumeLevel = 50;
327 bool result = AudioA2dpDevice::GetInstance().SetA2dpDeviceVolumeLevel(device, volumeLevel);
328 EXPECT_FALSE(result);
329 AudioA2dpDevice::GetInstance().DelA2dpDevice(device);
330 }
331
332 /**
333 * @tc.name: SetA2dpDeviceVolumeLevel_003
334 * @tc.desc: Test SetA2dpDeviceVolumeLevel when the device exists and supports absolute volume control.
335 * @tc.type: FUNC
336 * @tc.require: #I5Y4MZ
337 */
338 HWTEST_F(AudioA2dpDeviceUnitTest, SetA2dpDeviceVolumeLevel_003, TestSize.Level1)
339 {
340 A2dpDeviceConfigInfo configInfo;
341 configInfo.absVolumeSupport = true;
342 std::string device = "test_device";
343 AudioA2dpDevice::GetInstance().AddA2dpDevice(device, configInfo);
344 int32_t volumeLevel = 50;
345 bool result = AudioA2dpDevice::GetInstance().SetA2dpDeviceVolumeLevel(device, volumeLevel);
346 EXPECT_TRUE(result);
347 A2dpDeviceConfigInfo info;
348 bool getInfoResult = AudioA2dpDevice::GetInstance().GetA2dpDeviceInfo(device, info);
349 EXPECT_TRUE(getInfoResult);
350 EXPECT_EQ(info.volumeLevel, volumeLevel);
351 AudioA2dpDevice::GetInstance().DelA2dpDevice(device);
352 }
353
354 /**
355 * @tc.name: CheckHearingAidDeviceExist_001
356 * @tc.desc: Test CheckHearingAidDeviceExist_001 when the device exists and supports absolute volume control.
357 * @tc.type: FUNC
358 * @tc.require: #I5Y4MZ
359 */
360 HWTEST_F(AudioA2dpDeviceUnitTest, CheckHearingAidDeviceExist_001, TestSize.Level1)
361 {
362 A2dpDeviceConfigInfo configInfo;
363 std::string device = "test_device";
364 AudioA2dpDevice::GetInstance().AddHearingAidDevice(device, configInfo);
365
366 bool result = AudioA2dpDevice::GetInstance().CheckHearingAidDeviceExist(device);
367 EXPECT_TRUE(result);
368 AudioA2dpDevice::GetInstance().DelHearingAidDevice(device);
369
370 result = AudioA2dpDevice::GetInstance().CheckHearingAidDeviceExist(device);
371 EXPECT_FALSE(result);
372 }
373 } // namespace AudioStandard
374 } // namespace OHOS