blob: a50e1b43eaabe56b5acecb34f01d1bc8b5952ff7 [file] [log] [blame]
Shunkai Yao5bd4a302022-12-20 15:46:24 +00001/*
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
Shunkai Yao883d75b2022-12-24 05:15:15 +000017#include <unordered_set>
Shunkai Yao5bd4a302022-12-20 15:46:24 +000018
Shunkai Yao883d75b2022-12-24 05:15:15 +000019#include <aidl/android/hardware/audio/effect/NoiseSuppression.h>
Mikhail Naganov872d4a62023-03-09 18:19:01 -080020#define LOG_TAG "VtsHalNSParamTest"
21#include <android-base/logging.h>
22#include <android/binder_enums.h>
23
Shunkai Yao5bd4a302022-12-20 15:46:24 +000024#include "EffectHelper.h"
25
26using namespace android;
27
Shunkai Yao5bd4a302022-12-20 15:46:24 +000028using aidl::android::hardware::audio::effect::Descriptor;
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000029using aidl::android::hardware::audio::effect::getEffectTypeUuidNoiseSuppression;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000030using aidl::android::hardware::audio::effect::IEffect;
31using aidl::android::hardware::audio::effect::IFactory;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000032using aidl::android::hardware::audio::effect::NoiseSuppression;
33using aidl::android::hardware::audio::effect::Parameter;
Jaideep Sharma74498412023-09-13 15:25:25 +053034using android::hardware::audio::common::testing::detail::TestExecutionTracer;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000035
Shunkai Yao58aaf5b2023-02-06 07:25:09 +000036enum ParamName { PARAM_INSTANCE_NAME, PARAM_LEVEL, PARAM_TYPE };
37using NSParamTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>,
38 NoiseSuppression::Level, NoiseSuppression::Type>;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000039
40class NSParamTest : public ::testing::TestWithParam<NSParamTestParam>, public EffectHelper {
41 public:
Shunkai Yao58aaf5b2023-02-06 07:25:09 +000042 NSParamTest()
43 : mLevel(std::get<PARAM_LEVEL>(GetParam())), mType(std::get<PARAM_TYPE>(GetParam())) {
Shunkai Yao5bd4a302022-12-20 15:46:24 +000044 std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
45 }
46
47 void SetUp() override {
48 ASSERT_NE(nullptr, mFactory);
49 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
50
Jaideep Sharmab9859032023-07-07 14:35:48 +053051 std::optional<Parameter::Specific> specific = getDefaultParamSpecific();
Shunkai Yao61f9dd22024-05-08 22:34:36 +000052 Parameter::Common common = createParamCommon(
Shunkai Yao5bd4a302022-12-20 15:46:24 +000053 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
60 void TearDown() override {
61 ASSERT_NO_FATAL_FAILURE(close(mEffect));
62 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
63 }
64
Jaideep Sharmab9859032023-07-07 14:35:48 +053065 std::optional<Parameter::Specific> getDefaultParamSpecific() {
Shunkai Yao5bd4a302022-12-20 15:46:24 +000066 NoiseSuppression ns =
67 NoiseSuppression::make<NoiseSuppression::level>(NoiseSuppression::Level::MEDIUM);
Jaideep Sharmab9859032023-07-07 14:35:48 +053068 if (!isParameterValid<NoiseSuppression, Range::noiseSuppression>(ns, mDescriptor)) {
69 return std::nullopt;
70 }
71
Shunkai Yao5bd4a302022-12-20 15:46:24 +000072 Parameter::Specific specific =
73 Parameter::Specific::make<Parameter::Specific::noiseSuppression>(ns);
74 return specific;
75 }
76
77 static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000078 std::shared_ptr<IFactory> mFactory;
79 std::shared_ptr<IEffect> mEffect;
80 Descriptor mDescriptor;
81 NoiseSuppression::Level mLevel;
Shunkai Yao58aaf5b2023-02-06 07:25:09 +000082 NoiseSuppression::Type mType;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000083
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));
Jaideep Sharmab9859032023-07-07 14:35:48 +053092 const bool valid =
93 isParameterValid<NoiseSuppression, Range::noiseSuppression>(ns, desc);
94 const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000095
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
118 void addLevelParam(NoiseSuppression::Level level) {
119 NoiseSuppression ns;
120 ns.set<NoiseSuppression::level>(level);
121 mTags.push_back({NoiseSuppression::level, ns});
122 }
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000123 void addTypeParam(NoiseSuppression::Type type) {
124 NoiseSuppression ns;
125 ns.set<NoiseSuppression::type>(type);
126 mTags.push_back({NoiseSuppression::type, ns});
127 }
Shunkai Yao883d75b2022-12-24 05:15:15 +0000128 static std::unordered_set<NoiseSuppression::Level> getLevelValues() {
129 return {ndk::enum_range<NoiseSuppression::Level>().begin(),
130 ndk::enum_range<NoiseSuppression::Level>().end()};
131 }
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000132 static std::unordered_set<NoiseSuppression::Type> getTypeValues() {
133 return {ndk::enum_range<NoiseSuppression::Type>().begin(),
134 ndk::enum_range<NoiseSuppression::Type>().end()};
135 }
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000136
137 private:
138 std::vector<std::pair<NoiseSuppression::Tag, NoiseSuppression>> mTags;
139 void CleanUp() { mTags.clear(); }
140};
141
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000142TEST_P(NSParamTest, SetAndGetLevel) {
143 EXPECT_NO_FATAL_FAILURE(addLevelParam(mLevel));
144 SetAndGetParameters();
145}
146
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000147TEST_P(NSParamTest, SetAndGetType) {
148 EXPECT_NO_FATAL_FAILURE(addLevelParam(mLevel));
149 SetAndGetParameters();
150}
151
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000152INSTANTIATE_TEST_SUITE_P(
153 NSParamTest, NSParamTest,
154 ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000155 IFactory::descriptor, getEffectTypeUuidNoiseSuppression())),
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000156 testing::ValuesIn(NSParamTest::getLevelValues()),
157 testing::ValuesIn(NSParamTest::getTypeValues())),
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000158 [](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));
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000162 std::string type = aidl::android::hardware::audio::effect::toString(
163 std::get<PARAM_TYPE>(info.param));
Shunkai Yao9e60e632023-06-23 04:34:50 +0000164 std::string name = getPrefix(descriptor) + "_level_" + level + "_type_" + type;
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000165 std::replace_if(
166 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
167 return name;
168 });
169
170GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(NSParamTest);
171
172int main(int argc, char** argv) {
173 ::testing::InitGoogleTest(&argc, argv);
Jaideep Sharma74498412023-09-13 15:25:25 +0530174 ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000175 ABinderProcess_setThreadPoolMaxThreadCount(1);
176 ABinderProcess_startThreadPool();
177 return RUN_ALL_TESTS();
Mikhail Naganov872d4a62023-03-09 18:19:01 -0800178}