• 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 "media_errors.h"
17 #include "parallel_stream_manager_test.h"
18 
19 using namespace testing;
20 using namespace testing::ext;
21 
22 namespace OHOS {
23 namespace Media {
24 
25 static const int32_t ID_TEST = 1;
26 static const int32_t NUM_TEST = 0;
27 static const int32_t DEFAULT_NUM = 10;
SetUpTestCase(void)28 void ParallelStreamManagerTest::SetUpTestCase(void) {}
TearDownTestCase(void)29 void ParallelStreamManagerTest::TearDownTestCase(void) {}
SetUp(void)30 void ParallelStreamManagerTest::SetUp(void)
31 {
32     int32_t maxStreams = 1;
33     AudioStandard::AudioRendererInfo audioRenderInfo;
34     testPtr_ = std::make_shared<ParallelStreamManager>(maxStreams, audioRenderInfo);
35 }
TearDown(void)36 void ParallelStreamManagerTest::TearDown(void)
37 {
38     testPtr_ = nullptr;
39 }
40 
41 /**
42  * @tc.name  : Test ~ParallelStreamManager
43  * @tc.number: ParallelStreamManagerTestDestruct_001
44  * @tc.desc  : Test frameWriteCallback_ != nullptr
45  */
46 HWTEST_F(ParallelStreamManagerTest, ParallelStreamManagerTestDestruct_001, TestSize.Level0)
47 {
48     ASSERT_NE(testPtr_, nullptr);
49     auto frameWriteCallback = std::make_shared<MockISoundPoolFrameWriteCallback>();
50     testPtr_->frameWriteCallback_ = frameWriteCallback;
51     EXPECT_NE(testPtr_->frameWriteCallback_, nullptr);
52 }
53 
54 /**
55  * @tc.name  : Test InitThreadPool
56  * @tc.number: ParallelStreamManagerInitThreadPool_001
57  * @tc.desc  : Test maxStreams_ < MIN_PLAY_STREAMS_NUMBER
58  */
59 HWTEST_F(ParallelStreamManagerTest, ParallelStreamManagerInitThreadPool_001, TestSize.Level0)
60 {
61     ASSERT_NE(testPtr_, nullptr);
62     testPtr_->maxStreams_ = NUM_TEST;
63     testPtr_->frameWriteCallback_ = nullptr;
64     auto ret = testPtr_->InitThreadPool();
65     EXPECT_EQ(ret, MSERR_OK);
66 }
67 
68 /**
69  * @tc.name  : Test RemoveFromWaitingDeque
70  * @tc.number: ParallelStreamManagerRemoveFromWaitingDeque_001
71  * @tc.desc  : Test it->first != streamId
72  */
73 HWTEST_F(ParallelStreamManagerTest, ParallelStreamManagerRemoveFromWaitingDeque_001, TestSize.Level0)
74 {
75     ASSERT_NE(testPtr_, nullptr);
76     int32_t idTest = ID_TEST;
77     std::shared_ptr<Stream> testPtr1 = nullptr;
78     Format trackFormat;
79     int32_t soundID = ID_TEST;
80     int32_t streamID = DEFAULT_NUM;
81     std::shared_ptr<ThreadPool> streamStopThreadPool;
82     std::shared_ptr<Stream> testPtr2 = std::make_shared<Stream>(trackFormat, soundID, streamID, streamStopThreadPool);
83     testPtr_->waitingStream_.push_back(std::make_pair(0, testPtr1));
84     testPtr_->waitingStream_.push_back(std::make_pair(idTest, testPtr2));
85     testPtr_->RemoveFromWaitingDeque(idTest);
86     EXPECT_EQ(testPtr_->waitingStream_.size(), 1);
87 }
88 
89 /**
90  * @tc.name  : Test AddToPlayingDeque
91  * @tc.number: ParallelStreamManagerAddToPlayingDeque_001
92  * @tc.desc  : Test stream->GetPriority() >= playingStream->GetPriority()
93  */
94 HWTEST_F(ParallelStreamManagerTest, ParallelStreamManagerAddToPlayingDeque_001, TestSize.Level0)
95 {
96     ASSERT_NE(testPtr_, nullptr);
97     int32_t streamID1 = ID_TEST;
98     Format trackFormat;
99     int32_t soundID = ID_TEST;
100     int32_t streamID2 = DEFAULT_NUM;
101     std::shared_ptr<ThreadPool> streamStopThreadPool;
102     std::shared_ptr<Stream> stream = std::make_shared<Stream>(trackFormat, soundID, streamID1, streamStopThreadPool);
103     std::shared_ptr<Stream> stream2 = std::make_shared<Stream>(trackFormat, soundID, streamID2, streamStopThreadPool);
104     testPtr_->playingStream_.push_back(std::make_pair(1, stream2));
105     testPtr_->AddToPlayingDeque(streamID1, stream);
106     EXPECT_EQ(testPtr_->playingStream_.size(), 2);
107 }
108 
109 /**
110  * @tc.name  : Test DoPlay
111  * @tc.number: ParallelStreamManagerDoPlay_001
112  * @tc.desc  : Test it->first == streamID
113  *             Test it->first != streamID
114  */
115 HWTEST_F(ParallelStreamManagerTest, ParallelStreamManagerDoPlay_001, TestSize.Level0)
116 {
117     ASSERT_NE(testPtr_, nullptr);
118     int32_t streamID1 = ID_TEST;
119     Format trackFormat;
120     int32_t soundID = ID_TEST;
121     int32_t streamID2 = DEFAULT_NUM;
122     std::shared_ptr<ThreadPool> streamStopThreadPool;
123     std::shared_ptr<Stream> stream1 = std::make_shared<Stream>(trackFormat, soundID, streamID2, streamStopThreadPool);
124     std::shared_ptr<Stream> stream2 = std::make_shared<Stream>(trackFormat, soundID, streamID2, streamStopThreadPool);
125     testPtr_->playingStream_.push_back(std::make_pair(0, stream1));
126     testPtr_->playingStream_.push_back(std::make_pair(1, stream2));
127     auto ret = testPtr_->DoPlay(streamID1);
128     EXPECT_EQ(ret, MSERR_INVALID_VAL);
129 }
130 
131 /**
132  * @tc.name  : Test UnloadStream
133  * @tc.number: ParallelStreamManagerUnloadStream_001
134  * @tc.desc  : Test it->second->GetSoundID() == soundId
135  *             Test it->second->GetSoundID() != soundId
136  *             Test it = playingStream_.begin(), it->second->GetSoundID() != soundId
137  */
138 HWTEST_F(ParallelStreamManagerTest, ParallelStreamManagerUnloadStream_001, TestSize.Level0)
139 {
140     ASSERT_NE(testPtr_, nullptr);
141     int32_t streamID1 = ID_TEST;
142     Format trackFormat;
143     int32_t soundID = ID_TEST;
144     int32_t streamID2 = DEFAULT_NUM;
145     std::shared_ptr<ThreadPool> streamStopThreadPool;
146     std::shared_ptr<Stream> stream1 = std::make_shared<Stream>(trackFormat, soundID, streamID2, streamStopThreadPool);
147     std::shared_ptr<Stream> stream2 = std::make_shared<Stream>(trackFormat, soundID, streamID2, streamStopThreadPool);
148     testPtr_->waitingStream_.push_back(std::make_pair(0, stream1));
149     testPtr_->waitingStream_.push_back(std::make_pair(1, stream2));
150 
151     std::shared_ptr<Stream> stream3 = std::make_shared<Stream>(trackFormat, soundID, streamID2, streamStopThreadPool);
152     std::shared_ptr<Stream> stream4 = std::make_shared<Stream>(trackFormat, soundID, streamID2, streamStopThreadPool);
153     testPtr_->playingStream_.push_back(std::make_pair(0, stream3));
154     testPtr_->playingStream_.push_back(std::make_pair(1, stream4));
155     auto ret = testPtr_->UnloadStream(streamID1);
156     EXPECT_EQ(ret, MSERR_OK);
157 }
158 
159 /**
160  * @tc.name  : Test ReorderStream
161  * @tc.number: ParallelStreamManagerReorderStream_001
162  * @tc.desc  : Test (left != nullptr && right != nullptr && left->GetPriority() < right->GetPriority()) == true
163  */
164 HWTEST_F(ParallelStreamManagerTest, ParallelStreamManagerReorderStream_001, TestSize.Level0)
165 {
166     ASSERT_NE(testPtr_, nullptr);
167     Format trackFormat;
168     int32_t soundID = ID_TEST;
169     int32_t streamID2 = DEFAULT_NUM;
170     std::shared_ptr<ThreadPool> streamStopThreadPool;
171     std::shared_ptr<Stream> stream1 = std::make_shared<Stream>(trackFormat, soundID, streamID2, streamStopThreadPool);
172     std::shared_ptr<Stream> stream2 = std::make_shared<Stream>(trackFormat, soundID, streamID2, streamStopThreadPool);
173     testPtr_->playingStream_.push_back(std::make_pair(0, stream1));
174     testPtr_->playingStream_.push_back(std::make_pair(1, stream2));
175     testPtr_->ReorderStream();
176     EXPECT_EQ(testPtr_->playingStream_.size(), 2);
177 }
178 
179 /**
180  * @tc.name  : Test SetFrameWriteCallback
181  * @tc.number: ParallelStreamManagerSetFrameWriteCallback_001
182  * @tc.desc  : Test all
183  */
184 HWTEST_F(ParallelStreamManagerTest, ParallelStreamManagerSetFrameWriteCallback_001, TestSize.Level0)
185 {
186     ASSERT_NE(testPtr_, nullptr);
187     auto frameWriteCallback = std::make_shared<MockISoundPoolFrameWriteCallback>();
188     std::shared_ptr<ISoundPoolFrameWriteCallback> callback = frameWriteCallback;
189     auto ret = testPtr_->SetFrameWriteCallback(callback);
190     EXPECT_EQ(ret, MSERR_OK);
191 }
192 } // namespace Media
193 } // namespace OHOS
194