Sham Rathod | 73aa2c3 | 2022-12-20 17:03:40 +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 "VtsHalPresetReverbTargetTest" |
| 18 | |
| 19 | #include <Utils.h> |
| 20 | #include <aidl/Vintf.h> |
| 21 | #include <android/binder_enums.h> |
| 22 | #include "EffectHelper.h" |
| 23 | |
| 24 | using namespace android; |
| 25 | |
| 26 | using aidl::android::hardware::audio::effect::Capability; |
| 27 | using aidl::android::hardware::audio::effect::Descriptor; |
| 28 | using aidl::android::hardware::audio::effect::IEffect; |
| 29 | using aidl::android::hardware::audio::effect::IFactory; |
| 30 | using aidl::android::hardware::audio::effect::kEffectNullUuid; |
| 31 | using aidl::android::hardware::audio::effect::kPresetReverbTypeUUID; |
| 32 | using aidl::android::hardware::audio::effect::Parameter; |
| 33 | using aidl::android::hardware::audio::effect::PresetReverb; |
| 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_PRESETS }; |
| 40 | using PresetReverbParamTestParam = |
| 41 | std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, PresetReverb::Presets>; |
| 42 | |
| 43 | // Testing for enum values |
| 44 | const std::vector<PresetReverb::Presets> kPresetsValues{ |
| 45 | ndk::enum_range<PresetReverb::Presets>().begin(), |
| 46 | ndk::enum_range<PresetReverb::Presets>().end()}; |
| 47 | |
| 48 | class PresetReverbParamTest : public ::testing::TestWithParam<PresetReverbParamTestParam>, |
| 49 | public EffectHelper { |
| 50 | public: |
| 51 | PresetReverbParamTest() : mParamPresets(std::get<PARAM_PRESETS>(GetParam())) { |
| 52 | std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam()); |
| 53 | } |
| 54 | |
| 55 | void SetUp() override { |
| 56 | ASSERT_NE(nullptr, mFactory); |
| 57 | ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor)); |
| 58 | |
| 59 | Parameter::Specific specific = getDefaultParamSpecific(); |
| 60 | Parameter::Common common = EffectHelper::createParamCommon( |
| 61 | 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */, |
| 62 | kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */); |
| 63 | IEffect::OpenEffectReturn ret; |
| 64 | ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE)); |
| 65 | ASSERT_NE(nullptr, mEffect); |
| 66 | } |
| 67 | |
| 68 | void TearDown() override { |
| 69 | ASSERT_NO_FATAL_FAILURE(close(mEffect)); |
| 70 | ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect)); |
| 71 | } |
| 72 | |
| 73 | static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100; |
| 74 | std::shared_ptr<IFactory> mFactory; |
| 75 | std::shared_ptr<IEffect> mEffect; |
| 76 | Descriptor mDescriptor; |
| 77 | PresetReverb::Presets mParamPresets = PresetReverb::Presets::NONE; |
| 78 | |
| 79 | void SetAndGetPresetReverbParameters() { |
| 80 | for (auto& it : mTags) { |
| 81 | auto& tag = it.first; |
| 82 | auto& pr = it.second; |
| 83 | |
| 84 | // validate parameter |
| 85 | Descriptor desc; |
| 86 | ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc)); |
| 87 | const bool valid = isTagInRange(it.first, it.second, desc); |
| 88 | const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT; |
| 89 | |
| 90 | // set parameter |
| 91 | Parameter expectParam; |
| 92 | Parameter::Specific specific; |
| 93 | specific.set<Parameter::Specific::presetReverb>(pr); |
| 94 | expectParam.set<Parameter::specific>(specific); |
| 95 | // All values are valid, set parameter should succeed |
| 96 | EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString(); |
| 97 | |
| 98 | // get parameter |
| 99 | Parameter getParam; |
| 100 | Parameter::Id id; |
| 101 | PresetReverb::Id prId; |
| 102 | prId.set<PresetReverb::Id::commonTag>(tag); |
| 103 | id.set<Parameter::Id::presetReverbTag>(prId); |
| 104 | EXPECT_STATUS(expected, mEffect->getParameter(id, &getParam)); |
| 105 | |
| 106 | EXPECT_EQ(expectParam, getParam); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | void addPresetsParam(PresetReverb::Presets preset) { |
| 111 | PresetReverb pr; |
| 112 | pr.set<PresetReverb::preset>(preset); |
| 113 | mTags.push_back({PresetReverb::preset, pr}); |
| 114 | } |
| 115 | |
| 116 | bool isTagInRange(const PresetReverb::Tag& tag, const PresetReverb& pr, |
| 117 | const Descriptor& desc) const { |
| 118 | const PresetReverb::Capability& prCap = desc.capability.get<Capability::presetReverb>(); |
| 119 | switch (tag) { |
| 120 | case PresetReverb::preset: { |
| 121 | PresetReverb::Presets preset = pr.get<PresetReverb::preset>(); |
| 122 | return isPresetInRange(prCap, preset); |
| 123 | } |
| 124 | default: |
| 125 | return false; |
| 126 | } |
| 127 | return false; |
| 128 | } |
| 129 | |
| 130 | bool isPresetInRange(const PresetReverb::Capability& cap, PresetReverb::Presets preset) const { |
| 131 | for (auto i : cap.supportedPresets) { |
| 132 | if (preset == i) return true; |
| 133 | } |
| 134 | return false; |
| 135 | } |
| 136 | |
| 137 | Parameter::Specific getDefaultParamSpecific() { |
| 138 | PresetReverb pr = PresetReverb::make<PresetReverb::preset>(PresetReverb::Presets::NONE); |
| 139 | Parameter::Specific specific = |
| 140 | Parameter::Specific::make<Parameter::Specific::presetReverb>(pr); |
| 141 | return specific; |
| 142 | } |
| 143 | |
| 144 | private: |
| 145 | std::vector<std::pair<PresetReverb::Tag, PresetReverb>> mTags; |
| 146 | void CleanUp() { mTags.clear(); } |
| 147 | }; |
| 148 | |
| 149 | TEST_P(PresetReverbParamTest, SetAndGetPresets) { |
| 150 | EXPECT_NO_FATAL_FAILURE(addPresetsParam(mParamPresets)); |
| 151 | SetAndGetPresetReverbParameters(); |
| 152 | } |
| 153 | |
| 154 | INSTANTIATE_TEST_SUITE_P( |
| 155 | PresetReverbTest, PresetReverbParamTest, |
| 156 | ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors( |
| 157 | IFactory::descriptor, kPresetReverbTypeUUID)), |
| 158 | testing::ValuesIn(kPresetsValues)), |
| 159 | [](const testing::TestParamInfo<PresetReverbParamTest::ParamType>& info) { |
| 160 | auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second; |
| 161 | std::string preset = |
| 162 | std::to_string(static_cast<int>(std::get<PARAM_PRESETS>(info.param))); |
| 163 | std::string name = "Implementor_" + descriptor.common.implementor + "_name_" + |
| 164 | descriptor.common.name + "_UUID_" + |
| 165 | descriptor.common.id.uuid.toString() + "_preset" + preset; |
| 166 | std::replace_if( |
| 167 | name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_'); |
| 168 | return name; |
| 169 | }); |
| 170 | |
| 171 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(PresetReverbParamTest); |
| 172 | |
| 173 | int main(int argc, char** argv) { |
| 174 | ::testing::InitGoogleTest(&argc, argv); |
| 175 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 176 | ABinderProcess_startThreadPool(); |
| 177 | return RUN_ALL_TESTS(); |
| 178 | } |