1 /*
2 * Copyright (c) 2022 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 "hdi_service_common.h"
18
19 using namespace std;
20 using namespace testing::ext;
21 using namespace OHOS::Audio;
22
23 namespace {
24 const int BUFFER_SIZE = 16384;
25 const int BUFFER_SIZE_LITTLE = 0;
26 const uint64_t FILESIZE = 1024;
27
28 class AudioIdlHdiCaptureTest : public testing::Test {
29 public:
30 static void SetUpTestCase(void);
31 static void TearDownTestCase(void);
32 void SetUp();
33 void TearDown();
34 struct IAudioAdapter *adapter = nullptr;
35 struct IAudioCapture *capture = nullptr;
36 static TestAudioManager *manager;
37 uint32_t captureId_ = 0;
38 };
39
40 TestAudioManager *AudioIdlHdiCaptureTest::manager = nullptr;
41 using THREAD_FUNC = void *(*)(void *);
42
SetUpTestCase(void)43 void AudioIdlHdiCaptureTest::SetUpTestCase(void)
44 {
45 manager = IAudioManagerGet(IS_STUB);
46 ASSERT_NE(nullptr, manager);
47 }
48
TearDownTestCase(void)49 void AudioIdlHdiCaptureTest::TearDownTestCase(void)
50 {
51 if (manager != nullptr) {
52 (void)IAudioManagerRelease(manager, IS_STUB);
53 }
54 }
55
SetUp(void)56 void AudioIdlHdiCaptureTest::SetUp(void)
57 {
58 ASSERT_NE(nullptr, manager);
59 int32_t ret = AudioCreateCapture(manager, PIN_IN_MIC, ADAPTER_NAME, &adapter, &capture, &captureId_);
60 ASSERT_EQ(HDF_SUCCESS, ret);
61 }
62
TearDown(void)63 void AudioIdlHdiCaptureTest::TearDown(void)
64 {
65 int32_t ret = ReleaseCaptureSource(manager, adapter, capture, captureId_);
66 ASSERT_EQ(HDF_SUCCESS, ret);
67 }
68
69 /**
70 * @tc.name AudioCaptureFrame_001
71 * @tc.desc test AudioCaptureCaptureFrame interface,Returns 0 if the input data is read successfully
72 * @tc.type: FUNC
73 */
74 HWTEST_F(AudioIdlHdiCaptureTest, AudioCaptureFrame_001, TestSize.Level1)
75 {
76 int32_t ret;
77 uint32_t replyBytes = BUFFER_SIZE;
78 uint64_t requestBytes = BUFFER_SIZE;
79 ASSERT_NE(nullptr, capture);
80 ret = capture->Start(capture);
81 EXPECT_EQ(HDF_SUCCESS, ret);
82 int8_t *frame = (int8_t *)calloc(1, BUFFER_SIZE);
83 EXPECT_NE(nullptr, frame);
84 ret = capture->CaptureFrame(capture, frame, &replyBytes, &requestBytes);
85 EXPECT_EQ(HDF_SUCCESS, ret);
86 capture->Stop(capture);
87
88 if (frame != nullptr) {
89 free(frame);
90 frame = nullptr;
91 }
92 }
93 /**
94 * @tc.name AudioCaptureFrameNull_002
95 * @tc.desc test AudioCaptureCaptureFrame interface,Returns -3 if the incoming parameter frame is nullptr
96 * @tc.type: FUNC
97 */
98 HWTEST_F(AudioIdlHdiCaptureTest, AudioCaptureFrameNull_002, TestSize.Level1)
99 {
100 int32_t ret;
101 uint32_t replyBytes = BUFFER_SIZE;
102 uint64_t requestBytes = BUFFER_SIZE;
103 int8_t *frame = nullptr;
104 ASSERT_NE(nullptr, capture);
105 ret = capture->Start(capture);
106 EXPECT_EQ(HDF_SUCCESS, ret);
107 ret = capture->CaptureFrame(capture, frame, &replyBytes, &requestBytes);
108 EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret);
109 capture->Stop(capture);
110 }
111 #ifdef AUDIO_ADM_PASSTHROUGH
112 /**
113 * @tc.name AudioCaptureFrameNull_003
114 * @tc.desc test AudioCaptureCaptureFrame interface,Returns -3 if the incoming parameter replyBytes is nullptr
115 * @tc.type: FUNC
116 */
117 HWTEST_F(AudioIdlHdiCaptureTest, AudioCaptureFrameNull_003, TestSize.Level1)
118 {
119 int32_t ret;
120 uint64_t requestBytes = BUFFER_SIZE;
121 uint32_t *replyBytes = nullptr;
122 ASSERT_NE(nullptr, capture);
123 ret = capture->Start(capture);
124 EXPECT_EQ(HDF_SUCCESS, ret);
125 int8_t *frame = (int8_t *)calloc(1, BUFFER_SIZE);
126 EXPECT_NE(nullptr, frame);
127 ret = capture->CaptureFrame(capture, frame, replyBytes, &requestBytes);
128 EXPECT_EQ(HDF_ERR_INVALID_PARAM, ret);
129
130 capture->Stop(capture);
131
132 if (frame != nullptr) {
133 free(frame);
134 frame = nullptr;
135 }
136 }
137 #endif
138 /**
139 * @tc.name AudioCaptureFrameNull_004
140 * @tc.desc test AudioCaptureCaptureFrame interface,Returns -3/-4 if the incoming parameter capture is nullptr
141 * @tc.type: FUNC
142 */
143 HWTEST_F(AudioIdlHdiCaptureTest, AudioCaptureFrameNull_004, TestSize.Level1)
144 {
145 int32_t ret;
146 uint64_t requestBytes = BUFFER_SIZE;
147 uint32_t replyBytes = BUFFER_SIZE;
148 struct IAudioCapture *captureNull = nullptr;
149 ASSERT_NE(nullptr, capture);
150 ret = capture->Start(capture);
151 EXPECT_EQ(HDF_SUCCESS, ret);
152 int8_t *frame = (int8_t *)calloc(1, BUFFER_SIZE);
153 EXPECT_NE(nullptr, frame);
154 ret = capture->CaptureFrame(captureNull, frame, &replyBytes, &requestBytes);
155 ASSERT_TRUE(ret == HDF_ERR_INVALID_PARAM || ret == HDF_ERR_INVALID_OBJECT);
156
157 capture->Stop(capture);
158
159 if (frame != nullptr) {
160 free(frame);
161 frame = nullptr;
162 }
163 }
164 /**
165 * @tc.name AudioCaptureFrame_005
166 * @tc.desc Test AudioCaptureFrame interface,Returns -3 if without calling interface capturestart
167 * @tc.type: FUNC
168 */
169 HWTEST_F(AudioIdlHdiCaptureTest, AudioCaptureFrame_005, TestSize.Level1)
170 {
171 int32_t ret;
172 uint64_t requestBytes = BUFFER_SIZE;
173 uint32_t replyBytes = BUFFER_SIZE;
174 ASSERT_NE(nullptr, capture);
175 int8_t *frame = (int8_t *)calloc(1, BUFFER_SIZE);
176 EXPECT_NE(nullptr, frame);
177 ret = capture->CaptureFrame(capture, frame, &replyBytes, &requestBytes);
178 ASSERT_TRUE(ret == HDF_ERR_INVALID_PARAM || ret == HDF_SUCCESS);
179
180 if (frame != nullptr) {
181 free(frame);
182 frame = nullptr;
183 }
184 }
185 /**
186 less than interface requirements
187 * @tc.name AudioCaptureFrame_006
188 * @tc.desc test AudioCaptureCaptureFrame interface,Returns -1 if the incoming parameter
189 requestBytes less than interface requirements
190 * @tc.type: FUNC
191 */
192 HWTEST_F(AudioIdlHdiCaptureTest, AudioCaptureFrame_006, TestSize.Level1)
193 {
194 int32_t ret;
195 uint64_t requestBytes = BUFFER_SIZE_LITTLE;
196 uint32_t replyBytes = BUFFER_SIZE;
197
198 ASSERT_NE(nullptr, capture);
199 ret = capture->Start(capture);
200 EXPECT_EQ(HDF_SUCCESS, ret);
201 int8_t *frame = (int8_t *)calloc(1, BUFFER_SIZE);
202 EXPECT_NE(nullptr, frame);
203 ret = capture->CaptureFrame(capture, frame, &replyBytes, &requestBytes);
204 EXPECT_EQ(HDF_SUCCESS, ret);
205
206 capture->Stop(capture);
207
208 if (frame != nullptr) {
209 free(frame);
210 frame = nullptr;
211 }
212 }
213 /**
214 * @tc.name AudioCaptureGetCapturePosition_001
215 * @tc.desc Test AudioCaptureGetCapturePosition interface,Returns 0 if get CapturePosition during playing.
216 * @tc.type: FUNC
217 */
218 HWTEST_F(AudioIdlHdiCaptureTest, AudioCaptureGetCapturePosition_001, TestSize.Level1)
219 {
220 int32_t ret;
221 uint64_t frames = 0;
222 int64_t timeExp = 0;
223 struct AudioTimeStamp time = {.tvSec = 0, .tvNSec = 0};
224 ASSERT_NE(nullptr, capture);
225 struct PrepareAudioPara audiopara = {
226 .capture = capture, .portType = PORT_IN, .adapterName = ADAPTER_NAME.c_str(), .pins = PIN_IN_MIC,
227 .path = AUDIO_CAPTURE_FILE.c_str(), .fileSize = FILESIZE
228 };
229
230 ret = pthread_create(&audiopara.tids, NULL, (THREAD_FUNC)RecordAudio, &audiopara);
231 ASSERT_EQ(HDF_SUCCESS, ret);
232 sleep(1);
233 if (audiopara.capture != nullptr) {
234 ret = audiopara.capture->GetCapturePosition(audiopara.capture, &frames, &time);
235 if (ret == HDF_SUCCESS) {
236 EXPECT_GT((time.tvSec) * SECTONSEC + (time.tvNSec), timeExp);
237 EXPECT_GT(frames, INITIAL_VALUE);
238 }
239 }
240
241 ret = ThreadRelease(audiopara);
242 ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_ERR_NOT_SUPPORT);
243 }
244 /**
245 * @tc.name AudioCaptureGetCapturePosition_002
246 * @tc.desc Test GetCapturePosition interface,Returns 0 if get Position after Pause and resume during playing
247 * @tc.type: FUNC
248 */
249 HWTEST_F(AudioIdlHdiCaptureTest, AudioCaptureGetCapturePosition_002, TestSize.Level1)
250 {
251 int32_t ret;
252 int64_t timeExp = 0;
253 uint64_t frames = 0;
254 struct AudioTimeStamp timeCount = {.tvSec = 0, .tvNSec = 0};
255 ASSERT_NE(nullptr, capture);
256 struct PrepareAudioPara audiopara = {
257 .capture = capture, .portType = PORT_IN, .adapterName = ADAPTER_NAME.c_str(), .pins = PIN_IN_MIC,
258 .path = AUDIO_CAPTURE_FILE.c_str(), .fileSize = FILESIZE
259 };
260
261 ret = pthread_create(&audiopara.tids, NULL, (THREAD_FUNC)RecordAudio, &audiopara);
262 ASSERT_EQ(HDF_SUCCESS, ret);
263 sleep(1);
264 if (audiopara.capture != nullptr) {
265 ret = audiopara.capture->Pause(audiopara.capture);
266 ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_ERR_NOT_SUPPORT);
267 ret = audiopara.capture->GetCapturePosition(audiopara.capture, &frames, &timeCount);
268 ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_ERR_NOT_SUPPORT);
269 if (ret == HDF_SUCCESS) {
270 EXPECT_GT((timeCount.tvSec) * SECTONSEC + (timeCount.tvNSec), timeExp);
271 EXPECT_GT(frames, INITIAL_VALUE);
272 }
273
274 ret = audiopara.capture->Resume(audiopara.capture);
275 ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_ERR_NOT_SUPPORT);
276 ret = audiopara.capture->GetCapturePosition(audiopara.capture, &frames, &timeCount);
277 ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_ERR_NOT_SUPPORT);
278 if (ret == HDF_SUCCESS) {
279 EXPECT_GT((timeCount.tvSec) * SECTONSEC + (timeCount.tvNSec), timeExp);
280 EXPECT_GT(frames, INITIAL_VALUE);
281 }
282 }
283
284 ret = ThreadRelease(audiopara);
285 ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_ERR_NOT_SUPPORT);
286 }
287 /**
288 * @tc.name AudioCaptureGetCapturePosition_003
289 * @tc.desc Test GetCapturePosition interface,Returns 0 if get CapturePosition after stop during playing
290 * @tc.type: FUNC
291 */
292 HWTEST_F(AudioIdlHdiCaptureTest, AudioCaptureGetCapturePosition_003, TestSize.Level1)
293 {
294 int32_t ret;
295 uint64_t frames = 0;
296 struct AudioTimeStamp time = {.tvSec = 0, .tvNSec = 0};
297 int64_t timeExp = 0;
298 ASSERT_NE(nullptr, capture);
299 ret = AudioCaptureStartAndOneFrame(capture);
300 ASSERT_EQ(HDF_SUCCESS, ret);
301 ret = capture->Stop(capture);
302 EXPECT_EQ(HDF_SUCCESS, ret);
303 ret = capture->GetCapturePosition(capture, &frames, &time);
304 if (ret == HDF_SUCCESS) {
305 EXPECT_GT((time.tvSec) * SECTONSEC + (time.tvNSec), timeExp);
306 EXPECT_GT(frames, INITIAL_VALUE);
307 }
308 }
309 /**
310 * @tc.name AudioCaptureGetCapturePosition_004
311 * @tc.desc Test GetCapturePosition interface, return 0 if get CapturePosition after the object is created
312 * @tc.type: FUNC
313 */
314 HWTEST_F(AudioIdlHdiCaptureTest, AudioCaptureGetCapturePosition_004, TestSize.Level1)
315 {
316 int32_t ret;
317 uint64_t frames = 0;
318 struct AudioTimeStamp time = {.tvSec = 0, .tvNSec = 0};
319 int64_t timeExp = 0;
320 ASSERT_NE(nullptr, capture);
321 ret = capture->GetCapturePosition(capture, &frames, &time);
322 ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_ERR_NOT_SUPPORT);
323 EXPECT_EQ((time.tvSec) * SECTONSEC + (time.tvNSec), timeExp);
324 }
325 /**
326 * @tc.name AudioCaptureGetCapturePositionNull_005
327 * @tc.desc Test GetCapturePosition interface, return -3/-4 if setting the parameter Capture is nullptr
328 * @tc.type: FUNC
329 */
330 HWTEST_F(AudioIdlHdiCaptureTest, AudioCaptureGetCapturePositionNull_005, TestSize.Level1)
331 {
332 int32_t ret;
333 struct IAudioCapture *captureNull = nullptr;
334 uint64_t frames = 0;
335 struct AudioTimeStamp time = {};
336 ASSERT_NE(nullptr, capture);
337 ret = AudioCaptureStartAndOneFrame(capture);
338 ASSERT_EQ(HDF_SUCCESS, ret);
339 ret = capture->GetCapturePosition(captureNull, &frames, &time);
340 ASSERT_TRUE(ret == HDF_ERR_INVALID_PARAM || ret == HDF_ERR_INVALID_OBJECT);
341 capture->Stop(capture);
342 }
343 /**
344 * @tc.name AudioCaptureGetCapturePositionNull_006
345 * @tc.desc Test GetCapturePosition interface, return -3 if setting the parameter frames is nullptr
346 * @tc.type: FUNC
347 */
348 HWTEST_F(AudioIdlHdiCaptureTest, AudioCaptureGetCapturePositionNull_006, TestSize.Level1)
349 {
350 int32_t ret;
351 uint64_t *framesNull = nullptr;
352 struct AudioTimeStamp time = {.tvSec = 0, .tvNSec = 0};
353 ASSERT_NE(nullptr, capture);
354 ret = AudioCaptureStartAndOneFrame(capture);
355 ASSERT_EQ(HDF_SUCCESS, ret);
356 ret = capture->GetCapturePosition(capture, framesNull, &time);
357 ASSERT_TRUE(ret == HDF_ERR_INVALID_PARAM || ret == HDF_ERR_NOT_SUPPORT);
358 capture->Stop(capture);
359 }
360 /**
361 * @tc.name AudioCaptureGetCapturePositionNull_007
362 * @tc.desc Test GetCapturePosition interface, return -3 if setting the parameter time is nullptr
363 * @tc.type: FUNC
364 */
365 HWTEST_F(AudioIdlHdiCaptureTest, AudioCaptureGetCapturePositionNull_007, TestSize.Level1)
366 {
367 int32_t ret;
368 uint64_t frames = 0;
369 struct AudioTimeStamp *timeNull = nullptr;
370 ASSERT_NE(nullptr, capture);
371 ret = AudioCaptureStartAndOneFrame(capture);
372 ASSERT_EQ(HDF_SUCCESS, ret);
373 ret = capture->GetCapturePosition(capture, &frames, timeNull);
374 ASSERT_TRUE(ret == HDF_ERR_INVALID_PARAM || ret == HDF_ERR_NOT_SUPPORT);
375 capture->Stop(capture);
376 }
377 /**
378 * @tc.name AudioCaptureGetCapturePosition_008
379 * @tc.desc Test GetCapturePosition interface, return 0 if the GetCapturePosition was called twice
380 * @tc.type: FUNC
381 */
382 HWTEST_F(AudioIdlHdiCaptureTest, AudioCaptureGetCapturePosition_008, TestSize.Level1)
383 {
384 int32_t ret;
385 uint64_t frames = 0;
386 struct AudioTimeStamp time = {.tvSec = 0, .tvNSec = 0};
387 struct AudioTimeStamp timeSec = {.tvSec = 0, .tvNSec = 0};
388 int64_t timeExp = 0;
389 ASSERT_NE(nullptr, capture);
390 ret = AudioCaptureStartAndOneFrame(capture);
391 ASSERT_EQ(HDF_SUCCESS, ret);
392 ret = capture->GetCapturePosition(capture, &frames, &time);
393 ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_ERR_NOT_SUPPORT);
394 if (ret == HDF_SUCCESS) {
395 EXPECT_GT((time.tvSec) * SECTONSEC + (time.tvNSec), timeExp);
396 EXPECT_GT(frames, INITIAL_VALUE);
397 }
398 ret = capture->GetCapturePosition(capture, &frames, &timeSec);
399 ASSERT_TRUE(ret == HDF_SUCCESS || ret == HDF_ERR_NOT_SUPPORT);
400 if (ret == HDF_SUCCESS) {
401 EXPECT_GT((time.tvSec) * SECTONSEC + (time.tvNSec), timeExp);
402 EXPECT_GT(frames, INITIAL_VALUE);
403 }
404 capture->Stop(capture);
405 }
406
407 /**
408 * @tc.name AudioCaptureReqMmapBufferNull_006
409 * @tc.desc Test ReqMmapBuffer interface,return -3/-4 if call ReqMmapBuffer interface unsuccessful when setting the
410 incoming parameter handle is nullptr
411 * @tc.type: FUNC
412 */
413 HWTEST_F(AudioIdlHdiCaptureTest, AudioCaptureReqMmapBufferNull_006, TestSize.Level1)
414 {
415 int32_t ret;
416 bool isRender = false;
417 int32_t reqSize = 0;
418 struct AudioMmapBufferDescriptor desc = {};
419 struct IAudioCapture *captureNull = nullptr;
420 ASSERT_NE(nullptr, capture);
421 ret = InitMmapDesc(AUDIO_LOW_LATENCY_CAPTURE_FILE, desc, reqSize, isRender);
422 EXPECT_EQ(HDF_SUCCESS, ret);
423 ret = capture->Start(capture);
424 EXPECT_EQ(HDF_SUCCESS, ret);
425 ret = capture->ReqMmapBuffer(captureNull, reqSize, &desc);
426 ASSERT_TRUE(ret == HDF_ERR_INVALID_PARAM || ret == HDF_ERR_INVALID_OBJECT);
427 capture->Stop(capture);
428 }
429
430 /**
431 * @tc.name AudioCaptureGetMmapPositionNull_003
432 * @tc.desc Test GetMmapPosition interface,return -3 if Error in incoming parameter.
433 * @tc.type: FUNC
434 */
435 HWTEST_F(AudioIdlHdiCaptureTest, AudioCaptureGetMmapPositionNull_003, TestSize.Level1)
436 {
437 int32_t ret;
438 uint64_t *frames = nullptr;
439 ASSERT_NE(nullptr, capture);
440 struct PrepareAudioPara audiopara = {
441 .capture = capture, .portType = PORT_IN, .adapterName = ADAPTER_NAME.c_str(), .pins = PIN_IN_MIC,
442 .path = AUDIO_LOW_LATENCY_CAPTURE_FILE.c_str()
443 };
444
445 ret = audiopara.capture->GetMmapPosition(audiopara.capture, frames, &(audiopara.time));
446 ASSERT_TRUE(ret == HDF_ERR_INVALID_PARAM || ret == HDF_ERR_NOT_SUPPORT);
447 }
448 /**
449 * @tc.name AudioCaptureGetMmapPositionNull_004
450 * @tc.desc Test GetMmapPosition interface,return -3 if Error in incoming parameter.
451 * @tc.type: FUNC
452 */
453 HWTEST_F(AudioIdlHdiCaptureTest, AudioCaptureGetMmapPositionNull_004, TestSize.Level1)
454 {
455 int32_t ret;
456 uint64_t frames = 0;
457 struct AudioTimeStamp *time = nullptr;
458 ASSERT_NE(nullptr, capture);
459 struct PrepareAudioPara audiopara = {
460 .capture = capture, .portType = PORT_IN, .adapterName = ADAPTER_NAME.c_str(), .pins = PIN_IN_MIC,
461 .path = AUDIO_LOW_LATENCY_CAPTURE_FILE.c_str()
462 };
463
464 ret = audiopara.capture->GetMmapPosition(audiopara.capture, &frames, time);
465 ASSERT_TRUE(ret == HDF_ERR_INVALID_PARAM || ret == HDF_ERR_NOT_SUPPORT);
466 }
467 /**
468 * @tc.name AudioCaptureGetMmapPositionNull_005
469 * @tc.desc Test GetMmapPosition interface,return -3/-4 if Error in incoming parameter.
470 * @tc.type: FUNC
471 */
472 HWTEST_F(AudioIdlHdiCaptureTest, AudioCaptureGetMmapPositionNull_005, TestSize.Level1)
473 {
474 int32_t ret;
475 uint64_t frames = 0;
476 struct IAudioCapture *captureNull = nullptr;
477 ASSERT_NE(nullptr, capture);
478 struct PrepareAudioPara audiopara = {
479 .capture = capture, .portType = PORT_IN, .adapterName = ADAPTER_NAME.c_str(), .pins = PIN_IN_MIC,
480 .path = AUDIO_LOW_LATENCY_CAPTURE_FILE.c_str()
481 };
482
483 ret = audiopara.capture->GetMmapPosition(captureNull, &frames, &(audiopara.time));
484 ASSERT_TRUE(ret == HDF_ERR_INVALID_PARAM || ret == HDF_ERR_INVALID_OBJECT);
485 }
486 }
487