• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "stream_id_manager_unit_test.h"
17 #include "media_errors.h"
18 #include "stream_id_manager.h"
19 
20 using namespace OHOS;
21 using namespace OHOS::Media;
22 using namespace testing::ext;
23 using namespace std;
24 
25 const int32_t MAX_STREAMS = 3;
26 const int32_t BEGIN_NUM = 0;
27 const int32_t STREAM_ID_BEGIN = 1;
28 const int32_t SOUND_ID_BEGIN = 1;
29 
30 namespace {
31 constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, LOG_DOMAIN_SOUNDPOOL, "StreamIdManagerUnitTest"};
32 }
33 
34 namespace OHOS {
35 namespace Media {
SetUpTestCase(void)36 void StreamIDManagerUnitTest::SetUpTestCase(void)
37 {}
38 
TearDownTestCase(void)39 void StreamIDManagerUnitTest::TearDownTestCase(void)
40 {
41     std::cout << "sleep one second to protect PlayerEngine safely exit." << endl;
42     sleep(1);  // let PlayEngine safe exit.
43 }
44 
SetUp(void)45 void StreamIDManagerUnitTest::SetUp(void)
46 {
47     AudioStandard::AudioRendererInfo audioRenderInfo;
48     audioRenderInfo.contentType = AudioStandard::CONTENT_TYPE_MUSIC;
49     audioRenderInfo.streamUsage = AudioStandard::STREAM_USAGE_MUSIC;
50     audioRenderInfo.rendererFlags = 0;
51     streamIDManager_ = std::make_shared<StreamIDManager>(MAX_STREAMS, audioRenderInfo);
52 }
53 
TearDown(void)54 void StreamIDManagerUnitTest::TearDown(void)
55 {
56     if (streamIDManager_ != nullptr) {
57         streamIDManager_.reset();
58     }
59 }
60 
61 /**
62  * @tc.name: streamId_function_001
63  * @tc.desc: function test MulInitThreadPool
64  * @tc.type: FUNC
65  * @tc.require:
66  */
67 HWTEST_F(StreamIDManagerUnitTest, streamId_function_001, TestSize.Level2)
68 {
69     MEDIA_LOGI("streamId_function_001 before");
70     streamIDManager_->isStreamPlayingThreadPoolStarted_.store(true);
71     EXPECT_EQ(MSERR_OK, streamIDManager_->InitThreadPool());
72     MEDIA_LOGI("streamId_function_001 after");
73 }
74 
75 /**
76  * @tc.name: streamId_function_002
77  * @tc.desc: function test streamNum not enough
78  * @tc.type: FUNC
79  * @tc.require:
80  */
81 HWTEST_F(StreamIDManagerUnitTest, streamId_function_002, TestSize.Level2)
82 {
83     MEDIA_LOGI("streamId_function_002 before");
84     streamIDManager_->isStreamPlayingThreadPoolStarted_.store(false);
85     streamIDManager_->maxStreams_ = 0;
86     EXPECT_EQ(MSERR_OK, streamIDManager_->InitThreadPool());
87     MEDIA_LOGI("streamId_function_002 after");
88 }
89 
90 /**
91  * @tc.name: streamId_function_003
92  * @tc.desc: function test play brefore Init
93  * @tc.type: FUNC
94  * @tc.require:
95  */
96 HWTEST_F(StreamIDManagerUnitTest, streamId_function_003, TestSize.Level2)
97 {
98     MEDIA_LOGI("streamId_function_003 before");
99     streamIDManager_->isStreamPlayingThreadPoolStarted_.store(false);
100     PlayParams playParams;
101     EXPECT_EQ(MSERR_INVALID_VAL, streamIDManager_->SetPlay(0, 0, playParams));
102     MEDIA_LOGI("streamId_function_003 after");
103 }
104 
105 /**
106  * @tc.name: streamId_function_004
107  * @tc.desc: function test QueueAndSortPlayingStreamID
108  * @tc.type: FUNC
109  * @tc.require:
110  */
111 HWTEST_F(StreamIDManagerUnitTest, streamId_function_004, TestSize.Level2)
112 {
113     MEDIA_LOGI("streamId_function_004 before");
114     streamIDManager_->isStreamPlayingThreadPoolStarted_.store(false);
115     streamIDManager_->playingStreamIDs_.emplace_back(BEGIN_NUM);
116     streamIDManager_->QueueAndSortPlayingStreamID(BEGIN_NUM);
117     EXPECT_EQ(1, streamIDManager_->playingStreamIDs_.size());
118     Format format;
119     std::deque<std::shared_ptr<AudioBufferEntry>> cacheData;
120     auto cacheBuffer = std::make_shared<CacheBuffer>(format, 0, 0, nullptr);
121     streamIDManager_->playingStreamIDs_.emplace_back(BEGIN_NUM);
122     streamIDManager_->cacheBuffers_.emplace(BEGIN_NUM, cacheBuffer);
123     streamIDManager_->QueueAndSortPlayingStreamID(BEGIN_NUM + 1);
124     EXPECT_EQ(2, streamIDManager_->playingStreamIDs_.size());
125     MEDIA_LOGI("streamId_function_004 after");
126 }
127 
128 /**
129  * @tc.name: streamId_function_005
130  * @tc.desc: function test QueueAndSortWillPlayStreamID
131  * @tc.type: FUNC
132  * @tc.require:
133  */
134 HWTEST_F(StreamIDManagerUnitTest, streamId_function_005, TestSize.Level2)
135 {
136     MEDIA_LOGI("streamId_function_005 before");
137     StreamIDManager::StreamIDAndPlayParamsInfo freshStreamIDAndPlayParamsInfo;
138     freshStreamIDAndPlayParamsInfo.streamID = BEGIN_NUM;
139     streamIDManager_->QueueAndSortWillPlayStreamID(freshStreamIDAndPlayParamsInfo);
140     EXPECT_EQ(1, streamIDManager_->willPlayStreamInfos_.size());
141 
142     Format format;
143     std::deque<std::shared_ptr<AudioBufferEntry>> cacheData;
144     auto cacheBuffer = std::make_shared<CacheBuffer>(format, 0, 0, nullptr);
145     streamIDManager_->cacheBuffers_.emplace(BEGIN_NUM, cacheBuffer);
146     StreamIDManager::StreamIDAndPlayParamsInfo freshStreamIDAndPlayParamsInfo1;
147     freshStreamIDAndPlayParamsInfo.streamID = BEGIN_NUM + 1;
148     streamIDManager_->QueueAndSortWillPlayStreamID(freshStreamIDAndPlayParamsInfo1);
149     EXPECT_EQ(2, streamIDManager_->willPlayStreamInfos_.size());
150 
151     cacheBuffer->priority_ = 0;
152     streamIDManager_->QueueAndSortWillPlayStreamID(freshStreamIDAndPlayParamsInfo);
153     EXPECT_EQ(2, streamIDManager_->willPlayStreamInfos_.size());
154     MEDIA_LOGI("streamId_function_005 after");
155 }
156 
157 /**
158  * @tc.name: streamId_function_006
159  * @tc.desc: function test DoPlay
160  * @tc.type: FUNC
161  * @tc.require:
162  */
163 HWTEST_F(StreamIDManagerUnitTest, streamId_function_006, TestSize.Level2)
164 {
165     MEDIA_LOGI("streamId_function_006 before");
166     Format format;
167     std::deque<std::shared_ptr<AudioBufferEntry>> cacheData;
168     auto cacheBuffer = std::make_shared<CacheBuffer>(format, 0, 0, nullptr);
169     streamIDManager_->cacheBuffers_.emplace(BEGIN_NUM, cacheBuffer);
170     streamIDManager_->playingStreamIDs_.emplace_back(BEGIN_NUM + 1);
171     EXPECT_EQ(MSERR_INVALID_VAL, streamIDManager_->DoPlay(BEGIN_NUM + 1));
172     streamIDManager_->playingStreamIDs_.emplace_back(BEGIN_NUM);
173     EXPECT_EQ(MSERR_INVALID_VAL, streamIDManager_->DoPlay(BEGIN_NUM + 1));
174     cacheBuffer->isRunning_.store(true);
175     EXPECT_EQ(MSERR_INVALID_VAL, streamIDManager_->DoPlay(BEGIN_NUM + 1));
176     cacheBuffer->isRunning_.store(false);
177     EXPECT_EQ(MSERR_INVALID_VAL, streamIDManager_->DoPlay(BEGIN_NUM + 1));
178     MEDIA_LOGI("streamId_function_006 after");
179 }
180 
181 /**
182  * @tc.name: streamId_function_007
183  * @tc.desc: function test ClearStreamIDInDeque
184  * @tc.type: FUNC
185  * @tc.require:
186  */
187 HWTEST_F(StreamIDManagerUnitTest, streamId_function_007, TestSize.Level2)
188 {
189     MEDIA_LOGI("streamId_function_007 before");
190     streamIDManager_->playingStreamIDs_.emplace_back(BEGIN_NUM);
191     streamIDManager_->playingStreamIDs_.emplace_back(STREAM_ID_BEGIN);
192     streamIDManager_->ClearStreamIDInDeque(STREAM_ID_BEGIN, SOUND_ID_BEGIN);
193     EXPECT_EQ(MSERR_OK, streamIDManager_->ClearStreamIDInDeque(STREAM_ID_BEGIN, SOUND_ID_BEGIN));
194 
195     StreamIDManager::StreamIDAndPlayParamsInfo streamIDAndPlayParamsInfo;
196     streamIDManager_->willPlayStreamInfos_.emplace_back(streamIDAndPlayParamsInfo);
197     StreamIDManager::StreamIDAndPlayParamsInfo streamIDAndPlayParamsInfo1;
198     streamIDManager_->willPlayStreamInfos_.emplace_back(streamIDAndPlayParamsInfo1);
199     EXPECT_EQ(MSERR_OK, streamIDManager_->ClearStreamIDInDeque(STREAM_ID_BEGIN, SOUND_ID_BEGIN));
200     MEDIA_LOGI("streamId_function_007 after");
201 }
202 
203 }  // namespace Media
204 }  // namespace OHOS