Sham Rathod | 2d319dc | 2022-11-29 15:06:12 +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 | |
Sham Rathod | 2d319dc | 2022-11-29 15:06:12 +0530 | [diff] [blame] | 17 | #include <aidl/Vintf.h> |
Mikhail Naganov | 872d4a6 | 2023-03-09 18:19:01 -0800 | [diff] [blame] | 18 | #define LOG_TAG "VtsHalVirtualizerTest" |
| 19 | #include <android-base/logging.h> |
| 20 | |
Sham Rathod | 2d319dc | 2022-11-29 15:06:12 +0530 | [diff] [blame] | 21 | #include "EffectHelper.h" |
| 22 | |
| 23 | using namespace android; |
| 24 | |
Sham Rathod | 2d319dc | 2022-11-29 15:06:12 +0530 | [diff] [blame] | 25 | using aidl::android::hardware::audio::effect::Descriptor; |
| 26 | using aidl::android::hardware::audio::effect::IEffect; |
| 27 | using aidl::android::hardware::audio::effect::IFactory; |
| 28 | using aidl::android::hardware::audio::effect::kVirtualizerTypeUUID; |
| 29 | using aidl::android::hardware::audio::effect::Parameter; |
| 30 | using aidl::android::hardware::audio::effect::Virtualizer; |
| 31 | |
| 32 | /** |
| 33 | * Here we focus on specific parameter checking, general IEffect interfaces testing performed in |
| 34 | * VtsAudioEffectTargetTest. |
| 35 | */ |
| 36 | enum ParamName { PARAM_INSTANCE_NAME, PARAM_STRENGTH }; |
| 37 | using VirtualizerParamTestParam = std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int>; |
| 38 | |
| 39 | /* |
| 40 | * Testing parameter range, assuming the parameter supported by effect is in this range. |
| 41 | * Parameter should be within the valid range defined in the documentation, |
| 42 | * for any supported value test expects EX_NONE from IEffect.setParameter(), |
| 43 | * otherwise expect EX_ILLEGAL_ARGUMENT. |
| 44 | */ |
| 45 | |
Sham Rathod | 2d319dc | 2022-11-29 15:06:12 +0530 | [diff] [blame] | 46 | class VirtualizerParamTest : public ::testing::TestWithParam<VirtualizerParamTestParam>, |
| 47 | public EffectHelper { |
| 48 | public: |
| 49 | VirtualizerParamTest() : mParamStrength(std::get<PARAM_STRENGTH>(GetParam())) { |
| 50 | std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam()); |
| 51 | } |
| 52 | |
| 53 | void SetUp() override { |
| 54 | ASSERT_NE(nullptr, mFactory); |
| 55 | ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor)); |
| 56 | |
| 57 | Parameter::Specific specific = getDefaultParamSpecific(); |
| 58 | Parameter::Common common = EffectHelper::createParamCommon( |
| 59 | 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */, |
| 60 | kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */); |
| 61 | IEffect::OpenEffectReturn ret; |
| 62 | ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE)); |
| 63 | ASSERT_NE(nullptr, mEffect); |
| 64 | } |
| 65 | |
| 66 | void TearDown() override { |
| 67 | ASSERT_NO_FATAL_FAILURE(close(mEffect)); |
| 68 | ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect)); |
| 69 | } |
| 70 | |
| 71 | Parameter::Specific getDefaultParamSpecific() { |
Sham Rathod | e808b07 | 2022-12-27 11:56:30 +0530 | [diff] [blame] | 72 | Virtualizer vr = Virtualizer::make<Virtualizer::strengthPm>(0); |
Sham Rathod | 2d319dc | 2022-11-29 15:06:12 +0530 | [diff] [blame] | 73 | Parameter::Specific specific = |
| 74 | Parameter::Specific::make<Parameter::Specific::virtualizer>(vr); |
| 75 | return specific; |
| 76 | } |
| 77 | |
| 78 | static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100; |
| 79 | std::shared_ptr<IFactory> mFactory; |
| 80 | std::shared_ptr<IEffect> mEffect; |
| 81 | Descriptor mDescriptor; |
Sham Rathod | e808b07 | 2022-12-27 11:56:30 +0530 | [diff] [blame] | 82 | int mParamStrength = 0; |
Sham Rathod | 2d319dc | 2022-11-29 15:06:12 +0530 | [diff] [blame] | 83 | |
| 84 | void SetAndGetVirtualizerParameters() { |
| 85 | for (auto& it : mTags) { |
| 86 | auto& tag = it.first; |
| 87 | auto& vr = it.second; |
| 88 | |
| 89 | // validate parameter |
| 90 | Descriptor desc; |
| 91 | ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc)); |
Shunkai Yao | 0a0c45e | 2023-02-13 17:41:11 +0000 | [diff] [blame] | 92 | const bool valid = isParameterValid<Virtualizer, Range::virtualizer>(it.second, desc); |
Sham Rathod | 2d319dc | 2022-11-29 15:06:12 +0530 | [diff] [blame] | 93 | const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT; |
| 94 | |
| 95 | // set parameter |
| 96 | Parameter expectParam; |
| 97 | Parameter::Specific specific; |
| 98 | specific.set<Parameter::Specific::virtualizer>(vr); |
| 99 | expectParam.set<Parameter::specific>(specific); |
| 100 | EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString(); |
| 101 | |
| 102 | // only get if parameter in range and set success |
| 103 | if (expected == EX_NONE) { |
| 104 | Parameter getParam; |
| 105 | Parameter::Id id; |
| 106 | Virtualizer::Id vrId; |
| 107 | vrId.set<Virtualizer::Id::commonTag>(tag); |
| 108 | id.set<Parameter::Id::virtualizerTag>(vrId); |
| 109 | // if set success, then get should match |
| 110 | EXPECT_STATUS(expected, mEffect->getParameter(id, &getParam)); |
| 111 | EXPECT_EQ(expectParam, getParam); |
| 112 | } |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | void addStrengthParam(int strength) { |
| 117 | Virtualizer vr; |
| 118 | vr.set<Virtualizer::strengthPm>(strength); |
| 119 | mTags.push_back({Virtualizer::strengthPm, vr}); |
| 120 | } |
| 121 | |
Sham Rathod | 2d319dc | 2022-11-29 15:06:12 +0530 | [diff] [blame] | 122 | private: |
| 123 | std::vector<std::pair<Virtualizer::Tag, Virtualizer>> mTags; |
| 124 | void CleanUp() { mTags.clear(); } |
| 125 | }; |
| 126 | |
| 127 | TEST_P(VirtualizerParamTest, SetAndGetStrength) { |
| 128 | EXPECT_NO_FATAL_FAILURE(addStrengthParam(mParamStrength)); |
| 129 | SetAndGetVirtualizerParameters(); |
| 130 | } |
| 131 | |
Shunkai Yao | 0a0c45e | 2023-02-13 17:41:11 +0000 | [diff] [blame] | 132 | std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kDescPair; |
Sham Rathod | 2d319dc | 2022-11-29 15:06:12 +0530 | [diff] [blame] | 133 | INSTANTIATE_TEST_SUITE_P( |
| 134 | VirtualizerTest, VirtualizerParamTest, |
Shunkai Yao | 0a0c45e | 2023-02-13 17:41:11 +0000 | [diff] [blame] | 135 | ::testing::Combine( |
| 136 | testing::ValuesIn(kDescPair = EffectFactoryHelper::getAllEffectDescriptors( |
| 137 | IFactory::descriptor, kVirtualizerTypeUUID)), |
| 138 | testing::ValuesIn(EffectHelper::getTestValueSet< |
| 139 | Virtualizer, int, Range::virtualizer, Virtualizer::strengthPm>( |
| 140 | kDescPair, EffectHelper::expandTestValueBasic<int>))), |
Sham Rathod | 2d319dc | 2022-11-29 15:06:12 +0530 | [diff] [blame] | 141 | [](const testing::TestParamInfo<VirtualizerParamTest::ParamType>& info) { |
| 142 | auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second; |
| 143 | std::string strength = std::to_string(std::get<PARAM_STRENGTH>(info.param)); |
| 144 | std::string name = "Implementor_" + descriptor.common.implementor + "_name_" + |
| 145 | descriptor.common.name + "_UUID_" + |
| 146 | descriptor.common.id.uuid.toString() + "_strength" + strength; |
| 147 | std::replace_if( |
| 148 | name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_'); |
| 149 | return name; |
| 150 | }); |
| 151 | |
| 152 | GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VirtualizerParamTest); |
| 153 | |
| 154 | int main(int argc, char** argv) { |
| 155 | ::testing::InitGoogleTest(&argc, argv); |
| 156 | ABinderProcess_setThreadPoolMaxThreadCount(1); |
| 157 | ABinderProcess_startThreadPool(); |
| 158 | return RUN_ALL_TESTS(); |
| 159 | } |