Sham Rathod | 1b6c1f0 | 2022-11-22 17:39:22 +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 "VtsHalVolumeTest" |
| 18 | |
| 19 | #include <Utils.h> |
| 20 | #include <aidl/Vintf.h> |
| 21 | #include "EffectHelper.h" |
| 22 | |
| 23 | using namespace android; |
| 24 | |
| 25 | using aidl::android::hardware::audio::effect::Capability; |
| 26 | using aidl::android::hardware::audio::effect::Descriptor; |
| 27 | using aidl::android::hardware::audio::effect::IEffect; |
| 28 | using aidl::android::hardware::audio::effect::IFactory; |
| 29 | using aidl::android::hardware::audio::effect::kVolumeTypeUUID; |
| 30 | using aidl::android::hardware::audio::effect::Parameter; |
| 31 | using aidl::android::hardware::audio::effect::Volume; |
| 32 | |
| 33 | /** |
| 34 | * Here we focus on specific parameter checking, general IEffect interfaces testing performed in |
| 35 | * VtsAudioEffectTargetTest. |
| 36 | */ |
| 37 | enum ParamName { PARAM_INSTANCE_NAME, PARAM_LEVEL_DB, PARAM_MUTE }; |
| 38 | using VolumeParamTestParam = |
| 39 | std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int, bool>; |
| 40 | |
| 41 | const std::vector<int> kLevelValues = {Volume::MIN_LEVEL_DB - 1, Volume::MIN_LEVEL_DB, -100, |
| 42 | Volume::MAX_LEVEL_DB, Volume::MAX_LEVEL_DB + 1}; |
| 43 | |
| 44 | class VolumeParamTest : public ::testing::TestWithParam<VolumeParamTestParam>, public EffectHelper { |
| 45 | public: |
| 46 | VolumeParamTest() |
| 47 | : mParamLevel(std::get<PARAM_LEVEL_DB>(GetParam())), |
| 48 | mParamMute(std::get<PARAM_MUTE>(GetParam())) { |
| 49 | std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam()); |
| 50 | } |
| 51 | |
| 52 | void SetUp() override { |
| 53 | ASSERT_NE(nullptr, mFactory); |
| 54 | ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor)); |
| 55 | |
| 56 | Parameter::Specific specific = getDefaultParamSpecific(); |
| 57 | Parameter::Common common = EffectHelper::createParamCommon( |
| 58 | 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */, |
| 59 | kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */); |
| 60 | IEffect::OpenEffectReturn ret; |
| 61 | ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE)); |
| 62 | ASSERT_NE(nullptr, mEffect); |
| 63 | } |
| 64 | void TearDown() override { |
| 65 | ASSERT_NO_FATAL_FAILURE(close(mEffect)); |
| 66 | ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect)); |
| 67 | } |
| 68 | |
| 69 | Parameter::Specific getDefaultParamSpecific() { |
| 70 | Volume vol = Volume::make<Volume::levelDb>(Volume::MIN_LEVEL_DB); |
| 71 | Parameter::Specific specific = Parameter::Specific::make<Parameter::Specific::volume>(vol); |
| 72 | return specific; |
| 73 | } |
| 74 | |
| 75 | static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100; |
| 76 | std::shared_ptr<IFactory> mFactory; |
| 77 | std::shared_ptr<IEffect> mEffect; |
| 78 | Descriptor mDescriptor; |
| 79 | int mParamLevel = 0; |
| 80 | bool mParamMute = false; |
| 81 | |
| 82 | void SetAndGetParameters() { |
| 83 | for (auto& it : mTags) { |
| 84 | auto& tag = it.first; |
| 85 | auto& vol = it.second; |
| 86 | |
| 87 | // validate parameter |
| 88 | Descriptor desc; |
| 89 | ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc)); |
| 90 | const bool valid = isTagInRange(it.first, it.second, desc); |
| 91 | const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT; |
| 92 | |
| 93 | // set parameter |
| 94 | Parameter expectParam; |
| 95 | Parameter::Specific specific; |
| 96 | specific.set<Parameter::Specific::volume>(vol); |
| 97 | expectParam.set<Parameter::specific>(specific); |
| 98 | EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString(); |
| 99 | |
| 100 | // only get if parameter in range and set success |
| 101 | if (expected == EX_NONE) { |
| 102 | Parameter getParam; |
| 103 | Parameter::Id id; |
| 104 | Volume::Id volId; |
| 105 | volId.set<Volume::Id::commonTag>(tag); |
| 106 | id.set<Parameter::Id::volumeTag>(volId); |
| 107 | EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam)); |
| 108 | |
| 109 | EXPECT_EQ(expectParam, getParam) << "\nexpect:" << expectParam.toString() |
| 110 | << "\ngetParam:" << getParam.toString(); |
| 111 | } |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void addLevelParam(int level) { |
| 116 | Volume vol; |
| 117 | vol.set<Volume::levelDb>(level); |
| 118 | mTags.push_back({Volume::levelDb, vol}); |
| 119 | } |
| 120 | |
| 121 | void addMuteParam(bool mute) { |
| 122 | Volume vol; |
| 123 | vol.set<Volume::mute>(mute); |
| 124 | mTags.push_back({Volume::mute, vol}); |
| 125 | } |
| 126 | |
| 127 | bool isTagInRange(const Volume::Tag& tag, const Volume& vol, const Descriptor& desc) const { |
| 128 | const Volume::Capability& volCap = desc.capability.get<Capability::volume>(); |
| 129 | switch (tag) { |
| 130 | case Volume::levelDb: { |
| 131 | int level = vol.get<Volume::levelDb>(); |
| 132 | return isLevelInRange(volCap, level); |
| 133 | } |
| 134 | case Volume::mute: |
| 135 | return true; |
| 136 | default: |
| 137 | return false; |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | bool isLevelInRange(const Volume::Capability& cap, int level) const { |
| 142 | return level >= Volume::MIN_LEVEL_DB && level <= Volume::MAX_LEVEL_DB && |
| 143 | level <= cap.maxLevel; |
| 144 | } |
| 145 | |
| 146 | private: |
| 147 | std::vector<std::pair<Volume::Tag, Volume>> mTags; |
| 148 | void CleanUp() { mTags.clear(); } |
| 149 | }; |
| 150 | |
| 151 | TEST_P(VolumeParamTest, SetAndGetLevel) { |
| 152 | EXPECT_NO_FATAL_FAILURE(addLevelParam(mParamLevel)); |
| 153 | SetAndGetParameters(); |
| 154 | } |
| 155 | |
| 156 | TEST_P(VolumeParamTest, SetAndGetMute) { |
| 157 | EXPECT_NO_FATAL_FAILURE(addMuteParam(mParamMute)); |
| 158 | SetAndGetParameters(); |
| 159 | } |
| 160 | |
| 161 | INSTANTIATE_TEST_SUITE_P( |
| 162 | VolumeTest, VolumeParamTest, |
| 163 | ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors( |
| 164 | IFactory::descriptor, kVolumeTypeUUID)), |
| 165 | testing::ValuesIn(kLevelValues), testing::Bool()), |
| 166 | [](const testing::TestParamInfo<VolumeParamTest::ParamType>& info) { |
| 167 | auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second; |
| 168 | std::string level = std::to_string(std::get<PARAM_LEVEL_DB>(info.param)); |
| 169 | std::string mute = std::to_string(std::get<PARAM_MUTE>(info.param)); |
| 170 | std::string name = "Implementor_" + descriptor.common.implementor + "_name_" + |
| 171 | descriptor.common.name + "_UUID_" + |
| 172 | descriptor.common.id.uuid.toString() + "_level" + level + "_mute" + |
| 173 | mute; |
| 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(VolumeParamTest); |
| 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 | } |