Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 1 | /* |
| 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 | |
| 25 | using namespace android; |
| 26 | |
| 27 | using aidl::android::hardware::audio::effect::BassBoost; |
| 28 | using aidl::android::hardware::audio::effect::Capability; |
| 29 | using aidl::android::hardware::audio::effect::Descriptor; |
| 30 | using aidl::android::hardware::audio::effect::IEffect; |
| 31 | using aidl::android::hardware::audio::effect::IFactory; |
| 32 | using aidl::android::hardware::audio::effect::kBassBoostTypeUUID; |
| 33 | using 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 | */ |
| 39 | enum ParamName { PARAM_INSTANCE_NAME, PARAM_STRENGTH }; |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 40 | using BassBoostParamTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int>; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 41 | |
| 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 | |
| 49 | const 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 | |
| 58 | class BassBoostParamTest : public ::testing::TestWithParam<BassBoostParamTestParam>, |
| 59 | public EffectHelper { |
| 60 | public: |
| 61 | BassBoostParamTest() : mParamStrength(std::get<PARAM_STRENGTH>(GetParam())) { |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 62 | std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam()); |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 63 | } |
| 64 | |
| 65 | void SetUp() override { |
| 66 | ASSERT_NE(nullptr, mFactory); |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 67 | ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor)); |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 68 | |
| 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 Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 93 | Descriptor mDescriptor; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 94 | 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 | |
| 158 | TEST_P(BassBoostParamTest, SetAndGetStrength) { |
| 159 | EXPECT_NO_FATAL_FAILURE(addStrengthParam(mParamStrength)); |
| 160 | SetAndGetBassBoostParameters(); |
| 161 | } |
| 162 | |
| 163 | INSTANTIATE_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 Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 169 | auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 170 | std::string strength = std::to_string(std::get<PARAM_STRENGTH>(info.param)); |
Shunkai Yao | cb0fc41 | 2022-12-15 20:34:32 +0000 | [diff] [blame] | 171 | std::string name = "Implementor_" + descriptor.common.implementor + "_name_" + |
| 172 | descriptor.common.name + "_UUID_" + |
| 173 | descriptor.common.id.uuid.toString() + "_strength_" + strength; |
Shraddha Basantwani | f627d80 | 2022-11-08 14:45:07 +0530 | [diff] [blame] | 174 | std::replace_if( |
| 175 | name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_'); |
| 176 | return name; |
| 177 | }); |
| 178 | |
| 179 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(BassBoostParamTest); |
| 180 | |
| 181 | int main(int argc, char** argv) { |
| 182 | ::testing::InitGoogleTest(&argc, argv); |
| 183 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 184 | ABinderProcess_startThreadPool(); |
| 185 | return RUN_ALL_TESTS(); |
| 186 | } |