1 /*
2 * Copyright (C) 2023 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 "soundpool_mock.h"
17
18 using namespace std;
19 using namespace OHOS;
20 using namespace OHOS::Media;
21 using namespace testing::ext;
22
OnError(int32_t errorCode)23 void SoundPoolCallbackTest::OnError(int32_t errorCode)
24 {
25 cout << "Error received, errorCode:" << errorCode << endl;
26 }
27
OnLoadCompleted(int32_t soundId)28 void SoundPoolCallbackTest::OnLoadCompleted(int32_t soundId)
29 {
30 soundCounter_.Increment();
31 }
32
OnPlayFinished(int32_t streamID)33 void SoundPoolCallbackTest::OnPlayFinished(int32_t streamID)
34 {
35 cout << "OnPlayFinished havePlayedSoundNumInner_: "<< havePlayedSoundNumInner_ << endl;
36 havePlayedSoundNumInner_++;
37 if (streamID > 0) {
38 vector_.push_back(streamID);
39 }
40 }
41
CreateSoundPool(int maxStreams,AudioStandard::AudioRendererInfo audioRenderInfo)42 bool SoundPoolMock::CreateSoundPool(int maxStreams, AudioStandard::AudioRendererInfo audioRenderInfo)
43 {
44 if (soundPool_ != nullptr) {
45 soundPool_->Release();
46 soundPool_ = nullptr;
47 }
48 soundPool_ = SoundPoolFactory::CreateSoundPool(maxStreams, audioRenderInfo);
49 return soundPool_ != nullptr;
50 }
51
Load(std::string url)52 int32_t SoundPoolMock::Load(std::string url)
53 {
54 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
55 return soundPool_->Load(url);
56 }
57
Load(int32_t fd,int64_t offset,int64_t length)58 int32_t SoundPoolMock::Load(int32_t fd, int64_t offset, int64_t length)
59 {
60 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
61 return soundPool_->Load(fd, offset, length);
62 }
63
Play(int32_t soundID,PlayParams playParameters)64 int32_t SoundPoolMock::Play(int32_t soundID, PlayParams playParameters)
65 {
66 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
67 return soundPool_->Play(soundID, playParameters);
68 }
69
Stop(int32_t streamID)70 int32_t SoundPoolMock::Stop(int32_t streamID)
71 {
72 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
73 if (isExit_.load()) {
74 isExit_.store(true);
75 }
76 return soundPool_->Stop(streamID);
77 }
78
SetLoop(int32_t streamID,int32_t loop)79 int32_t SoundPoolMock::SetLoop(int32_t streamID, int32_t loop)
80 {
81 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
82 return soundPool_->SetLoop(streamID, loop);
83 }
84
SetPriority(int32_t streamID,int32_t priority)85 int32_t SoundPoolMock::SetPriority(int32_t streamID, int32_t priority)
86 {
87 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
88 return soundPool_->SetPriority(streamID, priority);
89 }
90
SetRate(int32_t streamID,AudioStandard::AudioRendererRate renderRate)91 int32_t SoundPoolMock::SetRate(int32_t streamID, AudioStandard::AudioRendererRate renderRate)
92 {
93 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
94 return soundPool_->SetRate(streamID, renderRate);
95 }
96
SetVolume(int32_t streamID,float leftVolume,float rigthVolume)97 int32_t SoundPoolMock::SetVolume(int32_t streamID, float leftVolume, float rigthVolume)
98 {
99 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
100 return soundPool_->SetVolume(streamID, leftVolume, rigthVolume);
101 }
102
Unload(int32_t soundID)103 int32_t SoundPoolMock::Unload(int32_t soundID)
104 {
105 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
106 return soundPool_->Unload(soundID);
107 }
108
Release()109 int32_t SoundPoolMock::Release()
110 {
111 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
112 if (isExit_.load()) {
113 isExit_.store(true);
114 }
115 return soundPool_->Release();
116 }
117
SetSoundPoolCallback(const std::shared_ptr<ISoundPoolCallback> & soundPoolCallback)118 int32_t SoundPoolMock::SetSoundPoolCallback(const std::shared_ptr<ISoundPoolCallback> &soundPoolCallback)
119 {
120 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
121 return soundPool_->SetSoundPoolCallback(soundPoolCallback);
122 }
123
GetFileSize(const std::string & fileName)124 size_t SoundPoolMock::GetFileSize(const std::string& fileName)
125 {
126 size_t fileSize = 0;
127 if (!fileName.empty()) {
128 struct stat fileStatus {};
129 if (stat(fileName.c_str(), &fileStatus) == 0) {
130 fileSize = static_cast<size_t>(fileStatus.st_size);
131 }
132 }
133 return fileSize;
134 }
135
CreateParallelSoundPool(int maxStreams,AudioStandard::AudioRendererInfo audioRenderInfo)136 bool SoundPoolParallelMock::CreateParallelSoundPool(int maxStreams, AudioStandard::AudioRendererInfo audioRenderInfo)
137 {
138 if (soundPoolParallel_ != nullptr) {
139 soundPoolParallel_->Release();
140 soundPoolParallel_ = nullptr;
141 }
142 soundPoolParallel_ = SoundPoolFactory::CreateParallelSoundPool(maxStreams, audioRenderInfo);
143 return soundPoolParallel_ != nullptr;
144 }
145
Load(std::string url)146 int32_t SoundPoolParallelMock::Load(std::string url)
147 {
148 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPoolParallel_ != nullptr, MSERR_INVALID_OPERATION, "soundPool nullptr");
149 return soundPoolParallel_->Load(url);
150 }
151
Load(int32_t fd,int64_t offset,int64_t length)152 int32_t SoundPoolParallelMock::Load(int32_t fd, int64_t offset, int64_t length)
153 {
154 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPoolParallel_ != nullptr, MSERR_INVALID_OPERATION, "soundPool nullptr");
155 return soundPoolParallel_->Load(fd, offset, length);
156 }
157
Play(int32_t soundID,PlayParams playParameters)158 int32_t SoundPoolParallelMock::Play(int32_t soundID, PlayParams playParameters)
159 {
160 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPoolParallel_ != nullptr, MSERR_INVALID_OPERATION, "soundPool nullptr");
161 return soundPoolParallel_->Play(soundID, playParameters);
162 }
163
Stop(int32_t streamID)164 int32_t SoundPoolParallelMock::Stop(int32_t streamID)
165 {
166 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPoolParallel_ != nullptr, MSERR_INVALID_OPERATION, "soundPool nullptr");
167 return soundPoolParallel_->Stop(streamID);
168 }
169
SetLoop(int32_t streamID,int32_t loop)170 int32_t SoundPoolParallelMock::SetLoop(int32_t streamID, int32_t loop)
171 {
172 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPoolParallel_ != nullptr, MSERR_INVALID_OPERATION, "soundPool nullptr");
173 return soundPoolParallel_->SetLoop(streamID, loop);
174 }
175
SetPriority(int32_t streamID,int32_t priority)176 int32_t SoundPoolParallelMock::SetPriority(int32_t streamID, int32_t priority)
177 {
178 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPoolParallel_ != nullptr, MSERR_INVALID_OPERATION, "soundPool nullptr");
179 return soundPoolParallel_->SetPriority(streamID, priority);
180 }
181
SetRate(int32_t streamID,AudioStandard::AudioRendererRate renderRate)182 int32_t SoundPoolParallelMock::SetRate(int32_t streamID, AudioStandard::AudioRendererRate renderRate)
183 {
184 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPoolParallel_ != nullptr, MSERR_INVALID_OPERATION, "soundPool nullptr");
185 return soundPoolParallel_->SetRate(streamID, renderRate);
186 }
187
SetVolume(int32_t streamID,float leftVolume,float rigthVolume)188 int32_t SoundPoolParallelMock::SetVolume(int32_t streamID, float leftVolume, float rigthVolume)
189 {
190 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPoolParallel_ != nullptr, MSERR_INVALID_OPERATION, "soundPool nullptr");
191 return soundPoolParallel_->SetVolume(streamID, leftVolume, rigthVolume);
192 }
193
Unload(int32_t soundID)194 int32_t SoundPoolParallelMock::Unload(int32_t soundID)
195 {
196 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPoolParallel_ != nullptr, MSERR_INVALID_OPERATION, "soundPool nullptr");
197 return soundPoolParallel_->Unload(soundID);
198 }
199
Release()200 int32_t SoundPoolParallelMock::Release()
201 {
202 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPoolParallel_ != nullptr, MSERR_INVALID_OPERATION, "soundPool nullptr");
203 return soundPoolParallel_->Release();
204 }
205
SetSoundPoolCallback(const std::shared_ptr<ISoundPoolCallback> & soundPoolCallback)206 int32_t SoundPoolParallelMock::SetSoundPoolCallback(const std::shared_ptr<ISoundPoolCallback> &soundPoolCallback)
207 {
208 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPoolParallel_ != nullptr, MSERR_INVALID_OPERATION, "soundPool nullptr");
209 return soundPoolParallel_->SetSoundPoolCallback(soundPoolCallback);
210 }
211
GetFileSize(const std::string & fileName)212 size_t SoundPoolParallelMock::GetFileSize(const std::string& fileName)
213 {
214 size_t fileSize = 0;
215 if (!fileName.empty()) {
216 struct stat fileStatus {};
217 if (stat(fileName.c_str(), &fileStatus) == 0) {
218 fileSize = static_cast<size_t>(fileStatus.st_size);
219 }
220 }
221 return fileSize;
222 }
223
224