blob: d49a86542a95ba5469c84d7485d220dfd2387f33 [file] [log] [blame]
Shraddha Basantwanif627d802022-11-08 14:45:07 +05301/*
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
17#define LOG_TAG "VtsHalBassBoostTest"
18
19#include <Utils.h>
20#include <aidl/Vintf.h>
21#include <limits.h>
22
23#include "EffectHelper.h"
24
25using namespace android;
26
27using aidl::android::hardware::audio::effect::BassBoost;
28using aidl::android::hardware::audio::effect::Capability;
29using 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::kBassBoostTypeUUID;
33using aidl::android::hardware::audio::effect::Parameter;
34
35/**
36 * Here we focus on specific parameter checking, general IEffect interfaces testing performed in
37 * VtsAudioEffectTargetTest.
38 */
39enum ParamName { PARAM_INSTANCE_NAME, PARAM_STRENGTH };
Shunkai Yaocb0fc412022-12-15 20:34:32 +000040using BassBoostParamTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int>;
Shraddha Basantwanif627d802022-11-08 14:45:07 +053041
42/*
43 * Testing parameter range, assuming the parameter supported by effect is in this range.
44 * Parameter should be within the valid range defined in the documentation,
45 * for any supported value test expects EX_NONE from IEffect.setParameter(),
46 * otherwise expect EX_ILLEGAL_ARGUMENT.
47 */
48
Shraddha Basantwanif627d802022-11-08 14:45:07 +053049class BassBoostParamTest : public ::testing::TestWithParam<BassBoostParamTestParam>,
50 public EffectHelper {
51 public:
52 BassBoostParamTest() : mParamStrength(std::get<PARAM_STRENGTH>(GetParam())) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +000053 std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
Shraddha Basantwanif627d802022-11-08 14:45:07 +053054 }
55
56 void SetUp() override {
57 ASSERT_NE(nullptr, mFactory);
Shunkai Yaocb0fc412022-12-15 20:34:32 +000058 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
Shraddha Basantwanif627d802022-11-08 14:45:07 +053059
60 Parameter::Specific specific = getDefaultParamSpecific();
61 Parameter::Common common = EffectHelper::createParamCommon(
62 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
63 kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
64 IEffect::OpenEffectReturn ret;
65 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE));
66 ASSERT_NE(nullptr, mEffect);
67 }
68
69 void TearDown() override {
70 ASSERT_NO_FATAL_FAILURE(close(mEffect));
71 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
72 }
73
74 Parameter::Specific getDefaultParamSpecific() {
Sham Rathod8411fd22022-12-27 10:27:03 +053075 BassBoost bb = BassBoost::make<BassBoost::strengthPm>(0);
Shraddha Basantwanif627d802022-11-08 14:45:07 +053076 Parameter::Specific specific =
77 Parameter::Specific::make<Parameter::Specific::bassBoost>(bb);
78 return specific;
79 }
80
81 static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
82 std::shared_ptr<IFactory> mFactory;
83 std::shared_ptr<IEffect> mEffect;
Shunkai Yaocb0fc412022-12-15 20:34:32 +000084 Descriptor mDescriptor;
Sham Rathod8411fd22022-12-27 10:27:03 +053085 int mParamStrength = 0;
Shraddha Basantwanif627d802022-11-08 14:45:07 +053086
87 void SetAndGetBassBoostParameters() {
88 for (auto& it : mTags) {
89 auto& tag = it.first;
90 auto& bb = it.second;
91
92 // validate parameter
93 Descriptor desc;
94 ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc));
95 const bool valid = isTagInRange(it.first, it.second, desc);
96 const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT;
97
98 // set parameter
99 Parameter expectParam;
100 Parameter::Specific specific;
101 specific.set<Parameter::Specific::bassBoost>(bb);
102 expectParam.set<Parameter::specific>(specific);
103 EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString();
104
105 // only get if parameter in range and set success
106 if (expected == EX_NONE) {
107 Parameter getParam;
108 Parameter::Id id;
109 BassBoost::Id bbId;
110 bbId.set<BassBoost::Id::commonTag>(tag);
111 id.set<Parameter::Id::bassBoostTag>(bbId);
112 // if set success, then get should match
113 EXPECT_STATUS(expected, mEffect->getParameter(id, &getParam));
114 EXPECT_EQ(expectParam, getParam);
115 }
116 }
117 }
118
119 void addStrengthParam(int strength) {
120 BassBoost bb;
121 bb.set<BassBoost::strengthPm>(strength);
122 mTags.push_back({BassBoost::strengthPm, bb});
123 }
124
125 bool isTagInRange(const BassBoost::Tag& tag, const BassBoost& bb,
126 const Descriptor& desc) const {
127 const BassBoost::Capability& bbCap = desc.capability.get<Capability::bassBoost>();
128 switch (tag) {
129 case BassBoost::strengthPm: {
130 int strength = bb.get<BassBoost::strengthPm>();
131 return isStrengthInRange(bbCap, strength);
132 }
133 default:
134 return false;
135 }
136 return false;
137 }
138
139 bool isStrengthInRange(const BassBoost::Capability& cap, int strength) const {
Sham Rathod8411fd22022-12-27 10:27:03 +0530140 return cap.strengthSupported && strength >= 0 && strength <= cap.maxStrengthPm;
141 }
142
143 static std::vector<int> getStrengthTestValues(
144 std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kFactoryDescList) {
145 const auto max = std::max_element(
146 kFactoryDescList.begin(), kFactoryDescList.end(),
147 [](const std::pair<std::shared_ptr<IFactory>, Descriptor>& a,
148 const std::pair<std::shared_ptr<IFactory>, Descriptor>& b) {
149 return a.second.capability.get<Capability::bassBoost>().maxStrengthPm <
150 b.second.capability.get<Capability::bassBoost>().maxStrengthPm;
151 });
152 if (max == kFactoryDescList.end()) {
153 return {0};
154 }
155 int maxStrength = max->second.capability.get<Capability::bassBoost>().maxStrengthPm;
156 return {std::numeric_limits<int>::min(),
157 -1,
158 0,
159 maxStrength >> 1,
160 maxStrength,
161 maxStrength + 1,
162 std::numeric_limits<int>::max()};
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530163 }
164
165 private:
166 std::vector<std::pair<BassBoost::Tag, BassBoost>> mTags;
167 void CleanUp() { mTags.clear(); }
168};
169
170TEST_P(BassBoostParamTest, SetAndGetStrength) {
171 EXPECT_NO_FATAL_FAILURE(addStrengthParam(mParamStrength));
172 SetAndGetBassBoostParameters();
173}
174
175INSTANTIATE_TEST_SUITE_P(
176 BassBoostTest, BassBoostParamTest,
Sham Rathod8411fd22022-12-27 10:27:03 +0530177 ::testing::Combine(
178 testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(IFactory::descriptor,
179 kBassBoostTypeUUID)),
180 testing::ValuesIn(BassBoostParamTest::getStrengthTestValues(
181 EffectFactoryHelper::getAllEffectDescriptors(IFactory::descriptor,
182 kBassBoostTypeUUID)))),
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530183 [](const testing::TestParamInfo<BassBoostParamTest::ParamType>& info) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000184 auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530185 std::string strength = std::to_string(std::get<PARAM_STRENGTH>(info.param));
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000186 std::string name = "Implementor_" + descriptor.common.implementor + "_name_" +
187 descriptor.common.name + "_UUID_" +
188 descriptor.common.id.uuid.toString() + "_strength_" + strength;
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530189 std::replace_if(
190 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
191 return name;
192 });
193
194GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BassBoostParamTest);
195
196int main(int argc, char** argv) {
197 ::testing::InitGoogleTest(&argc, argv);
198 ABinderProcess_setThreadPoolMaxThreadCount(1);
199 ABinderProcess_startThreadPool();
200 return RUN_ALL_TESTS();
201}