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 cout << "OnLoadCompleted soundId:" << soundId << ", haveLoadedSoundNumInner_: "<< haveLoadedSoundNumInner_ << endl;
31 haveLoadedSoundNumInner_++;
32 }
33
OnPlayFinished(int32_t streamID)34 void SoundPoolCallbackTest::OnPlayFinished(int32_t streamID)
35 {
36 cout << "OnPlayFinished haveLoadedSoundNumInner_: "<< havePlayedSoundNumInner_ << endl;
37 havePlayedSoundNumInner_++;
38 if (streamID > 0) {
39 vector_.push_back(streamID);
40 }
41 }
42
CreateSoundPool(int maxStreams,AudioStandard::AudioRendererInfo audioRenderInfo)43 bool SoundPoolMock::CreateSoundPool(int maxStreams, AudioStandard::AudioRendererInfo audioRenderInfo)
44 {
45 soundPool_ = SoundPoolFactory::CreateSoundPool(maxStreams, audioRenderInfo);
46 return soundPool_ != nullptr;
47 }
48
Load(std::string url)49 int32_t SoundPoolMock::Load(std::string url)
50 {
51 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
52 return soundPool_->Load(url);
53 }
54
Load(int32_t fd,int64_t offset,int64_t length)55 int32_t SoundPoolMock::Load(int32_t fd, int64_t offset, int64_t length)
56 {
57 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
58 return soundPool_->Load(fd, offset, length);
59 }
60
Play(int32_t soundID,PlayParams playParameters)61 int32_t SoundPoolMock::Play(int32_t soundID, PlayParams playParameters)
62 {
63 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
64 return soundPool_->Play(soundID, playParameters);
65 }
66
Stop(int32_t streamID)67 int32_t SoundPoolMock::Stop(int32_t streamID)
68 {
69 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
70 if (isExit_.load()) {
71 isExit_.store(true);
72 }
73 return soundPool_->Stop(streamID);
74 }
75
SetLoop(int32_t streamID,int32_t loop)76 int32_t SoundPoolMock::SetLoop(int32_t streamID, int32_t loop)
77 {
78 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
79 return soundPool_->SetLoop(streamID, loop);
80 }
81
SetPriority(int32_t streamID,int32_t priority)82 int32_t SoundPoolMock::SetPriority(int32_t streamID, int32_t priority)
83 {
84 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
85 return soundPool_->SetPriority(streamID, priority);
86 }
87
SetRate(int32_t streamID,AudioStandard::AudioRendererRate renderRate)88 int32_t SoundPoolMock::SetRate(int32_t streamID, AudioStandard::AudioRendererRate renderRate)
89 {
90 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
91 return soundPool_->SetRate(streamID, renderRate);
92 }
93
SetVolume(int32_t streamID,float leftVolume,float rigthVolume)94 int32_t SoundPoolMock::SetVolume(int32_t streamID, float leftVolume, float rigthVolume)
95 {
96 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
97 return soundPool_->SetVolume(streamID, leftVolume, rigthVolume);
98 }
99
Unload(int32_t soundID)100 int32_t SoundPoolMock::Unload(int32_t soundID)
101 {
102 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
103 return soundPool_->Unload(soundID);
104 }
105
Release()106 int32_t SoundPoolMock::Release()
107 {
108 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
109 if (isExit_.load()) {
110 isExit_.store(true);
111 }
112 return soundPool_->Release();
113 }
114
SetSoundPoolCallback(const std::shared_ptr<ISoundPoolCallback> & soundPoolCallback)115 int32_t SoundPoolMock::SetSoundPoolCallback(const std::shared_ptr<ISoundPoolCallback> &soundPoolCallback)
116 {
117 UNITTEST_CHECK_AND_RETURN_RET_LOG(soundPool_ != nullptr, MSERR_INVALID_OPERATION, "soundPool_ == nullptr");
118 return soundPool_->SetSoundPoolCallback(soundPoolCallback);
119 }
120
GetFileSize(const std::string & fileName)121 size_t SoundPoolMock::GetFileSize(const std::string& fileName)
122 {
123 size_t fileSize = 0;
124 if (!fileName.empty()) {
125 struct stat fileStatus {};
126 if (stat(fileName.c_str(), &fileStatus) == 0) {
127 fileSize = static_cast<size_t>(fileStatus.st_size);
128 }
129 }
130 return fileSize;
131 }