blob: 0b922901ea8a7e53ec67a4920fb25384607fb617 [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 <Utils.h>
Shunkai Yao5bd4a302022-12-20 15:46:24 +000018#include <aidl/Vintf.h>
Shunkai Yao883d75b2022-12-24 05:15:15 +000019#include <android/binder_enums.h>
20#include <unordered_set>
Shunkai Yao5bd4a302022-12-20 15:46:24 +000021
22#define LOG_TAG "VtsHalNSParamTest"
23
Shunkai Yao883d75b2022-12-24 05:15:15 +000024#include <aidl/android/hardware/audio/effect/NoiseSuppression.h>
Shunkai Yao5bd4a302022-12-20 15:46:24 +000025#include "EffectHelper.h"
26
27using namespace android;
28
29using aidl::android::hardware::audio::effect::Capability;
30using aidl::android::hardware::audio::effect::Descriptor;
31using aidl::android::hardware::audio::effect::IEffect;
32using aidl::android::hardware::audio::effect::IFactory;
33using aidl::android::hardware::audio::effect::kNoiseSuppressionTypeUUID;
34using aidl::android::hardware::audio::effect::NoiseSuppression;
35using aidl::android::hardware::audio::effect::Parameter;
36
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
52 Parameter::Specific specific = getDefaultParamSpecific();
53 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
66 Parameter::Specific getDefaultParamSpecific() {
67 NoiseSuppression ns =
68 NoiseSuppression::make<NoiseSuppression::level>(NoiseSuppression::Level::MEDIUM);
69 Parameter::Specific specific =
70 Parameter::Specific::make<Parameter::Specific::noiseSuppression>(ns);
71 return specific;
72 }
73
74 static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000075 std::shared_ptr<IFactory> mFactory;
76 std::shared_ptr<IEffect> mEffect;
77 Descriptor mDescriptor;
78 NoiseSuppression::Level mLevel;
Shunkai Yao58aaf5b2023-02-06 07:25:09 +000079 NoiseSuppression::Type mType;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000080
81 void SetAndGetParameters() {
82 for (auto& it : mTags) {
83 auto& tag = it.first;
84 auto& ns = it.second;
85
86 // validate parameter
87 Descriptor desc;
88 ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc));
89 const binder_exception_t expected = EX_NONE;
90
91 // set parameter
92 Parameter expectParam;
93 Parameter::Specific specific;
94 specific.set<Parameter::Specific::noiseSuppression>(ns);
95 expectParam.set<Parameter::specific>(specific);
96 EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString();
97
98 // only get if parameter in range and set success
99 if (expected == EX_NONE) {
100 Parameter getParam;
101 Parameter::Id id;
102 NoiseSuppression::Id specificId;
103 specificId.set<NoiseSuppression::Id::commonTag>(tag);
104 id.set<Parameter::Id::noiseSuppressionTag>(specificId);
105 EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam));
106
107 EXPECT_EQ(expectParam, getParam) << "\nexpect:" << expectParam.toString()
108 << "\ngetParam:" << getParam.toString();
109 }
110 }
111 }
112
113 void addLevelParam(NoiseSuppression::Level level) {
114 NoiseSuppression ns;
115 ns.set<NoiseSuppression::level>(level);
116 mTags.push_back({NoiseSuppression::level, ns});
117 }
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000118 void addTypeParam(NoiseSuppression::Type type) {
119 NoiseSuppression ns;
120 ns.set<NoiseSuppression::type>(type);
121 mTags.push_back({NoiseSuppression::type, ns});
122 }
Shunkai Yao883d75b2022-12-24 05:15:15 +0000123 static std::unordered_set<NoiseSuppression::Level> getLevelValues() {
124 return {ndk::enum_range<NoiseSuppression::Level>().begin(),
125 ndk::enum_range<NoiseSuppression::Level>().end()};
126 }
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000127 static std::unordered_set<NoiseSuppression::Type> getTypeValues() {
128 return {ndk::enum_range<NoiseSuppression::Type>().begin(),
129 ndk::enum_range<NoiseSuppression::Type>().end()};
130 }
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000131
132 private:
133 std::vector<std::pair<NoiseSuppression::Tag, NoiseSuppression>> mTags;
134 void CleanUp() { mTags.clear(); }
135};
136
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000137TEST_P(NSParamTest, SetAndGetLevel) {
138 EXPECT_NO_FATAL_FAILURE(addLevelParam(mLevel));
139 SetAndGetParameters();
140}
141
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000142TEST_P(NSParamTest, SetAndGetType) {
143 EXPECT_NO_FATAL_FAILURE(addLevelParam(mLevel));
144 SetAndGetParameters();
145}
146
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000147INSTANTIATE_TEST_SUITE_P(
148 NSParamTest, NSParamTest,
149 ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(
150 IFactory::descriptor, kNoiseSuppressionTypeUUID)),
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000151 testing::ValuesIn(NSParamTest::getLevelValues()),
152 testing::ValuesIn(NSParamTest::getTypeValues())),
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000153 [](const testing::TestParamInfo<NSParamTest::ParamType>& info) {
154 auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
155 std::string level = aidl::android::hardware::audio::effect::toString(
156 std::get<PARAM_LEVEL>(info.param));
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000157 std::string type = aidl::android::hardware::audio::effect::toString(
158 std::get<PARAM_TYPE>(info.param));
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000159 std::string name = "Implementor_" + descriptor.common.implementor + "_name_" +
160 descriptor.common.name + "_UUID_" +
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000161 descriptor.common.id.uuid.toString() + "_level_" + level + "_type_" +
162 type;
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000163 std::replace_if(
164 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
165 return name;
166 });
167
168GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(NSParamTest);
169
170int main(int argc, char** argv) {
171 ::testing::InitGoogleTest(&argc, argv);
172 ABinderProcess_setThreadPoolMaxThreadCount(1);
173 ABinderProcess_startThreadPool();
174 return RUN_ALL_TESTS();
175}