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 #ifndef SOUNDPOOL_MOCK_H
16 #define SOUNDPOOL_MOCK_H
17 
18 #include <fcntl.h>
19 #include <cstdio>
20 #include <mutex>
21 #include <cstdlib>
22 #include <thread>
23 #include <string>
24 #include <vector>
25 #include <memory>
26 #include <atomic>
27 #include <iostream>
28 #include "gtest/gtest.h"
29 #include "unittest_log.h"
30 #include "media_errors.h"
31 #include "nocopyable.h"
32 #include "isoundpool.h"
33 
34 namespace OHOS {
35 namespace Media {
36 using namespace std;
37 using namespace AudioStandard;
38 class SoundPoolMock {
39 public:
40     SoundPoolMock() = default;
41     ~SoundPoolMock() = default;
42     bool CreateSoundPool(int maxStreams, AudioStandard::AudioRendererInfo audioRenderInfo);
43     int32_t Load(std::string url);
44     int32_t Load(int32_t fd, int64_t offset, int64_t length);
45     int32_t Play(int32_t soundID, PlayParams playParameters);
46     int32_t Stop(int32_t streamID);
47     int32_t SetLoop(int32_t streamID, int32_t loop);
48     int32_t SetPriority(int32_t streamID, int32_t priority);
49     int32_t SetRate(int32_t streamID, AudioStandard::AudioRendererRate renderRate);
50     int32_t SetVolume(int32_t streamID, float leftVolume, float rigthVolume);
51     int32_t Unload(int32_t soundID);
52     int32_t Release();
53     int32_t SetSoundPoolCallback(const std::shared_ptr<ISoundPoolCallback> &soundPoolCallback);
54     size_t GetFileSize(const std::string& fileName);
55 private:
56     std::shared_ptr<ISoundPool> soundPool_ = nullptr;
57     std::atomic<bool> isExit_ { false };
58 };
59 
60 class SoundPoolCallbackTest : public ISoundPoolCallback, public NoCopyable {
61 public:
SoundPoolCallbackTest(std::shared_ptr<SoundPoolMock> soundPool)62     explicit SoundPoolCallbackTest(std::shared_ptr<SoundPoolMock> soundPool)
63     {
64         soundPool_ = soundPool;
65     }
~SoundPoolCallbackTest()66     ~SoundPoolCallbackTest()
67     {
68         soundPool_ = nullptr;
69     }
GetHaveLoadedSoundNum()70     int32_t GetHaveLoadedSoundNum()
71     {
72         cout << "GetHaveLoadedSoundNum haveLoadedSoundNumInner_:" << haveLoadedSoundNumInner_ << endl;
73         return haveLoadedSoundNumInner_;
74     }
ResetHaveLoadedSoundNum()75     void ResetHaveLoadedSoundNum()
76     {
77         cout << "Before ResetHaveLoadedSoundNum haveLoadedSoundNumInner_:" << haveLoadedSoundNumInner_ << endl;
78         haveLoadedSoundNumInner_ = 0;
79         cout << "After ResetHaveLoadedSoundNum haveLoadedSoundNumInner_:" << haveLoadedSoundNumInner_ << endl;
80     }
81 
GetHavePlayedSoundNum()82     int32_t GetHavePlayedSoundNum()
83     {
84         cout << "GetHavePlayedSoundNum havePlayedSoundNumInner_:" << havePlayedSoundNumInner_ << endl;
85         return havePlayedSoundNumInner_;
86     }
ResetHavePlayedSoundNum()87     void ResetHavePlayedSoundNum()
88     {
89         cout << "Before ResetHavePlayedSoundNum havePlayedSoundNumInner_:" << havePlayedSoundNumInner_ << endl;
90         havePlayedSoundNumInner_ = 0;
91         cout << "After ResetHavePlayedSoundNum havePlayedSoundNumInner_:" << havePlayedSoundNumInner_ << endl;
92     }
93     std::shared_ptr<SoundPoolMock> soundPool_ = nullptr;
94     void OnLoadCompleted(int32_t soundId) override;
95     void OnPlayFinished() override;
96     void OnError(int32_t errorCode) override;
97 
98 private:
99     int32_t haveLoadedSoundNumInner_ = 0;
100     int32_t havePlayedSoundNumInner_ = 0;
101 };
102 } // namespace Media
103 } // namespace OHOS
104 #endif