1 /*
2 * Copyright (C) 2022 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17 #include <unordered_set>
18
19 #include <aidl/android/hardware/audio/effect/NoiseSuppression.h>
20 #define LOG_TAG "VtsHalNSParamTest"
21 #include <android-base/logging.h>
22 #include <android/binder_enums.h>
23
24 #include "EffectHelper.h"
25
26 using namespace android;
27
28 using aidl::android::hardware::audio::effect::Descriptor;
29 using aidl::android::hardware::audio::effect::getEffectTypeUuidNoiseSuppression;
30 using aidl::android::hardware::audio::effect::IEffect;
31 using aidl::android::hardware::audio::effect::IFactory;
32 using aidl::android::hardware::audio::effect::NoiseSuppression;
33 using aidl::android::hardware::audio::effect::Parameter;
34 using android::hardware::audio::common::testing::detail::TestExecutionTracer;
35
36 enum ParamName { PARAM_INSTANCE_NAME, PARAM_LEVEL, PARAM_TYPE };
37 using NSParamTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>,
38 NoiseSuppression::Level, NoiseSuppression::Type>;
39
40 class NSParamTest : public ::testing::TestWithParam<NSParamTestParam>, public EffectHelper {
41 public:
NSParamTest()42 NSParamTest()
43 : mLevel(std::get<PARAM_LEVEL>(GetParam())), mType(std::get<PARAM_TYPE>(GetParam())) {
44 std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
45 }
46
SetUp()47 void SetUp() override {
48 ASSERT_NE(nullptr, mFactory);
49 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
50
51 std::optional<Parameter::Specific> specific = getDefaultParamSpecific();
52 Parameter::Common common = createParamCommon(
53 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
54 kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
55 IEffect::OpenEffectReturn ret;
56 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE));
57 ASSERT_NE(nullptr, mEffect);
58 }
59
TearDown()60 void TearDown() override {
61 ASSERT_NO_FATAL_FAILURE(close(mEffect));
62 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
63 }
64
getDefaultParamSpecific()65 std::optional<Parameter::Specific> getDefaultParamSpecific() {
66 NoiseSuppression ns =
67 NoiseSuppression::make<NoiseSuppression::level>(NoiseSuppression::Level::MEDIUM);
68 if (!isParameterValid<NoiseSuppression, Range::noiseSuppression>(ns, mDescriptor)) {
69 return std::nullopt;
70 }
71
72 Parameter::Specific specific =
73 Parameter::Specific::make<Parameter::Specific::noiseSuppression>(ns);
74 return specific;
75 }
76
77 static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
78 std::shared_ptr<IFactory> mFactory;
79 std::shared_ptr<IEffect> mEffect;
80 Descriptor mDescriptor;
81 NoiseSuppression::Level mLevel;
82 NoiseSuppression::Type mType;
83
SetAndGetParameters()84 void SetAndGetParameters() {
85 for (auto& it : mTags) {
86 auto& tag = it.first;
87 auto& ns = it.second;
88
89 // validate parameter
90 Descriptor desc;
91 ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc));
92 const bool valid =
93 isParameterValid<NoiseSuppression, Range::noiseSuppression>(ns, desc);
94 const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT;
95
96 // set parameter
97 Parameter expectParam;
98 Parameter::Specific specific;
99 specific.set<Parameter::Specific::noiseSuppression>(ns);
100 expectParam.set<Parameter::specific>(specific);
101 EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString();
102
103 // only get if parameter in range and set success
104 if (expected == EX_NONE) {
105 Parameter getParam;
106 Parameter::Id id;
107 NoiseSuppression::Id specificId;
108 specificId.set<NoiseSuppression::Id::commonTag>(tag);
109 id.set<Parameter::Id::noiseSuppressionTag>(specificId);
110 EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam));
111
112 EXPECT_EQ(expectParam, getParam) << "\nexpect:" << expectParam.toString()
113 << "\ngetParam:" << getParam.toString();
114 }
115 }
116 }
117
addLevelParam(NoiseSuppression::Level level)118 void addLevelParam(NoiseSuppression::Level level) {
119 NoiseSuppression ns;
120 ns.set<NoiseSuppression::level>(level);
121 mTags.push_back({NoiseSuppression::level, ns});
122 }
addTypeParam(NoiseSuppression::Type type)123 void addTypeParam(NoiseSuppression::Type type) {
124 NoiseSuppression ns;
125 ns.set<NoiseSuppression::type>(type);
126 mTags.push_back({NoiseSuppression::type, ns});
127 }
getLevelValues()128 static std::unordered_set<NoiseSuppression::Level> getLevelValues() {
129 return {ndk::enum_range<NoiseSuppression::Level>().begin(),
130 ndk::enum_range<NoiseSuppression::Level>().end()};
131 }
getTypeValues()132 static std::unordered_set<NoiseSuppression::Type> getTypeValues() {
133 return {ndk::enum_range<NoiseSuppression::Type>().begin(),
134 ndk::enum_range<NoiseSuppression::Type>().end()};
135 }
136
137 private:
138 std::vector<std::pair<NoiseSuppression::Tag, NoiseSuppression>> mTags;
CleanUp()139 void CleanUp() { mTags.clear(); }
140 };
141
TEST_P(NSParamTest,SetAndGetLevel)142 TEST_P(NSParamTest, SetAndGetLevel) {
143 EXPECT_NO_FATAL_FAILURE(addLevelParam(mLevel));
144 SetAndGetParameters();
145 }
146
TEST_P(NSParamTest,SetAndGetType)147 TEST_P(NSParamTest, SetAndGetType) {
148 EXPECT_NO_FATAL_FAILURE(addLevelParam(mLevel));
149 SetAndGetParameters();
150 }
151
152 INSTANTIATE_TEST_SUITE_P(
153 NSParamTest, NSParamTest,
154 ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(
155 IFactory::descriptor, getEffectTypeUuidNoiseSuppression())),
156 testing::ValuesIn(NSParamTest::getLevelValues()),
157 testing::ValuesIn(NSParamTest::getTypeValues())),
__anon4ac48c300102(const testing::TestParamInfo<NSParamTest::ParamType>& info) 158 [](const testing::TestParamInfo<NSParamTest::ParamType>& info) {
159 auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
160 std::string level = aidl::android::hardware::audio::effect::toString(
161 std::get<PARAM_LEVEL>(info.param));
162 std::string type = aidl::android::hardware::audio::effect::toString(
163 std::get<PARAM_TYPE>(info.param));
164 std::string name = getPrefix(descriptor) + "_level_" + level + "_type_" + type;
165 std::replace_if(
166 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
167 return name;
168 });
169
170 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(NSParamTest);
171
main(int argc,char ** argv)172 int main(int argc, char** argv) {
173 ::testing::InitGoogleTest(&argc, argv);
174 ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
175 ABinderProcess_setThreadPoolMaxThreadCount(1);
176 ABinderProcess_startThreadPool();
177 return RUN_ALL_TESTS();
178 }
179