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 "audio_pipe_manager_unit_test.h"
17 #include "audio_policy_utils.h"
18 #include "audio_stream_descriptor.h"
19
20 using namespace testing::ext;
21
22 namespace OHOS {
23 namespace AudioStandard {
24
25 static std::shared_ptr<AudioPipeManager> sPipeManager_ = nullptr;
26
SetUpTestCase(void)27 void AudioPipeManagerExtendedUnitTest::SetUpTestCase(void) {}
TearDownTestCase(void)28 void AudioPipeManagerExtendedUnitTest::TearDownTestCase(void) {}
SetUp(void)29 void AudioPipeManagerExtendedUnitTest::SetUp(void)
30 {
31 sPipeManager_ = std::make_shared<AudioPipeManager>();
32 }
TearDown(void)33 void AudioPipeManagerExtendedUnitTest::TearDown(void)
34 {
35 sPipeManager_ = nullptr;
36 }
37
38 /**
39 * @tc.name: AudioPipeManager_001
40 * @tc.desc: Test AudioPipeManager RemoveAudioPipeInfo.
41 * @tc.type: FUNC
42 */
43 HWTEST_F(AudioPipeManagerExtendedUnitTest, AudioPipeManager_001, TestSize.Level4)
44 {
45 auto pipeInfo1 = std::make_shared<AudioPipeInfo>();
46 pipeInfo1->routeFlag_ = AUDIO_INPUT_FLAG_NORMAL;
47 pipeInfo1->id_ = 1;
48 auto pipeInfo2 = std::make_shared<AudioPipeInfo>();
49 pipeInfo2->routeFlag_ = AUDIO_INPUT_FLAG_FAST;
50 pipeInfo2->id_ = 2;
51 sPipeManager_->AddAudioPipeInfo(pipeInfo1);
52 sPipeManager_->RemoveAudioPipeInfo(pipeInfo2);
53 EXPECT_FALSE(sPipeManager_->GetPipeList().empty());
54 }
55
56 /**
57 * @tc.name: AudioPipeManager_002
58 * @tc.desc: Test AudioPipeManager GetUnusedRecordPipe.
59 * @tc.type: FUNC
60 */
61 HWTEST_F(AudioPipeManagerExtendedUnitTest, AudioPipeManager_002, TestSize.Level4)
62 {
63 auto pipeInfo1 = std::make_shared<AudioPipeInfo>();
64 pipeInfo1->pipeRole_ = PIPE_ROLE_INPUT;
65 pipeInfo1->adapterName_ = PRIMARY_CLASS;
66 pipeInfo1->routeFlag_ = AUDIO_INPUT_FLAG_NORMAL;
67 sPipeManager_->AddAudioPipeInfo(pipeInfo1);
68 auto pipeInfo2 = std::make_shared<AudioPipeInfo>(pipeInfo1);
69 pipeInfo2->adapterName_ = "test_adapter";
70 sPipeManager_->AddAudioPipeInfo(pipeInfo2);
71 auto pipeInfo3 = std::make_shared<AudioPipeInfo>(pipeInfo2);
72 pipeInfo3->streamDescriptors_.emplace_back(std::make_shared<AudioStreamDescriptor>());
73 sPipeManager_->AddAudioPipeInfo(pipeInfo3);
74 auto pipeInfo4 = std::make_shared<AudioPipeInfo>(pipeInfo1);
75 pipeInfo4->streamDescriptors_.emplace_back(std::make_shared<AudioStreamDescriptor>());
76 sPipeManager_->AddAudioPipeInfo(pipeInfo4);
77
78 auto pipeInfo5 = std::make_shared<AudioPipeInfo>();
79 pipeInfo5->pipeRole_ = PIPE_ROLE_NONE;
80 pipeInfo5->streamDescriptors_.emplace_back(std::make_shared<AudioStreamDescriptor>());
81 pipeInfo5->adapterName_ = "test_adapter";
82 sPipeManager_->AddAudioPipeInfo(pipeInfo5);
83 auto pipeInfo6 = std::make_shared<AudioPipeInfo>(pipeInfo5);
84 pipeInfo6->streamDescriptors_.clear();
85 sPipeManager_->AddAudioPipeInfo(pipeInfo6);
86 auto pipeInfo7 = std::make_shared<AudioPipeInfo>(pipeInfo5);
87 pipeInfo7->adapterName_ = PRIMARY_CLASS;
88 pipeInfo7->routeFlag_ = AUDIO_INPUT_FLAG_NORMAL;
89 sPipeManager_->AddAudioPipeInfo(pipeInfo7);
90 auto pipeInfo8 = std::make_shared<AudioPipeInfo>(pipeInfo7);
91 pipeInfo8->streamDescriptors_.clear();
92 sPipeManager_->AddAudioPipeInfo(pipeInfo8);
93
94 EXPECT_EQ(sPipeManager_->GetUnusedRecordPipe().size(), 1);
95 }
96
97 /**
98 * @tc.name: AudioPipeManager_003
99 * @tc.desc: Test AudioPipeManager IsNormalRecordPipe.
100 * @tc.type: FUNC
101 */
102 HWTEST_F(AudioPipeManagerExtendedUnitTest, AudioPipeManager_003, TestSize.Level4)
103 {
104 auto pipeInfo = std::make_shared<AudioPipeInfo>();
105 pipeInfo->adapterName_ = PRIMARY_CLASS;
106 pipeInfo->routeFlag_ = AUDIO_INPUT_FLAG_NORMAL;
107 EXPECT_TRUE(sPipeManager_->IsNormalRecordPipe(pipeInfo));
108
109 pipeInfo->adapterName_ = USB_CLASS;
110 EXPECT_TRUE(sPipeManager_->IsNormalRecordPipe(pipeInfo));
111
112 pipeInfo->routeFlag_ = AUDIO_INPUT_FLAG_FAST;
113 EXPECT_FALSE(sPipeManager_->IsNormalRecordPipe(pipeInfo));
114 }
115
116 /**
117 * @tc.name: AudioPipeManager_004
118 * @tc.desc: Test AudioPipeManager GetStreamDescsByIoHandle.
119 * @tc.type: FUNC
120 */
121 HWTEST_F(AudioPipeManagerExtendedUnitTest, AudioPipeManager_004, TestSize.Level4)
122 {
123 AudioIOHandle id = 1;
124 auto pipeInfo = std::make_shared<AudioPipeInfo>();
125 pipeInfo->id_ = id;
126 pipeInfo->streamDescriptors_.emplace_back(std::make_shared<AudioStreamDescriptor>());
127 sPipeManager_->AddAudioPipeInfo(pipeInfo);
128 EXPECT_FALSE(sPipeManager_->GetStreamDescsByIoHandle(id).empty());
129 }
130
131 /**
132 * @tc.name: AudioPipeManager_005
133 * @tc.desc: Test AudioPipeManager Dump.
134 * @tc.type: FUNC
135 */
136 HWTEST_F(AudioPipeManagerExtendedUnitTest, AudioPipeManager_005, TestSize.Level4)
137 {
138 auto pipeInfo = std::make_shared<AudioPipeInfo>();
139 pipeInfo->adapterName_ = "test_adapterName";
140 pipeInfo->moduleInfo_.rate = "test_rate";
141 pipeInfo->moduleInfo_.channels = "test_channels";
142 pipeInfo->moduleInfo_.format = "test_format";
143 pipeInfo->moduleInfo_.bufferSize = "test_bufferSize";
144 pipeInfo->moduleInfo_.renderInIdleState = "test_renderInIdleState";
145 pipeInfo->moduleInfo_.sourceType = "test_sourceType";
146 sPipeManager_->AddAudioPipeInfo(pipeInfo);
147 sPipeManager_->curPipeList_.push_back(nullptr);
148 EXPECT_EQ(sPipeManager_->curPipeList_.size(), 2);
149 std::string dumpString = "test_dumpString";
150 EXPECT_NO_THROW(sPipeManager_->Dump(dumpString));
151 }
152
153 /**
154 * @tc.name: AudioPipeManager_006
155 * @tc.desc: Test AudioPipeManager GetModemCommunicationStreamDescById.
156 * @tc.type: FUNC
157 */
158 HWTEST_F(AudioPipeManagerExtendedUnitTest, AudioPipeManager_006, TestSize.Level4)
159 {
160 uint32_t sessionId = 1;
161 std::shared_ptr<AudioStreamDescriptor> streamDesc = std::make_shared<AudioStreamDescriptor>();
162 sPipeManager_->AddModemCommunicationId(sessionId, streamDesc);
163 EXPECT_TRUE(sPipeManager_->GetModemCommunicationStreamDescById(sessionId) != nullptr);
164
165 sessionId = 2;
166 EXPECT_TRUE(sPipeManager_->GetModemCommunicationStreamDescById(sessionId) == nullptr);
167 }
168
169 /**
170 * @tc.name: AudioPipeManager_007
171 * @tc.desc: Test AudioPipeManager GetNormalSourceInfo.
172 * @tc.type: FUNC
173 */
174 HWTEST_F(AudioPipeManagerExtendedUnitTest, AudioPipeManager_007, TestSize.Level4)
175 {
176 auto pipeInfo = std::make_shared<AudioPipeInfo>();
177 pipeInfo->moduleInfo_.name = PRIMARY_CLASS;
178 pipeInfo->routeFlag_ = AUDIO_INPUT_FLAG_NORMAL;
179 bool isEcFeatureEnable = true;
180 EXPECT_TRUE(sPipeManager_->GetNormalSourceInfo(isEcFeatureEnable) == nullptr);
181 isEcFeatureEnable = false;
182 EXPECT_TRUE(sPipeManager_->GetNormalSourceInfo(isEcFeatureEnable) == nullptr);
183 }
184
185 /**
186 * @tc.name: AudioPipeManager_008
187 * @tc.desc: Test AudioPipeManager GetStreamIdsByUid.
188 * @tc.type: FUNC
189 */
190 HWTEST_F(AudioPipeManagerExtendedUnitTest, AudioPipeManager_008, TestSize.Level4)
191 {
192 auto pipeInfo = std::make_shared<AudioPipeInfo>();
193 pipeInfo->moduleInfo_.name = PRIMARY_CLASS;
194 pipeInfo->routeFlag_ = AUDIO_INPUT_FLAG_NORMAL;
195 auto streamDescriptor1 = std::make_shared<AudioStreamDescriptor>();
196 streamDescriptor1->callerUid_ = 1;
197 auto streamDescriptor2 = std::make_shared<AudioStreamDescriptor>();
198 streamDescriptor2->callerUid_ = 2;
199 pipeInfo->streamDescriptors_.push_back(streamDescriptor1);
200 pipeInfo->streamDescriptors_.push_back(streamDescriptor2);
201 sPipeManager_->AddAudioPipeInfo(pipeInfo);
202
203 uint32_t uid = 1;
204 uint32_t routeFlagMask = AUDIO_INPUT_FLAG_NORMAL;
205 EXPECT_EQ(sPipeManager_->GetStreamIdsByUid(uid).size(), 1);
206 }
207
208 /**
209 * @tc.name: AudioPipeManager_009
210 * @tc.desc: Test AudioPipeManager UpdateOutputStreamDescsByIoHandle.
211 * @tc.type: FUNC
212 */
213 HWTEST_F(AudioPipeManagerExtendedUnitTest, AudioPipeManager_009, TestSize.Level4)
214 {
215 auto pipeInfo = std::make_shared<AudioPipeInfo>();
216 pipeInfo->id_ = 1;
217 sPipeManager_->AddAudioPipeInfo(pipeInfo);
218 AudioIOHandle id = 2;
219 std::vector<std::shared_ptr<AudioStreamDescriptor>> descs;
220 descs.push_back(std::make_shared<AudioStreamDescriptor>());
221 sPipeManager_->UpdateOutputStreamDescsByIoHandle(id, descs);
222 EXPECT_TRUE(pipeInfo->streamDescriptors_.empty());
223 }
224 } // namespace AudioStandard
225 } // namespace OHOS