1 /* 2 * Copyright (c) 2023-2025 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #include <cstdint> 17 #include <thread> 18 #include <gtest/gtest.h> 19 #include "gmock/gmock.h" 20 #include "audio_utils.h" 21 #include "parameter.h" 22 #include "audio_channel_blend.h" 23 #include "volume_ramp.h" 24 #include "audio_speed.h" 25 #include "audio_errors.h" 26 #include "audio_scope_exit.h" 27 #include "audio_safe_block_queue.h" 28 #include "audio_utils_c.h" 29 30 using namespace testing::ext; 31 using namespace testing; 32 using namespace std; 33 namespace OHOS { 34 namespace AudioStandard { 35 36 /** 37 * @tc.name : Test SetVolumeRampConfig API 38 * @tc.type : FUNC 39 * @tc.number: SetVolumeRampConfig_002 40 * @tc.desc : Test SetVolumeRampConfig API, 41 * when rampDirection_ is RAMP_UP 42 */ 43 HWTEST(AudioUtilsUnitTest, SetVolumeRampConfig_002, TestSize.Level1) 44 { 45 shared_ptr<VolumeRamp> volumeRamp = std::make_shared<VolumeRamp>(); 46 volumeRamp->SetVolumeRampConfig(10.1f, 9.9f, 4); 47 EXPECT_EQ(volumeRamp->rampDirection_, RAMP_UP); 48 } 49 50 /** 51 * @tc.name : Test GetRampVolume API 52 * @tc.type : FUNC 53 * @tc.number: GetRampVolume_003 54 * @tc.desc : Test GetRampVolume API, 55 * when ret is 0.0f 56 */ 57 HWTEST(AudioUtilsUnitTest, GetRampVolume_003, TestSize.Level1) 58 { 59 shared_ptr<VolumeRamp> volumeRamp = std::make_shared<VolumeRamp>(); 60 volumeRamp->isVolumeRampActive_ = true; 61 volumeRamp->initTime_ = 1; 62 float ret = volumeRamp->GetRampVolume(); 63 EXPECT_EQ(ret, 0.0f); 64 } 65 66 /** 67 * @tc.name : Test audio_channel_blend API 68 * @tc.type : FUNC 69 * @tc.number: audio_channel_blend_020 70 * @tc.desc : Test AudioBlend Process API,Return buffer 71 * when blendMode is MODE_BLEND_LR,channel 6 72 */ 73 HWTEST(AudioUtilsUnitTest, audio_channel_blend_020, TestSize.Level1) 74 { 75 uint8_t b[8] = {2, 4, 6, 8, 10, 12, 14, 16}; 76 uint8_t format = SAMPLE_F32LE; 77 uint8_t channels = CHANNEL_3; 78 ChannelBlendMode blendMode = MODE_ALL_RIGHT; 79 shared_ptr<AudioBlend> audioBlend = std::make_shared<AudioBlend>(blendMode, format, channels); 80 audioBlend->Process(b, 8); 81 EXPECT_EQ(b[0], 2); 82 } 83 84 /** 85 * @tc.name : Test audio_channel_blend API 86 * @tc.type : FUNC 87 * @tc.number: audio_channel_blend_021 88 * @tc.desc : Test AudioBlend Process API,Return buffer 89 * when blendMode is MODE_BLEND_LR,channel 6 90 */ 91 HWTEST(AudioUtilsUnitTest, audio_channel_blend_021, TestSize.Level1) 92 { 93 uint8_t b[8] = {2, 4, 6, 8, 10, 12, 14, 16}; 94 uint8_t format = INVALID_WIDTH; 95 uint8_t channels = CHANNEL_3; 96 ChannelBlendMode blendMode = MODE_ALL_RIGHT; 97 shared_ptr<AudioBlend> audioBlend = std::make_shared<AudioBlend>(blendMode, format, channels); 98 audioBlend->Process(b, 8); 99 EXPECT_EQ(b[0], 2); 100 } 101 102 /** 103 * @tc.name : Test GetAudioFormatSize API 104 * @tc.type : FUNC 105 * @tc.number: GetAudioFormatSize_001 106 * @tc.desc : Test GetAudioFormatSize 107 */ 108 HWTEST(AudioUtilsUnitTest, GetAudioFormatSize_001, TestSize.Level1) 109 { 110 uint8_t b[8] = {2, 4, 6, 8, 10, 12, 14, 16}; 111 uint8_t format = INVALID_WIDTH; 112 uint8_t channels = CHANNEL_3; 113 ChannelBlendMode blendMode = MODE_ALL_RIGHT; 114 shared_ptr<AudioBlend> audioBlend = std::make_shared<AudioBlend>(blendMode, format, channels); 115 audioBlend->GetAudioFormatSize(); 116 EXPECT_EQ(b[0], 2); 117 } 118 119 /** 120 * @tc.name : Test CountVolume API 121 * @tc.type : FUNC 122 * @tc.number: CountVolume_001 123 * @tc.desc : Test CountVolume 124 */ 125 HWTEST(AudioUtilsUnitTest, CountVolume_001, TestSize.Level1) 126 { 127 std::string value = "Test"; 128 Trace::CountVolume(value, 0); 129 Trace::CountVolume(value, 2); 130 EXPECT_FALSE(static_cast<size_t>(0)); 131 } 132 133 /** 134 * @tc.name : Test ConvertFromFloatTo24Bit API 135 * @tc.type : FUNC 136 * @tc.number: ConvertFromFloatTo24Bit_001 137 * @tc.desc : Test ConvertFromFloatTo24Bit 138 */ 139 HWTEST(AudioUtilsUnitTest, ConvertFromFloatTo24Bit_001, TestSize.Level1) 140 { 141 float a = 2.0f; 142 uint8_t b = 1; 143 ConvertFromFloatTo24Bit(1, &a, &b); 144 a = -2.5f; 145 ConvertFromFloatTo24Bit(1, &a, &b); 146 EXPECT_FALSE(static_cast<size_t>(0)); 147 } 148 149 /** 150 * @tc.name : Test IsInnerCapSinkName API 151 * @tc.type : FUNC 152 * @tc.number: IsInnerCapSinkName_001 153 * @tc.desc : Test IsInnerCapSinkName 154 */ 155 HWTEST(AudioUtilsUnitTest, IsInnerCapSinkName_001, TestSize.Level1) 156 { 157 char pattern[MAX_MEM_MALLOC_SIZE + 1] = {0}; 158 EXPECT_EQ(IsInnerCapSinkName(pattern), false); 159 } 160 161 /** 162 * @tc.name : Test IsInnerCapSinkName API 163 * @tc.type : FUNC 164 * @tc.number: IsInnerCapSinkName_002 165 * @tc.desc : Test IsInnerCapSinkName 166 */ 167 HWTEST(AudioUtilsUnitTest, IsInnerCapSinkName_002, TestSize.Level1) 168 { 169 char pattern[] = "invalid_pattern"; 170 EXPECT_FALSE(IsInnerCapSinkName(pattern)); 171 } 172 173 /** 174 * @tc.name : Test GetFormatByteSize API 175 * @tc.type : FUNC 176 * @tc.number: GetFormatByteSize_005 177 * @tc.desc : Test GetFormatByteSize 178 */ 179 HWTEST(AudioUtilsUnitTest, GetFormatByteSize_005, TestSize.Level0) 180 { 181 int32_t format = SAMPLE_F32LE; 182 int32_t formatByteSize = GetFormatByteSize(format); 183 EXPECT_EQ(formatByteSize, 4); 184 } 185 186 /** 187 * @tc.name : Test CloseFd API 188 * @tc.type : FUNC 189 * @tc.number: CloseFd_001 190 * @tc.desc : Test CloseFd 191 */ 192 HWTEST(AudioUtilsUnitTest, CloseFd_001, TestSize.Level0) 193 { 194 CloseFd(STDIN_FILENO); 195 EXPECT_FALSE(static_cast<size_t>(0)); 196 } 197 198 /** 199 * @tc.name : Test MockPcmData API 200 * @tc.type : FUNC 201 * @tc.number: MockPcmData_003 202 * @tc.desc : Test MockPcmData API if format is SAMPLE_S16LE 203 * when mockedTime_ >= MOCK_INTERVAL 204 */ 205 HWTEST(AudioUtilsUnitTest, MockPcmData_003, TestSize.Level1) 206 { 207 std::shared_ptr<AudioLatencyMeasurement> audioLatencyMeasurement = 208 std::make_shared<AudioLatencyMeasurement>(44100, 2, 16, "com.example.null", 1); 209 uint8_t buffer[1024] = {}; 210 size_t bufferLen = sizeof(buffer); 211 size_t mockInterval = 2000; 212 213 audioLatencyMeasurement->mockedTime_ = mockInterval + 1; 214 audioLatencyMeasurement->format_ = SAMPLE_S16LE; 215 bool ret = audioLatencyMeasurement->MockPcmData(buffer, bufferLen); 216 EXPECT_EQ(ret, true); 217 } 218 219 /** 220 * @tc.name : Test MockPcmData API 221 * @tc.type : FUNC 222 * @tc.number: MockPcmData_004 223 * @tc.desc : Test MockPcmData API if format is SAMPLE_S32LE 224 * when mockedTime_ >= MOCK_INTERVAL 225 */ 226 HWTEST(AudioUtilsUnitTest, MockPcmData_004, TestSize.Level1) 227 { 228 std::shared_ptr<AudioLatencyMeasurement> audioLatencyMeasurement = 229 std::make_shared<AudioLatencyMeasurement>(44100, 2, 16, "com.example.null", 1); 230 uint8_t buffer[1024] = {}; 231 size_t bufferLen = sizeof(buffer); 232 size_t mockInterval = 2000; 233 234 audioLatencyMeasurement->mockedTime_ = mockInterval + 1; 235 audioLatencyMeasurement->format_ = SAMPLE_S32LE; 236 bool ret = audioLatencyMeasurement->MockPcmData(buffer, bufferLen); 237 EXPECT_EQ(ret, true); 238 } 239 240 /** 241 * @tc.name : Test CallEndAndClear API 242 * @tc.type : FUNC 243 * @tc.number: CallEndAndClear_001 244 * @tc.desc : Test CallEndAndClear when *cTrace is nullptr 245 */ 246 HWTEST(AudioUtilsUnitTest, CallEndAndClear_001, TestSize.Level0) 247 { 248 CTrace *cTrace = nullptr; 249 CallEndAndClear(&cTrace); 250 EXPECT_TRUE(cTrace == nullptr); 251 } 252 253 /** 254 * @tc.name : Test CallEndAndClear API 255 * @tc.type : FUNC 256 * @tc.number: CallEndAndClear_002 257 * @tc.desc : Test CallEndAndClear when **cTrace is nullptr 258 */ 259 HWTEST(AudioUtilsUnitTest, CallEndAndClear_002, TestSize.Level0) 260 { 261 CTrace **cTrace = nullptr; 262 CallEndAndClear(cTrace); 263 EXPECT_TRUE(cTrace == nullptr); 264 } 265 266 /** 267 * @tc.name : Test AudioLatencyMeasurement API 268 * @tc.type : FUNC 269 * @tc.number: AudioLatencyMeasurement_001 270 * @tc.desc : Test AudioLatencyMeasurement when **cTrace is nullptr 271 */ 272 HWTEST(AudioUtilsUnitTest, AudioLatencyMeasurement_001, TestSize.Level1) 273 { 274 AudioLatencyMeasurement audioLatencyMeasurement(44100, 2, 16, "com.example.null", 1); 275 EXPECT_EQ(audioLatencyMeasurement.sessionId_, 1); 276 } 277 } // namespace AudioStandard 278 } // namespace OHOS