From 94b4c9e247d1acb0dae7ba93acf9b145a2664453 Mon Sep 17 00:00:00 2001 From: Sulav Mulmi Date: Wed, 14 Jul 2021 10:18:19 +0530 Subject: [PATCH 1/2] Added gtest cases for audio renderer Signed-off-by: Sulav Mulmi --- frameworks/innerkitsimpl/test/BUILD.gn | 20 + .../test/moduletest/renderer_test/BUILD.gn | 40 ++ .../include/audio_renderer_module_test.h | 28 + .../src/audio_renderer_module_test.cpp | 540 ++++++++++++++++++ ohos.build | 3 + 5 files changed, 631 insertions(+) create mode 100644 frameworks/innerkitsimpl/test/BUILD.gn create mode 100644 frameworks/innerkitsimpl/test/moduletest/renderer_test/BUILD.gn create mode 100644 frameworks/innerkitsimpl/test/moduletest/renderer_test/include/audio_renderer_module_test.h create mode 100644 frameworks/innerkitsimpl/test/moduletest/renderer_test/src/audio_renderer_module_test.cpp diff --git a/frameworks/innerkitsimpl/test/BUILD.gn b/frameworks/innerkitsimpl/test/BUILD.gn new file mode 100644 index 0000000000..c27443d820 --- /dev/null +++ b/frameworks/innerkitsimpl/test/BUILD.gn @@ -0,0 +1,20 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +group("audio_test") { + testonly = true + + deps = [ + "moduletest/renderer_test:moduletest" + ] +} \ No newline at end of file diff --git a/frameworks/innerkitsimpl/test/moduletest/renderer_test/BUILD.gn b/frameworks/innerkitsimpl/test/moduletest/renderer_test/BUILD.gn new file mode 100644 index 0000000000..e4a05f69f5 --- /dev/null +++ b/frameworks/innerkitsimpl/test/moduletest/renderer_test/BUILD.gn @@ -0,0 +1,40 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") + +module_output_path = "audio_standard/audio_renderer" + +group("moduletest") { + testonly = true + + deps = [ + ":audio_renderer_module_test", + ] +} + +ohos_moduletest("audio_renderer_module_test") { + module_out_path = module_output_path + include_dirs = [ + "./include", + "//foundation/multimedia/audio_standard/interfaces/innerkits/native/audiocommon/include", + ] + + sources = [ + "src/audio_renderer_module_test.cpp", + ] + + deps = [ + "//foundation/multimedia/audio_standard/interfaces/innerkits/native/audiorenderer:audio_renderer", + ] +} \ No newline at end of file diff --git a/frameworks/innerkitsimpl/test/moduletest/renderer_test/include/audio_renderer_module_test.h b/frameworks/innerkitsimpl/test/moduletest/renderer_test/include/audio_renderer_module_test.h new file mode 100644 index 0000000000..d1f535a685 --- /dev/null +++ b/frameworks/innerkitsimpl/test/moduletest/renderer_test/include/audio_renderer_module_test.h @@ -0,0 +1,28 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "gtest/gtest.h" + +class AudioRendererModuleTest : public testing::Test { +public: + // SetUpTestCase: Called before all test cases + static void SetUpTestCase(void); + // TearDownTestCase: Called after all test case + static void TearDownTestCase(void); + // SetUp: Called before each test cases + void SetUp(void); + // TearDown: Called after each test cases + void TearDown(void); +}; \ No newline at end of file diff --git a/frameworks/innerkitsimpl/test/moduletest/renderer_test/src/audio_renderer_module_test.cpp b/frameworks/innerkitsimpl/test/moduletest/renderer_test/src/audio_renderer_module_test.cpp new file mode 100644 index 0000000000..1d817190de --- /dev/null +++ b/frameworks/innerkitsimpl/test/moduletest/renderer_test/src/audio_renderer_module_test.cpp @@ -0,0 +1,540 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "audio_renderer_module_test.h" + +#include "audio_renderer.h" + +using namespace std; +using namespace OHOS::AudioStandard; +using namespace testing::ext; + +namespace RendererModuleTestConstants { + const char *AUDIO_TEST_FILE_PATH = "/data/test_44100_2.wav"; + const unsigned int ZERO = 0; + // Writing only 500 KB of data for test + const uint64_t WRITE_SIZE_LIMIT = 500 * 1024; +} + +void AudioRendererModuleTest::SetUpTestCase(void) {} +void AudioRendererModuleTest::TearDownTestCase(void) {} +void AudioRendererModuleTest::SetUp(void) {} +void AudioRendererModuleTest::TearDown(void) {} + +/* + * Feature: Audio render + * Function: Playback + * SubFunction: NA + * FunctionPoints: NA + * EnvConditions: NA + * CaseDescription: Get supported formats + */ +HWTEST(AudioRendererModuleTest, audio_renderer_get_supported_formats_001, TestSize.Level1) +{ + vector supportedFormatList = AudioRenderer::GetSupportedFormats(); + EXPECT_EQ(AUDIO_SUPPORTED_FORMATS.size(), supportedFormatList.size()); +} + +/* + * Feature: Audio render + * Function: Playback + * SubFunction: NA + * FunctionPoints: NA + * EnvConditions: NA + * CaseDescription: Get supported channels + */ +HWTEST(AudioRendererModuleTest, audio_renderer_get_supported_channels_001, TestSize.Level1) +{ + vector supportedChannelList = AudioRenderer::GetSupportedChannels(); + EXPECT_EQ(AUDIO_SUPPORTED_CHANNELS.size(), supportedChannelList.size()); +} + +/* + * Feature: Audio render + * Function: Playback + * SubFunction: NA + * FunctionPoints: NA + * EnvConditions: NA + * CaseDescription: Get supported channels + */ +HWTEST(AudioRendererModuleTest, audio_renderer_get_supported_encoding_types_001, TestSize.Level1) +{ + vector supportedEncodingTypes + = AudioRenderer::GetSupportedEncodingTypes(); + EXPECT_EQ(AUDIO_SUPPORTED_ENCODING_TYPES.size(), supportedEncodingTypes.size()); +} + +/* + * Feature: Audio render + * Function: Playback + * SubFunction: NA + * FunctionPoints: NA + * EnvConditions: NA + * CaseDescription: Get supported sampling rates + */ +HWTEST(AudioRendererModuleTest, audio_renderer_get_supported_sampling_rates_001, TestSize.Level1) +{ + vector supportedSamplingRates = AudioRenderer::GetSupportedSamplingRates(); + EXPECT_EQ(AUDIO_SUPPORTED_SAMPLING_RATES.size(), supportedSamplingRates.size()); +} + +/* + * Feature: Audio render + * Function: Playback + * SubFunction: NA + * FunctionPoints: NA + * EnvConditions: NA + * CaseDescription: Create renderer instance + */ +HWTEST(AudioRendererModuleTest, audio_renderer_create_001, TestSize.Level1) +{ + unique_ptr audioRenderer = AudioRenderer::Create(AudioStreamType::STREAM_MUSIC); + EXPECT_NE(nullptr, audioRenderer); +} + +/* + * Feature: Audio render + * Function: Playback + * SubFunction: NA + * FunctionPoints: NA + * EnvConditions: NA + * CaseDescription: Set renderer parameters + */ +HWTEST(AudioRendererModuleTest, audio_renderer_set_params_001, TestSize.Level1) +{ + int32_t ret = 0; + unique_ptr audioRenderer = AudioRenderer::Create(AudioStreamType::STREAM_MUSIC); + AudioRendererParams rendererParams; + rendererParams.sampleFormat = AudioSampleFormat::SAMPLE_S16LE; + rendererParams.sampleRate = AudioSamplingRate::SAMPLE_RATE_44100; + rendererParams.channelCount = AudioChannel::STEREO; + rendererParams.encodingType = AudioEncodingType::ENCODING_PCM; + + ret = audioRenderer->SetParams(rendererParams); + EXPECT_EQ(0, ret); + audioRenderer->Release(); +} + +/* + * Feature: Audio render + * Function: Playback + * SubFunction: NA + * FunctionPoints: NA + * EnvConditions: NA + * CaseDescription: Get renderer parameters + */ +HWTEST(AudioRendererModuleTest, audio_renderer_get_params_001, TestSize.Level1) +{ + int32_t ret = 0; + unique_ptr audioRenderer = AudioRenderer::Create(AudioStreamType::STREAM_MUSIC); + AudioRendererParams rendererParams; + rendererParams.sampleFormat = AudioSampleFormat::SAMPLE_S16LE; + rendererParams.sampleRate = AudioSamplingRate::SAMPLE_RATE_44100; + rendererParams.channelCount = AudioChannel::STEREO; + rendererParams.encodingType = AudioEncodingType::ENCODING_PCM; + + ret = audioRenderer->SetParams(rendererParams); + EXPECT_EQ(0, ret); + + AudioRendererParams paRendererParams; + ret = audioRenderer->GetParams(paRendererParams); + EXPECT_EQ(0, ret); + EXPECT_EQ(paRendererParams.sampleFormat, rendererParams.sampleFormat); + EXPECT_EQ(paRendererParams.sampleRate, rendererParams.sampleRate); + EXPECT_EQ(paRendererParams.channelCount, rendererParams.channelCount); + EXPECT_EQ(paRendererParams.encodingType, rendererParams.encodingType); + + audioRenderer->Release(); +} + +/* + * Feature: Audio render + * Function: Playback + * SubFunction: NA + * FunctionPoints: NA + * EnvConditions: NA + * CaseDescription: Get buffer size + */ +HWTEST(AudioRendererModuleTest, audio_renderer_get_buffer_size_001, TestSize.Level1) +{ + int32_t ret = 0; + unique_ptr audioRenderer = AudioRenderer::Create(AudioStreamType::STREAM_MUSIC); + AudioRendererParams rendererParams; + rendererParams.sampleFormat = AudioSampleFormat::SAMPLE_S16LE; + rendererParams.sampleRate = AudioSamplingRate::SAMPLE_RATE_44100; + rendererParams.channelCount = AudioChannel::STEREO; + rendererParams.encodingType = AudioEncodingType::ENCODING_PCM; + + ret = audioRenderer->SetParams(rendererParams); + EXPECT_EQ(0, ret); + size_t bufferLen; + ret = audioRenderer->GetBufferSize(bufferLen); + EXPECT_EQ(0, ret); + + audioRenderer->Release(); +} + +/* + * Feature: Audio render + * Function: Playback + * SubFunction: NA + * FunctionPoints: NA + * EnvConditions: NA + * CaseDescription: Get frame count + */ +HWTEST(AudioRendererModuleTest, audio_renderer_get_frame_count_001, TestSize.Level1) +{ + int32_t ret = 0; + unique_ptr audioRenderer = AudioRenderer::Create(AudioStreamType::STREAM_MUSIC); + AudioRendererParams rendererParams; + rendererParams.sampleFormat = AudioSampleFormat::SAMPLE_S16LE; + rendererParams.sampleRate = AudioSamplingRate::SAMPLE_RATE_44100; + rendererParams.channelCount = AudioChannel::STEREO; + rendererParams.encodingType = AudioEncodingType::ENCODING_PCM; + + ret = audioRenderer->SetParams(rendererParams); + EXPECT_EQ(0, ret); + uint32_t frameCount; + ret = audioRenderer->GetFrameCount(frameCount); + EXPECT_EQ(0, ret); + + audioRenderer->Release(); +} + +/* + * Feature: Audio render + * Function: Playback + * SubFunction: NA + * FunctionPoints: NA + * EnvConditions: NA + * CaseDescription: Start audio renderer + */ +HWTEST(AudioRendererModuleTest, audio_renderer_start_001, TestSize.Level1) +{ + int32_t ret = 0; + unique_ptr audioRenderer = AudioRenderer::Create(AudioStreamType::STREAM_MUSIC); + AudioRendererParams rendererParams; + rendererParams.sampleFormat = AudioSampleFormat::SAMPLE_S16LE; + rendererParams.sampleRate = AudioSamplingRate::SAMPLE_RATE_44100; + rendererParams.channelCount = AudioChannel::STEREO; + rendererParams.encodingType = AudioEncodingType::ENCODING_PCM; + + ret = audioRenderer->SetParams(rendererParams); + EXPECT_EQ(0, ret); + bool isStarted = audioRenderer->Start(); + EXPECT_EQ(true, isStarted); + + audioRenderer->Release(); +} + +/* + * Feature: Audio render + * Function: Playback + * SubFunction: NA + * FunctionPoints: NA + * EnvConditions: NA + * CaseDescription: write audio renderer + */ +HWTEST(AudioRendererModuleTest, audio_renderer_write_001, TestSize.Level1) +{ + int32_t ret = 0; + + FILE *wavFile = fopen(RendererModuleTestConstants::AUDIO_TEST_FILE_PATH, "rb"); + EXPECT_NE(nullptr, wavFile); + + unique_ptr audioRenderer = AudioRenderer::Create(AudioStreamType::STREAM_MUSIC); + AudioRendererParams rendererParams; + rendererParams.sampleFormat = AudioSampleFormat::SAMPLE_S16LE; + rendererParams.sampleRate = AudioSamplingRate::SAMPLE_RATE_44100; + rendererParams.channelCount = AudioChannel::STEREO; + rendererParams.encodingType = AudioEncodingType::ENCODING_PCM; + + ret = audioRenderer->SetParams(rendererParams); + EXPECT_EQ(0, ret); + bool isStarted = audioRenderer->Start(); + EXPECT_EQ(true, isStarted); + size_t bufferLen; + ret = audioRenderer->GetBufferSize(bufferLen); + EXPECT_EQ(0, ret); + + uint8_t *buffer = nullptr; + int32_t n = 2; + buffer = (uint8_t *) malloc(n * bufferLen); + size_t bytesToWrite = 0; + size_t bytesWritten = 0; + size_t minBytes = 4; + size_t tempBytesWritten = 0; + size_t totalBytesWritten = 0; + while (!feof(wavFile)) { + bytesToWrite = fread(buffer, 1, bufferLen, wavFile); + bytesWritten = 0; + while ((bytesWritten < bytesToWrite) && ((bytesToWrite - bytesWritten) > minBytes)) { + tempBytesWritten = audioRenderer->Write(buffer + bytesWritten, + bytesToWrite - bytesWritten); + EXPECT_GE(tempBytesWritten, RendererModuleTestConstants::ZERO); + bytesWritten += tempBytesWritten; + } + totalBytesWritten += bytesWritten; + if (totalBytesWritten >= RendererModuleTestConstants::WRITE_SIZE_LIMIT) { + break; + } + } + + audioRenderer->Drain(); + audioRenderer->Release(); + + free(buffer); + fclose(wavFile); +} + +/* + * Feature: Audio render + * Function: Playback + * SubFunction: NA + * FunctionPoints: NA + * EnvConditions: NA + * CaseDescription: Get audio time + */ +HWTEST(AudioRendererModuleTest, audio_renderer_get_audio_time_001, TestSize.Level1) +{ + int32_t ret = 0; + + FILE *wavFile = fopen(RendererModuleTestConstants::AUDIO_TEST_FILE_PATH, "rb"); + EXPECT_NE(nullptr, wavFile); + + unique_ptr audioRenderer = AudioRenderer::Create(AudioStreamType::STREAM_MUSIC); + AudioRendererParams rendererParams; + rendererParams.sampleFormat = AudioSampleFormat::SAMPLE_S16LE; + rendererParams.sampleRate = AudioSamplingRate::SAMPLE_RATE_44100; + rendererParams.channelCount = AudioChannel::STEREO; + rendererParams.encodingType = AudioEncodingType::ENCODING_PCM; + + ret = audioRenderer->SetParams(rendererParams); + EXPECT_EQ(0, ret); + bool isStarted = audioRenderer->Start(); + EXPECT_EQ(true, isStarted); + size_t bufferLen; + ret = audioRenderer->GetBufferSize(bufferLen); + EXPECT_EQ(0, ret); + + uint8_t *buffer = nullptr; + int32_t n = 2; + buffer = (uint8_t *) malloc(n * bufferLen); + size_t bytesToWrite = 0; + size_t bytesWritten = 0; + size_t minBytes = 4; + size_t tempBytesWritten = 0; + size_t totalBytesWritten = 0; + while (!feof(wavFile)) { + bytesToWrite = fread(buffer, 1, bufferLen, wavFile); + bytesWritten = 0; + while ((bytesWritten < bytesToWrite) && ((bytesToWrite - bytesWritten) > minBytes)) { + tempBytesWritten = audioRenderer->Write(buffer + bytesWritten, + bytesToWrite - bytesWritten); + EXPECT_GE(tempBytesWritten, RendererModuleTestConstants::ZERO); + bytesWritten += tempBytesWritten; + } + totalBytesWritten += bytesWritten; + if (totalBytesWritten >= RendererModuleTestConstants::WRITE_SIZE_LIMIT) { + break; + } + } + + audioRenderer->Drain(); + + Timestamp timeStamp; + bool getAudioTime = audioRenderer->GetAudioTime(timeStamp, Timestamp::Timestampbase::MONOTONIC); + EXPECT_EQ(true, getAudioTime); + EXPECT_GE(timeStamp.time.tv_sec, (const long)RendererModuleTestConstants::ZERO); + EXPECT_GE(timeStamp.time.tv_nsec, (const long)RendererModuleTestConstants::ZERO); + + audioRenderer->Release(); + + free(buffer); + fclose(wavFile); +} + +/* + * Feature: Audio render + * Function: Playback + * SubFunction: NA + * FunctionPoints: NA + * EnvConditions: NA + * CaseDescription: Get latency + */ +HWTEST(AudioRendererModuleTest, audio_renderer_get_latency_001, TestSize.Level1) +{ + int32_t ret = 0; + + FILE *wavFile = fopen(RendererModuleTestConstants::AUDIO_TEST_FILE_PATH, "rb"); + EXPECT_NE(nullptr, wavFile); + + unique_ptr audioRenderer = AudioRenderer::Create(AudioStreamType::STREAM_MUSIC); + AudioRendererParams rendererParams; + rendererParams.sampleFormat = AudioSampleFormat::SAMPLE_S16LE; + rendererParams.sampleRate = AudioSamplingRate::SAMPLE_RATE_44100; + rendererParams.channelCount = AudioChannel::STEREO; + rendererParams.encodingType = AudioEncodingType::ENCODING_PCM; + + ret = audioRenderer->SetParams(rendererParams); + EXPECT_EQ(0, ret); + bool isStarted = audioRenderer->Start(); + EXPECT_EQ(true, isStarted); + size_t bufferLen; + ret = audioRenderer->GetBufferSize(bufferLen); + EXPECT_EQ(0, ret); + + uint8_t *buffer = nullptr; + int32_t n = 2; + buffer = (uint8_t *) malloc(n * bufferLen); + size_t bytesToWrite = 0; + size_t bytesWritten = 0; + size_t minBytes = 4; + size_t tempBytesWritten = 0; + size_t totalBytesWritten = 0; + while (!feof(wavFile)) { + bytesToWrite = fread(buffer, 1, bufferLen, wavFile); + bytesWritten = 0; + uint64_t latency; + ret = audioRenderer->GetLatency(latency); + EXPECT_EQ(0, ret); + while ((bytesWritten < bytesToWrite) && ((bytesToWrite - bytesWritten) > minBytes)) { + tempBytesWritten = audioRenderer->Write(buffer + bytesWritten, + bytesToWrite - bytesWritten); + EXPECT_GE(tempBytesWritten, RendererModuleTestConstants::ZERO); + bytesWritten += tempBytesWritten; + } + totalBytesWritten += bytesWritten; + if (totalBytesWritten >= RendererModuleTestConstants::WRITE_SIZE_LIMIT) { + break; + } + } + + audioRenderer->Drain(); + audioRenderer->Release(); + + free(buffer); + fclose(wavFile); +} + + +/* + * Feature: Audio render + * Function: Playback + * SubFunction: NA + * FunctionPoints: NA + * EnvConditions: NA + * CaseDescription: Stop playback + */ +HWTEST(AudioRendererModuleTest, audio_renderer_stop_001, TestSize.Level1) +{ + int32_t ret = 0; + + FILE *wavFile = fopen(RendererModuleTestConstants::AUDIO_TEST_FILE_PATH, "rb"); + EXPECT_NE(nullptr, wavFile); + + unique_ptr audioRenderer = AudioRenderer::Create(AudioStreamType::STREAM_MUSIC); + AudioRendererParams rendererParams; + rendererParams.sampleFormat = AudioSampleFormat::SAMPLE_S16LE; + rendererParams.sampleRate = AudioSamplingRate::SAMPLE_RATE_44100; + rendererParams.channelCount = AudioChannel::STEREO; + rendererParams.encodingType = AudioEncodingType::ENCODING_PCM; + + ret = audioRenderer->SetParams(rendererParams); + EXPECT_EQ(0, ret); + bool isStarted = audioRenderer->Start(); + EXPECT_EQ(true, isStarted); + size_t bufferLen; + ret = audioRenderer->GetBufferSize(bufferLen); + EXPECT_EQ(0, ret); + + uint8_t *buffer = nullptr; + int32_t n = 2; + buffer = (uint8_t *) malloc(n * bufferLen); + size_t bytesToWrite = 0; + size_t bytesWritten = 0; + size_t minBytes = 4; + size_t tempBytesWritten = 0; + size_t totalBytesWritten = 0; + while (!feof(wavFile)) { + bytesToWrite = fread(buffer, 1, bufferLen, wavFile); + bytesWritten = 0; + while ((bytesWritten < bytesToWrite) && ((bytesToWrite - bytesWritten) > minBytes)) { + tempBytesWritten = audioRenderer->Write(buffer + bytesWritten, + bytesToWrite - bytesWritten); + EXPECT_GE(tempBytesWritten, RendererModuleTestConstants::ZERO); + bytesWritten += tempBytesWritten; + } + totalBytesWritten += bytesWritten; + if (totalBytesWritten >= RendererModuleTestConstants::WRITE_SIZE_LIMIT) { + break; + } + } + + bool isStopped = audioRenderer->Stop(); + EXPECT_EQ(true, isStopped); + + audioRenderer->Drain(); + audioRenderer->Release(); + + free(buffer); + fclose(wavFile); +} + +/* + * Feature: Audio render + * Function: Playback + * SubFunction: NA + * FunctionPoints: NA + * EnvConditions: NA + * CaseDescription: Check states + */ +HWTEST(AudioRendererModuleTest, audio_renderer_check_status_001, TestSize.Level1) +{ + int32_t ret = 0; + + FILE *wavFile = fopen(RendererModuleTestConstants::AUDIO_TEST_FILE_PATH, "rb"); + EXPECT_NE(nullptr, wavFile); + + unique_ptr audioRenderer = AudioRenderer::Create(AudioStreamType::STREAM_MUSIC); + RendererState state = audioRenderer->GetStatus(); + EXPECT_EQ(RendererState::RENDERER_NEW, state); + + AudioRendererParams rendererParams; + rendererParams.sampleFormat = AudioSampleFormat::SAMPLE_S16LE; + rendererParams.sampleRate = AudioSamplingRate::SAMPLE_RATE_44100; + rendererParams.channelCount = AudioChannel::STEREO; + rendererParams.encodingType = AudioEncodingType::ENCODING_PCM; + + ret = audioRenderer->SetParams(rendererParams); + EXPECT_EQ(0, ret); + state = audioRenderer->GetStatus(); + EXPECT_EQ(RendererState::RENDERER_PREPARED, state); + + bool isStarted = audioRenderer->Start(); + EXPECT_EQ(true, isStarted); + state = audioRenderer->GetStatus(); + EXPECT_EQ(RendererState::RENDERER_RUNNING, state); + + bool isStopped = audioRenderer->Stop(); + EXPECT_EQ(true, isStopped); + state = audioRenderer->GetStatus(); + EXPECT_EQ(RendererState::RENDERER_STOPPED, state); + + bool isReleased = audioRenderer->Release(); + EXPECT_EQ(true, isReleased); + state = audioRenderer->GetStatus(); + EXPECT_EQ(RendererState::RENDERER_RELEASED, state); +} \ No newline at end of file diff --git a/ohos.build b/ohos.build index f84d36fe1f..92ca3e932e 100644 --- a/ohos.build +++ b/ohos.build @@ -86,6 +86,9 @@ "header_base": "//foundation/multimedia/audio_standard/interfaces/kits/js/audio_manager/include" } } + ], + "test_list": [ + "//foundation/multimedia/audio_standard/frameworks/innerkitsimpl/test:audio_test" ] } } -- Gitee From 65e09d1867bfc29d097bcd3eafbbc2ce90b81532 Mon Sep 17 00:00:00 2001 From: Vaidegi B Date: Tue, 20 Jul 2021 12:09:59 +0530 Subject: [PATCH 2/2] gtest app for audio recorder Signed-off-by: Vaidegi B --- frameworks/innerkitsimpl/test/BUILD.gn | 5 +- .../test/moduletest/recorder_test/BUILD.gn | 32 + .../include/audio_recorder_module_test.h | 35 ++ .../src/audio_recorder_module_test.cpp | 568 ++++++++++++++++++ 4 files changed, 638 insertions(+), 2 deletions(-) create mode 100644 frameworks/innerkitsimpl/test/moduletest/recorder_test/BUILD.gn create mode 100644 frameworks/innerkitsimpl/test/moduletest/recorder_test/include/audio_recorder_module_test.h create mode 100644 frameworks/innerkitsimpl/test/moduletest/recorder_test/src/audio_recorder_module_test.cpp diff --git a/frameworks/innerkitsimpl/test/BUILD.gn b/frameworks/innerkitsimpl/test/BUILD.gn index c27443d820..4743d8a688 100644 --- a/frameworks/innerkitsimpl/test/BUILD.gn +++ b/frameworks/innerkitsimpl/test/BUILD.gn @@ -15,6 +15,7 @@ group("audio_test") { testonly = true deps = [ - "moduletest/renderer_test:moduletest" + "moduletest/renderer_test:moduletest", + "moduletest/recorder_test:audio_recorder_module_test" ] -} \ No newline at end of file +} diff --git a/frameworks/innerkitsimpl/test/moduletest/recorder_test/BUILD.gn b/frameworks/innerkitsimpl/test/moduletest/recorder_test/BUILD.gn new file mode 100644 index 0000000000..2eb2888b5a --- /dev/null +++ b/frameworks/innerkitsimpl/test/moduletest/recorder_test/BUILD.gn @@ -0,0 +1,32 @@ +# Copyright (C) 2021 Huawei Device Co., Ltd. +# Licensed under the Apache License, Version 2.0 (the "License"); +# you may not use this file except in compliance with the License. +# You may obtain a copy of the License at +# +# http://www.apache.org/licenses/LICENSE-2.0 +# +# Unless required by applicable law or agreed to in writing, software +# distributed under the License is distributed on an "AS IS" BASIS, +# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +# See the License for the specific language governing permissions and +# limitations under the License. + +import("//build/test.gni") + +module_output_path = "audio_standard/audio_recorder" + +ohos_moduletest("audio_recorder_module_test") { + module_out_path = module_output_path + include_dirs = [ + "./include", + "//foundation/multimedia/audio_standard/interfaces/innerkits/native/audiocommon/include", + ] + + sources = [ + "src/audio_recorder_module_test.cpp", + ] + + deps = [ + "//foundation/multimedia/audio_standard/interfaces/innerkits/native/audiorecorder:audio_recorder", + ] +} diff --git a/frameworks/innerkitsimpl/test/moduletest/recorder_test/include/audio_recorder_module_test.h b/frameworks/innerkitsimpl/test/moduletest/recorder_test/include/audio_recorder_module_test.h new file mode 100644 index 0000000000..a5a11c8d90 --- /dev/null +++ b/frameworks/innerkitsimpl/test/moduletest/recorder_test/include/audio_recorder_module_test.h @@ -0,0 +1,35 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "audio_recorder.h" +#include "gtest/gtest.h" + +namespace OHOS { +namespace AudioStandard { +class AudioRecorderModuleTest : public testing::Test { +public: + // SetUpTestCase: Called before all test cases + static void SetUpTestCase(void); + // TearDownTestCase: Called after all test case + static void TearDownTestCase(void); + // SetUp: Called before each test cases + void SetUp(void); + // TearDown: Called after each test cases + void TearDown(void); + // Init Recorder + static int32_t InitializeRecorder(std::unique_ptr &audioRecorder); +}; +} // namespace AudioStandard +} // namespace OHOS diff --git a/frameworks/innerkitsimpl/test/moduletest/recorder_test/src/audio_recorder_module_test.cpp b/frameworks/innerkitsimpl/test/moduletest/recorder_test/src/audio_recorder_module_test.cpp new file mode 100644 index 0000000000..bc6446867d --- /dev/null +++ b/frameworks/innerkitsimpl/test/moduletest/recorder_test/src/audio_recorder_module_test.cpp @@ -0,0 +1,568 @@ +/* + * Copyright (C) 2021 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "audio_recorder_module_test.h" + + +#include "audio_info.h" +#include "audio_recorder.h" + +using namespace std; +using namespace testing::ext; + +namespace OHOS { +namespace AudioStandard { +namespace { + const string AUDIO_RECORD_FILE1 = "/data/audioreordtest_blocking.pcm"; + const string AUDIO_RECORD_FILE2 = "/data/audioreordtest_nonblocking.pcm"; + const string AUDIO_RECORD_FILE3 = "/data/audioreordtest_out.pcm"; + const int32_t READ_BUFFERS_COUNT = 128; + const int32_t VALUE_ZERO = 0; + const int32_t SUCCESS = 0; +} // namespace + +void AudioRecorderModuleTest::SetUpTestCase(void) {} +void AudioRecorderModuleTest::TearDownTestCase(void) {} +void AudioRecorderModuleTest::SetUp(void) {} +void AudioRecorderModuleTest::TearDown(void) {} + +int32_t AudioRecorderModuleTest::InitializeRecorder(unique_ptr &audioRecorder) +{ + AudioRecorderParams recorderParams; + recorderParams.audioSampleFormat = SAMPLE_S16LE; + recorderParams.samplingRate = SAMPLE_RATE_44100; + recorderParams.audioChannel = STEREO; + recorderParams.audioEncoding = ENCODING_PCM; + + return audioRecorder->SetParams(recorderParams); +} + +/** +* @tc.name : Test GetSupportedFormats API +* @tc.number: Auido_Recorder_GetSupportedFormats_001 +* @tc.desc : test GetSupportedFormats interface, Returns supported Formats on success. +*/ +HWTEST(AudioRecorderModuleTest, Auido_Recorder_GetSupportedFormats_001, TestSize.Level1) +{ + vector supportedFormatList = AudioRecorder::GetSupportedFormats(); + EXPECT_EQ(AUDIO_SUPPORTED_FORMATS.size(), supportedFormatList.size()); +} + +/** +* @tc.name : Test GetSupportedChannels API +* @tc.number: Auido_Recorder_GetSupportedChannels_001 +* @tc.desc : test GetSupportedChannels interface, Returns supported Channels on success. +*/ +HWTEST(AudioRecorderModuleTest, Auido_Recorder_GetSupportedChannels_001, TestSize.Level1) +{ + vector supportedChannelList = AudioRecorder::GetSupportedChannels(); + EXPECT_EQ(AUDIO_SUPPORTED_CHANNELS.size(), supportedChannelList.size()); +} + +/** +* @tc.name : Test GetSupportedEncodingTypes API +* @tc.number: Auido_Recorder_GetSupportedEncodingTypes_001 +* @tc.desc : test GetSupportedEncodingTypes interface, Returns supported Encoding types on success. +*/ +HWTEST(AudioRecorderModuleTest, Auido_Recorder_GetSupportedEncodingTypes_001, TestSize.Level1) +{ + vector supportedEncodingTypes + = AudioRecorder::GetSupportedEncodingTypes(); + EXPECT_EQ(AUDIO_SUPPORTED_ENCODING_TYPES.size(), supportedEncodingTypes.size()); +} + +/** +* @tc.name : Test GetSupportedSamplingRates API +* @tc.number: Auido_Recorder_GetSupportedSamplingRates_001 +* @tc.desc : test GetSupportedSamplingRates interface, Returns supported Sampling rates on success. +*/ +HWTEST(AudioRecoderModuleTest, Auido_Recorder_GetSupportedSamplingRates_001, TestSize.Level1) +{ + vector supportedSamplingRates = AudioRecorder::GetSupportedSamplingRates(); + EXPECT_EQ(AUDIO_SUPPORTED_SAMPLING_RATES.size(), supportedSamplingRates.size()); +} + +/** +* @tc.name : Test Create API via legal input. +* @tc.number: Auido_Recorder_Create_001 +* @tc.desc : test Create interface, Returns audioRecorder instance if create is Successful. +*/ +HWTEST(AudioRecorderModuleTest, Auido_Recorder_Create_001, TestSize.Level1) +{ + unique_ptr audioRecorder = AudioRecorder::Create(STREAM_MUSIC); + EXPECT_NE(nullptr, audioRecorder); +} + +/** +* @tc.name : Test SetParams API via legal input. +* @tc.number: Auido_Recorder_SetParams_001 +* @tc.desc : test SetParams interface, Returns 0 {SUCCESS} if the setting is successful. +*/ +HWTEST(AudioRecorderModuleTest, Auido_Recorder_SetParams_001, TestSize.Level1) +{ + unique_ptr audioRecorder = AudioRecorder::Create(STREAM_MUSIC); + ASSERT_NE(nullptr, audioRecorder); + + AudioRecorderParams recorderParams; + recorderParams.audioSampleFormat = SAMPLE_S16LE; + recorderParams.samplingRate = SAMPLE_RATE_44100; + recorderParams.audioChannel = STEREO; + recorderParams.audioEncoding = ENCODING_PCM; + + int32_t ret = audioRecorder->SetParams(recorderParams); + EXPECT_EQ(SUCCESS, ret); + audioRecorder->Release(); +} + +/** +* @tc.name : Test GetParams API via legal input. +* @tc.number: Auido_Recorder_GetParams_001 +* @tc.desc : test GetParams interface, Returns 0 {SUCCESS} if the getting is successful. +*/ +HWTEST(AudioRecorderModuleTest, Auido_Recorder_GetParams_001, TestSize.Level1) +{ + int32_t ret = -1; + unique_ptr audioRecorder = AudioRecorder::Create(STREAM_MUSIC); + ASSERT_NE(nullptr, audioRecorder); + + AudioRecorderParams recorderParams; + recorderParams.audioSampleFormat = SAMPLE_S16LE; + recorderParams.samplingRate = SAMPLE_RATE_44100; + recorderParams.audioChannel = STEREO; + recorderParams.audioEncoding = ENCODING_PCM; + ret = audioRecorder->SetParams(recorderParams); + EXPECT_EQ(SUCCESS, ret); + + AudioRecorderParams getRecorderParams; + ret = audioRecorder->GetParams(getRecorderParams); + EXPECT_EQ(SUCCESS, ret); + EXPECT_EQ(recorderParams.audioSampleFormat, getRecorderParams.audioSampleFormat); + EXPECT_EQ(recorderParams.samplingRate, getRecorderParams.samplingRate); + EXPECT_EQ(recorderParams.audioChannel, getRecorderParams.audioChannel); + EXPECT_EQ(recorderParams.audioEncoding, getRecorderParams.audioEncoding); + + audioRecorder->Release(); +} + +/** +* @tc.name : Test GetBufferSize API via legal input. +* @tc.number: Auido_Recorder_GetBufferSize_001 +* @tc.desc : test GetBufferSize interface, Returns 0 {SUCCESS} if the getting is successful. +*/ +HWTEST(AudioRecorderModuleTest, Auido_Recorder_GetBufferSize_001, TestSize.Level1) +{ + int32_t ret = -1; + unique_ptr audioRecorder = AudioRecorder::Create(STREAM_MUSIC); + EXPECT_NE(nullptr, audioRecorder); + + ret = AudioRecorderModuleTest::InitializeRecorder(audioRecorder); + EXPECT_EQ(SUCCESS, ret); + size_t bufferLen; + ret = audioRecorder->GetBufferSize(bufferLen); + EXPECT_EQ(SUCCESS, ret); + + audioRecorder->Release(); +} + +/** +* @tc.name : Test GetFrameCount API via legal input. +* @tc.number: Auido_Recorder_GetFrameCount_001 +* @tc.desc : test GetFrameCount interface, Returns 0 {SUCCESS} if the getting is successful. +*/ +HWTEST(AudioRecorderModuleTest, Auido_Recorder_GetFrameCount_001, TestSize.Level1) +{ + int32_t ret = -1; + unique_ptr audioRecorder = AudioRecorder::Create(STREAM_MUSIC); + EXPECT_NE(nullptr, audioRecorder); + + ret = AudioRecorderModuleTest::InitializeRecorder(audioRecorder); + EXPECT_EQ(SUCCESS, ret); + + uint32_t frameCount; + ret = audioRecorder->GetFrameCount(frameCount); + EXPECT_EQ(SUCCESS, ret); + + audioRecorder->Release(); +} + +/** +* @tc.name : Test Start API. +* @tc.number: Audio_Recorder_Start_001 +* @tc.desc : test Start interface, Returns true if start is successful. +*/ +HWTEST(AudioRecorderModuleTest, Audio_Recorder_Start_001, TestSize.Level1) +{ + int32_t ret = -1; + unique_ptr audioRecorder = AudioRecorder::Create(STREAM_MUSIC); + EXPECT_NE(nullptr, audioRecorder); + + ret = AudioRecorderModuleTest::InitializeRecorder(audioRecorder); + EXPECT_EQ(SUCCESS, ret); + + bool isStarted = audioRecorder->Start(); + EXPECT_EQ(true, isStarted); + + audioRecorder->Release(); +} + +/** +* @tc.name : Test Read API via isBlockingRead = true. +* @tc.number: Audio_Recorder_Read_001 +* @tc.desc : test GetFrameCount interface, Returns number of bytes read if the read is successful. +*/ +HWTEST(AudioRecorderModuleTest, Audio_Recorder_Read_001, TestSize.Level1) +{ + int32_t ret = -1; + bool isBlockingRead = true; + unique_ptr audioRecorder = AudioRecorder::Create(STREAM_MUSIC); + EXPECT_NE(nullptr, audioRecorder); + + ret = AudioRecorderModuleTest::InitializeRecorder(audioRecorder); + EXPECT_EQ(SUCCESS, ret); + + bool isStarted = audioRecorder->Start(); + EXPECT_EQ(true, isStarted); + + size_t bufferLen; + ret = audioRecorder->GetBufferSize(bufferLen); + EXPECT_EQ(SUCCESS, ret); + + uint8_t* buffer = (uint8_t *) malloc(bufferLen); + ASSERT_NE(nullptr, buffer); + FILE *recFile = fopen(AUDIO_RECORD_FILE1.c_str(), "wb"); + ASSERT_NE(nullptr, recFile); + + size_t size = 1; + int32_t bytesRead = 0; + int32_t numBuffersToRecord = READ_BUFFERS_COUNT; + + while (numBuffersToRecord) { + bytesRead = audioRecorder->Read(*buffer, bufferLen, isBlockingRead); + if (bytesRead < 0) { + break; + } else if (bytesRead > 0) { + fwrite(buffer, size, bytesRead, recFile); + numBuffersToRecord--; + } + } + + audioRecorder->Flush(); + audioRecorder->Stop(); + audioRecorder->Release(); + + free(buffer); + fclose(recFile); +} + +/** +* @tc.name : Test Read API via isBlockingRead = false. +* @tc.number: Audio_Recorder_Read_002 +* @tc.desc : test GetFrameCount interface, Returns number of bytes read if the read is successful. +*/ +HWTEST(AudioRecorderModuleTest, Audio_Recorder_Read_002, TestSize.Level1) +{ + int32_t ret = -1; + bool isBlockingRead = false; + unique_ptr audioRecorder = AudioRecorder::Create(STREAM_MUSIC); + EXPECT_NE(nullptr, audioRecorder); + + ret = AudioRecorderModuleTest::InitializeRecorder(audioRecorder); + EXPECT_EQ(SUCCESS, ret); + + bool isStarted = audioRecorder->Start(); + EXPECT_EQ(true, isStarted); + + size_t bufferLen; + ret = audioRecorder->GetBufferSize(bufferLen); + EXPECT_EQ(SUCCESS, ret); + + uint8_t* buffer = (uint8_t *) malloc(bufferLen); + ASSERT_NE(nullptr, buffer); + FILE *recFile = fopen(AUDIO_RECORD_FILE2.c_str(), "wb"); + ASSERT_NE(nullptr, recFile); + + size_t size = 1; + int32_t bytesRead = 0; + int32_t numBuffersToRecord = READ_BUFFERS_COUNT; + + while (numBuffersToRecord) { + bytesRead = audioRecorder->Read(*buffer, bufferLen, isBlockingRead); + if (bytesRead < 0) { + break; + } else if (bytesRead > 0) { + fwrite(buffer, size, bytesRead, recFile); + numBuffersToRecord--; + } + } + + audioRecorder->Flush(); + audioRecorder->Stop(); + audioRecorder->Release(); + + free(buffer); + fclose(recFile); +} + +/** +* @tc.name : Test GetAudioTime API via legal input. +* @tc.number: Auido_Recorder_GetAudioTime_001 +* @tc.desc : test GetAudioTime interface, Returns true if the getting is successful. +*/ +HWTEST(AudioRecorderModuleTest, Auido_Recorder_GetAudioTime_001, TestSize.Level1) +{ + int32_t ret = -1; + bool isBlockingRead = true; + unique_ptr audioRecorder = AudioRecorder::Create(STREAM_MUSIC); + EXPECT_NE(nullptr, audioRecorder); + + ret = AudioRecorderModuleTest::InitializeRecorder(audioRecorder); + EXPECT_EQ(SUCCESS, ret); + + bool isStarted = audioRecorder->Start(); + EXPECT_EQ(true, isStarted); + + size_t bufferLen; + ret = audioRecorder->GetBufferSize(bufferLen); + EXPECT_EQ(SUCCESS, ret); + + uint8_t* buffer = (uint8_t *) malloc(bufferLen); + ASSERT_NE(nullptr, buffer); + FILE *recFile = fopen(AUDIO_RECORD_FILE3.c_str(), "wb"); + ASSERT_NE(nullptr, recFile); + + size_t size = 1; + int32_t bytesRead = 0; + int32_t numBuffersToRecord = 5; + + while (numBuffersToRecord) { + bytesRead = audioRecorder->Read(*buffer, bufferLen, isBlockingRead); + if (bytesRead < 0) { + break; + } else if (bytesRead > 0) { + fwrite(buffer, size, bytesRead, recFile); + numBuffersToRecord--; + } + } + + Timestamp timeStamp; + bool getAudioTime = audioRecorder->GetAudioTime(timeStamp, Timestamp::Timestampbase::MONOTONIC); + EXPECT_EQ(true, getAudioTime); + EXPECT_GE(timeStamp.time.tv_sec, (const long)VALUE_ZERO); + EXPECT_GE(timeStamp.time.tv_nsec, (const long)VALUE_ZERO); + + audioRecorder->Flush(); + audioRecorder->Stop(); + audioRecorder->Release(); + + free(buffer); + fclose(recFile); +} + +/** +* @tc.name : Test Flush API. +* @tc.number: Auido_Recorder_Flush_001 +* @tc.desc : test Flush interface, Returns true if the flush is successful. +*/ +HWTEST(AudioRecorderModuleTest, Auido_Recorder_Flush_001, TestSize.Level1) +{ + int32_t ret = -1; + bool isBlockingRead = true; + unique_ptr audioRecorder = AudioRecorder::Create(STREAM_MUSIC); + EXPECT_NE(nullptr, audioRecorder); + + ret = AudioRecorderModuleTest::InitializeRecorder(audioRecorder); + EXPECT_EQ(SUCCESS, ret); + + bool isStarted = audioRecorder->Start(); + EXPECT_EQ(true, isStarted); + + size_t bufferLen; + ret = audioRecorder->GetBufferSize(bufferLen); + EXPECT_EQ(SUCCESS, ret); + + uint8_t* buffer = (uint8_t *) malloc(bufferLen); + ASSERT_NE(nullptr, buffer); + FILE *recFile = fopen(AUDIO_RECORD_FILE3.c_str(), "wb"); + ASSERT_NE(nullptr, recFile); + + size_t size = 1; + int32_t bytesRead = 0; + int32_t numBuffersToRecord = 5; + + while (numBuffersToRecord) { + bytesRead = audioRecorder->Read(*buffer, bufferLen, isBlockingRead); + if (bytesRead < 0) { + break; + } else if (bytesRead > 0) { + fwrite(buffer, size, bytesRead, recFile); + numBuffersToRecord--; + } + } + + bool isFlushed = audioRecorder->Flush(); + EXPECT_EQ(true, isFlushed); + + audioRecorder->Stop(); + audioRecorder->Release(); + + free(buffer); + fclose(recFile); +} + +/** +* @tc.name : Test Stop API. +* @tc.number: Auido_Recorder_Stop_001 +* @tc.desc : test Stop interface, Returns true if the stop is successful. +*/ +HWTEST(AudioRecorderModuleTest, Auido_Recorder_Stop_001, TestSize.Level1) +{ + int32_t ret = -1; + bool isBlockingRead = true; + unique_ptr audioRecorder = AudioRecorder::Create(STREAM_MUSIC); + EXPECT_NE(nullptr, audioRecorder); + + ret = AudioRecorderModuleTest::InitializeRecorder(audioRecorder); + EXPECT_EQ(SUCCESS, ret); + + bool isStarted = audioRecorder->Start(); + EXPECT_EQ(true, isStarted); + + size_t bufferLen; + ret = audioRecorder->GetBufferSize(bufferLen); + EXPECT_EQ(SUCCESS, ret); + + uint8_t* buffer = (uint8_t *) malloc(bufferLen); + ASSERT_NE(nullptr, buffer); + FILE *recFile = fopen(AUDIO_RECORD_FILE3.c_str(), "wb"); + ASSERT_NE(nullptr, recFile); + + size_t size = 1; + int32_t bytesRead = 0; + int32_t numBuffersToRecord = 5; + + while (numBuffersToRecord) { + bytesRead = audioRecorder->Read(*buffer, bufferLen, isBlockingRead); + if (bytesRead < 0) { + break; + } else if (bytesRead > 0) { + fwrite(buffer, size, bytesRead, recFile); + numBuffersToRecord--; + } + } + + audioRecorder->Flush(); + + bool isStopped = audioRecorder->Stop(); + EXPECT_EQ(true, isStopped); + + audioRecorder->Release(); + + free(buffer); + fclose(recFile); +} + +/** +* @tc.name : Test Release API. +* @tc.number: Auido_Recorder_Release_001 +* @tc.desc : test Release interface, Returns true if the release is successful. +*/ +HWTEST(AudioRecorderModuleTest, Auido_Recorder_Release_001, TestSize.Level1) +{ + int32_t ret = -1; + bool isBlockingRead = true; + unique_ptr audioRecorder = AudioRecorder::Create(STREAM_MUSIC); + EXPECT_NE(nullptr, audioRecorder); + + ret = AudioRecorderModuleTest::InitializeRecorder(audioRecorder); + EXPECT_EQ(SUCCESS, ret); + + bool isStarted = audioRecorder->Start(); + EXPECT_EQ(true, isStarted); + + size_t bufferLen; + ret = audioRecorder->GetBufferSize(bufferLen); + EXPECT_EQ(SUCCESS, ret); + + uint8_t* buffer = (uint8_t *) malloc(bufferLen); + ASSERT_NE(nullptr, buffer); + FILE *recFile = fopen(AUDIO_RECORD_FILE3.c_str(), "wb"); + ASSERT_NE(nullptr, recFile); + + size_t size = 1; + int32_t bytesRead = 0; + int32_t numBuffersToRecord = 5; + + while (numBuffersToRecord) { + bytesRead = audioRecorder->Read(*buffer, bufferLen, isBlockingRead); + if (bytesRead < 0) { + break; + } else if (bytesRead > 0) { + fwrite(buffer, size, bytesRead, recFile); + numBuffersToRecord--; + } + } + + audioRecorder->Flush(); + audioRecorder->Stop(); + + bool isReleased = audioRecorder->Release(); + EXPECT_EQ(true, isReleased); + + free(buffer); + fclose(recFile); +} + +/** +* @tc.name : Test GetStatus API. +* @tc.number: Auido_Recorder_GetStatus_001 +* @tc.desc : test GetStatus interface, Returns correct state on success. +*/ +HWTEST(AudioRecorderModuleTest, Audio_Recorder_GetStatus_001, TestSize.Level1) +{ + int32_t ret = -1; + RecorderState state = RECORDER_INVALID; + + unique_ptr audioRecorder = AudioRecorder::Create(STREAM_MUSIC); + ASSERT_NE(nullptr, audioRecorder); + state = audioRecorder->GetStatus(); + EXPECT_EQ(RECORDER_NEW, state); + + AudioRecorderParams recorderParams; + recorderParams.audioSampleFormat = SAMPLE_S16LE; + recorderParams.samplingRate = SAMPLE_RATE_44100; + recorderParams.audioChannel = STEREO; + recorderParams.audioEncoding = ENCODING_PCM; + ret = audioRecorder->SetParams(recorderParams); + EXPECT_EQ(SUCCESS, ret); + state = audioRecorder->GetStatus(); + EXPECT_EQ(RECORDER_PREPARED, state); + + bool isStarted = audioRecorder->Start(); + EXPECT_EQ(true, isStarted); + state = audioRecorder->GetStatus(); + EXPECT_EQ(RECORDER_RUNNING, state); + + bool isStopped = audioRecorder->Stop(); + EXPECT_EQ(true, isStopped); + state = audioRecorder->GetStatus(); + EXPECT_EQ(RECORDER_STOPPED, state); + + bool isReleased = audioRecorder->Release(); + EXPECT_EQ(true, isReleased); + state = audioRecorder->GetStatus(); + EXPECT_EQ(RECORDER_RELEASED, state); +} +} // namespace AudioStandard +} // namespace OHOS -- Gitee