1 /*
2 * Copyright (c) 2024 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 "dspeaker_dev_test.h"
17
18 using namespace testing::ext;
19
20 namespace OHOS {
21 namespace DistributedHardware {
22 constexpr int32_t DH_ID = 1;
23 constexpr int32_t DH_ID_SPK = 134217728;
24 const std::string DEV_ID = "Test_Dev_Id";
25 const std::string CAP = "Test_Capability";
26
SetUpTestCase(void)27 void DSpeakerDevTest::SetUpTestCase(void) {}
28
TearDownTestCase(void)29 void DSpeakerDevTest::TearDownTestCase(void) {}
30
SetUp(void)31 void DSpeakerDevTest::SetUp(void)
32 {
33 eventCb_ = std::make_shared<MockIAudioEventCallback>();
34 spk_ = std::make_shared<DSpeakerDev>(DEV_ID, eventCb_);
35 }
36
TearDown(void)37 void DSpeakerDevTest::TearDown(void)
38 {
39 eventCb_ = nullptr;
40 spk_ = nullptr;
41 }
42
43 /**
44 * @tc.name: InitSenderEngine_001
45 * @tc.desc: Verify InitSenderEngine function.
46 * @tc.type: FUNC
47 * @tc.require: AR000H0E5F
48 */
49 HWTEST_F(DSpeakerDevTest, InitSenderEngine_001, TestSize.Level1)
50 {
51 IAVEngineProvider *providerPtr = nullptr;
52 AVTransEvent event = { EventType::EVENT_START_SUCCESS, "", "" };
53 spk_->OnEngineTransEvent(event);
54 event.type = EventType::EVENT_STOP_SUCCESS;
55 spk_->OnEngineTransEvent(event);
56 event.type = EventType::EVENT_CHANNEL_CLOSED;
57 spk_->OnEngineTransEvent(event);
58 event.type = EventType::EVENT_START_FAIL;
59 spk_->OnEngineTransEvent(event);
60 std::shared_ptr<AVTransMessage> message = nullptr;
61 spk_->OnEngineTransMessage(message);
62 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->InitSenderEngine(providerPtr));
63 spk_->speakerTrans_ = std::make_shared<MockIAudioDataTransport>();
64 EXPECT_EQ(DH_SUCCESS, spk_->InitSenderEngine(providerPtr));
65 }
66
67 /**
68 * @tc.name: InitReceiverEngine_001
69 * @tc.desc: Verify InitReceiverEngine function.
70 * @tc.type: FUNC
71 * @tc.require: AR000H0E5F
72 */
73 HWTEST_F(DSpeakerDevTest, InitReceiverEngine_001, TestSize.Level1)
74 {
75 IAVEngineProvider *providerPtr = nullptr;
76 EXPECT_EQ(DH_SUCCESS, spk_->InitReceiverEngine(providerPtr));;
77 }
78
79 /**
80 * @tc.name: EnableDSpeaker_001
81 * @tc.desc: Verify EnableDSpeaker and EnableDevice function.
82 * @tc.type: FUNC
83 * @tc.require: AR000H0E5F
84 */
85 HWTEST_F(DSpeakerDevTest, EnableDSpeaker_001, TestSize.Level1)
86 {
87 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->EnableDevice(DH_ID, CAP));
88 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->EnableDevice(DH_ID, CAP));
89
90 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->EnableDevice(DH_ID_SPK, CAP));
91 }
92
93 /**
94 * @tc.name: DisableDSpeaker_001
95 * @tc.desc: Verify DisableDSpeaker and DisableDevice function.
96 * @tc.type: FUNC
97 * @tc.require: AR000H0E5F
98 */
99 HWTEST_F(DSpeakerDevTest, DisableDSpeaker_001, TestSize.Level1)
100 {
101 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->DisableDevice(DH_ID));
102
103 spk_->curPort_ = DH_ID_SPK;
104 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->DisableDevice(DH_ID_SPK));
105 EXPECT_FALSE(spk_->IsOpened());
106 }
107
108 /**
109 * @tc.name: CreateStream_001
110 * @tc.desc: Verify CreateStream function.
111 * @tc.type: FUNC
112 * @tc.require: AR000H0E5F
113 */
114 HWTEST_F(DSpeakerDevTest, CreateStream_001, TestSize.Level1)
115 {
116 EXPECT_EQ(DH_SUCCESS, spk_->CreateStream(streamId_));
117
118 eventCb_ = nullptr;
119 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->CreateStream(streamId_));
120 }
121
122 /**
123 * @tc.name: DestroyStream_001
124 * @tc.desc: Verify DestroyStream function.
125 * @tc.type: FUNC
126 * @tc.require: AR000H0E5F
127 */
128 HWTEST_F(DSpeakerDevTest, DestroyStream_001, TestSize.Level1)
129 {
130 EXPECT_EQ(DH_SUCCESS, spk_->DestroyStream(streamId_));
131
132 eventCb_ = nullptr;
133 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->DestroyStream(streamId_));
134 }
135
136 /**
137 * @tc.name: SetParameters_001
138 * @tc.desc: Verify SetParameters and GetAudioParam function.
139 * @tc.type: FUNC
140 * @tc.require: AR000H0E5F
141 */
142 HWTEST_F(DSpeakerDevTest, SetParameters_001, TestSize.Level1)
143 {
144 const AudioParamHDF param = {
145 .sampleRate = SAMPLE_RATE_8000,
146 .channelMask = STEREO,
147 .bitFormat = SAMPLE_U8,
148 .streamUsage = STREAM_USAGE_UNKNOWN,
149 .frameSize = 30,
150 .period = 0,
151 .ext = "Test",
152 };
153 EXPECT_EQ(DH_SUCCESS, spk_->SetParameters(streamId_, param));
154 spk_->GetAudioParam();
155 }
156
157 /**
158 * @tc.name: SetParameters_002
159 * @tc.desc: Verify SetParameters and GetAudioParam function.
160 * @tc.type: FUNC
161 * @tc.require: AR000H0E5F
162 */
163 HWTEST_F(DSpeakerDevTest, SetParameters_002, TestSize.Level1)
164 {
165 const AudioParamHDF param = {
166 .sampleRate = SAMPLE_RATE_8000,
167 .channelMask = STEREO,
168 .bitFormat = SAMPLE_U8,
169 .streamUsage = STREAM_USAGE_VOICE_COMMUNICATION,
170 .frameSize = 30,
171 .period = 0,
172 .ext = "Test",
173 };
174 std::vector<AudioCodecType> container = spk_->codec_;
175 spk_->codec_.clear();
176 spk_->GetCodecCaps(OHOS::DistributedHardware::AAC);
177 spk_->GetCodecCaps(OHOS::DistributedHardware::OPUS);
178 auto ret = spk_->SetParameters(streamId_, param);
179 spk_->GetAudioParam();
180 spk_->codec_ = container;
181 EXPECT_EQ(DH_SUCCESS, ret);
182 }
183
184 /**
185 * @tc.name: NotifyEvent_001
186 * @tc.desc: Verify NotifyEvent function.
187 * @tc.type: FUNC
188 * @tc.require: AR000H0E5F
189 */
190 HWTEST_F(DSpeakerDevTest, NotifyEvent_001, TestSize.Level1)
191 {
192 AudioEvent event = AudioEvent(OPEN_SPEAKER, "OPEN_SPEAKER");
193 EXPECT_EQ(DH_SUCCESS, spk_->NotifyEvent(streamId_, event));
194
195 event.type = EVENT_UNKNOWN;
196 EXPECT_EQ(DH_SUCCESS, spk_->NotifyEvent(streamId_, event));
197
198 eventCb_ = nullptr;
199 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->NotifyEvent(streamId_, event));
200 }
201
202 /**
203 * @tc.name: SetUp_001
204 * @tc.desc: Verify SetUp function.
205 * @tc.type: FUNC
206 * @tc.require: AR000H0E5F
207 */
208 HWTEST_F(DSpeakerDevTest, SetUp_001, TestSize.Level1)
209 {
210 spk_->speakerTrans_ = nullptr;
211 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->SetUp());
212
213 spk_->speakerTrans_ = std::make_shared<MockIAudioDataTransport>();
214 EXPECT_EQ(DH_SUCCESS, spk_->SetUp());
215 }
216
217 /**
218 * @tc.name: Start_001
219 * @tc.desc: Verify Start and IsOpened function.
220 * @tc.type: FUNC
221 * @tc.require: AR000H0E5F
222 */
223 HWTEST_F(DSpeakerDevTest, Start_001, TestSize.Level1)
224 {
225 spk_->speakerTrans_ = nullptr;
226 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->Start());
227
228 spk_->speakerTrans_ = std::make_shared<AVTransSenderTransport>(DEV_ID, spk_);
229 EXPECT_NE(DH_SUCCESS, spk_->Start());
230 EXPECT_FALSE(spk_->IsOpened());
231 }
232
233 /**
234 * @tc.name: Start_002
235 * @tc.desc: Verify Start and IsOpened function.
236 * @tc.type: FUNC
237 * @tc.require: AR000H0E5F
238 */
239 HWTEST_F(DSpeakerDevTest, Start_002, TestSize.Level1)
240 {
241 spk_->speakerTrans_ = nullptr;
242 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->SetUp());
243 EXPECT_NE(DH_SUCCESS, spk_->Start());
244 EXPECT_FALSE(spk_->IsOpened());
245 }
246
247 /**
248 * @tc.name: Start_003
249 * @tc.desc: Verify Start and IsOpened function.
250 * @tc.type: FUNC
251 * @tc.require: AR000H0E5F
252 */
253 HWTEST_F(DSpeakerDevTest, Start_003, TestSize.Level1)
254 {
255 spk_->speakerTrans_ = std::make_shared<MockIAudioDataTransport>();
256 EXPECT_EQ(DH_SUCCESS, spk_->SetUp());
257 EXPECT_EQ(ERR_DH_AUDIO_SA_WAIT_TIMEOUT, spk_->Start());
258
259 spk_->isTransReady_.store(true);
260 EXPECT_EQ(DH_SUCCESS, spk_->Start());
261 EXPECT_TRUE(spk_->IsOpened());
262 }
263
264 /**
265 * @tc.name: Stop_001
266 * @tc.desc: Verify Stop and IsOpened function.
267 * @tc.type: FUNC
268 * @tc.require: AR000H0E5F
269 */
270 HWTEST_F(DSpeakerDevTest, Stop_001, TestSize.Level1)
271 {
272 spk_->speakerTrans_ = nullptr;
273 EXPECT_EQ(DH_SUCCESS, spk_->Stop());
274 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->SetUp());
275 EXPECT_EQ(DH_SUCCESS, spk_->Stop());
276
277 spk_->speakerTrans_ = std::make_shared<MockIAudioDataTransport>();
278 EXPECT_EQ(DH_SUCCESS, spk_->Stop());
279 EXPECT_FALSE(spk_->IsOpened());
280 }
281
282 /**
283 * @tc.name: Stop_002
284 * @tc.desc: Verify Stop and IsOpened function.
285 * @tc.type: FUNC
286 * @tc.require: AR000H0E5F
287 */
288 HWTEST_F(DSpeakerDevTest, Stop_002, TestSize.Level1)
289 {
290 spk_->speakerTrans_ = nullptr;
291 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->SetUp());
292 EXPECT_NE(DH_SUCCESS, spk_->Start());
293 EXPECT_EQ(DH_SUCCESS, spk_->Stop());
294 EXPECT_FALSE(spk_->IsOpened());
295 }
296
297 /**
298 * @tc.name: Pause_001
299 * @tc.desc: Verify Pause function.
300 * @tc.type: FUNC
301 * @tc.require: AR000H0E5F
302 */
303 HWTEST_F(DSpeakerDevTest, Pause_001, TestSize.Level1)
304 {
305 spk_->speakerTrans_ = nullptr;
306 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->Pause());
307
308 spk_->speakerTrans_ = std::make_shared<AVTransSenderTransport>(DEV_ID, spk_);
309 EXPECT_NE(DH_SUCCESS, spk_->Pause());
310
311 spk_->speakerTrans_ = std::make_shared<MockIAudioDataTransport>();
312 EXPECT_EQ(DH_SUCCESS, spk_->Pause());
313 }
314
315 /**
316 * @tc.name: Restart_001
317 * @tc.desc: Verify Restart function.
318 * @tc.type: FUNC
319 * @tc.require: AR000H0E5F
320 */
321 HWTEST_F(DSpeakerDevTest, Restart_001, TestSize.Level1)
322 {
323 spk_->speakerTrans_ = nullptr;
324 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->Restart());
325
326 const AudioParamHDF param = {
327 .sampleRate = SAMPLE_RATE_8000,
328 .channelMask = STEREO,
329 .bitFormat = SAMPLE_U8,
330 .streamUsage = STREAM_USAGE_UNKNOWN,
331 .frameSize = 30,
332 .period = 0,
333 .ext = "Test",
334 };
335 EXPECT_EQ(DH_SUCCESS, spk_->SetParameters(streamId_, param));
336 spk_->speakerTrans_ = std::make_shared<AVTransSenderTransport>(DEV_ID, spk_);
337 EXPECT_NE(DH_SUCCESS, spk_->Restart());
338
339 spk_->speakerTrans_ = std::make_shared<MockIAudioDataTransport>();
340 EXPECT_EQ(DH_SUCCESS, spk_->Restart());
341 }
342
343 /**
344 * @tc.name: Release_001
345 * @tc.desc: Verify Release function.
346 * @tc.type: FUNC
347 * @tc.require: AR000H0E5F
348 */
349 HWTEST_F(DSpeakerDevTest, Release_001, TestSize.Level1)
350 {
351 spk_->speakerTrans_ = nullptr;
352 EXPECT_EQ(DH_SUCCESS, spk_->Release());
353
354 spk_->speakerTrans_ = std::make_shared<AVTransSenderTransport>(DEV_ID, spk_);
355 EXPECT_EQ(DH_SUCCESS, spk_->Release());
356
357 spk_->speakerTrans_ = std::make_shared<MockIAudioDataTransport>();
358 EXPECT_EQ(DH_SUCCESS, spk_->Release());
359
360 int32_t fd = 10;
361 int32_t ashmemLength = 10;
362 int32_t streamId = 1;
363 int32_t lengthPerTrans = 10;
364 EXPECT_EQ(DH_SUCCESS, spk_->RefreshAshmemInfo(streamId, fd, ashmemLength, lengthPerTrans));
365 spk_->param_.renderOpts.renderFlags = MMAP_MODE;
366 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->RefreshAshmemInfo(streamId, fd, ashmemLength, lengthPerTrans));
367 }
368
369 /**
370 * @tc.name: WriteStreamData_001
371 * @tc.desc: Verify WriteStreamData and ReadStreamData function.
372 * @tc.type: FUNC
373 * @tc.require: AR000H0E5F
374 */
375 HWTEST_F(DSpeakerDevTest, WriteStreamData_001, TestSize.Level1)
376 {
377 const size_t capacity = 1;
378 auto writeData = std::make_shared<AudioData>(capacity);
379 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->WriteStreamData(streamId_, writeData));
380
381 std::shared_ptr<AudioData> readData = nullptr;
382 EXPECT_EQ(DH_SUCCESS, spk_->ReadStreamData(streamId_, readData));
383
384 std::shared_ptr<AudioData> data = nullptr;
385 EXPECT_EQ(DH_SUCCESS, spk_->OnDecodeTransDataDone(data));
386 }
387
388 /**
389 * @tc.name: WriteStreamData_002
390 * @tc.desc: Verify WriteStreamData function.
391 * @tc.type: FUNC
392 * @tc.require: AR000H0E5F
393 */
394 HWTEST_F(DSpeakerDevTest, WriteStreamData_002, TestSize.Level1)
395 {
396 const size_t capacity = 1;
397 auto writeData = std::make_shared<AudioData>(capacity);
398 spk_->speakerTrans_ = std::make_shared<AVTransSenderTransport>(DEV_ID, spk_);
399 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->WriteStreamData(streamId_, writeData));
400
401 spk_->speakerTrans_ = std::make_shared<MockIAudioDataTransport>();
402 EXPECT_EQ(DH_SUCCESS, spk_->WriteStreamData(streamId_, writeData));
403 }
404
405 /**
406 * @tc.name: NotifyHdfAudioEvent_001
407 * @tc.desc: Verify NotifyHdfAudioEvent function.
408 * @tc.type: FUNC
409 * @tc.require: AR000H0E5F
410 */
411 HWTEST_F(DSpeakerDevTest, ReadMmapPosition_001, TestSize.Level1)
412 {
413 int32_t streamId = 0;
414 uint64_t frames = 0;
415 CurrentTimeHDF time;
416 EXPECT_EQ(DH_SUCCESS, spk_->ReadMmapPosition(streamId, frames, time));
417 }
418
419 /**
420 * @tc.name: MmapStart_001
421 * @tc.desc: Verify MmapStart function.
422 * @tc.type: FUNC
423 * @tc.require: AR000H0E5F
424 */
425 HWTEST_F(DSpeakerDevTest, MmapStart_001, TestSize.Level1)
426 {
427 spk_->ashmem_ = nullptr;
428 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->MmapStart());
429 }
430
431 /**
432 * @tc.name: NotifyHdfAudioEvent_001
433 * @tc.desc: Verify NotifyHdfAudioEvent function.
434 * @tc.type: FUNC
435 * @tc.require: AR000H0E5F
436 */
437 HWTEST_F(DSpeakerDevTest, NotifyHdfAudioEvent_001, TestSize.Level1)
438 {
439 AudioEvent event = AudioEvent(OPEN_SPEAKER, "OPEN_SPEAKER");
440 int32_t dhId = 0;
441 EXPECT_EQ(DH_SUCCESS, spk_->NotifyHdfAudioEvent(event, dhId));
442
443 event.type = SPEAKER_OPENED;
444 dhId = DH_ID_SPK;
445 EXPECT_EQ(DH_SUCCESS, spk_->NotifyHdfAudioEvent(event, dhId));
446 }
447
448 /**
449 * @tc.name: OnStateChange_001
450 * @tc.desc: Verify OnStateChange function.
451 * @tc.type: FUNC
452 * @tc.require: AR000H0E5F
453 */
454 HWTEST_F(DSpeakerDevTest, OnStateChange_001, TestSize.Level1)
455 {
456 AudioEventType event = DATA_OPENED;
457 EXPECT_EQ(DH_SUCCESS, spk_->OnStateChange(event));
458
459 event = DATA_CLOSED;
460 EXPECT_EQ(DH_SUCCESS, spk_->OnStateChange(event));
461
462 event = EVENT_UNKNOWN;
463 EXPECT_EQ(DH_SUCCESS, spk_->OnStateChange(event));
464
465 eventCb_ = nullptr;
466 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->OnStateChange(event));
467 }
468
469 /**
470 * @tc.name: SendMessage_001
471 * @tc.desc: Verify SendMessage function.
472 * @tc.type: FUNC
473 * @tc.require: AR000H0E5F
474 */
475 HWTEST_F(DSpeakerDevTest, SendMessage_001, TestSize.Level1)
476 {
477 std::string content = "content";
478 std::string dstDevId = "dstDevId";
479 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->SendMessage(MIC_OPENED, content, dstDevId));
480 EXPECT_EQ(ERR_DH_AUDIO_NULLPTR, spk_->SendMessage(OPEN_SPEAKER, content, dstDevId));
481 spk_->speakerTrans_ = std::make_shared<MockIAudioDataTransport>();
482 spk_->InitCtrlTrans();
483 EXPECT_EQ(DH_SUCCESS, spk_->SendMessage(OPEN_SPEAKER, content, dstDevId));
484 }
485
486 /**
487 * @tc.name: AddToVec_001
488 * @tc.desc: Verify AddToVec function.
489 * @tc.type: FUNC
490 * @tc.require: AR000H0E5F
491 */
492 HWTEST_F(DSpeakerDevTest, AddToVec_001, TestSize.Level1)
493 {
494 std::vector<AudioCodecType> container;
495 spk_->AddToVec(container, AudioCodecType::AUDIO_CODEC_AAC);
496 EXPECT_EQ(1, container.size());
497 }
498
499 /**
500 * @tc.name: GetCodecCaps_001
501 * @tc.desc: Verify GetCodecCaps function.
502 * @tc.type: FUNC
503 * @tc.require: AR000H0E5F
504 */
505 HWTEST_F(DSpeakerDevTest, GetCodecCaps_001, TestSize.Level1)
506 {
507 std::vector<AudioCodecType> container = spk_->codec_;
508 spk_->codec_.clear();
509 spk_->GetCodecCaps(OHOS::DistributedHardware::AAC);
510 auto num = spk_->codec_.size();
511 EXPECT_EQ(1, num);
512 spk_->GetCodecCaps(OHOS::DistributedHardware::OPUS);
513 num = spk_->codec_.size();
514 spk_->codec_ = container;
515 EXPECT_EQ(2, num);
516 }
517
518 /**
519 * @tc.name: IsMimeSupported_001
520 * @tc.desc: Verify IsMimeSupported function.
521 * @tc.type: FUNC
522 * @tc.require: AR000H0E5F
523 */
524 HWTEST_F(DSpeakerDevTest, IsMimeSupported_001, TestSize.Level1)
525 {
526 std::vector<AudioCodecType> container = spk_->codec_;
527 spk_->codec_.clear();
528 spk_->GetCodecCaps(OHOS::DistributedHardware::AAC);
529 bool ret = spk_->IsMimeSupported(AudioCodecType::AUDIO_CODEC_AAC_EN);
530 EXPECT_EQ(ret, true);
531 ret = spk_->IsMimeSupported(AudioCodecType::AUDIO_CODEC_OPUS);
532 spk_->codec_ = container;
533 EXPECT_EQ(ret, false);
534 }
535 } // namespace DistributedHardware
536 } // namespace OHOS
537