1 /*
2  * Copyright (c) 2023 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 <gtest/gtest.h>
17 #include "hdf_base.h"
18 #include "hdf_log.h"
19 #include "v1_0/effect_types.h"
20 #include "v1_0/ieffect_control.h"
21 #include "v1_0/ieffect_model.h"
22 #include "effect_common.h"
23 #include "osal_mem.h"
24 
25 using namespace std;
26 using namespace testing::ext;
27 constexpr bool IS_DIRECTLY_CALL = false;
28 constexpr uint32_t MAX_DESCRIPTOR_NUM = 20;
29 
30 namespace {
31 class EffectModelTest : public testing::Test {
32 public:
33     struct IEffectModel *model_ = nullptr;
34     struct ControllerId contollerId_;
35     char *libName_ = nullptr;
36     char *effectId_ = nullptr;
37     virtual void SetUp();
38     virtual void TearDown();
39 };
40 
SetUp()41 void EffectModelTest::SetUp()
42 {
43     // input testcase setup step,setup invoked before each testcases
44     libName_ = strdup("libmock_effect_lib");
45     ASSERT_NE(nullptr, libName_);
46     effectId_ = strdup("aaaabbbb-8888-9999-6666-aabbccdd9966ff");
47     ASSERT_NE(nullptr, effectId_);
48     model_ = IEffectModelGet(IS_DIRECTLY_CALL);
49     ASSERT_NE(nullptr, model_);
50 }
51 
TearDown()52 void EffectModelTest::TearDown()
53 {
54     // input testcase teardown step,teardown invoked after each testcases
55     if (libName_ != nullptr) {
56         free(libName_);
57         libName_ = nullptr;
58     }
59 
60     if (effectId_ != nullptr) {
61         free(effectId_);
62         effectId_ = nullptr;
63     }
64 
65     if (model_ != nullptr) {
66         IEffectModelRelease(model_, IS_DIRECTLY_CALL);
67     }
68 }
69 
70 /**
71  * @tc.name: HdfAudioIsSupplyEffectLibs001
72  * @tc.desc: Verify the EffectModelIsSupplyEffectLibs function when the input parameter is invalid.
73  * @tc.type: FUNC
74  * @tc.require: I6I658
75  */
76 HWTEST_F(EffectModelTest, HdfAudioIsSupplyEffectLibs001, TestSize.Level1)
77 {
78     bool isSupport = false;
79     EXPECT_EQ(HDF_ERR_INVALID_OBJECT, model_->IsSupplyEffectLibs(nullptr, &isSupport));
80     EXPECT_EQ(HDF_ERR_INVALID_PARAM, model_->IsSupplyEffectLibs(model_, nullptr));
81 }
82 
83 /**
84  * @tc.name: HdfAudioIsSupplyEffectLibs002
85  * @tc.desc: Verify the EffectModelIsSupplyEffectLibs function.
86  * @tc.type: FUNC
87  * @tc.require: I6I658
88  */
89 HWTEST_F(EffectModelTest, HdfAudioIsSupplyEffectLibs002, TestSize.Level1)
90 {
91     bool isSupport = false;
92     int32_t ret = model_->IsSupplyEffectLibs(model_, &isSupport);
93     EXPECT_EQ(ret, HDF_SUCCESS);
94 }
95 
96 /**
97  * @tc.name: HdfAudioGetAllEffectDescriptors001
98  * @tc.desc: Verify the EffectModelGetAllEffectDescriptors function when the input parameter is invalid.
99  * @tc.type: FUNC
100  * @tc.require: I6I658
101  */
102 HWTEST_F(EffectModelTest, HdfAudioGetAllEffectDescriptors001, TestSize.Level1)
103 {
104     uint32_t descsLen = MAX_DESCRIPTOR_NUM;
105     struct EffectControllerDescriptor descs[MAX_DESCRIPTOR_NUM];
106 
107     EXPECT_EQ(HDF_ERR_INVALID_OBJECT, model_->GetAllEffectDescriptors(nullptr, descs, &descsLen));
108     EXPECT_EQ(HDF_ERR_INVALID_PARAM, model_->GetAllEffectDescriptors(model_, nullptr, &descsLen));
109     EXPECT_EQ(HDF_ERR_INVALID_PARAM, model_->GetAllEffectDescriptors(model_, descs, nullptr));
110 }
111 
112 /**
113  * @tc.name: HdfAudioGetAllEffectDescriptors002
114  * @tc.desc: Verify the EffectModelGetAllEffectDescriptors function.
115  * @tc.type: FUNC
116  * @tc.require: I6I658
117  */
118 HWTEST_F(EffectModelTest, HdfAudioGetAllEffectDescriptors002, TestSize.Level1)
119 {
120     uint32_t descsLen = MAX_DESCRIPTOR_NUM;
121     struct EffectControllerDescriptor descs[MAX_DESCRIPTOR_NUM];
122 
123     int32_t ret = model_->GetAllEffectDescriptors(model_, descs, &descsLen);
124     ASSERT_EQ(ret, HDF_SUCCESS);
125     EXPECT_GE(MAX_DESCRIPTOR_NUM, descsLen);
126 }
127 
128 /**
129  * @tc.name: HdfAudioGetAllEffectDescriptors003
130  * @tc.desc: Verify the descs of EffectModelGetAllEffectDescriptors function.
131  * @tc.type: FUNC
132  * @tc.require: I6I658
133  */
134 HWTEST_F(EffectModelTest, HdfAudioGetAllEffectDescriptors003, TestSize.Level1)
135 {
136     uint32_t descsLen = MAX_DESCRIPTOR_NUM;
137     struct EffectControllerDescriptor descs[MAX_DESCRIPTOR_NUM];
138 
139     int32_t ret = model_->GetAllEffectDescriptors(model_, descs, &descsLen);
140     ASSERT_EQ(ret, HDF_SUCCESS);
141     EXPECT_GE(MAX_DESCRIPTOR_NUM, descsLen);
142 
143     for (uint32_t i = 0; i < descsLen; i++) {
144         EXPECT_NE(nullptr, descs[i].effectId);
145     }
146 
147     OHOS::Audio::EffectControllerReleaseDescs(descs, &descsLen);
148 }
149 
150 /**
151  * @tc.name: HdfAudioCreateEffectController001
152  * @tc.desc: Verify the CreateEffectController function when the input parameter is invalid.
153  * @tc.type: FUNC
154  * @tc.require: I71E1I
155  */
156 HWTEST_F(EffectModelTest, HdfAudioCreateEffectController001, TestSize.Level1)
157 {
158     struct EffectInfo info = {
159         .libName = libName_,
160         .effectId = effectId_,
161         .ioDirection = 1,
162     };
163 
164     struct IEffectControl *contoller = NULL;
165     EXPECT_EQ(HDF_ERR_INVALID_OBJECT, model_->CreateEffectController(nullptr, &info, &contoller, &contollerId_));
166     EXPECT_EQ(HDF_ERR_INVALID_PARAM, model_->CreateEffectController(model_, nullptr, &contoller, &contollerId_));
167     EXPECT_EQ(HDF_ERR_INVALID_PARAM, model_->CreateEffectController(model_, &info, &contoller, nullptr));
168 }
169 
170 /**
171  * @tc.name: HdfAudioDestroyEffectController001
172  * @tc.desc: Verify the DestroyEffectController function when the input parameter is invalid.
173  * @tc.type: FUNC
174  * @tc.require: I71E1I
175  */
176 HWTEST_F(EffectModelTest, HdfAudioDestroyEffectController001, TestSize.Level1)
177 {
178     struct EffectInfo info = {
179         .libName = libName_,
180         .effectId = effectId_,
181         .ioDirection = 1,
182     };
183 
184     struct IEffectControl *contoller = NULL;
185     ASSERT_EQ(HDF_SUCCESS, model_->CreateEffectController(model_, &info, &contoller, &contollerId_));
186     ASSERT_NE(contoller, nullptr);
187 
188     EXPECT_EQ(HDF_ERR_INVALID_OBJECT, model_->DestroyEffectController(nullptr, &contollerId_));
189     EXPECT_EQ(HDF_ERR_INVALID_PARAM, model_->DestroyEffectController(model_, nullptr));
190 }
191 
192 /**
193  * @tc.name: HdfAudioCreateDestroyController001
194  * @tc.desc: Verify the EffectModelCreateEffectController and EffectModelDestroyEffectController function.
195  * @tc.type: FUNC
196  * @tc.require: I6I658
197  */
198 HWTEST_F(EffectModelTest, HdfAudioCreateDestroyController001, TestSize.Level1)
199 {
200     struct EffectInfo info = {
201         .libName = libName_,
202         .effectId = effectId_,
203         .ioDirection = 1,
204     };
205 
206     struct IEffectControl *contoller = NULL;
207     int32_t ret = model_->CreateEffectController(model_, &info, &contoller, &contollerId_);
208     if (ret == HDF_SUCCESS) {
209         ASSERT_NE(contoller, nullptr);
210     }
211 
212     if (contoller != nullptr) {
213         ret = model_->DestroyEffectController(model_, &contollerId_);
214     }
215 }
216 
217 /**
218  * @tc.name: HdfAudioGetEffectDescriptor001
219  * @tc.desc: Verify the EffectModelGetEffectDescriptor function when the input parameter is invalid.
220  * @tc.type: FUNC
221  * @tc.require: I6I658
222  */
223 HWTEST_F(EffectModelTest, HdfAudioGetEffectDescriptor001, TestSize.Level1)
224 {
225     struct EffectControllerDescriptor desc;
226 
227     EXPECT_EQ(HDF_ERR_INVALID_OBJECT, model_->GetEffectDescriptor(nullptr, effectId_, &desc));
228     EXPECT_EQ(HDF_ERR_INVALID_PARAM, model_->GetEffectDescriptor(model_, nullptr, &desc));
229     EXPECT_EQ(HDF_ERR_INVALID_PARAM, model_->GetEffectDescriptor(model_, effectId_, nullptr));
230 }
231 
232 /**
233  * @tc.name: HdfAudioGetEffectDescriptor002
234  * @tc.desc: Verify the EffectModelGetEffectDescriptor function.
235  * @tc.type: FUNC
236  * @tc.require: I6I658
237  */
238 HWTEST_F(EffectModelTest, HdfAudioGetEffectDescriptor002, TestSize.Level1)
239 {
240     struct EffectControllerDescriptor desc;
241 
242     int32_t ret = model_->GetEffectDescriptor(model_, effectId_, &desc);
243     ASSERT_EQ(ret, HDF_SUCCESS);
244     EXPECT_STREQ(desc.effectId, effectId_);
245     EXPECT_STREQ(desc.effectName, "mock_effect");
246     EXPECT_STREQ(desc.libName, libName_);
247     EXPECT_STREQ(desc.supplier, "mock");
248     OHOS::Audio::EffectControllerReleaseDesc(&desc);
249 }
250 } // end of namespace
251