blob: 93ad86de439a39a68bb0c2f87c7a11fe5a547e9b [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
37enum ParamName { PARAM_INSTANCE_NAME, PARAM_LEVEL };
38using NSParamTestParam =
39 std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, NoiseSuppression::Level>;
40
41class NSParamTest : public ::testing::TestWithParam<NSParamTestParam>, public EffectHelper {
42 public:
43 NSParamTest() : mLevel(std::get<PARAM_LEVEL>(GetParam())) {
44 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;
78
79 void SetAndGetParameters() {
80 for (auto& it : mTags) {
81 auto& tag = it.first;
82 auto& ns = it.second;
83
84 // validate parameter
85 Descriptor desc;
86 ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc));
87 const binder_exception_t expected = EX_NONE;
88
89 // set parameter
90 Parameter expectParam;
91 Parameter::Specific specific;
92 specific.set<Parameter::Specific::noiseSuppression>(ns);
93 expectParam.set<Parameter::specific>(specific);
94 EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString();
95
96 // only get if parameter in range and set success
97 if (expected == EX_NONE) {
98 Parameter getParam;
99 Parameter::Id id;
100 NoiseSuppression::Id specificId;
101 specificId.set<NoiseSuppression::Id::commonTag>(tag);
102 id.set<Parameter::Id::noiseSuppressionTag>(specificId);
103 EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam));
104
105 EXPECT_EQ(expectParam, getParam) << "\nexpect:" << expectParam.toString()
106 << "\ngetParam:" << getParam.toString();
107 }
108 }
109 }
110
111 void addLevelParam(NoiseSuppression::Level level) {
112 NoiseSuppression ns;
113 ns.set<NoiseSuppression::level>(level);
114 mTags.push_back({NoiseSuppression::level, ns});
115 }
Shunkai Yao883d75b2022-12-24 05:15:15 +0000116 static std::unordered_set<NoiseSuppression::Level> getLevelValues() {
117 return {ndk::enum_range<NoiseSuppression::Level>().begin(),
118 ndk::enum_range<NoiseSuppression::Level>().end()};
119 }
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000120
121 private:
122 std::vector<std::pair<NoiseSuppression::Tag, NoiseSuppression>> mTags;
123 void CleanUp() { mTags.clear(); }
124};
125
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000126TEST_P(NSParamTest, SetAndGetLevel) {
127 EXPECT_NO_FATAL_FAILURE(addLevelParam(mLevel));
128 SetAndGetParameters();
129}
130
131INSTANTIATE_TEST_SUITE_P(
132 NSParamTest, NSParamTest,
133 ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(
134 IFactory::descriptor, kNoiseSuppressionTypeUUID)),
Shunkai Yao883d75b2022-12-24 05:15:15 +0000135 testing::ValuesIn(NSParamTest::getLevelValues())),
Shunkai Yao5bd4a302022-12-20 15:46:24 +0000136 [](const testing::TestParamInfo<NSParamTest::ParamType>& info) {
137 auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
138 std::string level = aidl::android::hardware::audio::effect::toString(
139 std::get<PARAM_LEVEL>(info.param));
140 std::string name = "Implementor_" + descriptor.common.implementor + "_name_" +
141 descriptor.common.name + "_UUID_" +
142 descriptor.common.id.uuid.toString() + "_level_" + level;
143 std::replace_if(
144 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
145 return name;
146 });
147
148GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(NSParamTest);
149
150int main(int argc, char** argv) {
151 ::testing::InitGoogleTest(&argc, argv);
152 ABinderProcess_setThreadPoolMaxThreadCount(1);
153 ABinderProcess_startThreadPool();
154 return RUN_ALL_TESTS();
155}