blob: 12d56b01513b4632531abd2d7e61d96e7eab652d [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
Mikhail Naganov872d4a62023-03-09 18:19:01 -080019#include <aidl/Vintf.h>
Shunkai Yao883d75b2022-12-24 05:15:15 +000020#include <aidl/android/hardware/audio/effect/NoiseSuppression.h>
Mikhail Naganov872d4a62023-03-09 18:19:01 -080021#define LOG_TAG "VtsHalNSParamTest"
22#include <android-base/logging.h>
23#include <android/binder_enums.h>
24
Shunkai Yao5bd4a302022-12-20 15:46:24 +000025#include "EffectHelper.h"
26
27using namespace android;
28
Shunkai Yao5bd4a302022-12-20 15:46:24 +000029using aidl::android::hardware::audio::effect::Descriptor;
Shunkai Yaof8be1ac2023-03-06 18:41:27 +000030using aidl::android::hardware::audio::effect::getEffectTypeUuidNoiseSuppression;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000031using aidl::android::hardware::audio::effect::IEffect;
32using aidl::android::hardware::audio::effect::IFactory;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000033using aidl::android::hardware::audio::effect::NoiseSuppression;
34using aidl::android::hardware::audio::effect::Parameter;
Jaideep Sharma74498412023-09-13 15:25:25 +053035using android::hardware::audio::common::testing::detail::TestExecutionTracer;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000036
Shunkai Yao58aaf5b2023-02-06 07:25:09 +000037enum ParamName { PARAM_INSTANCE_NAME, PARAM_LEVEL, PARAM_TYPE };
38using NSParamTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>,
39 NoiseSuppression::Level, NoiseSuppression::Type>;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000040
41class NSParamTest : public ::testing::TestWithParam<NSParamTestParam>, public EffectHelper {
42 public:
Shunkai Yao58aaf5b2023-02-06 07:25:09 +000043 NSParamTest()
44 : mLevel(std::get<PARAM_LEVEL>(GetParam())), mType(std::get<PARAM_TYPE>(GetParam())) {
Shunkai Yao5bd4a302022-12-20 15:46:24 +000045 std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
46 }
47
48 void SetUp() override {
49 ASSERT_NE(nullptr, mFactory);
50 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
51
Jaideep Sharmab9859032023-07-07 14:35:48 +053052 std::optional<Parameter::Specific> specific = getDefaultParamSpecific();
Shunkai Yao5bd4a302022-12-20 15:46:24 +000053 Parameter::Common common = EffectHelper::createParamCommon(
54 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
55 kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
56 IEffect::OpenEffectReturn ret;
57 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE));
58 ASSERT_NE(nullptr, mEffect);
59 }
60
61 void TearDown() override {
62 ASSERT_NO_FATAL_FAILURE(close(mEffect));
63 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
64 }
65
Jaideep Sharmab9859032023-07-07 14:35:48 +053066 std::optional<Parameter::Specific> getDefaultParamSpecific() {
Shunkai Yao5bd4a302022-12-20 15:46:24 +000067 NoiseSuppression ns =
68 NoiseSuppression::make<NoiseSuppression::level>(NoiseSuppression::Level::MEDIUM);
Jaideep Sharmab9859032023-07-07 14:35:48 +053069 if (!isParameterValid<NoiseSuppression, Range::noiseSuppression>(ns, mDescriptor)) {
70 return std::nullopt;
71 }
72
Shunkai Yao5bd4a302022-12-20 15:46:24 +000073 Parameter::Specific specific =
74 Parameter::Specific::make<Parameter::Specific::noiseSuppression>(ns);
75 return specific;
76 }
77
78 static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000079 std::shared_ptr<IFactory> mFactory;
80 std::shared_ptr<IEffect> mEffect;
81 Descriptor mDescriptor;
82 NoiseSuppression::Level mLevel;
Shunkai Yao58aaf5b2023-02-06 07:25:09 +000083 NoiseSuppression::Type mType;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000084
85 void SetAndGetParameters() {
86 for (auto& it : mTags) {
87 auto& tag = it.first;
88 auto& ns = it.second;
89
90 // validate parameter
91 Descriptor desc;
92 ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc));
Jaideep Sharmab9859032023-07-07 14:35:48 +053093 const bool valid =
94 isParameterValid<NoiseSuppression, Range::noiseSuppression>(ns, desc);
95 const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000096
97 // set parameter
98 Parameter expectParam;
99 Parameter::Specific specific;
100 specific.set<Parameter::Specific::noiseSuppression>(ns);
101 expectParam.set<Parameter::specific>(specific);
102 EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString();
103
104 // only get if parameter in range and set success
105 if (expected == EX_NONE) {
106 Parameter getParam;
107 Parameter::Id id;
108 NoiseSuppression::Id specificId;
109 specificId.set<NoiseSuppression::Id::commonTag>(tag);
110 id.set<Parameter::Id::noiseSuppressionTag>(specificId);
111 EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam));
112
113 EXPECT_EQ(expectParam, getParam) << "\nexpect:" << expectParam.toString()
114 << "\ngetParam:" << getParam.toString();
115 }
116 }
117 }
118
119 void addLevelParam(NoiseSuppression::Level level) {
120 NoiseSuppression ns;
121 ns.set<NoiseSuppression::level>(level);
122 mTags.push_back({NoiseSuppression::level, ns});
123 }
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000124 void addTypeParam(NoiseSuppression::Type type) {
125 NoiseSuppression ns;
126 ns.set<NoiseSuppression::type>(type);
127 mTags.push_back({NoiseSuppression::type, ns});
128 }
Shunkai Yao883d75b2022-12-24 05:15:15 +0000129 static std::unordered_set<NoiseSuppression::Level> getLevelValues() {
130 return {ndk::enum_range<NoiseSuppression::Level>().begin(),
131 ndk::enum_range<NoiseSuppression::Level>().end()};
132 }
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000133 static std::unordered_set<NoiseSuppression::Type> getTypeValues() {
134 return {ndk::enum_range<NoiseSuppression::Type>().begin(),
135 ndk::enum_range<NoiseSuppression::Type>().end()};
136 }
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000137
138 private:
139 std::vector<std::pair<NoiseSuppression::Tag, NoiseSuppression>> mTags;
140 void CleanUp() { mTags.clear(); }
141};
142
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000143TEST_P(NSParamTest, SetAndGetLevel) {
144 EXPECT_NO_FATAL_FAILURE(addLevelParam(mLevel));
145 SetAndGetParameters();
146}
147
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000148TEST_P(NSParamTest, SetAndGetType) {
149 EXPECT_NO_FATAL_FAILURE(addLevelParam(mLevel));
150 SetAndGetParameters();
151}
152
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000153INSTANTIATE_TEST_SUITE_P(
154 NSParamTest, NSParamTest,
155 ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(
Shunkai Yaof8be1ac2023-03-06 18:41:27 +0000156 IFactory::descriptor, getEffectTypeUuidNoiseSuppression())),
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000157 testing::ValuesIn(NSParamTest::getLevelValues()),
158 testing::ValuesIn(NSParamTest::getTypeValues())),
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000159 [](const testing::TestParamInfo<NSParamTest::ParamType>& info) {
160 auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
161 std::string level = aidl::android::hardware::audio::effect::toString(
162 std::get<PARAM_LEVEL>(info.param));
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000163 std::string type = aidl::android::hardware::audio::effect::toString(
164 std::get<PARAM_TYPE>(info.param));
Shunkai Yao9e60e632023-06-23 04:34:50 +0000165 std::string name = getPrefix(descriptor) + "_level_" + level + "_type_" + type;
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000166 std::replace_if(
167 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
168 return name;
169 });
170
171GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(NSParamTest);
172
173int main(int argc, char** argv) {
174 ::testing::InitGoogleTest(&argc, argv);
Jaideep Sharma74498412023-09-13 15:25:25 +0530175 ::testing::UnitTest::GetInstance()->listeners().Append(new TestExecutionTracer());
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000176 ABinderProcess_setThreadPoolMaxThreadCount(1);
177 ABinderProcess_startThreadPool();
178 return RUN_ALL_TESTS();
Mikhail Naganov872d4a62023-03-09 18:19:01 -0800179}