1 /*
2 * Copyright (c) 2021-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 "audio_capturer_unit_test.h"
17
18 #include <thread>
19
20 #include "audio_capturer.h"
21 #include "audio_errors.h"
22 #include "audio_info.h"
23
24 using namespace std;
25 using namespace testing::ext;
26
27 namespace OHOS {
28 namespace AudioStandard {
29 namespace {
30 const string AUDIO_CAPTURE_FILE1 = "/data/audiocapturetest_blocking.pcm";
31 const string AUDIO_CAPTURE_FILE2 = "/data/audiocapturetest_nonblocking.pcm";
32 const string AUDIO_TIME_STABILITY_TEST_FILE = "/data/audiocapture_getaudiotime_stability_test.pcm";
33 const string AUDIO_FLUSH_STABILITY_TEST_FILE = "/data/audiocapture_flush_stability_test.pcm";
34 const int32_t READ_BUFFERS_COUNT = 128;
35 const int32_t VALUE_NEGATIVE = -1;
36 const int32_t VALUE_ZERO = 0;
37 const int32_t VALUE_HUNDRED = 100;
38 const int32_t VALUE_THOUSAND = 1000;
39 const int32_t CAPTURER_FLAG = 0;
40
41 constexpr uint64_t BUFFER_DURATION_FIVE = 5;
42 constexpr uint64_t BUFFER_DURATION_TEN = 10;
43 constexpr uint64_t BUFFER_DURATION_FIFTEEN = 15;
44 constexpr uint64_t BUFFER_DURATION_TWENTY = 20;
45 } // namespace
46
SetUpTestCase(void)47 void AudioCapturerUnitTest::SetUpTestCase(void) {}
TearDownTestCase(void)48 void AudioCapturerUnitTest::TearDownTestCase(void) {}
SetUp(void)49 void AudioCapturerUnitTest::SetUp(void) {}
TearDown(void)50 void AudioCapturerUnitTest::TearDown(void) {}
51
InitializeCapturer(unique_ptr<AudioCapturer> & audioCapturer)52 int32_t AudioCapturerUnitTest::InitializeCapturer(unique_ptr<AudioCapturer> &audioCapturer)
53 {
54 AudioCapturerParams capturerParams;
55 capturerParams.audioSampleFormat = SAMPLE_S16LE;
56 capturerParams.samplingRate = SAMPLE_RATE_44100;
57 capturerParams.audioChannel = STEREO;
58 capturerParams.audioEncoding = ENCODING_PCM;
59
60 return audioCapturer->SetParams(capturerParams);
61 }
62
InitializeCapturerOptions(AudioCapturerOptions & capturerOptions)63 void AudioCapturerUnitTest::InitializeCapturerOptions(AudioCapturerOptions &capturerOptions)
64 {
65 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_96000;
66 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
67 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_U8;
68 capturerOptions.streamInfo.channels = AudioChannel::MONO;
69 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
70 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
71 return;
72 }
73
StartCaptureThread(AudioCapturer * audioCapturer,const string filePath)74 void StartCaptureThread(AudioCapturer *audioCapturer, const string filePath)
75 {
76 int32_t ret = -1;
77 bool isBlockingRead = true;
78 size_t bufferLen;
79 ret = audioCapturer->GetBufferSize(bufferLen);
80 EXPECT_EQ(SUCCESS, ret);
81
82 auto buffer = std::make_unique<uint8_t[]>(bufferLen);
83 ASSERT_NE(nullptr, buffer);
84 FILE *capFile = fopen(filePath.c_str(), "wb");
85 ASSERT_NE(nullptr, capFile);
86
87 size_t size = 1;
88 int32_t bytesRead = 0;
89 int32_t numBuffersToCapture = READ_BUFFERS_COUNT;
90
91 while (numBuffersToCapture) {
92 bytesRead = audioCapturer->Read(*(buffer.get()), bufferLen, isBlockingRead);
93 if (bytesRead < 0) {
94 break;
95 } else if (bytesRead > 0) {
96 fwrite(buffer.get(), size, bytesRead, capFile);
97 numBuffersToCapture--;
98 }
99 }
100
101 audioCapturer->Flush();
102
103 fclose(capFile);
104 }
105
106 /**
107 * @tc.name : Test GetSupportedFormats API
108 * @tc.number: Audio_Capturer_GetSupportedFormats_001
109 * @tc.desc : Test GetSupportedFormats interface. Returns supported Formats on success.
110 */
111 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetSupportedFormats_001, TestSize.Level0)
112 {
113 vector<AudioSampleFormat> supportedFormatList = AudioCapturer::GetSupportedFormats();
114 EXPECT_EQ(AUDIO_SUPPORTED_FORMATS.size(), supportedFormatList.size());
115 }
116
117 /**
118 * @tc.name : Test GetSupportedChannels API
119 * @tc.number: Audio_Capturer_GetSupportedChannels_001
120 * @tc.desc : Test GetSupportedChannels interface. Returns supported Channels on success.
121 */
122 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetSupportedChannels_001, TestSize.Level0)
123 {
124 vector<AudioChannel> supportedChannelList = AudioCapturer::GetSupportedChannels();
125 EXPECT_EQ(AUDIO_SUPPORTED_CHANNELS.size(), supportedChannelList.size());
126 }
127
128 /**
129 * @tc.name : Test GetSupportedEncodingTypes API
130 * @tc.number: Audio_Capturer_GetSupportedEncodingTypes_001
131 * @tc.desc : Test GetSupportedEncodingTypes interface. Returns supported Encoding types on success.
132 */
133 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetSupportedEncodingTypes_001, TestSize.Level0)
134 {
135 vector<AudioEncodingType> supportedEncodingTypes
136 = AudioCapturer::GetSupportedEncodingTypes();
137 EXPECT_EQ(AUDIO_SUPPORTED_ENCODING_TYPES.size(), supportedEncodingTypes.size());
138 }
139
140 /**
141 * @tc.name : Test GetSupportedSamplingRates API
142 * @tc.number: Audio_Capturer_GetSupportedSamplingRates_001
143 * @tc.desc : Test GetSupportedSamplingRates interface. Returns supported Sampling rates on success.
144 */
145 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetSupportedSamplingRates_001, TestSize.Level0)
146 {
147 vector<AudioSamplingRate> supportedSamplingRates = AudioCapturer::GetSupportedSamplingRates();
148 EXPECT_EQ(AUDIO_SUPPORTED_SAMPLING_RATES.size(), supportedSamplingRates.size());
149 }
150
151 /**
152 * @tc.name : Test Create API via legal input.
153 * @tc.number: Audio_Capturer_Create_001
154 * @tc.desc : Test Create interface with STREAM_MUSIC. Returns audioCapturer instance, if create is successful.
155 */
156 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Create_001, TestSize.Level0)
157 {
158 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
159 EXPECT_NE(nullptr, audioCapturer);
160 }
161
162 /**
163 * @tc.name : Test Create API via legal input.
164 * @tc.number: Audio_Capturer_Create_002
165 * @tc.desc : Test Create interface with STREAM_RING. Returns audioCapturer instance, if create is successful.
166 */
167 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Create_002, TestSize.Level0)
168 {
169 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_RING);
170 EXPECT_NE(nullptr, audioCapturer);
171 }
172
173 /**
174 * @tc.name : Test Create API via legal input.
175 * @tc.number: Audio_Capturer_Create_003
176 * @tc.desc : Test Create interface with STREAM_VOICE_CALL. Returns audioCapturer instance if create is successful.
177 * Note: instance will be created but functional support for STREAM_VOICE_CALL not available yet.
178 */
179 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Create_003, TestSize.Level0)
180 {
181 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_VOICE_CALL);
182 EXPECT_NE(nullptr, audioCapturer);
183 }
184
185 /**
186 * @tc.name : Test Create API via legal input.
187 * @tc.number: Audio_Capturer_Create_004
188 * @tc.desc : Test Create interface with STREAM_SYSTEM. Returns audioCapturer instance, if create is successful.
189 * Note: instance will be created but functional support for STREAM_SYSTEM not available yet.
190 */
191 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Create_004, TestSize.Level0)
192 {
193 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_SYSTEM);
194 EXPECT_NE(nullptr, audioCapturer);
195 }
196
197 /**
198 * @tc.name : Test Create API via legal input.
199 * @tc.number: Audio_Capturer_Create_005
200 * @tc.desc : Test Create interface with STREAM_BLUETOOTH_SCO. Returns audioCapturer instance, if create is successful.
201 * Note: instance will be created but functional support for STREAM_BLUETOOTH_SCO not available yet
202 */
203 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Create_005, TestSize.Level0)
204 {
205 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_BLUETOOTH_SCO);
206 EXPECT_NE(nullptr, audioCapturer);
207 }
208
209 /**
210 * @tc.name : Test Create API via legal input.
211 * @tc.number: Audio_Capturer_Create_006
212 * @tc.desc : Test Create interface with STREAM_ALARM. Returns audioCapturer instance, if create is successful.
213 * Note: instance will be created but functional support for STREAM_ALARM not available yet.
214 */
215 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Create_006, TestSize.Level0)
216 {
217 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_ALARM);
218 EXPECT_NE(nullptr, audioCapturer);
219 }
220
221 /**
222 * @tc.name : Test Create API via legal input.
223 * @tc.number: Audio_Capturer_Create_007
224 * @tc.desc : Test Create interface with STREAM_NOTIFICATION. Returns audioCapturer instance, if create is successful.
225 * Note: instance will be created but functional support for STREAM_NOTIFICATION not available yet.
226 */
227 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Create_007, TestSize.Level0)
228 {
229 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_NOTIFICATION);
230 EXPECT_NE(nullptr, audioCapturer);
231 }
232
233 /**
234 * @tc.name : Test Create API via legal input.
235 * @tc.number: Audio_Capturer_Create_008
236 * @tc.desc : Test Create interface with AudioCapturerOptions below.
237 * Returns audioCapturer instance, if create is successful.
238 * capturerOptions.streamInfo.samplingRate = SAMPLE_RATE_96000;
239 * capturerOptions.streamInfo.encoding = ENCODING_PCM;
240 * capturerOptions.streamInfo.format = SAMPLE_U8;
241 * capturerOptions.streamInfo.channels = MONO;
242 * capturerOptions.capturerInfo.sourceType = SOURCE_TYPE_MIC;
243 * capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
244 */
245 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Create_008, TestSize.Level0)
246 {
247 AudioCapturerOptions capturerOptions;
248 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_96000;
249 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
250 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_U8;
251 capturerOptions.streamInfo.channels = AudioChannel::MONO;
252 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
253 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
254
255 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
256 EXPECT_NE(nullptr, audioCapturer);
257 audioCapturer->Release();
258 }
259
260 /**
261 * @tc.name : Test Create API via legal input.
262 * @tc.number: Audio_Capturer_Create_009
263 * @tc.desc : Test Create interface with AudioCapturerOptions below.
264 * Returns audioCapturer instance, if create is successful.
265 * capturerOptions.streamInfo.samplingRate = SAMPLE_RATE_96000;
266 * capturerOptions.streamInfo.encoding = ENCODING_PCM;
267 * capturerOptions.streamInfo.format = SAMPLE_U8;
268 * capturerOptions.streamInfo.channels = MONO;
269 * capturerOptions.capturerInfo.sourceType = SOURCE_TYPE_VOICE_COMMUNICATION;
270 * capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
271 */
272 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Create_009, TestSize.Level0)
273 {
274 AudioCapturerOptions capturerOptions;
275 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_96000;
276 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
277 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_U8;
278 capturerOptions.streamInfo.channels = AudioChannel::MONO;
279 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_VOICE_COMMUNICATION;
280 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
281
282 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
283 EXPECT_NE(nullptr, audioCapturer);
284 audioCapturer->Release();
285 }
286
287 /**
288 * @tc.name : Test Create API via legal input.
289 * @tc.number: Audio_Capturer_Create_010
290 * @tc.desc : Test Create interface with AudioCapturerOptions below.
291 * Returns audioCapturer instance, if create is successful.
292 * capturerOptions.streamInfo.samplingRate = SAMPLE_RATE_96000;
293 * capturerOptions.streamInfo.encoding = ENCODING_PCM;
294 * capturerOptions.streamInfo.format = SAMPLE_S32LE;
295 * capturerOptions.streamInfo.channels = MONO;
296 * capturerOptions.capturerInfo.sourceType = SOURCE_TYPE_VOICE_COMMUNICATION;
297 * capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
298 */
299 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Create_010, TestSize.Level0)
300 {
301 AudioCapturerOptions capturerOptions;
302 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_64000;
303 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
304 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_S32LE;
305 capturerOptions.streamInfo.channels = AudioChannel::MONO;
306 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_VOICE_COMMUNICATION;
307 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
308
309 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
310 EXPECT_NE(nullptr, audioCapturer);
311 audioCapturer->Release();
312 }
313
314 /**
315 * @tc.name : Test Create API via legal input.
316 * @tc.number: Audio_Capturer_Create_011
317 * @tc.desc : Test Create interface with AudioCapturerOptions below.
318 * Returns audioCapturer instance, if create is successful.
319 * capturerOptions.streamInfo.samplingRate = SAMPLE_RATE_64000;
320 * capturerOptions.streamInfo.encoding = ENCODING_PCM;
321 * capturerOptions.streamInfo.format = SAMPLE_S32LE;
322 * capturerOptions.streamInfo.channels = STEREO;
323 * capturerOptions.capturerInfo.sourceType = SOURCE_TYPE_MIC;
324 * capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
325 */
326 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Create_011, TestSize.Level0)
327 {
328 AudioCapturerOptions capturerOptions;
329 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_64000;
330 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
331 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_S32LE;
332 capturerOptions.streamInfo.channels = AudioChannel::STEREO;
333 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
334 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
335
336 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
337 EXPECT_NE(nullptr, audioCapturer);
338 audioCapturer->Release();
339 }
340
341 /**
342 * @tc.name : Test Create API via legal input.
343 * @tc.number: Audio_Capturer_Create_012
344 * @tc.desc : Test Create interface with AudioCapturerOptions below.
345 * Returns audioCapturer instance, if create is successful.
346 * capturerOptions.streamInfo.samplingRate = SAMPLE_RATE_44100;
347 * capturerOptions.streamInfo.encoding = ENCODING_PCM;
348 * capturerOptions.streamInfo.format = SAMPLE_S16LE;
349 * capturerOptions.streamInfo.channels = STEREO;
350 * capturerOptions.capturerInfo.sourceType = SOURCE_TYPE_MIC;
351 * capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
352 */
353 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Create_012, TestSize.Level0)
354 {
355 AudioCapturerOptions capturerOptions;
356 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_44100;
357 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
358 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_S16LE;
359 capturerOptions.streamInfo.channels = AudioChannel::STEREO;
360 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
361 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
362
363 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
364 EXPECT_NE(nullptr, audioCapturer);
365 audioCapturer->Release();
366 }
367
368 /**
369 * @tc.name : Test SetParams API via legal input
370 * @tc.number: Audio_Capturer_SetParams_001
371 * @tc.desc : Test SetParams interface. Returns 0 {SUCCESS}, if the setting is successful.
372 * capturerParams.audioSampleFormat = SAMPLE_S16LE;
373 * capturerParams.samplingRate = SAMPLE_RATE_44100;
374 * capturerParams.audioChannel = STEREO;
375 * capturerParams.audioEncoding = ENCODING_PCM;
376 */
377 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetParams_001, TestSize.Level1)
378 {
379 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
380 ASSERT_NE(nullptr, audioCapturer);
381
382 AudioCapturerParams capturerParams;
383 capturerParams.audioSampleFormat = SAMPLE_S16LE;
384 capturerParams.samplingRate = SAMPLE_RATE_44100;
385 capturerParams.audioChannel = STEREO;
386 capturerParams.audioEncoding = ENCODING_PCM;
387
388 int32_t ret = audioCapturer->SetParams(capturerParams);
389 EXPECT_EQ(SUCCESS, ret);
390 audioCapturer->Release();
391 }
392
393 /**
394 * @tc.name : Test SetParams API via legal input.
395 * @tc.number: Audio_Capturer_SetParams_002
396 * @tc.desc : Test SetParams interface. Returns 0 {SUCCESS}, if the setting is successful.
397 * capturerParams.audioSampleFormat = SAMPLE_S16LE;
398 * capturerParams.samplingRate = SAMPLE_RATE_8000;
399 * capturerParams.audioChannel = MONO;
400 * capturerParams.audioEncoding = ENCODING_PCM;
401 */
402 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetParams_002, TestSize.Level1)
403 {
404 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
405 ASSERT_NE(nullptr, audioCapturer);
406
407 AudioCapturerParams capturerParams;
408 capturerParams.audioSampleFormat = SAMPLE_S16LE;
409 capturerParams.samplingRate = SAMPLE_RATE_8000;
410 capturerParams.audioChannel = MONO;
411 capturerParams.audioEncoding = ENCODING_PCM;
412
413 int32_t ret = audioCapturer->SetParams(capturerParams);
414 EXPECT_EQ(SUCCESS, ret);
415 audioCapturer->Release();
416 }
417
418 /**
419 * @tc.name : Test SetParams API via legal input.
420 * @tc.number: Audio_Capturer_SetParams_003
421 * @tc.desc : Test SetParams interface. Returns 0 {SUCCESS}, if the setting is successful.
422 * capturerParams.audioSampleFormat = SAMPLE_S16LE;
423 * capturerParams.samplingRate = SAMPLE_RATE_11025;
424 * capturerParams.audioChannel = STEREO;
425 * capturerParams.audioEncoding = ENCODING_PCM;
426 */
427 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetParams_003, TestSize.Level1)
428 {
429 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
430 ASSERT_NE(nullptr, audioCapturer);
431
432 AudioCapturerParams capturerParams;
433 capturerParams.audioSampleFormat = SAMPLE_S16LE;
434 capturerParams.samplingRate = SAMPLE_RATE_11025;
435 capturerParams.audioChannel = STEREO;
436 capturerParams.audioEncoding = ENCODING_PCM;
437
438 int32_t ret = audioCapturer->SetParams(capturerParams);
439 EXPECT_EQ(SUCCESS, ret);
440 audioCapturer->Release();
441 }
442
443 /**
444 * @tc.name : Test SetParams API via legal input.
445 * @tc.number: Audio_Capturer_SetParams_004
446 * @tc.desc : Test SetParams interface. Returns 0 {SUCCESS}, if the setting is successful.
447 * capturerParams.audioSampleFormat = SAMPLE_S16LE;
448 * capturerParams.samplingRate = SAMPLE_RATE_22050;
449 * capturerParams.audioChannel = MONO;
450 * capturerParams.audioEncoding = ENCODING_PCM;
451 */
452 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetParams_004, TestSize.Level1)
453 {
454 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
455 ASSERT_NE(nullptr, audioCapturer);
456
457 AudioCapturerParams capturerParams;
458 capturerParams.audioSampleFormat = SAMPLE_S16LE;
459 capturerParams.samplingRate = SAMPLE_RATE_22050;
460 capturerParams.audioChannel = MONO;
461 capturerParams.audioEncoding = ENCODING_PCM;
462
463 int32_t ret = audioCapturer->SetParams(capturerParams);
464 EXPECT_EQ(SUCCESS, ret);
465 audioCapturer->Release();
466 }
467
468 /**
469 * @tc.name : Test SetParams API via legal input.
470 * @tc.number: Audio_Capturer_SetParams_005
471 * @tc.desc : Test SetParams interface. Returns 0 {SUCCESS}, if the setting is successful.
472 * capturerParams.audioSampleFormat = SAMPLE_S16LE;
473 * capturerParams.samplingRate = SAMPLE_RATE_96000;
474 * capturerParams.audioChannel = MONO;
475 * capturerParams.audioEncoding = ENCODING_PCM;
476 */
477 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetParams_005, TestSize.Level1)
478 {
479 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
480 ASSERT_NE(nullptr, audioCapturer);
481
482 AudioCapturerParams capturerParams;
483 capturerParams.audioSampleFormat = SAMPLE_S16LE;
484 capturerParams.samplingRate = SAMPLE_RATE_96000;
485 capturerParams.audioChannel = MONO;
486 capturerParams.audioEncoding = ENCODING_PCM;
487
488 int32_t ret = audioCapturer->SetParams(capturerParams);
489 EXPECT_EQ(SUCCESS, ret);
490 }
491
492 /**
493 * @tc.name : Test SetParams API via legal input.
494 * @tc.number: Audio_Capturer_SetParams_006
495 * @tc.desc : Test SetParams interface. Returns 0 {SUCCESS}, if the setting is successful.
496 * capturerParams.audioSampleFormat = SAMPLE_S24LE;
497 * capturerParams.samplingRate = SAMPLE_RATE_64000;
498 * capturerParams.audioChannel = MONO;
499 * capturerParams.audioEncoding = ENCODING_PCM;
500 */
501 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetParams_006, TestSize.Level1)
502 {
503 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
504 ASSERT_NE(nullptr, audioCapturer);
505
506 AudioCapturerParams capturerParams;
507 capturerParams.audioSampleFormat = SAMPLE_S24LE;
508 capturerParams.samplingRate = SAMPLE_RATE_64000;
509 capturerParams.audioChannel = MONO;
510 capturerParams.audioEncoding = ENCODING_PCM;
511
512 int32_t ret = audioCapturer->SetParams(capturerParams);
513 EXPECT_EQ(SUCCESS, ret);
514 }
515
516 /**
517 * @tc.name : Test SetParams API via illegal input.
518 * @tc.number: Audio_Capturer_SetParams_007
519 * @tc.desc : Test SetParams interface. Returns 0 {SUCCESS}, if the setting is successful.
520 * capturerParams.audioSampleFormat = SAMPLE_S16LE;
521 * capturerParams.samplingRate = SAMPLE_RATE_16000;
522 * capturerParams.audioChannel = STEREO;
523 * capturerParams.audioEncoding = ENCODING_PCM;
524 */
525 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetParams_007, TestSize.Level1)
526 {
527 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
528 ASSERT_NE(nullptr, audioCapturer);
529
530 AudioCapturerParams capturerParams;
531 capturerParams.audioSampleFormat = SAMPLE_S16LE;
532 capturerParams.samplingRate = SAMPLE_RATE_16000;
533 capturerParams.audioChannel = STEREO;
534 capturerParams.audioEncoding = ENCODING_PCM;
535
536 int32_t ret = audioCapturer->SetParams(capturerParams);
537 EXPECT_EQ(SUCCESS, ret);
538 audioCapturer->Release();
539 }
540
541 /**
542 * @tc.name : Test SetParams API stability.
543 * @tc.number: Audio_Capturer_SetParams_Stability_001
544 * @tc.desc : Test SetParams interface stability.
545 */
546 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetParams_Stability_001, TestSize.Level1)
547 {
548 int32_t ret = -1;
549 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
550 ASSERT_NE(nullptr, audioCapturer);
551
552 AudioCapturerParams capturerParams;
553 capturerParams.audioSampleFormat = SAMPLE_S16LE;
554 capturerParams.samplingRate = SAMPLE_RATE_16000;
555 capturerParams.audioChannel = STEREO;
556 capturerParams.audioEncoding = ENCODING_PCM;
557
558 for (int i = 0; i < VALUE_HUNDRED; i++) {
559 ret = audioCapturer->SetParams(capturerParams);
560 EXPECT_EQ(SUCCESS, ret);
561
562 AudioCapturerParams getCapturerParams;
563 ret = audioCapturer->GetParams(getCapturerParams);
564 EXPECT_EQ(SUCCESS, ret);
565 }
566
567 audioCapturer->Release();
568 }
569
570 /**
571 * @tc.name : Test GetParams API via legal input.
572 * @tc.number: Audio_Capturer_GetParams_001
573 * @tc.desc : Test GetParams interface. Returns 0 {SUCCESS}, if the getting is successful.
574 */
575 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetParams_001, TestSize.Level1)
576 {
577 int32_t ret = -1;
578 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
579 ASSERT_NE(nullptr, audioCapturer);
580
581 AudioCapturerParams capturerParams;
582 capturerParams.audioSampleFormat = SAMPLE_S16LE;
583 capturerParams.samplingRate = SAMPLE_RATE_44100;
584 capturerParams.audioChannel = STEREO;
585 capturerParams.audioEncoding = ENCODING_PCM;
586 ret = audioCapturer->SetParams(capturerParams);
587 EXPECT_EQ(SUCCESS, ret);
588
589 AudioCapturerParams getCapturerParams;
590 ret = audioCapturer->GetParams(getCapturerParams);
591 EXPECT_EQ(SUCCESS, ret);
592 EXPECT_EQ(capturerParams.audioSampleFormat, getCapturerParams.audioSampleFormat);
593 EXPECT_EQ(capturerParams.samplingRate, getCapturerParams.samplingRate);
594 EXPECT_EQ(capturerParams.audioChannel, getCapturerParams.audioChannel);
595 EXPECT_EQ(capturerParams.audioEncoding, getCapturerParams.audioEncoding);
596
597 audioCapturer->Release();
598 }
599
600 /**
601 * @tc.name : Test GetParams API via legal state, CAPTURER_RUNNING: GetParams after Start.
602 * @tc.number: Audio_Capturer_GetParams_002
603 * @tc.desc : Test GetParams interface. Returns 0 {SUCCESS} if the getting is successful.
604 */
605 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetParams_002, TestSize.Level1)
606 {
607 int32_t ret = -1;
608 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
609 ASSERT_NE(nullptr, audioCapturer);
610
611 AudioCapturerParams capturerParams;
612 capturerParams.audioSampleFormat = SAMPLE_S16LE;
613 capturerParams.samplingRate = SAMPLE_RATE_44100;
614 capturerParams.audioChannel = MONO;
615 capturerParams.audioEncoding = ENCODING_PCM;
616 ret = audioCapturer->SetParams(capturerParams);
617 EXPECT_EQ(SUCCESS, ret);
618
619 bool isStarted = audioCapturer->Start();
620 EXPECT_EQ(true, isStarted);
621
622 AudioCapturerParams getCapturerParams;
623 ret = audioCapturer->GetParams(getCapturerParams);
624 EXPECT_EQ(SUCCESS, ret);
625
626 audioCapturer->Release();
627 }
628
629 /**
630 * @tc.name : Test GetParams API via illegal state, CAPTURER_NEW: Call GetParams without SetParams.
631 * @tc.number: Audio_Capturer_GetParams_003
632 * @tc.desc : Test GetParams interface. Returns error code, if the capturer state is CAPTURER_NEW.
633 */
634 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetParams_003, TestSize.Level1)
635 {
636 int32_t ret = -1;
637 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
638 ASSERT_NE(nullptr, audioCapturer);
639
640 AudioCapturerParams capturerParams;
641 capturerParams.audioSampleFormat = SAMPLE_S16LE;
642 capturerParams.samplingRate = SAMPLE_RATE_44100;
643 capturerParams.audioChannel = MONO;
644 capturerParams.audioEncoding = ENCODING_PCM;
645
646 AudioCapturerParams getCapturerParams;
647 ret = audioCapturer->GetParams(getCapturerParams);
648 EXPECT_EQ(ERR_OPERATION_FAILED, ret);
649 }
650
651 /**
652 * @tc.name : Test GetParams API via illegal state, CAPTURER_RELEASED: Call GetParams after Release.
653 * @tc.number: Audio_Capturer_GetParams_004
654 * @tc.desc : Test GetParams interface. Returns error code, if the capturer state is CAPTURER_RELEASED.
655 */
656 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetParams_004, TestSize.Level1)
657 {
658 int32_t ret = -1;
659 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
660 ASSERT_NE(nullptr, audioCapturer);
661
662 ret = AudioCapturerUnitTest::InitializeCapturer(audioCapturer);
663 EXPECT_EQ(SUCCESS, ret);
664
665 bool isReleased = audioCapturer->Release();
666 EXPECT_EQ(true, isReleased);
667
668 AudioCapturerParams getCapturerParams;
669 ret = audioCapturer->GetParams(getCapturerParams);
670 EXPECT_EQ(ERR_OPERATION_FAILED, ret);
671 }
672
673 /**
674 * @tc.name : Test GetParams API via legal state, CAPTURER_STOPPED: GetParams after Stop.
675 * @tc.number: Audio_Capturer_GetParams_005
676 * @tc.desc : Test GetParams interface. Returns 0 {SUCCESS}, if the getting is successful.
677 */
678 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetParams_005, TestSize.Level1)
679 {
680 int32_t ret = -1;
681 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
682 ASSERT_NE(nullptr, audioCapturer);
683
684 ret = AudioCapturerUnitTest::InitializeCapturer(audioCapturer);
685 EXPECT_EQ(SUCCESS, ret);
686
687 bool isStarted = audioCapturer->Start();
688 EXPECT_EQ(true, isStarted);
689
690 bool isStopped = audioCapturer->Stop();
691 EXPECT_EQ(true, isStopped);
692
693 AudioCapturerParams getCapturerParams;
694 ret = audioCapturer->GetParams(getCapturerParams);
695 EXPECT_EQ(SUCCESS, ret);
696
697 audioCapturer->Release();
698 }
699
700 /**
701 * @tc.name : Test GetParams API stability.
702 * @tc.number: Audio_Capturer_GetParams_Stability_001
703 * @tc.desc : Test GetParams interface stability.
704 */
705 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetParams_Stability_001, TestSize.Level1)
706 {
707 int32_t ret = -1;
708 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
709 ASSERT_NE(nullptr, audioCapturer);
710
711 AudioCapturerParams capturerParams;
712 capturerParams.audioSampleFormat = SAMPLE_S16LE;
713 capturerParams.samplingRate = SAMPLE_RATE_44100;
714 capturerParams.audioChannel = MONO;
715 capturerParams.audioEncoding = ENCODING_PCM;
716
717 ret = audioCapturer->SetParams(capturerParams);
718 EXPECT_EQ(SUCCESS, ret);
719
720 for (int i = 0; i < VALUE_THOUSAND; i++) {
721 AudioCapturerParams getCapturerParams;
722 ret = audioCapturer->GetParams(getCapturerParams);
723 EXPECT_EQ(SUCCESS, ret);
724 }
725
726 audioCapturer->Release();
727 }
728
729 /**
730 * @tc.name : Test GetBufferSize API via legal input.
731 * @tc.number: Audio_Capturer_GetBufferSize_001
732 * @tc.desc : Test GetBufferSize interface. Returns 0 {SUCCESS}, if the getting is successful.
733 */
734 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetBufferSize_001, TestSize.Level1)
735 {
736 int32_t ret = -1;
737 AudioCapturerOptions capturerOptions;
738
739 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
740 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
741 ASSERT_NE(nullptr, audioCapturer);
742
743 size_t bufferLen;
744 ret = audioCapturer->GetBufferSize(bufferLen);
745 EXPECT_EQ(SUCCESS, ret);
746
747 audioCapturer->Release();
748 }
749
750 /**
751 * @tc.name : Test GetBufferSize API via illegal state, CAPTURER_NEW: without initializing the capturer.
752 * @tc.number: Audio_Capturer_GetBufferSize_002
753 * @tc.desc : Test GetBufferSize interface. Returns error code, if the capturer state is CAPTURER_NEW.
754 */
755 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetBufferSize_002, TestSize.Level1)
756 {
757 int32_t ret = -1;
758 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
759 ASSERT_NE(nullptr, audioCapturer);
760
761 size_t bufferLen;
762 ret = audioCapturer->GetBufferSize(bufferLen);
763 EXPECT_EQ(ERR_OPERATION_FAILED, ret);
764 }
765
766 /**
767 * @tc.name : Test GetBufferSize API via illegal state, CAPTURER_RELEASED: call Release before GetBufferSize
768 * @tc.number: Audio_Capturer_GetBufferSize_003
769 * @tc.desc : Test GetBufferSize interface. Returns error code, if the capturer state is CAPTURER_RELEASED.
770 */
771 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetBufferSize_003, TestSize.Level1)
772 {
773 int32_t ret = -1;
774 AudioCapturerOptions capturerOptions;
775
776 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
777 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
778 ASSERT_NE(nullptr, audioCapturer);
779
780 bool isReleased = audioCapturer->Release();
781 EXPECT_EQ(true, isReleased);
782
783 size_t bufferLen;
784 ret = audioCapturer->GetBufferSize(bufferLen);
785 EXPECT_EQ(ERR_OPERATION_FAILED, ret);
786 }
787
788 /**
789 * @tc.name : Test GetBufferSize API via legal state, CAPTURER_STOPPED: call Stop before GetBufferSize
790 * @tc.number: Audio_Capturer_GetBufferSize_004
791 * @tc.desc : Test GetBufferSize interface. Returns 0 {SUCCESS}, if the getting is successful.
792 */
793 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetBufferSize_004, TestSize.Level1)
794 {
795 int32_t ret = -1;
796 AudioCapturerOptions capturerOptions;
797
798 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
799 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
800 ASSERT_NE(nullptr, audioCapturer);
801
802 bool isStarted = audioCapturer->Start();
803 EXPECT_EQ(true, isStarted);
804
805 bool isStopped = audioCapturer->Stop();
806 EXPECT_EQ(true, isStopped);
807
808 size_t bufferLen;
809 ret = audioCapturer->GetBufferSize(bufferLen);
810 EXPECT_EQ(SUCCESS, ret);
811
812 audioCapturer->Release();
813 }
814
815 /**
816 * @tc.name : Test GetBufferSize API via legal state, CAPTURER_RUNNING: call Start before GetBufferSize
817 * @tc.number: Audio_Capturer_GetBufferSize_005
818 * @tc.desc : test GetBufferSize interface. Returns 0 {SUCCESS}, if the getting is successful.
819 */
820 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetBufferSize_005, TestSize.Level1)
821 {
822 int32_t ret = -1;
823 AudioCapturerOptions capturerOptions;
824
825 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
826 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
827 ASSERT_NE(nullptr, audioCapturer);
828
829 bool isStarted = audioCapturer->Start();
830 EXPECT_EQ(true, isStarted);
831
832 size_t bufferLen;
833 ret = audioCapturer->GetBufferSize(bufferLen);
834 EXPECT_EQ(SUCCESS, ret);
835
836 audioCapturer->Release();
837 }
838
839 /**
840 * @tc.name : Test GetFrameCount API via legal input.
841 * @tc.number: Audio_Capturer_GetFrameCount_001
842 * @tc.desc : test GetFrameCount interface, Returns 0 {SUCCESS}, if the getting is successful.
843 */
844 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetFrameCount_001, TestSize.Level1)
845 {
846 int32_t ret = -1;
847 AudioCapturerOptions capturerOptions;
848
849 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
850 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
851 ASSERT_NE(nullptr, audioCapturer);
852
853 uint32_t frameCount;
854 ret = audioCapturer->GetFrameCount(frameCount);
855 EXPECT_EQ(SUCCESS, ret);
856
857 audioCapturer->Release();
858 }
859
860 /**
861 * @tc.name : Test GetFrameCount API via illegal state, CAPTURER_NEW: without initialiing the capturer.
862 * @tc.number: Audio_Capturer_GetFrameCount_002
863 * @tc.desc : Test GetFrameCount interface. Returns error code, if the capturer state is CAPTURER_NEW.
864 */
865 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetFrameCount_002, TestSize.Level1)
866 {
867 int32_t ret = -1;
868 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
869 ASSERT_NE(nullptr, audioCapturer);
870
871 uint32_t frameCount;
872 ret = audioCapturer->GetFrameCount(frameCount);
873 EXPECT_EQ(ERR_OPERATION_FAILED, ret);
874 }
875
876 /**
877 * @tc.name : Test GetFrameCount API via legal state, CAPTURER_RUNNING: call Start before GetFrameCount.
878 * @tc.number: Audio_Capturer_GetFrameCount_003
879 * @tc.desc : Test GetFrameCount interface. Returns 0 {SUCCESS}, if the getting is successful.
880 */
881 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetFrameCount_003, TestSize.Level1)
882 {
883 int32_t ret = -1;
884 AudioCapturerOptions capturerOptions;
885
886 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
887 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
888 ASSERT_NE(nullptr, audioCapturer);
889
890 bool isStarted = audioCapturer->Start();
891 EXPECT_EQ(true, isStarted);
892
893 uint32_t frameCount;
894 ret = audioCapturer->GetFrameCount(frameCount);
895 EXPECT_EQ(SUCCESS, ret);
896
897 audioCapturer->Release();
898 }
899
900 /**
901 * @tc.name : Test GetFrameCount API via legal state, CAPTURER_STOPPED: call Stop before GetFrameCount
902 * @tc.number: Audio_Capturer_GetFrameCount_004
903 * @tc.desc : Test GetFrameCount interface. Returns 0 {SUCCESS}, if the getting is successful.
904 */
905 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetFrameCount_004, TestSize.Level1)
906 {
907 int32_t ret = -1;
908 AudioCapturerOptions capturerOptions;
909
910 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
911 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
912 ASSERT_NE(nullptr, audioCapturer);
913
914 bool isStarted = audioCapturer->Start();
915 EXPECT_EQ(true, isStarted);
916
917 bool isStopped = audioCapturer->Stop();
918 EXPECT_EQ(true, isStopped);
919
920 uint32_t frameCount;
921 ret = audioCapturer->GetFrameCount(frameCount);
922 EXPECT_EQ(SUCCESS, ret);
923
924 audioCapturer->Release();
925 }
926
927 /**
928 * @tc.name : Test GetFrameCount API via illegal state, CAPTURER_RELEASED: call Release before GetFrameCount
929 * @tc.number: Audio_Capturer_GetFrameCount_005
930 * @tc.desc : Test GetFrameCount interface. Returns error code, if the state is CAPTURER_RELEASED.
931 */
932 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetFrameCount_005, TestSize.Level1)
933 {
934 int32_t ret = -1;
935 AudioCapturerOptions capturerOptions;
936
937 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
938 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
939 ASSERT_NE(nullptr, audioCapturer);
940
941 bool isReleased = audioCapturer->Release();
942 EXPECT_EQ(true, isReleased);
943
944 uint32_t frameCount;
945 ret = audioCapturer->GetFrameCount(frameCount);
946 EXPECT_EQ(ERR_OPERATION_FAILED, ret);
947 }
948
949 /**
950 * @tc.name : Test Start API via legal state, CAPTURER_PREPARED.
951 * @tc.number: Audio_Capturer_Start_001
952 * @tc.desc : Test Start interface. Returns true if start is successful.
953 */
954 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Start_001, TestSize.Level1)
955 {
956 AudioCapturerOptions capturerOptions;
957
958 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
959 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
960 ASSERT_NE(nullptr, audioCapturer);
961
962 bool isStarted = audioCapturer->Start();
963 EXPECT_EQ(true, isStarted);
964
965 audioCapturer->Release();
966 }
967
968 /**
969 * @tc.name : Test Start API via illegal state, CAPTURER_NEW: without initializing the capturer.
970 * @tc.number: Audio_Capturer_Start_002
971 * @tc.desc : Test Start interface. Returns false, if the capturer state is CAPTURER_NEW.
972 */
973 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Start_002, TestSize.Level1)
974 {
975 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
976 ASSERT_NE(nullptr, audioCapturer);
977
978 bool isStarted = audioCapturer->Start();
979 EXPECT_EQ(false, isStarted);
980 }
981
982 /**
983 * @tc.name : Test Start API via illegal state, CAPTURER_RELEASED: call Start after Release
984 * @tc.number: Audio_Capturer_Start_003
985 * @tc.desc : Test Start interface. Returns false, if the capturer state is CAPTURER_RELEASED.
986 */
987 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Start_003, TestSize.Level1)
988 {
989 AudioCapturerOptions capturerOptions;
990
991 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
992 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
993 ASSERT_NE(nullptr, audioCapturer);
994
995 bool isReleased = audioCapturer->Release();
996 EXPECT_EQ(true, isReleased);
997
998 bool isStarted = audioCapturer->Start();
999 EXPECT_EQ(false, isStarted);
1000
1001 audioCapturer->Release();
1002 }
1003
1004 /**
1005 * @tc.name : Test Start API via legal state, CAPTURER_STOPPED: Start Stop and then Start again
1006 * @tc.number: Audio_Capturer_Start_004
1007 * @tc.desc : Test Start interface. Returns true, if the start is successful.
1008 */
1009 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Start_004, TestSize.Level1)
1010 {
1011 AudioCapturerOptions capturerOptions;
1012
1013 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1014 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1015 ASSERT_NE(nullptr, audioCapturer);
1016
1017 bool isStarted = audioCapturer->Start();
1018 EXPECT_EQ(true, isStarted);
1019
1020 bool isStopped = audioCapturer->Stop();
1021 EXPECT_EQ(true, isStopped);
1022
1023 isStarted = audioCapturer->Start();
1024 EXPECT_EQ(true, isStarted);
1025
1026 audioCapturer->Release();
1027 }
1028
1029 /**
1030 * @tc.name : Test Start API via illegal state, CAPTURER_RUNNING : call Start repeatedly
1031 * @tc.number: Audio_Capturer_Start_005
1032 * @tc.desc : Test Start interface. Returns false, if the capturer state is CAPTURER_RUNNING.
1033 */
1034 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Start_005, TestSize.Level1)
1035 {
1036 AudioCapturerOptions capturerOptions;
1037
1038 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1039 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1040 ASSERT_NE(nullptr, audioCapturer);
1041
1042 bool isStarted = audioCapturer->Start();
1043 EXPECT_EQ(true, isStarted);
1044
1045 isStarted = audioCapturer->Start();
1046 EXPECT_EQ(false, isStarted);
1047
1048 audioCapturer->Release();
1049 }
1050
1051 /**
1052 * @tc.name : Test Read API via isBlockingRead = true.
1053 * @tc.number: Audio_Capturer_Read_001
1054 * @tc.desc : Test Read interface. Returns number of bytes read, if the read is successful.
1055 */
1056 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Read_001, TestSize.Level1)
1057 {
1058 int32_t ret = -1;
1059 bool isBlockingRead = true;
1060 AudioCapturerOptions capturerOptions;
1061
1062 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1063 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1064 ASSERT_NE(nullptr, audioCapturer);
1065
1066 bool isStarted = audioCapturer->Start();
1067 EXPECT_EQ(true, isStarted);
1068
1069 size_t bufferLen;
1070 ret = audioCapturer->GetBufferSize(bufferLen);
1071 EXPECT_EQ(SUCCESS, ret);
1072
1073 uint8_t *buffer = (uint8_t *) malloc(bufferLen);
1074 ASSERT_NE(nullptr, buffer);
1075 FILE *capFile = fopen(AUDIO_CAPTURE_FILE1.c_str(), "wb");
1076 ASSERT_NE(nullptr, capFile);
1077
1078 size_t size = 1;
1079 int32_t bytesRead = 0;
1080 int32_t numBuffersToCapture = READ_BUFFERS_COUNT;
1081
1082 while (numBuffersToCapture) {
1083 bytesRead = audioCapturer->Read(*buffer, bufferLen, isBlockingRead);
1084 if (bytesRead < 0) {
1085 break;
1086 } else if (bytesRead > 0) {
1087 fwrite(buffer, size, bytesRead, capFile);
1088 numBuffersToCapture--;
1089 }
1090 }
1091
1092 audioCapturer->Flush();
1093 audioCapturer->Stop();
1094 audioCapturer->Release();
1095
1096 free(buffer);
1097 fclose(capFile);
1098 }
1099
1100 /**
1101 * @tc.name : Test Read API via isBlockingRead = false.
1102 * @tc.number: Audio_Capturer_Read_002
1103 * @tc.desc : Test Read interface. Returns number of bytes read, if the read is successful.
1104 */
1105 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Read_002, TestSize.Level1)
1106 {
1107 int32_t ret = -1;
1108 bool isBlockingRead = false;
1109 AudioCapturerOptions capturerOptions;
1110
1111 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1112 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1113 ASSERT_NE(nullptr, audioCapturer);
1114
1115 bool isStarted = audioCapturer->Start();
1116 EXPECT_EQ(true, isStarted);
1117
1118 size_t bufferLen;
1119 ret = audioCapturer->GetBufferSize(bufferLen);
1120 EXPECT_EQ(SUCCESS, ret);
1121
1122 uint8_t *buffer = (uint8_t *) malloc(bufferLen);
1123 ASSERT_NE(nullptr, buffer);
1124 FILE *capFile = fopen(AUDIO_CAPTURE_FILE2.c_str(), "wb");
1125 ASSERT_NE(nullptr, capFile);
1126
1127 size_t size = 1;
1128 int32_t bytesRead = 0;
1129 int32_t numBuffersToCapture = READ_BUFFERS_COUNT;
1130
1131 while (numBuffersToCapture) {
1132 bytesRead = audioCapturer->Read(*buffer, bufferLen, isBlockingRead);
1133 if (bytesRead < 0) {
1134 break;
1135 } else if (bytesRead > 0) {
1136 fwrite(buffer, size, bytesRead, capFile);
1137 numBuffersToCapture--;
1138 }
1139 }
1140
1141 audioCapturer->Flush();
1142 audioCapturer->Stop();
1143 audioCapturer->Release();
1144
1145 free(buffer);
1146 fclose(capFile);
1147 }
1148
1149 /**
1150 * @tc.name : Test Read API via illegl state, CAPTURER_NEW : without Initializing the capturer.
1151 * @tc.number: Audio_Capturer_Read_003
1152 * @tc.desc : Test Read interface. Returns error code, if the capturer state is CAPTURER_NEW.
1153 * : bufferLen is invalid here, firstly bufferLen is validated in Read. So it returns ERR_INVALID_PARAM.
1154 */
1155 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Read_003, TestSize.Level1)
1156 {
1157 int32_t ret = -1;
1158 bool isBlockingRead = true;
1159 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
1160 ASSERT_NE(nullptr, audioCapturer);
1161
1162 bool isStarted = audioCapturer->Start();
1163 EXPECT_EQ(false, isStarted);
1164
1165 size_t bufferLen;
1166 ret = audioCapturer->GetBufferSize(bufferLen);
1167 EXPECT_EQ(ERR_OPERATION_FAILED, ret);
1168
1169 uint8_t *buffer = (uint8_t *) malloc(bufferLen);
1170 ASSERT_NE(nullptr, buffer);
1171
1172 int32_t bytesRead = audioCapturer->Read(*buffer, bufferLen, isBlockingRead);
1173 EXPECT_EQ(ERR_INVALID_PARAM, bytesRead);
1174
1175 audioCapturer->Flush();
1176 audioCapturer->Stop();
1177 audioCapturer->Release();
1178
1179 free(buffer);
1180 }
1181
1182 /**
1183 * @tc.name : Test Read API via illegl state, CAPTURER_PREPARED : Read without Start.
1184 * @tc.number: Audio_Capturer_Read_004
1185 * @tc.desc : Test Read interface. Returns error code, if the capturer state is not CAPTURER_RUNNING.
1186 */
1187 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Read_004, TestSize.Level1)
1188 {
1189 int32_t ret = -1;
1190 bool isBlockingRead = true;
1191 AudioCapturerOptions capturerOptions;
1192
1193 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1194 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1195 ASSERT_NE(nullptr, audioCapturer);
1196
1197 size_t bufferLen;
1198 ret = audioCapturer->GetBufferSize(bufferLen);
1199 EXPECT_EQ(SUCCESS, ret);
1200
1201 uint8_t *buffer = (uint8_t *) malloc(bufferLen);
1202 ASSERT_NE(nullptr, buffer);
1203
1204 int32_t bytesRead = audioCapturer->Read(*buffer, bufferLen, isBlockingRead);
1205 EXPECT_EQ(ERR_ILLEGAL_STATE, bytesRead);
1206
1207 audioCapturer->Flush();
1208 audioCapturer->Stop();
1209 audioCapturer->Release();
1210
1211 free(buffer);
1212 }
1213
1214 /**
1215 * @tc.name : Test Read API via illegal input, bufferLength = 0.
1216 * @tc.number: Audio_Capturer_Read_005
1217 * @tc.desc : Test Read interface. Returns error code, if the bufferLength <= 0.
1218 */
1219 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Read_005, TestSize.Level1)
1220 {
1221 bool isBlockingRead = true;
1222 AudioCapturerOptions capturerOptions;
1223
1224 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1225 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1226 ASSERT_NE(nullptr, audioCapturer);
1227
1228 bool isStarted = audioCapturer->Start();
1229 EXPECT_EQ(true, isStarted);
1230
1231 size_t bufferLen = 0;
1232
1233 uint8_t *buffer = (uint8_t *) malloc(bufferLen);
1234 ASSERT_NE(nullptr, buffer);
1235
1236 int32_t bytesRead = audioCapturer->Read(*buffer, bufferLen, isBlockingRead);
1237 EXPECT_EQ(ERR_INVALID_PARAM, bytesRead);
1238
1239 audioCapturer->Flush();
1240 audioCapturer->Stop();
1241 audioCapturer->Release();
1242
1243 free(buffer);
1244 }
1245
1246 /**
1247 * @tc.name : Test Read API via illegal state, CAPTURER_STOPPED: Read after Stop.
1248 * @tc.number: Audio_Capturer_Read_006
1249 * @tc.desc : Test Read interface. Returns error code, if the capturer state is not CAPTURER_RUNNING.
1250 */
1251 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Read_006, TestSize.Level1)
1252 {
1253 int32_t ret = -1;
1254 bool isBlockingRead = true;
1255 AudioCapturerOptions capturerOptions;
1256
1257 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1258 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1259 ASSERT_NE(nullptr, audioCapturer);
1260
1261 bool isStarted = audioCapturer->Start();
1262 EXPECT_EQ(true, isStarted);
1263
1264 size_t bufferLen;
1265 ret = audioCapturer->GetBufferSize(bufferLen);
1266 EXPECT_EQ(SUCCESS, ret);
1267
1268 uint8_t *buffer = (uint8_t *) malloc(bufferLen);
1269 ASSERT_NE(nullptr, buffer);
1270
1271 bool isStopped = audioCapturer->Stop();
1272 EXPECT_EQ(true, isStopped);
1273
1274 int32_t bytesRead = audioCapturer->Read(*buffer, bufferLen, isBlockingRead);
1275 EXPECT_EQ(ERR_ILLEGAL_STATE, bytesRead);
1276
1277 audioCapturer->Release();
1278
1279 free(buffer);
1280 }
1281
1282 /**
1283 * @tc.name : Test Read API via illegal state, CAPTURER_RELEASED: Read after Release.
1284 * @tc.number: Audio_Capturer_Read_007
1285 * @tc.desc : Test Read interface. Returns error code, if the capturer state is not CAPTURER_RUNNING.
1286 */
1287 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Read_007, TestSize.Level1)
1288 {
1289 int32_t ret = -1;
1290 bool isBlockingRead = true;
1291 AudioCapturerOptions capturerOptions;
1292
1293 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1294 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1295 ASSERT_NE(nullptr, audioCapturer);
1296
1297 bool isStarted = audioCapturer->Start();
1298 EXPECT_EQ(true, isStarted);
1299
1300 size_t bufferLen;
1301 ret = audioCapturer->GetBufferSize(bufferLen);
1302 EXPECT_EQ(SUCCESS, ret);
1303
1304 uint8_t *buffer = (uint8_t *) malloc(bufferLen);
1305 ASSERT_NE(nullptr, buffer);
1306
1307 bool isReleased = audioCapturer->Release();
1308 EXPECT_EQ(true, isReleased);
1309
1310 int32_t bytesRead = audioCapturer->Read(*buffer, bufferLen, isBlockingRead);
1311 EXPECT_EQ(ERR_ILLEGAL_STATE, bytesRead);
1312
1313 free(buffer);
1314 }
1315
1316 /**
1317 * @tc.name : Test GetAudioTime API via legal input.
1318 * @tc.number: Audio_Capturer_GetAudioTime_001
1319 * @tc.desc : Test GetAudioTime interface. Returns true, if the getting is successful.
1320 */
1321 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetAudioTime_001, TestSize.Level1)
1322 {
1323 int32_t ret = -1;
1324 bool isBlockingRead = true;
1325 AudioCapturerOptions capturerOptions;
1326
1327 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1328 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1329 ASSERT_NE(nullptr, audioCapturer);
1330
1331 bool isStarted = audioCapturer->Start();
1332 EXPECT_EQ(true, isStarted);
1333
1334 size_t bufferLen;
1335 ret = audioCapturer->GetBufferSize(bufferLen);
1336 EXPECT_EQ(SUCCESS, ret);
1337
1338 uint8_t *buffer = (uint8_t *) malloc(bufferLen);
1339 ASSERT_NE(nullptr, buffer);
1340 int32_t bytesRead = audioCapturer->Read(*buffer, bufferLen, isBlockingRead);
1341 EXPECT_GE(bytesRead, VALUE_ZERO);
1342
1343 Timestamp timeStamp;
1344 bool getAudioTime = audioCapturer->GetAudioTime(timeStamp, Timestamp::Timestampbase::MONOTONIC);
1345 EXPECT_EQ(true, getAudioTime);
1346 EXPECT_GE(timeStamp.time.tv_sec, (const long)VALUE_ZERO);
1347 EXPECT_GE(timeStamp.time.tv_nsec, (const long)VALUE_ZERO);
1348
1349 audioCapturer->Flush();
1350 audioCapturer->Stop();
1351 audioCapturer->Release();
1352
1353 free(buffer);
1354 }
1355
1356 /**
1357 * @tc.name : Test GetAudioTime API via illegal state, CAPTURER_NEW: GetAudioTime without initializing the capturer.
1358 * @tc.number: Audio_Capturer_GetAudioTime_002
1359 * @tc.desc : Test GetAudioTime interface. Returns false, if the capturer state is CAPTURER_NEW.
1360 */
1361 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetAudioTime_002, TestSize.Level1)
1362 {
1363 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
1364 ASSERT_NE(nullptr, audioCapturer);
1365
1366 Timestamp timeStamp;
1367 bool getAudioTime = audioCapturer->GetAudioTime(timeStamp, Timestamp::Timestampbase::MONOTONIC);
1368 EXPECT_EQ(false, getAudioTime);
1369 }
1370
1371 /**
1372 * @tc.name : Test GetAudioTime API via legal state, CAPTURER_RUNNING.
1373 * @tc.number: Audio_Capturer_GetAudioTime_003
1374 * @tc.desc : test GetAudioTime interface. Returns true, if the getting is successful.
1375 */
1376 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetAudioTime_003, TestSize.Level1)
1377 {
1378 AudioCapturerOptions capturerOptions;
1379
1380 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1381 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1382 ASSERT_NE(nullptr, audioCapturer);
1383
1384 bool isStarted = audioCapturer->Start();
1385 EXPECT_EQ(true, isStarted);
1386
1387 Timestamp timeStamp;
1388 bool getAudioTime = audioCapturer->GetAudioTime(timeStamp, Timestamp::Timestampbase::MONOTONIC);
1389 EXPECT_EQ(true, getAudioTime);
1390
1391 audioCapturer->Release();
1392 }
1393
1394 /**
1395 * @tc.name : Test GetAudioTime API via legal state, CAPTURER_STOPPED.
1396 * @tc.number: Audio_Capturer_GetAudioTime_004
1397 * @tc.desc : Test GetAudioTime interface. Returns true, if the getting is successful.
1398 */
1399 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetAudioTime_004, TestSize.Level1)
1400 {
1401 AudioCapturerOptions capturerOptions;
1402
1403 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1404 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1405 ASSERT_NE(nullptr, audioCapturer);
1406
1407 bool isStarted = audioCapturer->Start();
1408 EXPECT_EQ(true, isStarted);
1409
1410 bool isStopped = audioCapturer->Stop();
1411 EXPECT_EQ(true, isStopped);
1412
1413 Timestamp timeStamp;
1414 bool getAudioTime = audioCapturer->GetAudioTime(timeStamp, Timestamp::Timestampbase::MONOTONIC);
1415 EXPECT_EQ(true, getAudioTime);
1416
1417 audioCapturer->Release();
1418 }
1419
1420 /**
1421 * @tc.name : Test GetAudioTime API via illegal state, CAPTURER_RELEASED: GetAudioTime after Release.
1422 * @tc.number: Audio_Capturer_GetAudioTime_005
1423 * @tc.desc : Test GetAudioTime interface. Returns false, if the capturer state is CAPTURER_RELEASED
1424 */
1425 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetAudioTime_005, TestSize.Level1)
1426 {
1427 AudioCapturerOptions capturerOptions;
1428
1429 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1430 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1431 ASSERT_NE(nullptr, audioCapturer);
1432
1433 bool isStarted = audioCapturer->Start();
1434 EXPECT_EQ(true, isStarted);
1435
1436 bool isStopped = audioCapturer->Stop();
1437 EXPECT_EQ(true, isStopped);
1438
1439 bool isReleased = audioCapturer->Release();
1440 EXPECT_EQ(true, isReleased);
1441
1442 Timestamp timeStamp;
1443 bool getAudioTime = audioCapturer->GetAudioTime(timeStamp, Timestamp::Timestampbase::MONOTONIC);
1444 EXPECT_EQ(false, getAudioTime);
1445 }
1446
1447 /**
1448 * @tc.name : Test GetAudioTime API stability.
1449 * @tc.number: Audio_Capturer_GetAudioTime_Stability_001
1450 * @tc.desc : Test GetAudioTime interface stability.
1451 */
1452 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetAudioTime_Stability_001, TestSize.Level1)
1453 {
1454 AudioCapturerOptions capturerOptions;
1455
1456 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1457 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1458 ASSERT_NE(nullptr, audioCapturer);
1459
1460 bool isStarted = audioCapturer->Start();
1461 EXPECT_EQ(true, isStarted);
1462
1463 thread captureThread(StartCaptureThread, audioCapturer.get(), AUDIO_TIME_STABILITY_TEST_FILE);
1464
1465 for (int i = 0; i < VALUE_THOUSAND; i++) {
1466 Timestamp timeStamp;
1467 bool getAudioTime = audioCapturer->GetAudioTime(timeStamp, Timestamp::Timestampbase::MONOTONIC);
1468 EXPECT_EQ(true, getAudioTime);
1469 }
1470
1471 captureThread.join();
1472
1473 bool isStopped = audioCapturer->Stop();
1474 EXPECT_EQ(true, isStopped);
1475
1476 bool isReleased = audioCapturer->Release();
1477 EXPECT_EQ(true, isReleased);
1478 }
1479
1480 /**
1481 * @tc.name : Test Flush API.
1482 * @tc.number: Audio_Capturer_Flush_001
1483 * @tc.desc : Test Flush interface. Returns true, if the flush is successful.
1484 */
1485 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Flush_001, TestSize.Level1)
1486 {
1487 int32_t ret = -1;
1488 bool isBlockingRead = true;
1489 AudioCapturerOptions capturerOptions;
1490
1491 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1492 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1493 ASSERT_NE(nullptr, audioCapturer);
1494
1495 bool isStarted = audioCapturer->Start();
1496 EXPECT_EQ(true, isStarted);
1497
1498 size_t bufferLen;
1499 ret = audioCapturer->GetBufferSize(bufferLen);
1500 EXPECT_EQ(SUCCESS, ret);
1501
1502 uint8_t *buffer = (uint8_t *) malloc(bufferLen);
1503 ASSERT_NE(nullptr, buffer);
1504
1505 int32_t bytesRead = audioCapturer->Read(*buffer, bufferLen, isBlockingRead);
1506 EXPECT_GE(bytesRead, VALUE_ZERO);
1507
1508 bool isFlushed = audioCapturer->Flush();
1509 EXPECT_EQ(true, isFlushed);
1510
1511 audioCapturer->Stop();
1512 audioCapturer->Release();
1513
1514 free(buffer);
1515 }
1516
1517 /**
1518 * @tc.name : Test Flush API via illegal state, CAPTURER_NEW: Without initializing the capturer.
1519 * @tc.number: Audio_Capturer_Flush_002
1520 * @tc.desc : Test Flush interface. Returns false, if the capturer state is not CAPTURER_RUNNING.
1521 */
1522 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Flush_002, TestSize.Level1)
1523 {
1524 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
1525 ASSERT_NE(nullptr, audioCapturer);
1526
1527 bool isFlushed = audioCapturer->Flush();
1528 EXPECT_EQ(false, isFlushed);
1529
1530 audioCapturer->Release();
1531 }
1532
1533 /**
1534 * @tc.name : Test Flush API via illegal state, CAPTURER_PREPARED: Without Start.
1535 * @tc.number: Audio_Capturer_Flush_003
1536 * @tc.desc : Test Flush interface. Returns false, if the capturer state is not CAPTURER_RUNNING.
1537 */
1538 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Flush_003, TestSize.Level1)
1539 {
1540 AudioCapturerOptions capturerOptions;
1541
1542 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1543 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1544 ASSERT_NE(nullptr, audioCapturer);
1545
1546 bool isFlushed = audioCapturer->Flush();
1547 EXPECT_EQ(false, isFlushed);
1548
1549 audioCapturer->Release();
1550 }
1551
1552 /**
1553 * @tc.name : Test Flush API: call Stop before Flush.
1554 * @tc.number: Audio_Capturer_Flush_004
1555 * @tc.desc : Test Flush interface. Returns true, if the capturer state is CAPTURER_STOPPED.
1556 */
1557 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Flush_004, TestSize.Level1)
1558 {
1559 AudioCapturerOptions capturerOptions;
1560
1561 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1562 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1563 ASSERT_NE(nullptr, audioCapturer);
1564
1565 bool isStarted = audioCapturer->Start();
1566 EXPECT_EQ(true, isStarted);
1567
1568 bool isStopped = audioCapturer->Stop();
1569 EXPECT_EQ(true, isStopped);
1570
1571 bool isFlushed = audioCapturer->Flush();
1572 EXPECT_EQ(true, isFlushed);
1573
1574 audioCapturer->Release();
1575 }
1576
1577 /**
1578 * @tc.name : Test Flush API via illegal state, CAPTURER_RELEASED: call Release before Flush.
1579 * @tc.number: Audio_Capturer_Flush_005
1580 * @tc.desc : Test Flush interface. Returns false, if the capturer state is not CAPTURER_RUNNING.
1581 */
1582 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Flush_005, TestSize.Level1)
1583 {
1584 AudioCapturerOptions capturerOptions;
1585
1586 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1587 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1588 ASSERT_NE(nullptr, audioCapturer);
1589
1590 bool isStarted = audioCapturer->Start();
1591 EXPECT_EQ(true, isStarted);
1592
1593 bool isReleased = audioCapturer->Release();
1594 EXPECT_EQ(true, isReleased);
1595
1596 bool isFlushed = audioCapturer->Flush();
1597 EXPECT_EQ(false, isFlushed);
1598
1599 audioCapturer->Release();
1600 }
1601
1602 /**
1603 * @tc.name : Test Flush API stability.
1604 * @tc.number: Audio_Capturer_Flush_Stability_001
1605 * @tc.desc : Test Flush interface stability.
1606 */
1607 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Flush_Stability_001, TestSize.Level1)
1608 {
1609 AudioCapturerOptions capturerOptions;
1610
1611 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1612 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1613 ASSERT_NE(nullptr, audioCapturer);
1614
1615 bool isStarted = audioCapturer->Start();
1616 EXPECT_EQ(true, isStarted);
1617
1618 thread captureThread(StartCaptureThread, audioCapturer.get(), AUDIO_FLUSH_STABILITY_TEST_FILE);
1619
1620 for (int i = 0; i < VALUE_HUNDRED; i++) {
1621 bool isFlushed = audioCapturer->Flush();
1622 EXPECT_EQ(true, isFlushed);
1623 }
1624
1625 captureThread.join();
1626
1627 bool isStopped = audioCapturer->Stop();
1628 EXPECT_EQ(true, isStopped);
1629
1630 bool isReleased = audioCapturer->Release();
1631 EXPECT_EQ(true, isReleased);
1632 }
1633
1634 /**
1635 * @tc.name : Test Stop API.
1636 * @tc.number: Audio_Capturer_Stop_001
1637 * @tc.desc : Test Stop interface. Returns true, if the stop is successful.
1638 */
1639 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Stop_001, TestSize.Level1)
1640 {
1641 int32_t ret = -1;
1642 bool isBlockingRead = true;
1643 AudioCapturerOptions capturerOptions;
1644
1645 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1646 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1647 ASSERT_NE(nullptr, audioCapturer);
1648
1649 bool isStarted = audioCapturer->Start();
1650 EXPECT_EQ(true, isStarted);
1651
1652 size_t bufferLen;
1653 ret = audioCapturer->GetBufferSize(bufferLen);
1654 EXPECT_EQ(SUCCESS, ret);
1655
1656 uint8_t *buffer = (uint8_t *) malloc(bufferLen);
1657 ASSERT_NE(nullptr, buffer);
1658
1659 int32_t bytesRead = audioCapturer->Read(*buffer, bufferLen, isBlockingRead);
1660 EXPECT_GE(bytesRead, VALUE_ZERO);
1661
1662 audioCapturer->Flush();
1663
1664 bool isStopped = audioCapturer->Stop();
1665 EXPECT_EQ(true, isStopped);
1666
1667 audioCapturer->Release();
1668
1669 free(buffer);
1670 }
1671
1672 /**
1673 * @tc.name : Test Stop API via illegal state, CAPTURER_NEW: call Stop without Initializing the capturer.
1674 * @tc.number: Audio_Capturer_Stop_002
1675 * @tc.desc : Test Stop interface. Returns false, if the capturer state is not CAPTURER_RUNNING.
1676 */
1677 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Stop_002, TestSize.Level1)
1678 {
1679 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
1680 ASSERT_NE(nullptr, audioCapturer);
1681
1682 bool isStopped = audioCapturer->Stop();
1683 EXPECT_EQ(false, isStopped);
1684 }
1685
1686 /**
1687 * @tc.name : Test Stop API via illegal state, CAPTURER_PREPARED: call Stop without Start.
1688 * @tc.number: Audio_Capturer_Stop_003
1689 * @tc.desc : Test Stop interface. Returns false, if the capturer state is not CAPTURER_RUNNING.
1690 */
1691 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Stop_003, TestSize.Level1)
1692 {
1693 AudioCapturerOptions capturerOptions;
1694
1695 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1696 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1697 ASSERT_NE(nullptr, audioCapturer);
1698
1699 bool isStopped = audioCapturer->Stop();
1700 EXPECT_EQ(false, isStopped);
1701
1702 audioCapturer->Release();
1703 }
1704
1705 /**
1706 * @tc.name : Test Stop API via illegal state, CAPTURER_RELEASED: call Stop after Release.
1707 * @tc.number: Audio_Capturer_Stop_004
1708 * @tc.desc : Test Stop interface. Returns false, if the capturer state is not CAPTURER_RUNNING.
1709 */
1710 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Stop_004, TestSize.Level1)
1711 {
1712 AudioCapturerOptions capturerOptions;
1713
1714 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1715 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1716 ASSERT_NE(nullptr, audioCapturer);
1717
1718 bool isStarted = audioCapturer->Start();
1719 EXPECT_EQ(true, isStarted);
1720
1721 bool isReleased = audioCapturer->Release();
1722 EXPECT_EQ(true, isReleased);
1723
1724 bool isStopped = audioCapturer->Stop();
1725 EXPECT_EQ(false, isStopped);
1726 }
1727
1728 /**
1729 * @tc.name : Test Stop API via legal state. call Start, Stop, Start and Stop again
1730 * @tc.number: Audio_Capturer_Stop_005
1731 * @tc.desc : Test Stop interface. Returns true , if the stop is successful.
1732 */
1733 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Stop_005, TestSize.Level1)
1734 {
1735 AudioCapturerOptions capturerOptions;
1736
1737 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1738 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1739 ASSERT_NE(nullptr, audioCapturer);
1740
1741 bool isStarted = audioCapturer->Start();
1742 EXPECT_EQ(true, isStarted);
1743
1744 bool isStopped = audioCapturer->Stop();
1745 EXPECT_EQ(true, isStopped);
1746
1747 isStarted = audioCapturer->Start();
1748 EXPECT_EQ(true, isStarted);
1749
1750 isStopped = audioCapturer->Stop();
1751 EXPECT_EQ(true, isStopped);
1752 audioCapturer->Release();
1753 }
1754
1755 /**
1756 * @tc.name : Test Release API.
1757 * @tc.number: Audio_Capturer_Release_001
1758 * @tc.desc : Test Release interface. Returns true, if the release is successful.
1759 */
1760 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Release_001, TestSize.Level1)
1761 {
1762 int32_t ret = -1;
1763 bool isBlockingRead = true;
1764 AudioCapturerOptions capturerOptions;
1765
1766 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1767 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1768 ASSERT_NE(nullptr, audioCapturer);
1769
1770 bool isStarted = audioCapturer->Start();
1771 EXPECT_EQ(true, isStarted);
1772
1773 size_t bufferLen;
1774 ret = audioCapturer->GetBufferSize(bufferLen);
1775 EXPECT_EQ(SUCCESS, ret);
1776
1777 uint8_t *buffer = (uint8_t *) malloc(bufferLen);
1778 ASSERT_NE(nullptr, buffer);
1779
1780 int32_t bytesRead = audioCapturer->Read(*buffer, bufferLen, isBlockingRead);
1781 EXPECT_GE(bytesRead, VALUE_ZERO);
1782
1783 audioCapturer->Flush();
1784 audioCapturer->Stop();
1785
1786 bool isReleased = audioCapturer->Release();
1787 EXPECT_EQ(true, isReleased);
1788
1789 free(buffer);
1790 }
1791
1792 /**
1793 * @tc.name : Test Release API via illegal state, CAPTURER_NEW: Call Release without initializing the capturer.
1794 * @tc.number: Audio_Capturer_Release_002
1795 * @tc.desc : Test Release interface, Returns false, if the state is CAPTURER_NEW.
1796 */
1797 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Release_002, TestSize.Level1)
1798 {
1799 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
1800 ASSERT_NE(nullptr, audioCapturer);
1801
1802 bool isReleased = audioCapturer->Release();
1803 EXPECT_EQ(false, isReleased);
1804 }
1805
1806 /**
1807 * @tc.name : Test Release API via illegal state, CAPTURER_RELEASED: call Release repeatedly.
1808 * @tc.number: Audio_Capturer_Release_003
1809 * @tc.desc : Test Release interface. Returns false, if the state is already CAPTURER_RELEASED.
1810 */
1811 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Release_003, TestSize.Level1)
1812 {
1813 AudioCapturerOptions capturerOptions;
1814
1815 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1816 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1817 ASSERT_NE(nullptr, audioCapturer);
1818
1819 bool isReleased = audioCapturer->Release();
1820 EXPECT_EQ(true, isReleased);
1821
1822 isReleased = audioCapturer->Release();
1823 EXPECT_EQ(false, isReleased);
1824 }
1825
1826 /**
1827 * @tc.name : Test Release API via legal state, CAPTURER_RUNNING: call Release after Start
1828 * @tc.number: Audio_Capturer_Release_004
1829 * @tc.desc : Test Release interface. Returns true, if the release is successful.
1830 */
1831 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Release_004, TestSize.Level1)
1832 {
1833 AudioCapturerOptions capturerOptions;
1834
1835 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1836 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1837 ASSERT_NE(nullptr, audioCapturer);
1838
1839 bool isStarted = audioCapturer->Start();
1840 EXPECT_EQ(true, isStarted);
1841
1842 bool isReleased = audioCapturer->Release();
1843 EXPECT_EQ(true, isReleased);
1844 }
1845
1846 /**
1847 * @tc.name : Test Release API via legal state, CAPTURER_STOPPED: call release after Start and Stop
1848 * @tc.number: Audio_Capturer_Release_005
1849 * @tc.desc : Test Release interface. Returns true, if the release is successful.
1850 */
1851 HWTEST(AudioCapturerUnitTest, Audio_Capturer_Release_005, TestSize.Level1)
1852 {
1853 AudioCapturerOptions capturerOptions;
1854
1855 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1856 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1857 ASSERT_NE(nullptr, audioCapturer);
1858
1859 bool isStarted = audioCapturer->Start();
1860 EXPECT_EQ(true, isStarted);
1861
1862 bool isStopped = audioCapturer->Stop();
1863 EXPECT_EQ(true, isStopped);
1864
1865 bool isReleased = audioCapturer->Release();
1866 EXPECT_EQ(true, isReleased);
1867 }
1868
1869 /**
1870 * @tc.name : Test GetStatus API.
1871 * @tc.number: Audio_Capturer_GetStatus_001
1872 * @tc.desc : Test GetStatus interface. Returns correct state on success.
1873 */
1874 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetStatus_001, TestSize.Level1)
1875 {
1876 CapturerState state = CAPTURER_INVALID;
1877 AudioCapturerOptions capturerOptions;
1878
1879 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1880 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1881 ASSERT_NE(nullptr, audioCapturer);
1882
1883 state = audioCapturer->GetStatus();
1884 EXPECT_EQ(CAPTURER_PREPARED, state);
1885
1886 bool isStarted = audioCapturer->Start();
1887 EXPECT_EQ(true, isStarted);
1888 state = audioCapturer->GetStatus();
1889 EXPECT_EQ(CAPTURER_RUNNING, state);
1890
1891 bool isStopped = audioCapturer->Stop();
1892 EXPECT_EQ(true, isStopped);
1893 state = audioCapturer->GetStatus();
1894 EXPECT_EQ(CAPTURER_STOPPED, state);
1895
1896 bool isReleased = audioCapturer->Release();
1897 EXPECT_EQ(true, isReleased);
1898 state = audioCapturer->GetStatus();
1899 EXPECT_EQ(CAPTURER_RELEASED, state);
1900 }
1901
1902 /**
1903 * @tc.name : Test GetStatus API, call Start without Initializing the capturer
1904 * @tc.number: Audio_Capturer_GetStatus_002
1905 * @tc.desc : Test GetStatus interface. Not changes to CAPTURER_RUNNING, if the current state is CAPTURER_NEW.
1906 */
1907 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetStatus_002, TestSize.Level1)
1908 {
1909 CapturerState state = CAPTURER_INVALID;
1910
1911 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
1912 ASSERT_NE(nullptr, audioCapturer);
1913
1914 bool isStarted = audioCapturer->Start();
1915 EXPECT_EQ(false, isStarted);
1916 state = audioCapturer->GetStatus();
1917 EXPECT_NE(CAPTURER_RUNNING, state);
1918 EXPECT_EQ(CAPTURER_NEW, state);
1919 }
1920
1921 /**
1922 * @tc.name : Test GetStatus API, call Stop without Start
1923 * @tc.number: Audio_Capturer_GetStatus_003
1924 * @tc.desc : Test GetStatus interface. Not changes to CAPTURER_STOPPED, if the current state is CAPTURER_PREPARED.
1925 */
1926 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetStatus_003, TestSize.Level1)
1927 {
1928 CapturerState state = CAPTURER_INVALID;
1929 AudioCapturerOptions capturerOptions;
1930
1931 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1932 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1933 ASSERT_NE(nullptr, audioCapturer);
1934
1935 bool isStopped = audioCapturer->Stop();
1936 EXPECT_EQ(false, isStopped);
1937 state = audioCapturer->GetStatus();
1938 EXPECT_NE(CAPTURER_STOPPED, state);
1939 EXPECT_EQ(CAPTURER_PREPARED, state);
1940
1941 audioCapturer->Release();
1942 }
1943
1944 /**
1945 * @tc.name : Test GetStatus API, call Start, Stop and then Start again
1946 * @tc.number: Audio_Capturer_GetStatus_004
1947 * @tc.desc : Test GetStatus interface. Returns correct state on success.
1948 */
1949 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetStatus_004, TestSize.Level1)
1950 {
1951 CapturerState state = CAPTURER_INVALID;
1952 AudioCapturerOptions capturerOptions;
1953
1954 AudioCapturerUnitTest::InitializeCapturerOptions(capturerOptions);
1955 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
1956 ASSERT_NE(nullptr, audioCapturer);
1957
1958 bool isStarted = audioCapturer->Start();
1959 EXPECT_EQ(true, isStarted);
1960 state = audioCapturer->GetStatus();
1961 EXPECT_EQ(CAPTURER_RUNNING, state);
1962
1963 bool isStopped = audioCapturer->Stop();
1964 EXPECT_EQ(true, isStopped);
1965 state = audioCapturer->GetStatus();
1966 EXPECT_EQ(CAPTURER_STOPPED, state);
1967
1968 isStarted = audioCapturer->Start();
1969 EXPECT_EQ(true, isStarted);
1970 state = audioCapturer->GetStatus();
1971 EXPECT_EQ(CAPTURER_RUNNING, state);
1972
1973 audioCapturer->Release();
1974 }
1975
1976 /**
1977 * @tc.name : Test GetStatus API, call Release without initializing
1978 * @tc.number: Audio_Capturer_GetStatus_005
1979 * @tc.desc : Test GetStatus interface. Not changes to CAPTURER_RELEASED, if the current state is CAPTURER_NEW.
1980 */
1981 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetStatus_005, TestSize.Level1)
1982 {
1983 CapturerState state = CAPTURER_INVALID;
1984
1985 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
1986 ASSERT_NE(nullptr, audioCapturer);
1987
1988 bool isReleased = audioCapturer->Release();
1989 EXPECT_EQ(false, isReleased);
1990 state = audioCapturer->GetStatus();
1991 EXPECT_NE(CAPTURER_RELEASED, state);
1992 EXPECT_EQ(CAPTURER_NEW, state);
1993 }
1994
1995 /**
1996 * @tc.name : Test GetCapturerInfo API after calling create
1997 * @tc.number: Audio_Capturer_GetCapturerInfo_001
1998 * @tc.desc : Test GetCapturerInfo interface. Check whether capturer info returns proper data
1999 */
2000 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetCapturerInfo_001, TestSize.Level1)
2001 {
2002 AudioCapturerOptions capturerOptions;
2003 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_96000;
2004 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
2005 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_U8;
2006 capturerOptions.streamInfo.channels = AudioChannel::MONO;
2007 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
2008 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
2009
2010 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
2011 ASSERT_NE(nullptr, audioCapturer);
2012
2013 AudioCapturerInfo capturerInfo;
2014 audioCapturer->GetCapturerInfo(capturerInfo);
2015
2016 EXPECT_EQ(SourceType::SOURCE_TYPE_MIC, capturerInfo.sourceType);
2017 EXPECT_EQ(CAPTURER_FLAG, capturerInfo.capturerFlags);
2018 audioCapturer->Release();
2019 }
2020
2021 /**
2022 * @tc.name : Test GetCapturerInfo API after calling start
2023 * @tc.number: Audio_Capturer_GetCapturerInfo_002
2024 * @tc.desc : Test GetCapturerInfo interface. Check whether capturer info returns proper data
2025 */
2026 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetCapturerInfo_002, TestSize.Level1)
2027 {
2028 AudioCapturerOptions capturerOptions;
2029 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_96000;
2030 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
2031 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_U8;
2032 capturerOptions.streamInfo.channels = AudioChannel::MONO;
2033 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
2034 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
2035
2036 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
2037 ASSERT_NE(nullptr, audioCapturer);
2038
2039 bool isStarted = audioCapturer->Start();
2040 EXPECT_EQ(true, isStarted);
2041
2042 AudioCapturerInfo capturerInfo;
2043 audioCapturer->GetCapturerInfo(capturerInfo);
2044
2045 EXPECT_EQ(SourceType::SOURCE_TYPE_MIC, capturerInfo.sourceType);
2046 EXPECT_EQ(CAPTURER_FLAG, capturerInfo.capturerFlags);
2047
2048 audioCapturer->Stop();
2049 audioCapturer->Release();
2050 }
2051
2052 /**
2053 * @tc.name : Test GetCapturerInfo API after calling release
2054 * @tc.number: Audio_Capturer_GetCapturerInfo_003
2055 * @tc.desc : Test GetCapturerInfo interface. Check whether capturer info returns proper data
2056 */
2057 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetCapturerInfo_003, TestSize.Level1)
2058 {
2059 AudioCapturerOptions capturerOptions;
2060 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_96000;
2061 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
2062 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_U8;
2063 capturerOptions.streamInfo.channels = AudioChannel::MONO;
2064 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
2065 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
2066
2067 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
2068 ASSERT_NE(nullptr, audioCapturer);
2069
2070 bool isReleased = audioCapturer->Release();
2071 EXPECT_EQ(true, isReleased);
2072
2073 AudioCapturerInfo capturerInfo;
2074 audioCapturer->GetCapturerInfo(capturerInfo);
2075
2076 EXPECT_EQ(SourceType::SOURCE_TYPE_MIC, capturerInfo.sourceType);
2077 EXPECT_EQ(CAPTURER_FLAG, capturerInfo.capturerFlags);
2078 }
2079
2080 /**
2081 * @tc.name : Test GetCapturerInfo API after calling stop
2082 * @tc.number: Audio_Capturer_GetCapturerInfo_004
2083 * @tc.desc : Test GetCapturerInfo interface. Check whether capturer info returns proper data
2084 */
2085 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetCapturerInfo_004, TestSize.Level1)
2086 {
2087 AudioCapturerOptions capturerOptions;
2088 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_96000;
2089 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
2090 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_U8;
2091 capturerOptions.streamInfo.channels = AudioChannel::MONO;
2092 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
2093 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
2094
2095 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
2096 ASSERT_NE(nullptr, audioCapturer);
2097
2098 bool isStarted = audioCapturer->Start();
2099 EXPECT_EQ(true, isStarted);
2100
2101 bool isStopped = audioCapturer->Stop();
2102 EXPECT_EQ(true, isStopped);
2103
2104 AudioCapturerInfo capturerInfo;
2105 audioCapturer->GetCapturerInfo(capturerInfo);
2106
2107 EXPECT_EQ(SourceType::SOURCE_TYPE_MIC, capturerInfo.sourceType);
2108 EXPECT_EQ(CAPTURER_FLAG, capturerInfo.capturerFlags);
2109 audioCapturer->Release();
2110 }
2111
2112 /**
2113 * @tc.name : Test GetCapturerInfo API Stability
2114 * @tc.number: Audio_Capturer_GetCapturerInfo_Stability_001
2115 * @tc.desc : Test GetCapturerInfo interface. Check whether capturer info returns proper data
2116 */
2117 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetCapturerInfo_Stability_001, TestSize.Level1)
2118 {
2119 AudioCapturerOptions capturerOptions;
2120 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_96000;
2121 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
2122 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_U8;
2123 capturerOptions.streamInfo.channels = AudioChannel::MONO;
2124 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
2125 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
2126
2127 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
2128 ASSERT_NE(nullptr, audioCapturer);
2129
2130 for (int i = 0; i < VALUE_HUNDRED; i++) {
2131
2132 AudioCapturerInfo capturerInfo;
2133 audioCapturer->GetCapturerInfo(capturerInfo);
2134
2135 EXPECT_EQ(SourceType::SOURCE_TYPE_MIC, capturerInfo.sourceType);
2136 EXPECT_EQ(CAPTURER_FLAG, capturerInfo.capturerFlags);
2137 }
2138 audioCapturer->Release();
2139 }
2140
2141 /**
2142 * @tc.name : Test GetStreamInfo API after calling create
2143 * @tc.number: Audio_Capturer_GetStreamInfo_001
2144 * @tc.desc : Test GetStreamInfo interface. Check whether stream related data is returned correctly
2145 */
2146 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetStreamInfo_001, TestSize.Level1)
2147 {
2148 AudioCapturerOptions capturerOptions;
2149 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_96000;
2150 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
2151 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_U8;
2152 capturerOptions.streamInfo.channels = AudioChannel::MONO;
2153 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
2154 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
2155
2156 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
2157 ASSERT_NE(nullptr, audioCapturer);
2158
2159 AudioStreamInfo streamInfo;
2160 audioCapturer->GetStreamInfo(streamInfo);
2161
2162 EXPECT_EQ(AudioSamplingRate::SAMPLE_RATE_96000, streamInfo.samplingRate);
2163 EXPECT_EQ(AudioEncodingType::ENCODING_PCM, streamInfo.encoding);
2164 EXPECT_EQ(AudioSampleFormat::SAMPLE_U8, streamInfo.format);
2165 EXPECT_EQ(AudioChannel::MONO, streamInfo.channels);
2166 audioCapturer->Release();
2167 }
2168
2169 /**
2170 * @tc.name : Test GetStreamInfo API after calling start
2171 * @tc.number: Audio_Capturer_GetStreamInfo_002
2172 * @tc.desc : Test GetStreamInfo interface. Check whether stream related data is returned correctly
2173 */
2174 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetStreamInfo_002, TestSize.Level1)
2175 {
2176 int32_t ret = -1;
2177 AudioCapturerOptions capturerOptions;
2178
2179 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_96000;
2180 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
2181 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_U8;
2182 capturerOptions.streamInfo.channels = AudioChannel::MONO;
2183 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
2184 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
2185
2186 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
2187 ASSERT_NE(nullptr, audioCapturer);
2188
2189 bool isStarted = audioCapturer->Start();
2190 EXPECT_EQ(true, isStarted);
2191
2192 AudioStreamInfo streamInfo;
2193 ret = audioCapturer->GetStreamInfo(streamInfo);
2194
2195 EXPECT_EQ(SUCCESS, ret);
2196 EXPECT_EQ(AudioSamplingRate::SAMPLE_RATE_96000, streamInfo.samplingRate);
2197 EXPECT_EQ(AudioEncodingType::ENCODING_PCM, streamInfo.encoding);
2198 EXPECT_EQ(AudioSampleFormat::SAMPLE_U8, streamInfo.format);
2199 EXPECT_EQ(AudioChannel::MONO, streamInfo.channels);
2200
2201 audioCapturer->Stop();
2202 audioCapturer->Release();
2203 }
2204
2205 /**
2206 * @tc.name : Test GetStreamInfo API after calling stop
2207 * @tc.number: Audio_Capturer_GetStreamInfo_003
2208 * @tc.desc : Test GetStreamInfo interface. Check whether stream related data is returned correctly
2209 */
2210 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetStreamInfo_003, TestSize.Level1)
2211 {
2212 int32_t ret = -1;
2213 AudioCapturerOptions capturerOptions;
2214
2215 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_96000;
2216 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
2217 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_U8;
2218 capturerOptions.streamInfo.channels = AudioChannel::MONO;
2219 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
2220 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
2221
2222 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
2223 ASSERT_NE(nullptr, audioCapturer);
2224
2225 bool isStarted = audioCapturer->Start();
2226 EXPECT_EQ(true, isStarted);
2227
2228 bool isStopped = audioCapturer->Stop();
2229 EXPECT_EQ(true, isStopped);
2230
2231 AudioStreamInfo streamInfo;
2232 ret = audioCapturer->GetStreamInfo(streamInfo);
2233
2234 EXPECT_EQ(SUCCESS, ret);
2235 EXPECT_EQ(AudioSamplingRate::SAMPLE_RATE_96000, streamInfo.samplingRate);
2236 EXPECT_EQ(AudioEncodingType::ENCODING_PCM, streamInfo.encoding);
2237 EXPECT_EQ(AudioSampleFormat::SAMPLE_U8, streamInfo.format);
2238 EXPECT_EQ(AudioChannel::MONO, streamInfo.channels);
2239
2240 audioCapturer->Release();
2241 }
2242
2243 /**
2244 * @tc.name : Test GetStreamInfo API after calling release
2245 * @tc.number: Audio_Capturer_GetStreamInfo_004
2246 * @tc.desc : Test GetStreamInfo interface. Check whether stream related data is returned correctly
2247 */
2248 HWTEST(AudioCapturerUnitTest, Audio_Capturer_GetStreamInfo_004, TestSize.Level1)
2249 {
2250 int32_t ret = -1;
2251 AudioCapturerOptions capturerOptions;
2252
2253 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_96000;
2254 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
2255 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_U8;
2256 capturerOptions.streamInfo.channels = AudioChannel::MONO;
2257 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
2258 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
2259
2260 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
2261 ASSERT_NE(nullptr, audioCapturer);
2262
2263 bool isReleased = audioCapturer->Release();
2264 EXPECT_EQ(true, isReleased);
2265
2266 AudioStreamInfo streamInfo;
2267 ret = audioCapturer->GetStreamInfo(streamInfo);
2268
2269 EXPECT_EQ(ERR_OPERATION_FAILED, ret);
2270 }
2271
2272 /**
2273 * @tc.name : Test GetStreamInfo API after calling create
2274 * @tc.number: Audio_Renderer_GetStreamInfo_Stability_001
2275 * @tc.desc : Test GetStreamInfo interface. Check whether stream related data is returned correctly
2276 */
2277 HWTEST(AudioCapturerUnitTest, Audio_Renderer_GetStreamInfo_Stability_001, TestSize.Level1)
2278 {
2279 int32_t ret = -1;
2280 AudioCapturerOptions capturerOptions;
2281
2282 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_96000;
2283 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
2284 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_U8;
2285 capturerOptions.streamInfo.channels = AudioChannel::MONO;
2286 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
2287 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
2288
2289 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
2290 ASSERT_NE(nullptr, audioCapturer);
2291
2292 for (int i = 0; i < VALUE_HUNDRED; i++) {
2293 AudioStreamInfo streamInfo;
2294 ret = audioCapturer->GetStreamInfo(streamInfo);
2295 EXPECT_EQ(SUCCESS, ret);
2296 }
2297 audioCapturer->Release();
2298 }
2299
2300 /**
2301 * @tc.name : Test SetBufferDuration API
2302 * @tc.number: Audio_Capturer_SetBufferDuration_001
2303 * @tc.desc : Test SetBufferDuration interface. Check whether valid parameters are accepted.
2304 */
2305 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetBufferDuration_001, TestSize.Level1)
2306 {
2307 int32_t ret = -1;
2308
2309 AudioCapturerOptions capturerOptions;
2310 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_96000;
2311 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
2312 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_U8;
2313 capturerOptions.streamInfo.channels = AudioChannel::MONO;
2314 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
2315 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
2316
2317 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
2318 ASSERT_NE(nullptr, audioCapturer);
2319
2320 ret = audioCapturer->SetBufferDuration(BUFFER_DURATION_FIVE);
2321 EXPECT_EQ(SUCCESS, ret);
2322
2323 ret = audioCapturer->SetBufferDuration(BUFFER_DURATION_TEN);
2324 EXPECT_EQ(SUCCESS, ret);
2325
2326 ret = audioCapturer->SetBufferDuration(BUFFER_DURATION_FIFTEEN);
2327 EXPECT_EQ(SUCCESS, ret);
2328
2329 ret = audioCapturer->SetBufferDuration(BUFFER_DURATION_TWENTY);
2330 EXPECT_EQ(SUCCESS, ret);
2331 }
2332
2333 /**
2334 * @tc.name : Test SetBufferDuration API
2335 * @tc.number: Audio_Capturer_SetBufferDuration_002
2336 * @tc.desc : Test SetBufferDuration interface. Check whether invalid parameters are rejected.
2337 */
2338 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetBufferDuration_002, TestSize.Level1)
2339 {
2340 int32_t ret = -1;
2341
2342 AudioCapturerOptions capturerOptions;
2343 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_96000;
2344 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
2345 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_U8;
2346 capturerOptions.streamInfo.channels = AudioChannel::MONO;
2347 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
2348 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
2349
2350 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
2351 ASSERT_NE(nullptr, audioCapturer);
2352
2353 ret = audioCapturer->SetBufferDuration(VALUE_NEGATIVE);
2354 EXPECT_NE(SUCCESS, ret);
2355
2356 ret = audioCapturer->SetBufferDuration(VALUE_ZERO);
2357 EXPECT_NE(SUCCESS, ret);
2358
2359 ret = audioCapturer->SetBufferDuration(VALUE_HUNDRED);
2360 EXPECT_NE(SUCCESS, ret);
2361 }
2362
2363 /**
2364 * @tc.name : Test SetCapturerPositionCallback API
2365 * @tc.number: Audio_Capturer_SetCapturerPositionCallback_001
2366 * @tc.desc : Test SetCapturerPositionCallback interface to check set position callback is success for valid callback.
2367 */
2368 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerPositionCallback_001, TestSize.Level1)
2369 {
2370 int32_t ret = -1;
2371
2372 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
2373 ASSERT_NE(nullptr, audioCapturer);
2374
2375 shared_ptr<CapturerPositionCallbackTest> positionCB = std::make_shared<CapturerPositionCallbackTest>();
2376 ret = audioCapturer->SetCapturerPositionCallback(VALUE_THOUSAND, positionCB);
2377 EXPECT_EQ(SUCCESS, ret);
2378 }
2379
2380 /**
2381 * @tc.name : Test SetCapturerPositionCallback API
2382 * @tc.number: Audio_Capturer_SetCapturerPositionCallback_002
2383 * @tc.desc : Test SetCapturerPositionCallback interface again after unregister.
2384 */
2385 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerPositionCallback_002, TestSize.Level1)
2386 {
2387 int32_t ret = -1;
2388
2389 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
2390 ASSERT_NE(nullptr, audioCapturer);
2391
2392 shared_ptr<CapturerPositionCallbackTest> positionCB1 = std::make_shared<CapturerPositionCallbackTest>();
2393 ret = audioCapturer->SetCapturerPositionCallback(VALUE_THOUSAND, positionCB1);
2394 EXPECT_EQ(SUCCESS, ret);
2395
2396 audioCapturer->UnsetCapturerPositionCallback();
2397
2398 shared_ptr<CapturerPositionCallbackTest> positionCB2 = std::make_shared<CapturerPositionCallbackTest>();
2399 ret = audioCapturer->SetCapturerPositionCallback(VALUE_THOUSAND, positionCB2);
2400 EXPECT_EQ(SUCCESS, ret);
2401 }
2402
2403 /**
2404 * @tc.name : Test SetCapturerPositionCallback API
2405 * @tc.number: Audio_Capturer_SetCapturerPositionCallback_003
2406 * @tc.desc : Test SetCapturerPositionCallback interface with null callback.
2407 */
2408 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerPositionCallback_003, TestSize.Level1)
2409 {
2410 int32_t ret = -1;
2411
2412 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
2413 ASSERT_NE(nullptr, audioCapturer);
2414
2415 ret = audioCapturer->SetCapturerPositionCallback(VALUE_THOUSAND, nullptr);
2416 EXPECT_NE(SUCCESS, ret);
2417 }
2418
2419 /**
2420 * @tc.name : Test SetCapturerPositionCallback API
2421 * @tc.number: Audio_Capturer_SetCapturerPositionCallback_004
2422 * @tc.desc : Test SetCapturerPositionCallback interface with invalid parameter.
2423 */
2424 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerPositionCallback_004, TestSize.Level1)
2425 {
2426 int32_t ret = -1;
2427
2428 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
2429 ASSERT_NE(nullptr, audioCapturer);
2430
2431 shared_ptr<CapturerPositionCallbackTest> positionCB = std::make_shared<CapturerPositionCallbackTest>();
2432 ret = audioCapturer->SetCapturerPositionCallback(VALUE_ZERO, positionCB);
2433 EXPECT_NE(SUCCESS, ret);
2434
2435 ret = audioCapturer->SetCapturerPositionCallback(VALUE_NEGATIVE, positionCB);
2436 EXPECT_NE(SUCCESS, ret);
2437 }
2438
2439 /**
2440 * @tc.name : Test SetCapturerPeriodPositionCallback API
2441 * @tc.number: SetCapturerPeriodPositionCallback_001
2442 * @tc.desc : Test SetCapturerPeriodPositionCallback interface to check set period position
2443 * callback is success for valid callback.
2444 */
2445 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerPeriodPositionCallback_001, TestSize.Level1)
2446 {
2447 int32_t ret = -1;
2448
2449 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
2450 ASSERT_NE(nullptr, audioCapturer);
2451
2452 shared_ptr<CapturerPeriodPositionCallbackTest> positionCB = std::make_shared<CapturerPeriodPositionCallbackTest>();
2453 ret = audioCapturer->SetCapturerPeriodPositionCallback(VALUE_THOUSAND, positionCB);
2454 EXPECT_EQ(SUCCESS, ret);
2455 }
2456
2457 /**
2458 * @tc.name : Test SetCapturerPeriodPositionCallback API
2459 * @tc.number: Audio_Capturer_SetCapturerPeriodPositionCallback_002
2460 * @tc.desc : Test SetCapturerPeriodPositionCallback interface again after unregister.
2461 */
2462 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerPeriodPositionCallback_002, TestSize.Level1)
2463 {
2464 int32_t ret = -1;
2465
2466 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
2467 ASSERT_NE(nullptr, audioCapturer);
2468
2469 shared_ptr<CapturerPeriodPositionCallbackTest> positionCB1 = std::make_shared<CapturerPeriodPositionCallbackTest>();
2470 ret = audioCapturer->SetCapturerPeriodPositionCallback(VALUE_THOUSAND, positionCB1);
2471 EXPECT_EQ(SUCCESS, ret);
2472
2473 audioCapturer->UnsetCapturerPeriodPositionCallback();
2474
2475 shared_ptr<CapturerPeriodPositionCallbackTest> positionCB2 = std::make_shared<CapturerPeriodPositionCallbackTest>();
2476 ret = audioCapturer->SetCapturerPeriodPositionCallback(VALUE_THOUSAND, positionCB2);
2477 EXPECT_EQ(SUCCESS, ret);
2478 }
2479
2480 /**
2481 * @tc.name : Test SetCapturerPeriodPositionCallback API
2482 * @tc.number: Audio_Capturer_SetCapturerPeriodPositionCallback_003
2483 * @tc.desc : Test SetCapturerPeriodPositionCallback interface with null callback.
2484 */
2485 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerPeriodPositionCallback_003, TestSize.Level1)
2486 {
2487 int32_t ret = -1;
2488
2489 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
2490 ASSERT_NE(nullptr, audioCapturer);
2491
2492 ret = audioCapturer->SetCapturerPeriodPositionCallback(VALUE_THOUSAND, nullptr);
2493 EXPECT_NE(SUCCESS, ret);
2494 }
2495
2496 /**
2497 * @tc.name : Test SetCapturerPeriodPositionCallback API
2498 * @tc.number: Audio_Capturer_SetCapturerPeriodPositionCallback_004
2499 * @tc.desc : Test SetCapturerPeriodPositionCallback interface with invalid parameter.
2500 */
2501 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerPeriodPositionCallback_004, TestSize.Level1)
2502 {
2503 int32_t ret = -1;
2504
2505 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(STREAM_MUSIC);
2506 ASSERT_NE(nullptr, audioCapturer);
2507
2508 shared_ptr<CapturerPeriodPositionCallbackTest> positionCB = std::make_shared<CapturerPeriodPositionCallbackTest>();
2509 ret = audioCapturer->SetCapturerPeriodPositionCallback(VALUE_ZERO, positionCB);
2510 EXPECT_NE(SUCCESS, ret);
2511
2512 ret = audioCapturer->SetCapturerPeriodPositionCallback(VALUE_NEGATIVE, positionCB);
2513 EXPECT_NE(SUCCESS, ret);
2514 }
2515
2516 /**
2517 * @tc.name : Test SetCapturerCallback with null pointer.
2518 * @tc.number: Audio_Capturer_SetCapturerCallback_001
2519 * @tc.desc : Test SetCapturerCallback interface. Returns error code, if null pointer is set.
2520 */
2521 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerCallback_001, TestSize.Level1)
2522 {
2523 int32_t ret = -1;
2524
2525 AudioCapturerOptions capturerOptions;
2526 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_44100;
2527 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
2528 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_S16LE;
2529 capturerOptions.streamInfo.channels = AudioChannel::MONO;
2530 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
2531 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
2532
2533 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
2534 ASSERT_NE(nullptr, audioCapturer);
2535
2536 ret = audioCapturer->SetCapturerCallback(nullptr);
2537 EXPECT_NE(SUCCESS, ret);
2538 EXPECT_EQ(ERR_INVALID_PARAM, ret);
2539 }
2540
2541 /**
2542 * @tc.name : Test SetCapturerCallback with valid callback pointer.
2543 * @tc.number: Audio_Capturer_SetCapturerCallback_002
2544 * @tc.desc : Test SetCapturerCallback interface. Returns success, if valid callback is set.
2545 */
2546 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerCallback_002, TestSize.Level1)
2547 {
2548 int32_t ret = -1;
2549
2550 AudioCapturerOptions capturerOptions;
2551 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_44100;
2552 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
2553 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_S16LE;
2554 capturerOptions.streamInfo.channels = AudioChannel::MONO;
2555 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
2556 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
2557
2558 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
2559 ASSERT_NE(nullptr, audioCapturer);
2560
2561 shared_ptr<AudioCapturerCallbackTest> audioCapturerCB = std::make_shared<AudioCapturerCallbackTest>();
2562 ret = audioCapturer->SetCapturerCallback(audioCapturerCB);
2563 EXPECT_EQ(SUCCESS, ret);
2564 }
2565
2566 /**
2567 * @tc.name : Test SetCapturerCallback via illegal state, CAPTURER_RELEASED: After RELEASED
2568 * @tc.number: Audio_Capturer_SetCapturerCallback_003
2569 * @tc.desc : Test SetCapturerCallback interface. Returns error, if callback is set in released state.
2570 */
2571 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerCallback_003, TestSize.Level1)
2572 {
2573 int32_t ret = -1;
2574
2575 AudioCapturerOptions capturerOptions;
2576 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_44100;
2577 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
2578 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_S16LE;
2579 capturerOptions.streamInfo.channels = AudioChannel::MONO;
2580 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
2581 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
2582
2583 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
2584 ASSERT_NE(nullptr, audioCapturer);
2585
2586 bool isReleased = audioCapturer->Release();
2587 EXPECT_EQ(true, isReleased);
2588
2589 CapturerState state = audioCapturer->GetStatus();
2590 EXPECT_EQ(CAPTURER_RELEASED, state);
2591
2592 shared_ptr<AudioCapturerCallbackTest> audioCapturerCB = std::make_shared<AudioCapturerCallbackTest>();
2593 ret = audioCapturer->SetCapturerCallback(audioCapturerCB);
2594 EXPECT_NE(SUCCESS, ret);
2595 EXPECT_EQ(ERR_ILLEGAL_STATE, ret);
2596 }
2597
2598 /**
2599 * @tc.name : Test SetCapturerCallback via legal state, CAPTURER_PREPARED: After PREPARED
2600 * @tc.number: Audio_Capturer_SetCapturerCallback_004
2601 * @tc.desc : Test SetCapturerCallback interface. Returns success, if callback is set in proper state.
2602 */
2603 HWTEST(AudioCapturerUnitTest, Audio_Capturer_SetCapturerCallback_004, TestSize.Level1)
2604 {
2605 int32_t ret = -1;
2606
2607 AudioCapturerOptions capturerOptions;
2608 capturerOptions.streamInfo.samplingRate = AudioSamplingRate::SAMPLE_RATE_44100;
2609 capturerOptions.streamInfo.encoding = AudioEncodingType::ENCODING_PCM;
2610 capturerOptions.streamInfo.format = AudioSampleFormat::SAMPLE_S16LE;
2611 capturerOptions.streamInfo.channels = AudioChannel::MONO;
2612 capturerOptions.capturerInfo.sourceType = SourceType::SOURCE_TYPE_MIC;
2613 capturerOptions.capturerInfo.capturerFlags = CAPTURER_FLAG;
2614
2615 unique_ptr<AudioCapturer> audioCapturer = AudioCapturer::Create(capturerOptions);
2616 ASSERT_NE(nullptr, audioCapturer);
2617
2618 CapturerState state = audioCapturer->GetStatus();
2619 EXPECT_EQ(CAPTURER_PREPARED, state);
2620
2621 shared_ptr<AudioCapturerCallbackTest> audioCapturerCB = std::make_shared<AudioCapturerCallbackTest>();
2622 ret = audioCapturer->SetCapturerCallback(audioCapturerCB);
2623 EXPECT_EQ(SUCCESS, ret);
2624 }
2625 } // namespace AudioStandard
2626 } // namespace OHOS
2627