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