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 <iostream>
17 #include <gtest/gtest.h>
18 #include <gmock/gmock.h>
19 #include "audio_utils.h"
20 #include "common/hdi_adapter_info.h"
21 #include "manager/hdi_adapter_manager.h"
22
23 using namespace testing::ext;
24
25 namespace OHOS {
26 namespace AudioStandard {
27 class FastAudioCaptureSourceUnitTest : public testing::Test {
28 public:
29 static void SetUpTestCase();
30 static void TearDownTestCase();
SetUp()31 virtual void SetUp() {}
TearDown()32 virtual void TearDown() {}
33
34 void InitFastSource();
35 void DeInitFastSource();
36 void InitFastVoipSource();
37 void DeInitFastVoipSource();
38
39 protected:
40 static uint32_t fastId_;
41 static uint32_t fastVoipId_;
42 static std::shared_ptr<IAudioCaptureSource> fastSource_;
43 static std::shared_ptr<IAudioCaptureSource> fastVoipSource_;
44 static IAudioSourceAttr attr_;
45 };
46
47 uint32_t FastAudioCaptureSourceUnitTest::fastId_ = 0;
48 uint32_t FastAudioCaptureSourceUnitTest::fastVoipId_ = 0;
49 std::shared_ptr<IAudioCaptureSource> FastAudioCaptureSourceUnitTest::fastSource_ = nullptr;
50 std::shared_ptr<IAudioCaptureSource> FastAudioCaptureSourceUnitTest::fastVoipSource_ = nullptr;
51 IAudioSourceAttr FastAudioCaptureSourceUnitTest::attr_ = {};
52
SetUpTestCase()53 void FastAudioCaptureSourceUnitTest::SetUpTestCase()
54 {
55 HdiAdapterManager &manager = HdiAdapterManager::GetInstance();
56 fastId_ = manager.GetId(HDI_ID_BASE_CAPTURE, HDI_ID_TYPE_FAST, HDI_ID_INFO_DEFAULT, true);
57 fastVoipId_ = manager.GetId(HDI_ID_BASE_CAPTURE, HDI_ID_TYPE_FAST, HDI_ID_INFO_VOIP, true);
58 }
59
TearDownTestCase()60 void FastAudioCaptureSourceUnitTest::TearDownTestCase()
61 {
62 HdiAdapterManager::GetInstance().ReleaseId(fastId_);
63 HdiAdapterManager::GetInstance().ReleaseId(fastVoipId_);
64 }
65
InitFastSource()66 void FastAudioCaptureSourceUnitTest::InitFastSource()
67 {
68 fastSource_ = HdiAdapterManager::GetInstance().GetCaptureSource(fastId_, true);
69 if (fastSource_ == nullptr) {
70 return;
71 }
72 attr_.adapterName = "primary";
73 attr_.sampleRate = 48000; // 48000: sample rate
74 attr_.channel = 2; // 2: channel
75 attr_.format = SAMPLE_S16LE;
76 attr_.channelLayout = 3; // 3: channel layout
77 attr_.deviceType = DEVICE_TYPE_MIC;
78 attr_.openMicSpeaker = 1;
79 fastSource_->Init(attr_);
80 }
81
DeInitFastSource()82 void FastAudioCaptureSourceUnitTest::DeInitFastSource()
83 {
84 if (fastSource_ && fastSource_->IsInited()) {
85 fastSource_->DeInit();
86 }
87 fastSource_ = nullptr;
88 }
89
InitFastVoipSource()90 void FastAudioCaptureSourceUnitTest::InitFastVoipSource()
91 {
92 fastVoipSource_ = HdiAdapterManager::GetInstance().GetCaptureSource(fastVoipId_, true);
93 if (fastVoipSource_ == nullptr) {
94 return;
95 }
96 attr_.adapterName = "primary";
97 attr_.sampleRate = 48000; // 48000: sample rate
98 attr_.channel = 2; // 2: channel
99 attr_.format = SAMPLE_S16LE;
100 attr_.channelLayout = 3; // 3: channel layout
101 attr_.deviceType = DEVICE_TYPE_MIC;
102 attr_.openMicSpeaker = 1;
103 fastVoipSource_->Init(attr_);
104 }
105
DeInitFastVoipSource()106 void FastAudioCaptureSourceUnitTest::DeInitFastVoipSource()
107 {
108 if (fastVoipSource_ && fastVoipSource_->IsInited()) {
109 fastVoipSource_->DeInit();
110 }
111 fastVoipSource_ = nullptr;
112 }
113
114 /**
115 * @tc.name : Test FastSource API
116 * @tc.number : FastSourceUnitTest_001
117 * @tc.desc : Test fast source create
118 */
119 HWTEST_F(FastAudioCaptureSourceUnitTest, FastSourceUnitTest_001, TestSize.Level1)
120 {
121 InitFastSource();
122 EXPECT_TRUE(fastSource_ != nullptr);
123 DeInitFastSource();
124 }
125
126 /**
127 * @tc.name : Test FastSource API
128 * @tc.number : FastSourceUnitTest_002
129 * @tc.desc : Test fast source init
130 */
131 HWTEST_F(FastAudioCaptureSourceUnitTest, FastSourceUnitTest_002, TestSize.Level1)
132 {
133 InitFastSource();
134 EXPECT_TRUE(fastSource_ && fastSource_->IsInited());
135 DeInitFastSource();
136 }
137
138 /**
139 * @tc.name : Test FastSource API
140 * @tc.number : FastSourceUnitTest_003
141 * @tc.desc : Test fast source start, stop, resume, pause, flush, reset
142 */
143 HWTEST_F(FastAudioCaptureSourceUnitTest, FastSourceUnitTest_003, TestSize.Level1)
144 {
145 InitFastSource();
146 EXPECT_TRUE(fastSource_ && fastSource_->IsInited());
147 int32_t ret = fastSource_->Start();
148 EXPECT_EQ(ret, SUCCESS);
149 ret = fastSource_->Stop();
150 EXPECT_EQ(ret, SUCCESS);
151 ret = fastSource_->Start();
152 EXPECT_EQ(ret, SUCCESS);
153 ret = fastSource_->Resume();
154 EXPECT_EQ(ret, SUCCESS);
155 ret = fastSource_->Pause();
156 EXPECT_EQ(ret, ERR_OPERATION_FAILED);
157 ret = fastSource_->Flush();
158 EXPECT_EQ(ret, ERR_OPERATION_FAILED);
159 ret = fastSource_->Reset();
160 EXPECT_EQ(ret, ERR_OPERATION_FAILED);
161 ret = fastSource_->Stop();
162 EXPECT_EQ(ret, SUCCESS);
163 DeInitFastSource();
164 }
165
166 /**
167 * @tc.name : Test FastSource API
168 * @tc.number : FastSourceUnitTest_004
169 * @tc.desc : Test fast source set volume
170 */
171 HWTEST_F(FastAudioCaptureSourceUnitTest, FastSourceUnitTest_004, TestSize.Level1)
172 {
173 InitFastSource();
174 EXPECT_TRUE(fastSource_ && fastSource_->IsInited());
175 int32_t ret = fastSource_->SetVolume(1.0f, 1.0f);
176 EXPECT_EQ(ret, ERR_NOT_SUPPORTED);
177 DeInitFastSource();
178 }
179
180 /**
181 * @tc.name : Test FastVoipSource API
182 * @tc.number : FastVoipSourceUnitTest_001
183 * @tc.desc : Test fast voip source create
184 */
185 HWTEST_F(FastAudioCaptureSourceUnitTest, FastVoipSourceUnitTest_001, TestSize.Level1)
186 {
187 InitFastVoipSource();
188 EXPECT_TRUE(fastVoipSource_ != nullptr);
189 DeInitFastVoipSource();
190 }
191
192 /**
193 * @tc.name : Test FastVoipSource API
194 * @tc.number : FastVoipSourceUnitTest_002
195 * @tc.desc : Test fast voip source init
196 */
197 HWTEST_F(FastAudioCaptureSourceUnitTest, FastVoipSourceUnitTest_002, TestSize.Level1)
198 {
199 InitFastVoipSource();
200 EXPECT_TRUE(fastVoipSource_ && fastVoipSource_->IsInited());
201 DeInitFastVoipSource();
202 }
203
204 /**
205 * @tc.name : Test FastVoipSource API
206 * @tc.number : FastVoipSourceUnitTest_003
207 * @tc.desc : Test fast voip source start, stop, resume, pause, flush, reset
208 */
209 HWTEST_F(FastAudioCaptureSourceUnitTest, FastVoipSourceUnitTest_003, TestSize.Level1)
210 {
211 InitFastVoipSource();
212 EXPECT_TRUE(fastVoipSource_ && fastVoipSource_->IsInited());
213 int32_t ret = fastVoipSource_->Start();
214 EXPECT_EQ(ret, SUCCESS);
215 ret = fastVoipSource_->Stop();
216 EXPECT_EQ(ret, SUCCESS);
217 ret = fastVoipSource_->Start();
218 EXPECT_EQ(ret, SUCCESS);
219 ret = fastVoipSource_->Resume();
220 EXPECT_EQ(ret, SUCCESS);
221 ret = fastVoipSource_->Pause();
222 EXPECT_EQ(ret, ERR_OPERATION_FAILED);
223 ret = fastVoipSource_->Flush();
224 EXPECT_EQ(ret, ERR_OPERATION_FAILED);
225 ret = fastVoipSource_->Reset();
226 EXPECT_EQ(ret, ERR_OPERATION_FAILED);
227 DeInitFastSource();
228 }
229
230 /**
231 * @tc.name : Test FastVoipSource API
232 * @tc.number : FastVoipSourceUnitTest_004
233 * @tc.desc : Test fast voipsource set volume
234 */
235 HWTEST_F(FastAudioCaptureSourceUnitTest, FastVoipSourceUnitTest_004, TestSize.Level1)
236 {
237 InitFastVoipSource();
238 EXPECT_TRUE(fastVoipSource_ && fastVoipSource_->IsInited());
239 int32_t ret = fastVoipSource_->SetVolume(1.0f, 1.0f);
240 EXPECT_EQ(ret, ERR_NOT_SUPPORTED);
241 DeInitFastVoipSource();
242 }
243
244 } // namespace AudioStandard
245 } // namespace OHOS
246