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