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