blob: 34625e79dab50907e58f5e8f344cfdf60d7d22db [file] [log] [blame]
Sham Rathod1b6c1f02022-11-22 17:39:22 +05301/*
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
23using namespace android;
24
25using aidl::android::hardware::audio::effect::Capability;
26using aidl::android::hardware::audio::effect::Descriptor;
27using aidl::android::hardware::audio::effect::IEffect;
28using aidl::android::hardware::audio::effect::IFactory;
29using aidl::android::hardware::audio::effect::kVolumeTypeUUID;
30using aidl::android::hardware::audio::effect::Parameter;
31using 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 */
Sham Rathod85793d82022-12-22 19:09:10 +053037enum ParamName { PARAM_INSTANCE_NAME, PARAM_LEVEL, PARAM_MUTE };
Sham Rathod1b6c1f02022-11-22 17:39:22 +053038using VolumeParamTestParam =
39 std::tuple<std::pair<std::shared_ptr<IFactory>, Descriptor>, int, bool>;
40
Sham Rathod1b6c1f02022-11-22 17:39:22 +053041class VolumeParamTest : public ::testing::TestWithParam<VolumeParamTestParam>, public EffectHelper {
42 public:
43 VolumeParamTest()
Sham Rathod85793d82022-12-22 19:09:10 +053044 : mParamLevel(std::get<PARAM_LEVEL>(GetParam())),
Sham Rathod1b6c1f02022-11-22 17:39:22 +053045 mParamMute(std::get<PARAM_MUTE>(GetParam())) {
46 std::tie(mFactory, mDescriptor) = std::get<PARAM_INSTANCE_NAME>(GetParam());
47 }
48
49 void SetUp() override {
50 ASSERT_NE(nullptr, mFactory);
51 ASSERT_NO_FATAL_FAILURE(create(mFactory, mEffect, mDescriptor));
52
53 Parameter::Specific specific = getDefaultParamSpecific();
54 Parameter::Common common = EffectHelper::createParamCommon(
55 0 /* session */, 1 /* ioHandle */, 44100 /* iSampleRate */, 44100 /* oSampleRate */,
56 kInputFrameCount /* iFrameCount */, kOutputFrameCount /* oFrameCount */);
57 IEffect::OpenEffectReturn ret;
58 ASSERT_NO_FATAL_FAILURE(open(mEffect, common, specific, &ret, EX_NONE));
59 ASSERT_NE(nullptr, mEffect);
60 }
61 void TearDown() override {
62 ASSERT_NO_FATAL_FAILURE(close(mEffect));
63 ASSERT_NO_FATAL_FAILURE(destroy(mFactory, mEffect));
64 }
65
66 Parameter::Specific getDefaultParamSpecific() {
Sham Rathod85793d82022-12-22 19:09:10 +053067 Volume vol = Volume::make<Volume::levelDb>(-9600);
Sham Rathod1b6c1f02022-11-22 17:39:22 +053068 Parameter::Specific specific = Parameter::Specific::make<Parameter::Specific::volume>(vol);
69 return specific;
70 }
71
72 static const long kInputFrameCount = 0x100, kOutputFrameCount = 0x100;
73 std::shared_ptr<IFactory> mFactory;
74 std::shared_ptr<IEffect> mEffect;
75 Descriptor mDescriptor;
76 int mParamLevel = 0;
77 bool mParamMute = false;
78
79 void SetAndGetParameters() {
80 for (auto& it : mTags) {
81 auto& tag = it.first;
82 auto& vol = it.second;
83
84 // validate parameter
85 Descriptor desc;
86 ASSERT_STATUS(EX_NONE, mEffect->getDescriptor(&desc));
Sham Rathod85793d82022-12-22 19:09:10 +053087 // only set and get parameter if capability is valid
88 ASSERT_TRUE(isCapabilityValid(desc));
Sham Rathod1b6c1f02022-11-22 17:39:22 +053089 const bool valid = isTagInRange(it.first, it.second, desc);
90 const binder_exception_t expected = valid ? EX_NONE : EX_ILLEGAL_ARGUMENT;
91
92 // set parameter
93 Parameter expectParam;
94 Parameter::Specific specific;
95 specific.set<Parameter::Specific::volume>(vol);
96 expectParam.set<Parameter::specific>(specific);
97 EXPECT_STATUS(expected, mEffect->setParameter(expectParam)) << expectParam.toString();
98
Sham Rathod85793d82022-12-22 19:09:10 +053099 // only get if parameter is in range and set success
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530100 if (expected == EX_NONE) {
101 Parameter getParam;
102 Parameter::Id id;
103 Volume::Id volId;
104 volId.set<Volume::Id::commonTag>(tag);
105 id.set<Parameter::Id::volumeTag>(volId);
106 EXPECT_STATUS(EX_NONE, mEffect->getParameter(id, &getParam));
107
108 EXPECT_EQ(expectParam, getParam) << "\nexpect:" << expectParam.toString()
109 << "\ngetParam:" << getParam.toString();
110 }
111 }
112 }
113
114 void addLevelParam(int level) {
115 Volume vol;
116 vol.set<Volume::levelDb>(level);
117 mTags.push_back({Volume::levelDb, vol});
118 }
119
120 void addMuteParam(bool mute) {
121 Volume vol;
122 vol.set<Volume::mute>(mute);
123 mTags.push_back({Volume::mute, vol});
124 }
125
Sham Rathod85793d82022-12-22 19:09:10 +0530126 bool isCapabilityValid(const Descriptor& desc) {
127 const Volume::Capability& volCap = desc.capability.get<Capability::volume>();
128 return (volCap.minLevelDb <= volCap.maxLevelDb);
129 }
130
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530131 bool isTagInRange(const Volume::Tag& tag, const Volume& vol, const Descriptor& desc) const {
132 const Volume::Capability& volCap = desc.capability.get<Capability::volume>();
133 switch (tag) {
134 case Volume::levelDb: {
135 int level = vol.get<Volume::levelDb>();
136 return isLevelInRange(volCap, level);
137 }
138 case Volume::mute:
139 return true;
140 default:
141 return false;
142 }
143 }
144
Sham Rathod85793d82022-12-22 19:09:10 +0530145 static std::vector<int> getLevelTestValues(
146 std::vector<std::pair<std::shared_ptr<IFactory>, Descriptor>> kFactoryDescList) {
147 int minLevelDb = std::numeric_limits<int>::max();
148 int maxLevelDb = std::numeric_limits<int>::min();
149 for (const auto& it : kFactoryDescList) {
150 maxLevelDb =
151 std::max(it.second.capability.get<Capability::volume>().maxLevelDb, maxLevelDb);
152 minLevelDb = std::min(it.second.capability.get<Capability ::volume>().minLevelDb,
153 minLevelDb);
154 }
155 return {minLevelDb - 1, minLevelDb, -100, maxLevelDb, maxLevelDb + 1};
156 }
157
158 bool isLevelInRange(const Volume::Capability& volCap, int level) const {
159 return level >= volCap.minLevelDb && level <= volCap.maxLevelDb;
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530160 }
161
162 private:
163 std::vector<std::pair<Volume::Tag, Volume>> mTags;
164 void CleanUp() { mTags.clear(); }
165};
166
167TEST_P(VolumeParamTest, SetAndGetLevel) {
168 EXPECT_NO_FATAL_FAILURE(addLevelParam(mParamLevel));
169 SetAndGetParameters();
170}
171
172TEST_P(VolumeParamTest, SetAndGetMute) {
173 EXPECT_NO_FATAL_FAILURE(addMuteParam(mParamMute));
174 SetAndGetParameters();
175}
176
177INSTANTIATE_TEST_SUITE_P(
178 VolumeTest, VolumeParamTest,
Sham Rathod85793d82022-12-22 19:09:10 +0530179 ::testing::Combine(
180 testing::ValuesIn(EffectFactoryHelper::getAllEffectDescriptors(IFactory::descriptor,
181 kVolumeTypeUUID)),
182 testing::ValuesIn(VolumeParamTest::getLevelTestValues(
183 EffectFactoryHelper::getAllEffectDescriptors(IFactory::descriptor,
184 kVolumeTypeUUID))),
185 testing::Bool() /* mute */),
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530186 [](const testing::TestParamInfo<VolumeParamTest::ParamType>& info) {
187 auto descriptor = std::get<PARAM_INSTANCE_NAME>(info.param).second;
Sham Rathod85793d82022-12-22 19:09:10 +0530188 std::string level = std::to_string(std::get<PARAM_LEVEL>(info.param));
Sham Rathod1b6c1f02022-11-22 17:39:22 +0530189 std::string mute = std::to_string(std::get<PARAM_MUTE>(info.param));
190 std::string name = "Implementor_" + descriptor.common.implementor + "_name_" +
191 descriptor.common.name + "_UUID_" +
192 descriptor.common.id.uuid.toString() + "_level" + level + "_mute" +
193 mute;
194 std::replace_if(
195 name.begin(), name.end(), [](const char c) { return !std::isalnum(c); }, '_');
196 return name;
197 });
198
199GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(VolumeParamTest);
200
201int main(int argc, char** argv) {
202 ::testing::InitGoogleTest(&argc, argv);
203 ABinderProcess_setThreadPoolMaxThreadCount(1);
204 ABinderProcess_startThreadPool();
205 return RUN_ALL_TESTS();
206}