Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [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 | #include <aidl/Vintf.h> |
| 18 | |
| 19 | #define LOG_TAG "VtsHalNSParamTest" |
| 20 | |
| 21 | #include <Utils.h> |
Shunkai Yao | ab59e6d | 2022-12-22 00:45:23 +0000 | [diff] [blame] | 22 | #include <unordered_set> |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 23 | #include "EffectHelper.h" |
| 24 | |
| 25 | using namespace android; |
| 26 | |
| 27 | using aidl::android::hardware::audio::effect::Capability; |
| 28 | using aidl::android::hardware::audio::effect::Descriptor; |
| 29 | using aidl::android::hardware::audio::effect::IEffect; |
| 30 | using aidl::android::hardware::audio::effect::IFactory; |
| 31 | using aidl::android::hardware::audio::effect::kNoiseSuppressionTypeUUID; |
| 32 | using aidl::android::hardware::audio::effect::NoiseSuppression; |
| 33 | using aidl::android::hardware::audio::effect::Parameter; |
| 34 | |
| 35 | enum ParamName { PARAM_INSTANCE_NAME, PARAM_LEVEL }; |
| 36 | using NSParamTestParam = |
| 37 | std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, NoiseSuppression::Level>; |
| 38 | |
| 39 | class NSParamTest : public ::testing::TestWithParam<NSParamTestParam>, public EffectHelper { |
| 40 | public: |
| 41 | NSParamTest() : mLevel(std::get<PARAM_LEVEL>(GetParam())) { |
| 42 | std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam()); |
| 43 | } |
| 44 | |
| 45 | void SetUp() override { |
| 46 | ASSERT_NE(nullptr, mFactory); |
| 47 | ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor)); |
| 48 | |
| 49 | Parameter::Specific specific = getDefaultParamSpecific(); |
| 50 | Parameter::Common common = EffectHelper::createParamCommon( |
| 51 | 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */, |
| 52 | kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */); |
| 53 | IEffect::OpenEffectReturn ret; |
| 54 | ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE)); |
| 55 | ASSERT_NE(nullptr, mEffect); |
| 56 | } |
| 57 | |
| 58 | void TearDown() override { |
| 59 | ASSERT_NO_FATAL_FAILURE(close(mEffect)); |
| 60 | ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect)); |
| 61 | } |
| 62 | |
| 63 | Parameter::Specific getDefaultParamSpecific() { |
| 64 | NoiseSuppression ns = |
| 65 | NoiseSuppression::make<NoiseSuppression::level>(NoiseSuppression::Level::MEDIUM); |
| 66 | Parameter::Specific specific = |
| 67 | Parameter::Specific::make<Parameter::Specific::noiseSuppression>(ns); |
| 68 | return specific; |
| 69 | } |
| 70 | |
| 71 | static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100; |
| 72 | static const std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kFactoryDescList; |
Shunkai Yao | ab59e6d | 2022-12-22 00:45:23 +0000 | [diff] [blame] | 73 | static const std::unordered_set<NoiseSuppression::Level> kLevelValues; |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 74 | |
| 75 | std::shared_ptr<IFactory> mFactory; |
| 76 | std::shared_ptr<IEffect> mEffect; |
| 77 | Descriptor mDescriptor; |
| 78 | NoiseSuppression::Level mLevel; |
| 79 | |
| 80 | void SetAndGetParameters() { |
| 81 | for (auto& it : mTags) { |
| 82 | auto& tag = it.first; |
| 83 | auto& ns = it.second; |
| 84 | |
| 85 | // validate parameter |
| 86 | Descriptor desc; |
| 87 | ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc)); |
| 88 | const binder_exception_t expected = EX_NONE; |
| 89 | |
| 90 | // set parameter |
| 91 | Parameter expectParam; |
| 92 | Parameter::Specific specific; |
| 93 | specific.set<Parameter::Specific::noiseSuppression>(ns); |
| 94 | expectParam.set<Parameter::specific>(specific); |
| 95 | EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString(); |
| 96 | |
| 97 | // only get if parameter in range and set success |
| 98 | if (expected == EX_NONE) { |
| 99 | Parameter getParam; |
| 100 | Parameter::Id id; |
| 101 | NoiseSuppression::Id specificId; |
| 102 | specificId.set<NoiseSuppression::Id::commonTag>(tag); |
| 103 | id.set<Parameter::Id::noiseSuppressionTag>(specificId); |
| 104 | EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam)); |
| 105 | |
| 106 | EXPECT_EQ(expectParam, getParam) << "\nexpect:" << expectParam.toString() |
| 107 | << "\ngetParam:" << getParam.toString(); |
| 108 | } |
| 109 | } |
| 110 | } |
| 111 | |
| 112 | void addLevelParam(NoiseSuppression::Level level) { |
| 113 | NoiseSuppression ns; |
| 114 | ns.set<NoiseSuppression::level>(level); |
| 115 | mTags.push_back({NoiseSuppression::level, ns}); |
| 116 | } |
| 117 | |
| 118 | private: |
| 119 | std::vector<std::pair<NoiseSuppression::Tag, NoiseSuppression>> mTags; |
| 120 | void CleanUp() { mTags.clear(); } |
| 121 | }; |
| 122 | |
| 123 | const std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kFactoryDescList = |
| 124 | EffectFactoryHelper::getAllEffectDescriptors(IFactory::descriptor, |
| 125 | kNoiseSuppressionTypeUUID); |
Shunkai Yao | ab59e6d | 2022-12-22 00:45:23 +0000 | [diff] [blame] | 126 | const std::unordered_set<NoiseSuppression::Level> NSParamTest::kLevelValues = { |
| 127 | ndk::enum_range<NoiseSuppression::Level>().begin(), |
| 128 | ndk::enum_range<NoiseSuppression::Level>().end()}; |
Shunkai Yao | 5bd4a30 | 2022-12-20 15:46:24 +0000 | [diff] [blame] | 129 | |
| 130 | TEST_P(NSParamTest, SetAndGetLevel) { |
| 131 | EXPECT_NO_FATAL_FAILURE(addLevelParam(mLevel)); |
| 132 | SetAndGetParameters(); |
| 133 | } |
| 134 | |
| 135 | INSTANTIATE_TEST_SUITE_P( |
| 136 | NSParamTest, NSParamTest, |
| 137 | ::testing::Combine(testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors( |
| 138 | IFactory::descriptor, kNoiseSuppressionTypeUUID)), |
| 139 | testing::ValuesIn(NSParamTest::kLevelValues)), |
| 140 | [](const testing::TestParamInfo<NSParamTest::ParamType>& info) { |
| 141 | auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second; |
| 142 | std::string level = aidl::android::hardware::audio::effect::toString( |
| 143 | std::get<PARAM_LEVEL>(info.param)); |
| 144 | std::string name = "Implementor_" + descriptor.common.implementor + "_name_" + |
| 145 | descriptor.common.name + "_UUID_" + |
| 146 | descriptor.common.id.uuid.toString() + "_level_" + level; |
| 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(NSParamTest); |
| 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 | } |