1 /*
2 * Copyright (c) 2022 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 <gmock/gmock.h>
17 #include <gtest/gtest.h>
18 #include "audio_proxy_common_fun_test.h"
19
20 using namespace comfun;
21 using namespace testing::ext;
22 namespace {
23 class AudioProxyCaptureTest : public testing::Test {
24 public:
25 struct AudioManager *managerFuncs = nullptr;
26 struct AudioAdapterDescriptor *descs = nullptr;
27 struct AudioAdapterDescriptor *desc = nullptr;
28 struct AudioAdapter *adapter = nullptr;
29 struct AudioCapture *capture = nullptr;
30 struct AudioPort *audioPort = nullptr;
31 struct AudioDeviceDescriptor devDescCapture = {};
32 struct AudioSampleAttributes attrsCapture = {};
33 virtual void SetUp();
34 virtual void TearDown();
35 };
36
SetUp()37 void AudioProxyCaptureTest::SetUp()
38 {
39 managerFuncs = GetAudioManagerFuncs();
40 ASSERT_NE(managerFuncs, nullptr);
41 int32_t size = 0;
42 ASSERT_EQ(HDF_SUCCESS, managerFuncs->GetAllAdapters(managerFuncs, &descs, &size));
43
44 desc = &descs[0];
45 ASSERT_EQ(HDF_SUCCESS, managerFuncs->LoadAdapter(managerFuncs, desc, &adapter));
46 ASSERT_NE(adapter, nullptr);
47 ASSERT_EQ(HDF_SUCCESS, InitDevDescCapture(devDescCapture));
48 ASSERT_EQ(HDF_SUCCESS, InitAttrsCapture(attrsCapture));
49 ASSERT_EQ(HDF_SUCCESS, adapter->CreateCapture(adapter, &devDescCapture, &attrsCapture, &capture));
50 }
51
TearDown()52 void AudioProxyCaptureTest::TearDown()
53 {
54 if (adapter != nullptr) {
55 adapter->DestroyCapture(adapter, capture);
56 capture = nullptr;
57 }
58 if (managerFuncs != nullptr) {
59 managerFuncs->UnloadAdapter(managerFuncs, adapter);
60 adapter = nullptr;
61 managerFuncs->ReleaseAudioManagerObject(managerFuncs);
62 managerFuncs = nullptr;
63 }
64 }
65
66 HWTEST_F(AudioProxyCaptureTest, CaptureStart_001, TestSize.Level1)
67 {
68 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->control.Start(nullptr));
69 }
70
71 HWTEST_F(AudioProxyCaptureTest, CaptureStart_002, TestSize.Level1)
72 {
73 ASSERT_NE(capture, nullptr);
74 struct AudioHwCapture *hwCapture = (struct AudioHwCapture *)capture;
75 struct HdfRemoteService *proxyRemoteHandle = hwCapture->proxyRemoteHandle;
76 hwCapture->proxyRemoteHandle = nullptr;
77 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->control.Start((AudioHandle)capture));
78 hwCapture->proxyRemoteHandle = proxyRemoteHandle;
79 }
80
81 HWTEST_F(AudioProxyCaptureTest, CaptureStart_003, TestSize.Level1)
82 {
83 ASSERT_NE(capture, nullptr);
84 EXPECT_EQ(AUDIO_HAL_SUCCESS, capture->control.Start((AudioHandle)capture));
85 EXPECT_EQ(AUDIO_HAL_SUCCESS, capture->control.Stop((AudioHandle)capture));
86 }
87
88 HWTEST_F(AudioProxyCaptureTest, CaptureStop_001, TestSize.Level1)
89 {
90 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->control.Stop(nullptr));
91 }
92
93 HWTEST_F(AudioProxyCaptureTest, CaptureStop_002, TestSize.Level1)
94 {
95 ASSERT_NE(capture, nullptr);
96 struct AudioHwCapture *hwCapture = (struct AudioHwCapture *)capture;
97 struct HdfRemoteService *proxyRemoteHandle = hwCapture->proxyRemoteHandle;
98 hwCapture->proxyRemoteHandle = nullptr;
99 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->control.Stop((AudioHandle)capture));
100 hwCapture->proxyRemoteHandle = proxyRemoteHandle;
101 }
102
103 HWTEST_F(AudioProxyCaptureTest, CaptureGetFrameSize_001, TestSize.Level1)
104 {
105 ASSERT_NE(capture, nullptr);
106 uint64_t size = 0;
107 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->attr.GetFrameSize(nullptr, &size));
108 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->attr.GetFrameSize((AudioHandle)capture, nullptr));
109 }
110
111 HWTEST_F(AudioProxyCaptureTest, CaptureGetFrameSize_002, TestSize.Level1)
112 {
113 ASSERT_NE(capture, nullptr);
114 struct AudioHwCapture *hwCapture = (struct AudioHwCapture *)capture;
115 struct HdfRemoteService *proxyRemoteHandle = hwCapture->proxyRemoteHandle;
116 hwCapture->proxyRemoteHandle = nullptr;
117 uint64_t size = 0;
118 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->attr.GetFrameSize((AudioHandle)capture, &size));
119 hwCapture->proxyRemoteHandle = proxyRemoteHandle;
120 }
121
122 HWTEST_F(AudioProxyCaptureTest, CaptureGetFrameSize_003, TestSize.Level1)
123 {
124 ASSERT_NE(capture, nullptr);
125 uint64_t size = 0;
126 EXPECT_EQ(AUDIO_HAL_SUCCESS, capture->attr.GetFrameSize((AudioHandle)capture, &size));
127 EXPECT_NE(size, 0);
128 }
129
130 HWTEST_F(AudioProxyCaptureTest, CaptureGetFrameCount_001, TestSize.Level1)
131 {
132 ASSERT_NE(capture, nullptr);
133 uint64_t count = 0;
134 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->attr.GetFrameCount(nullptr, &count));
135 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->attr.GetFrameCount((AudioHandle)capture, nullptr));
136 }
137
138 HWTEST_F(AudioProxyCaptureTest, CaptureGetFrameCount_002, TestSize.Level1)
139 {
140 ASSERT_NE(capture, nullptr);
141 struct AudioHwCapture *hwCapture = (struct AudioHwCapture *)capture;
142 struct HdfRemoteService *proxyRemoteHandle = hwCapture->proxyRemoteHandle;
143 hwCapture->proxyRemoteHandle = nullptr;
144 uint64_t count = 0;
145 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->attr.GetFrameCount((AudioHandle)capture, &count));
146 hwCapture->proxyRemoteHandle = proxyRemoteHandle;
147 }
148
149 HWTEST_F(AudioProxyCaptureTest, CaptureGetCurrentChannelId_001, TestSize.Level1)
150 {
151 uint32_t channelId = 0;
152 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->attr.GetCurrentChannelId(nullptr, &channelId));
153 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->attr.GetCurrentChannelId((AudioHandle)capture, nullptr));
154 }
155
156 HWTEST_F(AudioProxyCaptureTest, CaptureGetCurrentChannelId_002, TestSize.Level1)
157 {
158 ASSERT_NE(capture, nullptr);
159 struct AudioHwCapture *hwCapture = (struct AudioHwCapture *)capture;
160 struct HdfRemoteService *proxyRemoteHandle = hwCapture->proxyRemoteHandle;
161 hwCapture->proxyRemoteHandle = nullptr;
162 uint32_t channelId = 0;
163 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->attr.GetCurrentChannelId((AudioHandle)capture, &channelId));
164 hwCapture->proxyRemoteHandle = proxyRemoteHandle;
165 }
166
167 HWTEST_F(AudioProxyCaptureTest, CaptureCheckSceneCapability_001, TestSize.Level1)
168 {
169 ASSERT_NE(capture, nullptr);
170 struct AudioSceneDescriptor scene;
171 scene.scene.id = 0;
172 scene.desc.pins = PIN_IN_MIC;
173 bool supported = false;
174 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->scene.CheckSceneCapability(nullptr, &scene, &supported));
175 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->scene.CheckSceneCapability((AudioHandle)capture, nullptr,
176 &supported));
177 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->scene.CheckSceneCapability((AudioHandle)capture, &scene,
178 nullptr));
179 }
180
181 HWTEST_F(AudioProxyCaptureTest, CaptureCheckSceneCapability_002, TestSize.Level1)
182 {
183 ASSERT_NE(capture, nullptr);
184 struct AudioHwCapture *hwCapture = (struct AudioHwCapture *)capture;
185 struct HdfRemoteService *proxyRemoteHandle = hwCapture->proxyRemoteHandle;
186 hwCapture->proxyRemoteHandle = nullptr;
187 struct AudioSceneDescriptor scene;
188 scene.scene.id = 0;
189 scene.desc.pins = PIN_IN_MIC;
190 bool supported = false;
191 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->scene.CheckSceneCapability((AudioHandle)capture, &scene,
192 &supported));
193 hwCapture->proxyRemoteHandle = proxyRemoteHandle;
194 }
195
196 HWTEST_F(AudioProxyCaptureTest, CaptureSelectScene_001, TestSize.Level1)
197 {
198 ASSERT_NE(capture, nullptr);
199 struct AudioSceneDescriptor scene;
200 scene.scene.id = 0;
201 scene.desc.pins = PIN_IN_MIC;
202 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->scene.SelectScene(nullptr, &scene));
203 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->scene.SelectScene((AudioHandle)capture, nullptr));
204 }
205
206 HWTEST_F(AudioProxyCaptureTest, CaptureSelectScene_002, TestSize.Level1)
207 {
208 ASSERT_NE(capture, nullptr);
209 struct AudioHwCapture *hwCapture = (struct AudioHwCapture *)capture;
210 struct HdfRemoteService *proxyRemoteHandle = hwCapture->proxyRemoteHandle;
211 hwCapture->proxyRemoteHandle = nullptr;
212 struct AudioSceneDescriptor scene;
213 scene.scene.id = 0;
214 scene.desc.pins = PIN_IN_MIC;
215 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->scene.SelectScene((AudioHandle)capture, &scene));
216 hwCapture->proxyRemoteHandle = proxyRemoteHandle;
217 }
218
219 HWTEST_F(AudioProxyCaptureTest, CaptureSelectScene_003, TestSize.Level1)
220 {
221 ASSERT_NE(capture, nullptr);
222 struct AudioSceneDescriptor *scene = new AudioSceneDescriptor;
223 scene->scene.id = 0; // 0 is Media
224 scene->desc.pins = PIN_IN_HS_MIC;
225 EXPECT_EQ(AUDIO_HAL_SUCCESS, capture->scene.SelectScene((AudioHandle)capture, scene));
226 scene->desc.pins = PIN_IN_MIC;
227 EXPECT_EQ(AUDIO_HAL_SUCCESS, capture->scene.SelectScene((AudioHandle)capture, scene));
228 delete scene;
229 scene = nullptr;
230 }
231
232 HWTEST_F(AudioProxyCaptureTest, CaptureGetCapturePosition_001, TestSize.Level1)
233 {
234 ASSERT_NE(capture, nullptr);
235 uint64_t frames;
236 struct AudioTimeStamp time;
237 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->GetCapturePosition(nullptr, &frames, &time));
238 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->GetCapturePosition(capture, nullptr, &time));
239 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->GetCapturePosition(capture, &frames, nullptr));
240 }
241
242 HWTEST_F(AudioProxyCaptureTest, CaptureGetCapturePosition_002, TestSize.Level1)
243 {
244 ASSERT_NE(capture, nullptr);
245 struct AudioHwCapture *hwCapture = (struct AudioHwCapture *)capture;
246 struct HdfRemoteService *proxyRemoteHandle = hwCapture->proxyRemoteHandle;
247 hwCapture->proxyRemoteHandle = nullptr;
248 uint64_t frames;
249 struct AudioTimeStamp time;
250 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->GetCapturePosition(capture, &frames, &time));
251 hwCapture->proxyRemoteHandle = proxyRemoteHandle;
252 }
253
254 HWTEST_F(AudioProxyCaptureTest, CaptureSetExtraParams_001, TestSize.Level1)
255 {
256 ASSERT_NE(capture, nullptr);
257 char keyValueList[AUDIO_CAPTURE_BUF_TEST];
258 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->attr.SetExtraParams(nullptr, keyValueList));
259 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->attr.SetExtraParams((AudioHandle)capture, nullptr));
260 }
261
262 HWTEST_F(AudioProxyCaptureTest, CaptureSetExtraParams_002, TestSize.Level1)
263 {
264 ASSERT_NE(capture, nullptr);
265 struct AudioHwCapture *hwCapture = (struct AudioHwCapture *)capture;
266 struct HdfRemoteService *proxyRemoteHandle = hwCapture->proxyRemoteHandle;
267 hwCapture->proxyRemoteHandle = nullptr;
268 char keyValueList[AUDIO_CAPTURE_BUF_TEST];
269 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->attr.SetExtraParams((AudioHandle)capture, keyValueList));
270 hwCapture->proxyRemoteHandle = proxyRemoteHandle;
271 }
272
273 HWTEST_F(AudioProxyCaptureTest, CaptureGetExtraParams_001, TestSize.Level1)
274 {
275 ASSERT_NE(capture, nullptr);
276 char keyValueList[AUDIO_CAPTURE_BUF_TEST];
277 int32_t listLenth = AUDIO_CAPTURE_BUF_TEST;
278 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->attr.GetExtraParams(nullptr, keyValueList, listLenth));
279 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->attr.GetExtraParams((AudioHandle)capture, nullptr, listLenth));
280 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->attr.GetExtraParams((AudioHandle)capture, keyValueList, 0));
281 }
282
283 HWTEST_F(AudioProxyCaptureTest, CaptureGetExtraParams_002, TestSize.Level1)
284 {
285 ASSERT_NE(capture, nullptr);
286 struct AudioHwCapture *hwCapture = (struct AudioHwCapture *)capture;
287 struct HdfRemoteService *proxyRemoteHandle = hwCapture->proxyRemoteHandle;
288 hwCapture->proxyRemoteHandle = nullptr;
289 char keyValueList[AUDIO_CAPTURE_BUF_TEST];
290 int32_t listLenth = AUDIO_CAPTURE_BUF_TEST;
291 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->attr.GetExtraParams((AudioHandle)capture, keyValueList,
292 listLenth));
293 hwCapture->proxyRemoteHandle = proxyRemoteHandle;
294 }
295
296 HWTEST_F(AudioProxyCaptureTest, CaptureTurnStandbyMode_001, TestSize.Level1)
297 {
298 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->control.TurnStandbyMode(nullptr));
299 }
300
301 HWTEST_F(AudioProxyCaptureTest, CaptureTurnStandbyMode_002, TestSize.Level1)
302 {
303 ASSERT_NE(capture, nullptr);
304 struct AudioHwCapture *hwCapture = (struct AudioHwCapture *)capture;
305 struct HdfRemoteService *proxyRemoteHandle = hwCapture->proxyRemoteHandle;
306 hwCapture->proxyRemoteHandle = nullptr;
307 EXPECT_EQ(AUDIO_HAL_ERR_INVALID_PARAM, capture->control.TurnStandbyMode((AudioHandle)capture));
308 hwCapture->proxyRemoteHandle = proxyRemoteHandle;
309 }
310 }
311