blob: 724a9c3ac0860f671d41bfc2ee64450094955142 [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
49const std::vector<int> kStrengthValues = {
50 std::numeric_limits<int>::min(),
51 BassBoost::MIN_PER_MILLE_STRENGTH - 1,
52 BassBoost::MIN_PER_MILLE_STRENGTH,
53 (BassBoost::MIN_PER_MILLE_STRENGTH + BassBoost::MAX_PER_MILLE_STRENGTH) >> 1,
54 BassBoost::MAX_PER_MILLE_STRENGTH,
55 BassBoost::MAX_PER_MILLE_STRENGTH + 2,
56 std::numeric_limits<int>::max()};
57
58class BassBoostParamTest : public ::testing::TestWithParam<BassBoostParamTestParam>,
59 public EffectHelper {
60 public:
61 BassBoostParamTest() : mParamStrength(std::get<PARAM_STRENGTH>(GetParam())) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +000062 std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
Shraddha Basantwanif627d802022-11-08 14:45:07 +053063 }
64
65 void SetUp() override {
66 ASSERT_NE(nullptr, mFactory);
Shunkai Yaocb0fc412022-12-15 20:34:32 +000067 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
Shraddha Basantwanif627d802022-11-08 14:45:07 +053068
69 Parameter::Specific specific = getDefaultParamSpecific();
70 Parameter::Common common = EffectHelper::createParamCommon(
71 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
72 kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
73 IEffect::OpenEffectReturn ret;
74 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE));
75 ASSERT_NE(nullptr, mEffect);
76 }
77
78 void TearDown() override {
79 ASSERT_NO_FATAL_FAILURE(close(mEffect));
80 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
81 }
82
83 Parameter::Specific getDefaultParamSpecific() {
84 BassBoost bb = BassBoost::make<BassBoost::strengthPm>(BassBoost::MIN_PER_MILLE_STRENGTH);
85 Parameter::Specific specific =
86 Parameter::Specific::make<Parameter::Specific::bassBoost>(bb);
87 return specific;
88 }
89
90 static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
91 std::shared_ptr<IFactory> mFactory;
92 std::shared_ptr<IEffect> mEffect;
Shunkai Yaocb0fc412022-12-15 20:34:32 +000093 Descriptor mDescriptor;
Shraddha Basantwanif627d802022-11-08 14:45:07 +053094 int mParamStrength = BassBoost::MIN_PER_MILLE_STRENGTH;
95
96 void SetAndGetBassBoostParameters() {
97 for (auto& it : mTags) {
98 auto& tag = it.first;
99 auto& bb = it.second;
100
101 // validate parameter
102 Descriptor desc;
103 ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc));
104 const bool valid = isTagInRange(it.first, it.second, desc);
105 const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT;
106
107 // set parameter
108 Parameter expectParam;
109 Parameter::Specific specific;
110 specific.set<Parameter::Specific::bassBoost>(bb);
111 expectParam.set<Parameter::specific>(specific);
112 EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString();
113
114 // only get if parameter in range and set success
115 if (expected == EX_NONE) {
116 Parameter getParam;
117 Parameter::Id id;
118 BassBoost::Id bbId;
119 bbId.set<BassBoost::Id::commonTag>(tag);
120 id.set<Parameter::Id::bassBoostTag>(bbId);
121 // if set success, then get should match
122 EXPECT_STATUS(expected, mEffect->getParameter(id, &getParam));
123 EXPECT_EQ(expectParam, getParam);
124 }
125 }
126 }
127
128 void addStrengthParam(int strength) {
129 BassBoost bb;
130 bb.set<BassBoost::strengthPm>(strength);
131 mTags.push_back({BassBoost::strengthPm, bb});
132 }
133
134 bool isTagInRange(const BassBoost::Tag& tag, const BassBoost& bb,
135 const Descriptor& desc) const {
136 const BassBoost::Capability& bbCap = desc.capability.get<Capability::bassBoost>();
137 switch (tag) {
138 case BassBoost::strengthPm: {
139 int strength = bb.get<BassBoost::strengthPm>();
140 return isStrengthInRange(bbCap, strength);
141 }
142 default:
143 return false;
144 }
145 return false;
146 }
147
148 bool isStrengthInRange(const BassBoost::Capability& cap, int strength) const {
149 return cap.strengthSupported && strength >= BassBoost::MIN_PER_MILLE_STRENGTH &&
150 strength <= BassBoost::MAX_PER_MILLE_STRENGTH;
151 }
152
153 private:
154 std::vector<std::pair<BassBoost::Tag, BassBoost>> mTags;
155 void CleanUp() { mTags.clear(); }
156};
157
158TEST_P(BassBoostParamTest, SetAndGetStrength) {
159 EXPECT_NO_FATAL_FAILURE(addStrengthParam(mParamStrength));
160 SetAndGetBassBoostParameters();
161}
162
163INSTANTIATE_TEST_SUITE_P(
164 BassBoostTest, BassBoostParamTest,
165 ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(
166 IFactory::descriptor, kBassBoostTypeUUID)),
167 testing::ValuesIn(kStrengthValues)),
168 [](const testing::TestParamInfo<BassBoostParamTest::ParamType>& info) {
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000169 auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530170 std::string strength = std::to_string(std::get<PARAM_STRENGTH>(info.param));
Shunkai Yaocb0fc412022-12-15 20:34:32 +0000171 std::string name = "Implementor_" + descriptor.common.implementor + "_name_" +
172 descriptor.common.name + "_UUID_" +
173 descriptor.common.id.uuid.toString() + "_strength_" + strength;
Shraddha Basantwanif627d802022-11-08 14:45:07 +0530174 std::replace_if(
175 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
176 return name;
177 });
178
179GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BassBoostParamTest);
180
181int main(int argc, char** argv) {
182 ::testing::InitGoogleTest(&argc, argv);
183 ABinderProcess_setThreadPoolMaxThreadCount(1);
184 ABinderProcess_startThreadPool();
185 return RUN_ALL_TESTS();
186}