blob: 16c79e3a61502dae132784ecd2f1dfc6eed5a92b [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
Shunkai Yao5bd4a302022-12-20 15:46:24 +000029using aidl::android::hardware::audio::effect::Descriptor;
30using aidl::android::hardware::audio::effect::IEffect;
31using aidl::android::hardware::audio::effect::IFactory;
32using aidl::android::hardware::audio::effect::kNoiseSuppressionTypeUUID;
33using aidl::android::hardware::audio::effect::NoiseSuppression;
34using aidl::android::hardware::audio::effect::Parameter;
35
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
51 Parameter::Specific specific = getDefaultParamSpecific();
52 Parameter::Common common = EffectHelper::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
60 void TearDown() override {
61 ASSERT_NO_FATAL_FAILURE(close(mEffect));
62 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
63 }
64
65 Parameter::Specific getDefaultParamSpecific() {
66 NoiseSuppression ns =
67 NoiseSuppression::make<NoiseSuppression::level>(NoiseSuppression::Level::MEDIUM);
68 Parameter::Specific specific =
69 Parameter::Specific::make<Parameter::Specific::noiseSuppression>(ns);
70 return specific;
71 }
72
73 static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000074 std::shared_ptr<IFactory> mFactory;
75 std::shared_ptr<IEffect> mEffect;
76 Descriptor mDescriptor;
77 NoiseSuppression::Level mLevel;
Shunkai Yao58aaf5b2023-02-06 07:25:09 +000078 NoiseSuppression::Type mType;
Shunkai Yao5bd4a302022-12-20 15:46:24 +000079
80 void SetAndGetParameters() {
81 for (auto& it : mTags) {
82 auto& tag = it.first;
83 auto& ns = it.second;
84
85 // validate parameter
86 Descriptor desc;
87 ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc));
88 const binder_exception_t expected = EX_NONE;
89
90 // set parameter
91 Parameter expectParam;
92 Parameter::Specific specific;
93 specific.set<Parameter::Specific::noiseSuppression>(ns);
94 expectParam.set<Parameter::specific>(specific);
95 EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString();
96
97 // only get if parameter in range and set success
98 if (expected == EX_NONE) {
99 Parameter getParam;
100 Parameter::Id id;
101 NoiseSuppression::Id specificId;
102 specificId.set<NoiseSuppression::Id::commonTag>(tag);
103 id.set<Parameter::Id::noiseSuppressionTag>(specificId);
104 EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam));
105
106 EXPECT_EQ(expectParam, getParam) << "\nexpect:" << expectParam.toString()
107 << "\ngetParam:" << getParam.toString();
108 }
109 }
110 }
111
112 void addLevelParam(NoiseSuppression::Level level) {
113 NoiseSuppression ns;
114 ns.set<NoiseSuppression::level>(level);
115 mTags.push_back({NoiseSuppression::level, ns});
116 }
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000117 void addTypeParam(NoiseSuppression::Type type) {
118 NoiseSuppression ns;
119 ns.set<NoiseSuppression::type>(type);
120 mTags.push_back({NoiseSuppression::type, ns});
121 }
Shunkai Yao883d75b2022-12-24 05:15:15 +0000122 static std::unordered_set<NoiseSuppression::Level> getLevelValues() {
123 return {ndk::enum_range<NoiseSuppression::Level>().begin(),
124 ndk::enum_range<NoiseSuppression::Level>().end()};
125 }
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000126 static std::unordered_set<NoiseSuppression::Type> getTypeValues() {
127 return {ndk::enum_range<NoiseSuppression::Type>().begin(),
128 ndk::enum_range<NoiseSuppression::Type>().end()};
129 }
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000130
131 private:
132 std::vector<std::pair<NoiseSuppression::Tag, NoiseSuppression>> mTags;
133 void CleanUp() { mTags.clear(); }
134};
135
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000136TEST_P(NSParamTest, SetAndGetLevel) {
137 EXPECT_NO_FATAL_FAILURE(addLevelParam(mLevel));
138 SetAndGetParameters();
139}
140
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000141TEST_P(NSParamTest, SetAndGetType) {
142 EXPECT_NO_FATAL_FAILURE(addLevelParam(mLevel));
143 SetAndGetParameters();
144}
145
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000146INSTANTIATE_TEST_SUITE_P(
147 NSParamTest, NSParamTest,
148 ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(
149 IFactory::descriptor, kNoiseSuppressionTypeUUID)),
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000150 testing::ValuesIn(NSParamTest::getLevelValues()),
151 testing::ValuesIn(NSParamTest::getTypeValues())),
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000152 [](const testing::TestParamInfo<NSParamTest::ParamType>& info) {
153 auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
154 std::string level = aidl::android::hardware::audio::effect::toString(
155 std::get<PARAM_LEVEL>(info.param));
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000156 std::string type = aidl::android::hardware::audio::effect::toString(
157 std::get<PARAM_TYPE>(info.param));
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000158 std::string name = "Implementor_" + descriptor.common.implementor + "_name_" +
159 descriptor.common.name + "_UUID_" +
Shunkai Yao58aaf5b2023-02-06 07:25:09 +0000160 descriptor.common.id.uuid.toString() + "_level_" + level + "_type_" +
161 type;
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000162 std::replace_if(
163 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
164 return name;
165 });
166
167GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(NSParamTest);
168
169int main(int argc, char** argv) {
170 ::testing::InitGoogleTest(&argc, argv);
171 ABinderProcess_setThreadPoolMaxThreadCount(1);
172 ABinderProcess_startThreadPool();
173 return RUN_ALL_TESTS();
174}